Light on/off

Hi again.

I want to be able to switch certain light on and off.

This is (messy) piece of a prototype code which initializes light. When I enable light, I call com.jme.light.PointLight.setEnabled(…) and then updateRenderStates on a node which parents LightNode with that light.

It doesn't work. Here's the code, help anyone, please.



        light = new PointLight();
        light.setEnabled(true);
        light.setShadowCaster(true);
        light.setAttenuate(true);
        light.setAmbient(ColorRGBA.gray);
        light.setDiffuse(ColorRGBA.white);
        LightState ls = DisplaySystem.getDisplaySystem().getRenderer().createLightState();
        ls.setEnabled(true);
        ls.attach(light);
        LightNode ln = new LightNode("tablamp-light", ls);
        ln.setLocalTranslation(0, 20, 0);
        attachChild(ln);

        // Just a test to find out light-node pos...
        Sphere lightPosSphere = new Sphere("lps", 3, 3, 5);
        lightPosSphere.setLocalTranslation(0, 20, 0);
        WireframeState ws = DisplaySystem.getDisplaySystem().getRenderer().createWireframeState();
        ws.setEnabled(true);
        lightPosSphere.setRenderState(ws);
        attachChild(lightPosSphere);

        updateRenderState();

Mmmmh. The Sphere seems to be attached to a node other than the one the LightNode is attached to.

It truly is, it is attached to 'this' (the InteractibleNode, a customized descendant of Node), but I dunno if its of any importance. Just in case that it is: 'this' holds three nodes: 1) loaded model TriMesh, 2) LightNode and 3) Sphere.



I can post whole code if needed.

My point was that typically a LightNode only affects its children.

Well, I don't know if this will help at all, since it does not use the LightNode, but:


    public void simpleInitGame()
    {
        lightState.detachAll();
        light = new PointLight();
        light.setEnabled(true);
        light.setShadowCaster(true);
        light.setAttenuate(true);
        light.setAmbient(ColorRGBA.gray);
        light.setDiffuse(ColorRGBA.white);
        ls = DisplaySystem.getDisplaySystem().getRenderer().createLightState();
        ls.setEnabled(true);
        ls.attach(light);
        //LightNode ln = new LightNode("tablamp-light", ls);
        //ln.setLocalTranslation(0, 20, 0);
        //rootNode.attachChild(ln);
        rootNode.setRenderState( ls );

        // Just a test to find out light-node pos...
        Sphere lightPosSphere = new Sphere("lps", 30, 30, 5);
        lightPosSphere.setLocalTranslation(0, -20, 0);
        WireframeState ws = DisplaySystem.getDisplaySystem().getRenderer().createWireframeState();
        ws.setEnabled(true);
        lightPosSphere.setRenderState(ws);
        rootNode.attachChild(lightPosSphere);

        rootNode.updateRenderState();
    }

duenez said:

My point was that typically a LightNode only affects its children.


Didn't know that!
So I'll need to attach LightNode to the room, not to the light mesh.. OK... will try that... thanks.

Actually, the light will affect whatever you set as the target:



LightNode.setTarget(Spatial);



Did you set the target properly?

That's very good to know!  XD I was trying to reproduce the problem and couldn't solve the problem  … I was wondering what was I doing wrong myself. :smiley: