Get light vector like the one returned by Lamp Data Node? [Python]

Hi, I have made this moon with nodes:


In game, the sun lamp will being changin its energy and color (because day and night cycle), that would be a problem for the moon, so I can’t leave it to have shading, So I put it the shading by using the dot product of the normal with the light vector of the sun (return by lamp data node).

Everything with that is perfect, and works good.

The problem is that I have the moon in another file and I want to link it to the sky file (and the sky and all that stuff will be linked to the main game file), as the material node is linked, I can’t set the lamp in the node to the sun in the sky file. And if I link the sun (in sky file) to the moon file, when I link the moon to the sky file (which have the sun linked in the moon file), it will not work, because the lamp disappears in the node. I test by leaving the sun in another file, and linking that sun to both file (sky and moon), it works, until I reopen the file, and It stops working.

So I think on getting the light vector from the object color, and change the object color using python.

I test somenthing like this:


light = scene.lights["sun"]
    
vector = light.getAxisVect([0, 1, 0])
vector.normalize()

moon.color = [vector.x, vector.y, vector.z, 1.0]

But the shadow and color of the moon is always facing to the camera. I guess I have to do some math to transform the vector to world cordinates. Can someone help me?

And do you think that thing about the linked material node and lamps is a bug?

Hi! Sorry I won’t answer your question… I didn’t understand well. But I’ve a question for you :smiley: :

Is this like that:


you recreated diffuse? Is it correct? (I’m beginning with nodes) Thanks!

http://www.pasteall.org/blend/36589 (numpad 6 to rotate the camera around, right arrow to rotate the sun)

I don’t understand your problem with camera position and color…

Ah, I read the title again and this:

http://www.pasteall.org/blend/36590

is maybe what you are asking for?

sunDirection = -sun.localOrientation.col[2]
normalizedSunDirection = sunDirection.normalized() # I think dot product works only with normalized Vector

EDIT: Of course I made the diffuse with vertex colors but if you want more precision, you need a GLSL shader (passing normalizedSunDirection as uniform and following this tutorial https://en.wikibooks.org/wiki/GLSL_Programming/Blender/Diffuse_Reflection (lightSource[0] is not precise)

Hi, the basic node setup is as the one you showed. I think I don’t explain well.
I made this file to show what I trying to do: python light vector.blend (152 KB)

Instead of use the lamp data node to pass the light vector to the math vector (that will get the dot product), I want it to get the light vector from the object color (see the node setup), and I will use python to send the light vector to the object color.

The problem if that if I do that, the shade is always facing to the camera (press P, the sun will roll).


I guess the lamp data not just pass the direction of the lamp, but a vector that have been changed to fit with the world space.

I don’t want to use a GLSL shader, because as I don’t know much about it. I prefer to continue with the nodes with this sphere…(but I think to learn it more in the future)

I presume that the material nodes are in camera-space:


def objectColor(controller):
    owner = controller.owner
    scene = owner.scene
    
    sun = scene.lights["Sun"]
    
    #youle code!
    sunDirection = -sun.worldOrientation.col[2]
    cam = scene.active_camera
    lightVector = cam.worldOrientation.inverted() * sunDirection.normalized()
    
    owner.color = [lightVector.x, lightVector.y, lightVector.z, 1.0]

Will probably suffice.

Thank you agoose! It works perfectly now, and thank you as well youle.

Cool! (I finally understood your problem carlo697 but I had not the answer) Thanks agoose77!