[SOLVED] Quad and PointLight, rotation of quad affects the lighting?

Hi again, different topic now.

I have a spatial that I create like this:

private Spatial createShip(int ship) {
    Quad quad = new Quad(CoreViewConstants.SHIPSIZE, CoreViewConstants.SHIPSIZE);
    //<-- Move into the material?
    float halfSize = CoreViewConstants.SHIPSIZE * 0.5f;
    quad.setBuffer(VertexBuffer.Type.Position, 3, this.getArray(halfSize));
    //-->
    quad.updateBound();
    Geometry geom = new Geometry("Ship", quad);
    Material mat = assets.loadMaterial("Materials/ShipMaterialLight.j3m");
    //Material mat = assets.loadMaterial("Materials/ShipMaterialUnshaded.j3m");

    geom.setMaterial(mat);
    setShipMaterialVariables(geom, ship);
    //mat.setInt("numTilesOffsetY", ship);

    geom.setQueueBucket(RenderQueue.Bucket.Transparent);
    
    PointLight myLight = new PointLight();
    myLight.setColor(ColorRGBA.White);
    myLight.setRadius(20);
    rootNode.addLight(myLight);
    ShipLightControl lightControl = new ShipLightControl(myLight);
    geom.addControl(lightControl);

    //Test:
    //AmbientLight al = new AmbientLight();
    //al.setColor(ColorRGBA.White.mult(1.3f));
    //rootNode.addLight(al);
    //<--
    
    return geom;
}

The ShipLightControl is this:

public class ShipLightControl extends LightControl {

PointLight pointLight;
Vector3f pos;

public ShipLightControl(PointLight pointLight) {
    super(pointLight);

    this.pointLight = pointLight;
}

@Override
public void update(float tpf) {
    //super.update(tpf);
    if (this.enabled && this.getSpatial() != null && pointLight != null) {
        pos = this.getSpatial().getLocalTranslation();
        pointLight.setPosition(pos.add(0, 0, 2));
    }
}
}

The material of the ship is this:

Material ShipMaterialLight : MatDefs/StaticSprite/StaticSpriteLightShader.j3md {
 MaterialParameters {
    //AniTexMap : Flip Textures/Subspace/ships.bmp
    numTilesX : 10
    numTilesY : 32
    numTilesOffsetX : 0
    numTilesOffsetY : 3

    DiffuseMap : Flip Textures/Subspace/ships.bmp
    UseMaterialColors : true
    Specular : 1.0 1.0 1.0 1.0
    Diffuse : 1.0 1.0 1.0 1.0
    Shininess : 1.0
 }
AdditionalRenderState {
  Blend Alpha
  Wireframe Off
}
}

I try to position the light above the ship-quad, so that the ship is illuminated by the light. It works to begin with:

image

But when I rotate the quad, the light fades:

image

image

image

Anyone have any pointers or places I can look to fix this ?

In the Docs, it says that there are these:

g_LightPosition: the position of the light

use for PointLight: x,y,z contain the world position of the light, the w component contains 1/lightRadius

But doesnt mention anything about direction for a pointlight. My shader is based on the lighting.j3md - I’m trying to see if lighting.j3md has been updated since I looked at it.

A pointLight doesn’t have a direction, it has a radius and a linear falloff from its location around its radius.

Okay, so that confirmed that. I’m probably to look into my Lighting j3md / vert / frag and see if have done something there that is affecting the light direction.

I’m not sure what your shader is doing but usually any problems with lighting revolve around bad normals. Are they pointing in the right direction?

1 Like

No idea. Haven’t dealt with normals before. The texture is a bitmap. No idea where to start.

Judging by the other threads you’ve created, you make your own quad.

Try the built-in quad mesh - which will contain the correct normals.

1 Like

Thanks. I found this now (knowing what to search for is important)
https://wiki.jmonkeyengine.org/docs/jme3/advanced/custom_meshes.html#example-using-meshes-with-lighting-j3md

Will read up on that. I have probably skipped this in the past just using Unshaded.

Hmm, I tried setting the normals :slight_smile:

{...
    Quad quad = new Quad(CoreViewConstants.SHIPSIZE, CoreViewConstants.SHIPSIZE);
    float halfSize = CoreViewConstants.SHIPSIZE * 0.5f;
    quad.setBuffer(VertexBuffer.Type.Position, 3, BufferUtils.createFloatBuffer(this.getVertices(halfSize)));
    quad.setBuffer(VertexBuffer.Type.Normal, 3, BufferUtils.createFloatBuffer(getNormals()));
...}

private float[] getVertices(float halfSize) {
    float[] res = new float[]{
        halfSize, 0, -halfSize,
        -halfSize, 0, -halfSize,
        -halfSize, 0, halfSize,
        halfSize, 0, halfSize
    };
    return res;
}
private float[] getNormals() {
    float[] normals = new float[12];
    normals = new float[]{0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0};

    return normals;
}

}

My game is in the xz-plane, y is up. I think there’s a discrepancy between the JavaDocs and the examples/Wiki of what is default “up”, but nonetheless, I didnt get through yet.It ends up unlit/black. Will have to see if I can get it working with a “default” Quad as you mentioned.

I think I got it working. I had the wrong position on the light according to both normals and “up”. Thanks for the help @jayfella!

2 Likes