Hi there, I’ve written a custom shader that does some depth and projection mapping, and has to run with metal for an iPad.
What I want to do is get the current light for a given fragment. I have a light in the scene that moves around, and I need to be able to tell if that light hits a frag, as in it doesn’t have to pass through a mesh to get to that frag. I don’t necessarily need to tell the strength of the light at that frag, just if any light at all hits it as if there is light, the colour of that frag will become the result of my projection map, and if there is no light the colour will become the result of my depth map.
Does anyone know where I can start? I’m really new to shaders so I’ve tried looking at lighting tutorials, but they all seem to try and fake lighting instead of taking an actual scene light into consideration.
I can’t use any form of baked lighting because the mesh for my scene is generated at runtime and changes almost every frame, and the camera moves every frame.
I’ve got some bad news for you … everything a GPU does when it comes to lighting is fake.
Seriously though, to detect if there’s something “in the way” between two positions, you either need raytracing, or you need to render the depth of the objects between those two positions. The later is what shadow mapping is; render from the ‘view’ of the light the depth of all opaque objects then sample that depth texture using the projection matrix of the light’s view to see if there’s something closer than the position of the surface you’re testing from. There’s no way to do this in a single shader, it takes a couple extra steps to get the data you need in a render texture before hand.
Ah, the legendary bgolus. I’ve seen so many of your responses over the last week while I’ve been scouring the internet for shader resources, and they’ve helped quite a bit.
Thanks for your answer, I ended up doing something similar to that and just a few minutes ago I got it to work by throwing lighting out the window.
For those that come find this later, I made an additional camera underneath my main camera, set the target texture to a render texture, blit that texture through a material with a basic Image Effect Shader with:
I could technically make this more accurate by splitting my Linear01Depth into R, G, B and A instead of duplicating the same value, but it works good enough right now. It’s accurate enough up to 10 meters because that’s what all of my cameras have as their far clip plane.