My 2D game has stealth elements where I want the lights to affect the player’s visibility to enemies. When there is 2D lights illuminating the player sprite, is should be more visible to enemies.
I would just need a way to get the intensity of light on a certain point, or sprite, but that doesn’t seem possible.
The current solution in my head would be to calculate the intensity at a point by looping through all the lights, and factoring in their radius and intensity. But then I would also have to figure out if the spot light angle of certain lights even points towards the player and things of that nature. Overall it seems computationally intensive and not the most accurate. Just too many factors to consider with different light shapes and fall off.
Certainly a relatively heavy bruteforce method would be to rerender the scene with untextured shaders (using camera.SetReplacementShader) with a 2nd camera onto a RenderTexture and then read a pixel at player pos like here: RenderTexture ReadPixels GetPixel shortcut
Whether you do this or classic computations two optimization options:
- Disable/ignore lights that are clearly out of range.
- Dplon’t calculate this every frame. 20 times per second is surely enough
Btw. don’t underestimate the power of modern PCs. as long as the data is prepared in an array, comparing thousands of distances is no challenge.
turn on shaded mode, it lets you view the object with its lights and shadows
Look at the Frame Debugger with your 2d lights on, there should be a section on _LightTexture’s. You can use those textures in your shaders to highlight your sprites etc.
here is some explanations on light textures
PS: you can access the light textures even if your shader isnt sprite custom lit