Jump to content

  • Log In with Google      Sign In   
  • Create Account

We're offering banner ads on our site from just $5!

1. Details HERE. 2. GDNet+ Subscriptions HERE. 3. Ad upload HERE.


Don't forget to read Tuesday's email newsletter for your chance to win a free copy of Construct 2!


How to change colour based on viewing angle


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
2 replies to this topic

#1 Daniel Wilson   Members   -  Reputation: 159

Like
0Likes
Like

Posted 13 August 2012 - 02:52 PM

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!

Sponsor:

#2 phil_t   Crossbones+   -  Reputation: 3948

Like
1Likes
Like

Posted 13 August 2012 - 05:33 PM

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).

Edited by phil_t, 13 August 2012 - 05:33 PM.


#3 Daniel Wilson   Members   -  Reputation: 159

Like
0Likes
Like

Posted 14 August 2012 - 07:54 AM

Very nice thanks for that cool answer, the effect is simple works well for my purposes!




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS