View Single Post
# 24 11-09-2003 , 07:26 PM
kbrown's Avatar
Moderator
Join Date: Sep 2002
Location: London, UK
Posts: 3,198
Ok here's one which I just figured out.

Driving a shader attribute with distance from camera

Let's dig in to this with an example:

1. Create a lambert shader
2. Create a ramp texture (delete the place2dTexture node if it is created)
3. Connect the ramp's output to the lambert's color channel
4. Create a sampler info node
5. Create an expression with following code:
Code:
// NOTE: I'm using default object names here

vector $vPw = << samplerInfo1.pointWorldX, samplerInfo1.pointWorldY, samplerInfo1.pointWorldZ >>;
vector $vCamPos = << persp.translateX, persp.translateY, persp.translateZ >>;

float $fDistance = mag($vPw - $vCamPos);

ramp1.vCoord = $fDistance / 50; // The divider (50) is acting like a max distance value. You may need to adjust it depending on your scene size.
To test this, create a nurbs plane. Scale it in X and Z by 20. Apply the new lambert shader to it and render from different angles. You should see that the plane is red at near distance, green in the middle and blue in far distance. If you get repeats and banding then you need to adjust the divider in the expression.


Kari
- My Website
- My IMDB

Do a lot, Fail a lot and Learn a lot!

Last edited by kbrown; 11-09-2003 at 07:33 PM.