So… I have this custom shader attached to my level pieces that works great for mimicking the effect of light attached to the player (it finds the position of the camera, and fades the color to black after a certain range.
My question is:
How can i modify this shader so that:
- Instead of using the location of the camera, it uses the location of a gameobject (possibly with a tag or name like “Light source”
- Any gameobject with this tag or label will have the desired effect on the surface, so that i can fake light sources in other areas besides the camera.
Any help would be greatly apreciated. I feel like banging my head against the wall! Here is the shader code:
Shader "Custom/Distance" {
Properties {
_MainTex ("Base (RGB)", 2D) = "white" {}
_MinColor ("Color in Minimal", Color) = (1, 1, 1, 1)
_MaxColor ("Color in Maxmal", Color) = (0, 0, 0, 0)
_MinDistance ("Min Distance", Float) = 5
_MaxDistance ("Max Distance", Float) = 20
}
SubShader {
Tags { "RenderType"="Opaque" }
LOD 200
CGPROGRAM
#pragma surface surf Lambert
sampler2D _MainTex;
struct Input {
float2 uv_MainTex;
float3 worldPos;
};
float _MaxDistance;
float _MinDistance;
half4 _MinColor;
half4 _MaxColor;
void surf (Input IN, inout SurfaceOutput o) {
half4 c = tex2D (_MainTex, IN.uv_MainTex);
float dist = distance(_WorldSpaceCameraPos, IN.worldPos);
half weight = saturate( (dist - _MinDistance) / (_MaxDistance - _MinDistance) );
half4 distanceColor = lerp(_MinColor, _MaxColor, weight);
o.Albedo = c.rgb * distanceColor.rgb;
o.Alpha = c.a;
}
ENDCG
}
FallBack "Diffuse"
}
so as you can see in the image above, thats not a point light or fog, but color fading to black.