Question about LightNode

Hey everyone,



I was trying to figure out how LightNodes work. But it looks like I do not fully understand it.

Take a look at this example:



[java]

import com.jme3.app.SimpleApplication;

import com.jme3.light.SpotLight;

import com.jme3.material.Material;

import com.jme3.math.ColorRGBA;

import com.jme3.scene.Geometry;

import com.jme3.scene.LightNode;

import com.jme3.scene.debug.Grid;

import com.jme3.scene.shape.Sphere;



public class LightNodesTest extends SimpleApplication {

public static void main(String[] args) {

new LightNodesTest().start();

}



@Override

public void simpleInitApp() {

this.prepareScene();



//x

this.addSphere(3, 0, 0);

this.addSphere(-3, 0, 0);

//y

this.addSphere(0, 3, 0);

this.addSphere(0, -3, 0);

//z

this.addSphere(0, 0, 3);

this.addSphere(0, 0, -3);



SpotLight dl = new SpotLight();

dl.setColor(new ColorRGBA(1, 0, 0, 1));



LightNode ln = new LightNode("a", dl);

//ln.setLocalTranslation(0, 10, 0);//UNCOMMENT THIS LINE TO SEE WHAT IS WRONG

rootNode.addLight(dl);

rootNode.attachChild(ln);

}



private void addSphere(float x, float y, float z) {

Sphere sphere = new Sphere(20, 20, 1);

Material material = new Material(assetManager, "Common/MatDefs/Light/Lighting.j3md");

material.setColor("Diffuse", new ColorRGBA(0, 0, 0, 1));

Geometry geometry = new Geometry("sphere", sphere);

geometry.setMaterial(material);

geometry.setLocalTranslation(x, y, z);

rootNode.attachChild(geometry);

}



private void prepareScene() {

Geometry grid = new Geometry("grid", new Grid(10, 10, 1));

Material gridMaterial = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");

gridMaterial.setColor("Color", ColorRGBA.DarkGray);

grid.setMaterial(gridMaterial);

grid.setLocalTranslation(-5, 0, -5);

rootNode.attachChild(grid);



viewPort.setBackgroundColor(ColorRGBA.Gray);

flyCam.setMoveSpeed(20);

cam.setFrustumFar(1000.0f);

cam.setFrustumNear(1.0f);

}

}

[/java]



When you run it you will have 6 spheres located on +3 and -3 positions of each axis.

I create a spot light in the center of the scene.

I can see a small red spot on the most bottm sphere (located at [0, -3, 0]).



Then I decided to move the light node. As I understood the light source should also move. The new position was [0, 10, 0] so I thought I would be seeing the spot at the top of the top most sphere (located at [0, 3, 0]).



But instead I see the spot where it was before. Nothing really changed.



Could you tell me what am I missing? I thought that when I move the node, the light will follow (it should copy the transform of the node).



Cheers,

Kaelthas

You add the spotlight to the rootNode, not to the lightnode. So, when moving the lightNode, the spotlight is not moved.

Oh, and use a PointLight instead of a SpotLight!

Duh…

[java]

private void spatialTolight(Light light) {

if (light instanceof PointLight) {

((PointLight) light).setPosition(spatial.getWorldTranslation());

}

TempVars vars = TempVars.get();



if (light instanceof DirectionalLight) {

((DirectionalLight) light).setDirection(vars.vect1.set(spatial.getWorldTranslation()).multLocal(-1.0f));

}

vars.release();

//TODO add code for Spot light here when it’s done

// if( light instanceof SpotLight){

// ((SpotLight)light).setPosition(spatial.getWorldTranslation());

// ((SpotLight)light).setRotation(spatial.getWorldRotation());

// }



}

[/java]

I guess it’s time to get this todo done :stuck_out_tongue:

it works with everything but spotlights for now, i’m gonna fix this.

Light is stored inside the LightNode here:



[java]

LightNode ln = new LightNode(“a”, dl);

[/java]

At first I wasn’t adding the light to the root node but then it simply didn’t work.



And I need a SpotLight because I wanna see how to rotate the light with the node as well as changing its position.

I committed a fix.

Now it should work

2 Likes

Thanks @nehon it works now :slight_smile:



Just one more question. I have two objects and one is hidden behind the other. the spot light can be seen on both of the objects and not the one closer to the light.

Is it ment to be so or do I have to do something else to make it work ??

1 Like

Lights make objects brighter based on distance, normals and the direction of the light. You are misguided by the natural but wrong assumption, that objects cast shadows, which is not true in general 3D environments. As far as I can tell, there is a basic shadow system with tutorials for jme3, but it seems to be restricted to a single directional light (basically an environment with a sun). I guess someone will provide a more sofisticated shadow system eventually, and everyone will tell him, how well he is doing:D

3 Likes

Thanks for explanation.



Hope someone will improve the light system in the future :slight_smile: