I would like to fake some caustics on a glassy surface very quickly, and the idea I have is to overlay a static caustic texture that appears based on the viewing angle, so when the angle is more grazing, more of the texture appears. How might I do this? A small piece of code would be seriously amazing.
You can just dot the normal of your surface with the unit vector that points from your current world position to the camera position. I have a planet atmosphere shader that does something like this (it makes things more transparent when viewed on edge, so that the atmosphere appears to gradually fade away).
In my pixel shader I have something like:
float3 directionToCamera = normalize(cameraPosition - input.WorldPosition); // cameraPosition is a shader constant, WorldPosition is passed through to PS float dp = dot(directionToCamera, input.WorldNormal); // WorldNormal is passed through to PS
Then, dp will be a value between 1 and -1. 1 when the surface faces the viewer, 0 when it's on edge, and -1 when it faces the opposite direction (if you're using backface culling presumably it won't be less than 0 in the PS). Then use this value to modify the texture amount.
You could probably do this in the vertex shader too. And you might be able to just use the viewspace normal (instead of passing cameraPosition into the shader), although I think this will give slightly different results (how much the surface faces the screen rather than the camera position).