Skip to content

Instantly share code, notes, and snippets.

@guidoschmidt
Created June 6, 2017 09:10
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save guidoschmidt/282f2fc311b6a5f3a88ed2bc9bb6f830 to your computer and use it in GitHub Desktop.
Save guidoschmidt/282f2fc311b6a5f3a88ed2bc9bb6f830 to your computer and use it in GitHub Desktop.
GLSL vertex normal computation
mat3 getTangentBasis( in vec3 tangent_y )
{
vec3 UpVector = vec3(0,1,0);
vec3 tangent_x = normalize( cross( UpVector, tangent_y ) );
vec3 tangent_z = cross( tangent_y, tangent_x );
return mat3( tangent_x, tangent_y, tangent_z );
}
vec3 applyDeformation( in vec3 pos, in vec3 norm )
{
// .. displace vertex
}
vec3 computeVertexNormal( in vec3 pos, in vec3 norm )
{
float offset = 0.1;
mat3 basis = getTangentBasis( norm );
vec3 xv = offset * basis[0];
vec3 zv = offset * basis[2];
vec3 x0 = applyDeformation( pos.xyz - xv, norm ).xyz;
vec3 x1 = applyDeformation( pos.xyz + xv, norm ).xyz;
vec3 z0 = applyDeformation( pos.xyz - zv, norm ).xyz;
vec3 z1 = applyDeformation( pos.xyz + zv, norm ).xyz;
return cross( x1 - x0, z1 - z0 );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment