*Effect Achieved! (Solved)* Please Help, Shader that preserves spot light color

Hello Everyone!

In an effort to make my game look better I wanted to make a very specific effect. A white grid with black squares. I will have a bullet shot across the grid and any spot lights directed at the grid will do what is below in the picture but keep the rest of the grid pure white. The results i’m getting right now is

This is my testing so it doesn’t look like a grid. But moving on. If i set the mat to be pure white it won’t show any light. What I want to achieve is the picture below.

Also if I have a red light and a blue light I want them to blend together in the overlapped parts to be a purple. Just learning about shaders, following tutorials, and looking at the docs. Still confused on how to actually access the lights/light colors in the scene.
Any help is greatly appreciated.


So to do what you want the way you’re trying to do it will be quite difficult. The interaction between the lights and the white surface is kind of the opposite of how lighting works, and because of the way Unity does its lighting in multiple passes makes it even more difficult.

Basically Unity’s lighting system is done with additive passes. If the surface already rendered pure white no amount of light added to that will be anything but the white it already is. You could cheat it and use subtractive lighting (white surface with a green light will make it red), but then you won’t get the purple when two lights mix (to get red and blue you need green and orange lights which when subtractively mixed gets you black).

What you’ll actually need to do is probably not use lights at all, but instead pass positions and colors to the shader using an array.

I literally forgot I made this post. Still working on the issue, watched a ton of Shader tutorials so I understand more. And your suggested solution seems like the easiest way to achieve this effect. Thank you bgolus for pointing me in the right direction and I’ll post again when I get this working.

Alright so as bgolus suggested I’m getting the bullet xyz from a C# script and syncing it with my shader with…

The line code below is in a regular mono behavior C# script (I Update the values every 0.1 seconds)

material.SetFloatArray("_Point_Lights_X", lightPosX);
material.SetFloatArray("_Point_Lights_Y", lightPosY);

In the vertexFunction of the shader I convert the local vertex to world position

OUT.worldPosition = mul(unity_ObjectToWorld, IN.vertex);

Then I just do some checks in the fragment function to see if the X of the current pixel is near one of the bullet points

// In Fragment Function (This is called for every pixel)
float4 pixel = float4(1,1,1,1); // default is white
for (int i = 0; i < _PointsLength; i++)
{ 
if (IN.worldPosition.x > _Point_Lights_X[i] && IN.worldPosition.x < _Point_Lights_X[i]+0.05)
{
     if (IN.worldPosition.y > _Point_Lights_Y[i] && IN.worldPosition.y < _Point_Lights_Y[i]+0.05)
     {
                                // Set the color to red
                                pixel.r = 1.0f;
                                pixel.g = 0.0f;
                                pixel.b = 0.0f;
    }
                         
}
}
return pixel;

I’m still learning but I hope in the future this might help someone since forum posts from years ago is usually where I find a lot of my answers XD

WorkInProgress Image

So after working with shaders for a few days it dawned on me that I could achieve my effect without using shaders. By using sprites I achieved my effect in less then 10 minutes. So in the end, I didn’t need to learn shaders for this effect but I’m sure I’ll do something cool with this new found knowledge.

My Solution:

Also I’m using an unlit mat on those white and black squares so they stay the pure white and black colors unless sprites effect them.