If not lit, use different texture

Is there a shader or way of making it so that when a sprite is currently being lit by a light then it uses a different texture? For example you have a big 10x10 area with 1x1 black squares, when a light is shone on the squares they become pink.

2 Answers

2

What to recommend here is highly dependent on the larger context of your app. It is possible to do something like you describe with a shader (though you’d have to write it since I cannot think of one posted on the net that already does this), but simulating the light using distance and angle calculations may also be a solution. it all depends on the needs of your app.

In your shader, use a dot product to determine at a most basic level how much light this square is receiving. You want the dot product between the light direction and the surface normal. Something like this:

if(dot(normalDirection, lightDirection) != 0) {
    //this area is being lit at LEAST a little, so make it red
    return half4(1, 0, 0, 1);
} else {
    //not getting any light, make it white
    return half4(1, 1, 1, 1);
}

The dot product will be 1 for total light exposure, and 0 for none. Values inbetween are varied levels.

This is a super crude example, but what you want to do is entirely possible if you are willing to write a custom shader.