Skip to content

Instantly share code, notes, and snippets.

@fxthomas
Created June 17, 2011 13:17
Show Gist options
  • Save fxthomas/1031401 to your computer and use it in GitHub Desktop.
Save fxthomas/1031401 to your computer and use it in GitHub Desktop.
Vertex & Fragment Shaders for Normal recomputation
<!-- Depth Vertex Shader -->
<script id="depth-vs" type="x-shader/x-vertex">
attribute vec3 vertexPosition;
uniform mat4 modelViewMatrix;
uniform mat4 perspectiveMatrix;
varying float depth;
void main (void) {
// Compute position un screen
vec4 eye_coord = modelViewMatrix * vec4 (vertexPosition, 1.0);
depth = eye_coord.z;
gl_Position = perspectiveMatrix * eye_coord;
}
</script>
<!-- Depth Fragment Shader -->
<script id="depth-fs" type="x-shader/x-fragment">
#ifdef GL_ES
precision highp float;
#endif
uniform float min_depth;
uniform float max_depth;
varying float depth;
void main (void) {
float gray = 1. - (abs(depth) - min_depth)/(max_depth - min_depth);
gray = gray * gray;
gl_FragColor = vec4 (gray, gray, gray, 1.0);
//gl_FragColor = vec4 (1., 1., 1., 1.0);
}
</script>
<!-- Fragment Shader for recomputing normals -->
<script id="recompute-normals-fs" type="x-shader/x-fragment">
#ifdef GL_ES
precision highp float;
#endif
uniform float min_depth;
uniform float max_depth;
varying vec3 vertexColor;
void main (void) {
gl_FragColor = vec4 (vertexColor, 1.0);
}
</script>
<!-- Vertex shader for recomputing normals -->
<script id="recompute-normals-vs" type="x-shader/x-vertex">
attribute vec3 vertexPosition;
uniform mat4 modelViewMatrix;
uniform mat4 perspectiveMatrix;
uniform sampler2D texture0;
uniform float canvasWidth;
uniform float canvasHeight;
uniform float min_depth;
uniform float max_depth;
uniform vec3 lightPos;
uniform vec3 ambient;
uniform vec3 lightColor;
varying vec3 vertexColor;
void main (void) {
// Compute position on screen
gl_Position = perspectiveMatrix * modelViewMatrix * vec4 (vertexPosition, 1.);
// Compute normal
float texU = gl_Position[0];
float texV = gl_Position[1];
float stepU = 1./canvasWidth;
float stepV = 1./canvasHeight;
float depthCenter = texture2D (texture0, vec2(texU, texV)).x;
float depthTop = texture2D (texture0, vec2(texU, texV-stepV)).x;
float depthRight = texture2D (texture0, vec2(texU+stepU, texV)).x;
float depthBot = texture2D (texture0, vec2(texU, texV+stepV)).x;
float depthLeft = texture2D (texture0, vec2(texU-stepU, texV)).x;
vec3 vecTop = vec3(texU*canvasWidth, (texV-stepV)*canvasHeight, (depthTop-depthCenter)*(max_depth-min_depth)+min_depth);
vec3 vecRight = vec3((texU+stepU)*canvasWidth, texV*canvasHeight, (depthRight-depthCenter)*(max_depth-min_depth)+min_depth);
vec3 vecBot = vec3(texU*canvasWidth, (texV+texV)*canvasHeight, (depthBot-depthCenter)*(max_depth-min_depth)+min_depth);
vec3 vecLeft = vec3((texU-stepU)*canvasWidth, texV*canvasHeight, (depthLeft-depthCenter)*(max_depth-min_depth)+min_depth);
vec3 normal = normalize ((cross (vecTop, vecLeft) + cross (vecLeft, vecBot) + cross (vecBot, vecRight) + cross (vecRight, vecTop)));
vec3 lightDir = normalize (lightPos - vertexPosition);
float diffuse = max (dot (normal, lightDir), 0.0);
vertexColor = lightColor*diffuse;
vertexColor.x *= vertexColor.x;
vertexColor.y *= vertexColor.y;
vertexColor.z *= vertexColor.z;
vertexColor += ambient;
}
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment