- Home /
How to access the lights properties from within a shader?
I am searching and trying around for about an hour now and am not able to solve it. I am just looking for a way to get the properties (in my case only position/direction and color) of the only light (directional) in my level. I found glstate.light[0].position to glstate.light[3].position and _ObjectSpaceLightPos. But glstate.light[0].position to glstate.light[3].position are all 0 as well as _ObjectSpaceLightPos0, while _ObjectSpaceLightPosN doesn is not defined as well as _ObjectSpaceLightPos[N].
I dont want to do anything else than to write a basic per pixel lighting shader (it is actually a bit more, but everything else is already working fine).
Please help me :) Thanks.
Just in case it helps, here is the whole code:
Shader "World Multi Texturing" { Properties { _MainTex0 ("Base 0 (RGB)", 2D) = "white" {} _MainTex1 ("Base 1 (RGB)", 2D) = "white" {} _MainTex2 ("Base 2 (RGB)", 2D) = "white" {} _MainTex3 ("Base 3 (RGB)", 2D) = "white" {} _MainTex4 ("Base 4 (RGB)", 2D) = "white" {}
_BlendTex ("Blend (RGB)", 2D) = "black" {}
_LightMap ("Light (RGB)", 2D) = "white" {}
}
SubShader
{
Pass
{
Lighting On
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
uniform sampler2D _MainTex0;
uniform sampler2D _MainTex1;
uniform sampler2D _MainTex2;
uniform sampler2D _MainTex3;
uniform sampler2D _MainTex4;
uniform sampler2D _BlendTex;
uniform sampler2D _LightMap;
struct v2f
{
float4 position : POSITION;
float3 normal : COLOR;
float2 texblend : TEXCOORD0;
float2 texcoord : TEXCOORD1;
};
v2f vert(appdata_base In)
{
v2f Out;
Out.position = mul(glstate.matrix.mvp, In.vertex);
Out.normal = mul(_Object2World, float4(In.normal, 0.0f)).xyz;
Out.texblend = (mul(_Object2World, In.vertex).xz+4.0f)/256.0f;
Out.texcoord = In.texcoord.xy;
return Out;
}
half4 frag(v2f In) : COLOR
{
float4 color0 = tex2D(_MainTex0, In.texcoord);
float4 color1 = tex2D(_MainTex1, In.texcoord);
float4 color2 = tex2D(_MainTex2, In.texcoord);
float4 color3 = tex2D(_MainTex3, In.texcoord);
float4 color4 = tex2D(_MainTex4, In.texcoord);
float4 blend = tex2D(_BlendTex, In.texblend);
float4 light = tex2D(_LightMap, In.texblend);
float4 color = lerp(color0, color1, blend.r);
color = lerp(color, color2, blend.g);
color = lerp(color, color3, blend.b);
color = lerp(color, color4, blend.a);
In.normal = normalize(In.normal);
float diffuse = 0.5;
diffuse += saturate(dot(normalize(glstate.light[0].position.xyz), In.normal));
diffuse += saturate(dot(normalize(glstate.light[1].position.xyz), In.normal));
diffuse += saturate(dot(normalize(glstate.light[2].position.xyz), In.normal));
diffuse += saturate(dot(normalize(glstate.light[3].position.xyz), In.normal));
return half4(color.rgb*light.rgb*diffuse, 1.0);
}
ENDCG
}
}
FallBack "Diffuse"
}
Answer by Aras · Jun 30, 2010 at 06:09 AM
I guess I at least partially answered your question in this lengthy post on Unity forums: http://forum.unity3d.com/viewtopic.php?p=343170#343170
Yes, that of course answered my question. Thanks again for that post :).
Hmm... Looks like that post has disappeared. Let us know if anyone finds its contents somewhere else...
Your answer
Follow this Question
Related Questions
Modify Light Projector Shader to be Additive 0 Answers
I need help with 2D light effect by mask or something, please! 1 Answer
Getting vertex data from lighting function in shaders. 1 Answer
Block/Obscure light without shadows 0 Answers
bumped diffuse Shader not behaving as expected (reversed) 0 Answers