• Advertisement
Sign in to follow this  

How to change colour based on viewing angle

This topic is 1985 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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.

Thankyou!

Share this post


Link to post
Share on other sites
Advertisement
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:

[b]float3 directionToCamera = normalize(cameraPosition - input.WorldPosition);[/b] // cameraPosition is a shader constant, WorldPosition is passed through to PS
[b]float dp = dot(directionToCamera, input.WorldNormal);[/b] // 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). Edited by phil_t

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement