Custom Fake light Shader: Almost there......

hey guys. I am making a mobile first person shooter and trying to optimize a solution for lighting. The levels will be put together with prefabs so they will be randomly generated, so for that reason, light maps are out of the question, and just a few real time point lights lag the performance too much on slower phones. The environments are dark, so i originally had a point light attached to the player to mimic a torch, with other point lights around walls for additional lighting (too slow)

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.

I mean… there is a shader forum in graphics, so, you should get this moved

moved

1 Like

Thanks for moving.

One way could be just simply having many vector properties, and assign the positions there in script,
similar to here: Ray-traced Fake Soft Shadows (Shader) « Unity Coding – Unity3D

Then to get objects based on tags, could manually take maybe 4 closest light objects,
assign their positions to the shader properties… (pos1, pos2, po3…)