Hi,
I’m wondering if there’s a way to create a shader that changes its properties depending upon a vertex’s normal positions relative to the camera. for example, I want a look that when the vertex normal is perpendicular to the camera it is transparent and when a vertex normal is parallel to the camera it is opaque. Any suggestions on where to start?
In general, what you’re after can be achieved in several ways:
Calculate what you need in a vertex shader, based on vertex normal. This is what X-Ray shader does.
Use one of automatic texture generation modes, e.g. TexGen CubeNormal - this would generate texture coordinates based on eye space normals. Then use special texture ramp that maps those eye space normals to something you need (e.g. transparency).
is the special texture ramp just a gradient file that goes from black to white? Also, do I need to compensate for objects that are different sizes that use the same material/shader? e.g. if I have a cube that is 5x5x5 and another cube that is 1x2x2 do I need to adjust for this somehow as the shader references the same ramp image? what resolution does the ramp image need to be? Also, can I generate the ramp with code instead of a graphic to optimize performance?
But going the TexGen route, a texture generation mode like CubeNormal would generate texture coordinates that are eye-space normals. You’ll most likely want to juse Z component of the normal to index into your texture (because Z will be zero when normal is perpendicular to view direction; and +1 or -1 - I forget which one - when looking straight to the viewer).
Now, regular textures use two texture coordinates; and here you’ll want to use 3rd texture coordinate. So it won’t “just work” out of the box. For that to work you’d set up a texture matrix that remaps Z component into X or Y component.
So now you’d have any models rendered with this TexGen mode + matrix having their texture coordinates that depend on normal’s direction towards or away from the viewer.
If you’d use a texture that goes from red to green, for example, then models would be colored red to green depending on normal’s angle to the camera. The texture could have any gradient you want.
It’s probably a lot simpler just to use a vertex shader to do the above (texgen + matrix) thing, just like XRay shader does.