# How to write a custom shader in Unity 3D that lights up a specific pixel or group of pixel?

I'm making a FPS game in Unity, and I want the environment to light up as the player is shooting on his environment. The map would be entirely put in darkness at the beginning, but as the player shoots the environment, it would be rendered with its "real texture". So say I have a tree. First it is entirely black or greyish, but if I shoot somewhere, I would see some green. To accomplish this, I'm using a raycast to get the impact point and so I can access any renderer of the point that the player is shooting on. I guess the next step would be to write a custom shader to light the exact pixel that is shot. Do you have any idea how I could write this shader or another way of doing this effect?

Regards

• What is the effect you are looking for? A lighter point where the shot hit? You could just spawn a light at that point. Nov 3 '15 at 15:36
• I want the map to be entirely put in darkness at the beginning, but as the player shoots the environment, it would be rendered with its "real texture". So say I have a tree. First it is entirely black or greyish, but if I shoot somewhere, I would see some green. Nov 3 '15 at 15:40

So basically you are looking to paint your environment as it is shot?

1. Apply 2 textures to your object, the main texture being pure black, the secondary being the actual coloured texture.
2. Grab the point on the object that the mesh was struck
3. Convert this point into the x,y coordinate on the texture itself
4. Grab the pixel (and maybe surrounding pixels) and adjust the alpha of the main texture to 0 to reveal the one underneath

Check this docs.unity3d.com/ScriptReference/RaycastHit-textureCoord.html

There are probably some other better ways of achieving this, but I believe that this would work, in fact I might even try it out.

• It may be preferable to use a smaller, non-alpha, format as a stencil. Texture2D<bool> would be perfect but doesn't exist. If you write 1's wherever clicked, then the shader becomes Color * Stencil (Color or 0).
– Jon
Mar 20 '16 at 7:29