Jump to content

Rim shader


photo

Recommended Posts

To implement a rim lighting shader (relative to camera angle) we need access to the normal and camera vector in the same co-ordinate space.

Which variables should we use in the shader for world space normal and camera direction (or some other shared space) ?

Link to comment

There is already Phong-Rim lighting implemented in Unigine. In vertex shader camera projection space is used, so camera position is (0,0,0), so -vertex.xyz is equal to camera direction. Variable normal contains vertex normal in the same space. Look at common/vertex_base_light_world.h and meshes/fragment_base_light.h shaders.

Link to comment

Thanks alexei, I used fragment_base_light as the reference.

Here's the code in case it's useful to someone

half rim = pow( 1.f - abs( dot( normalize( camera_direction ), normal ) ), rim_power ) * rim_intensity;

One caveat is that camera_direction doesn't seem to be normalized.

Link to comment
  • 2 weeks later...

One caveat is that camera_direction doesn't seem to be normalized.

Not quite sure what you mean by that, as in Unigine shaders camera_direction is normalized, check common/fragment_base_shading.h.

Link to comment

core/shaders/default/meshes/fragment_base_light.h

 

#elif DIRECT3D9
#include <core/shaders/default/meshes/fragment_base_sample.h>#ifdef PHONG_RIM || !QUALITY_MEDIUM
half3 camera_direction = IN.texcoord_2.xyz;

 

Without normalizing I get

 half rim = pow( 1.f - abs( dot( camera_direction, normal ) ), rim_power ) * rim_intensity;

post-225-0-79967600-1328531656_thumb.png

 

After normalizing I get

half rim = pow( 1.f - abs( dot( normalize( camera_direction ), normal ) ), rim_power ) * rim_intensity;

post-225-0-85833100-1328531689_thumb.png

Link to comment
×
×
  • Create New...