Matcap shader always facing the camera

Calling all shader experts :sunglasses:!

Merging DefMaterial with this Rendercam example produces this result:

rendercammaster_pkeodmatcap.zip (1.3 MB)

This happens only on mode 1, not 2, 3 or 4. Is there a way to modify the matcap shader to be stationary and always “facing” the camera, instead of following the camera?

Removing the var_position from model_spherical_enviroment_mapping.fpkind of works, but the map then appears to be 90 degrees offset:

From:
vec3 e = normalize( vec3( world_view * var_position ) );

To:
vec3 e = normalize( vec3( world_view ) );

Update:

Changing the model_spherical_enviroment_mapping.fp file to this:

// Varying in
varying highp vec4 var_position;
varying highp vec3 var_normal;
varying highp vec2 var_texcoord0;

// Uniforms
uniform highp mat4 world_view;
uniform highp sampler2D tex0;
uniform highp vec4 light;

void main() {

    vec3 e = normalize( vec3(0.0, 0.0, -90.0) );
    vec3 n = normalize( world_view * vec4(var_normal, 0.0) ).xyz;
    vec3 r = reflect( e, n );
    
    float m = 2. * sqrt(
        pow( r.x, 2. ) +
        pow( r.y, 2. ) +
        pow( r.z + 1., 2. )
    );
    vec2 vN = r.xy / m + .5;

    vec3 base = texture2D( tex0, vN).rgb;

    gl_FragColor = vec4( base, 1. ); // gl_FragColor out

}

Produces a for us acceptable result:

For some reason the example model is having a bit of a disco.

2 Likes