Created
December 11, 2017 04:00
-
-
Save raganmd/6a2e20b182f2cdd2a2128c4b8d7259d6 to your computer and use it in GitHub Desktop.
from Mike Walczyk: someone just posted this snippet in the cinder slack channel - a way of procedurally generating normals for a displaced surface, directly inside of the vertex shader
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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