how to pass custom variables to shaders

hello, how can i pass the position of a lightsource to a shader?

in the shader i already have this defined:

Properties {
    _MainTex ("Base (RGB)", 2D) = "white" {}    
    _LightPos ("LightPos", Vector) = (0,0,0,1)
}

but i dont know how to fill this LightPos variable from the application side.

thanks!

You don't need to define variables in the Properties block. It's only there to serve as a GUI.

1 Answer

1

You could use Material.SetVector:

renderer.material.SetVector("_LightPos", Vector4(0,0,0,1));

If you are trying to get the position of your light, it should be like this... light.transform.position, which you will have to convert to a Vector4 so you can pass it to the shader

Light data is availble in shaders though, no need to add it yourself. http://unity3d.com/support/documentation/Components/SL-SurfaceShaderLightingExamples.html

Well, you shouldn't be doing "light".transform.position, I was just using "light" as an example name. If your script is attached to the light object, than just do transform.position. If your script is attached to something else, do GameObject.Find("NAMEOFLIGHTINEDITOR").transform.position.