Skip to content

Instantly share code, notes, and snippets.

@jaekwon
Created February 11, 2013 00:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jaekwon/4751779 to your computer and use it in GitHub Desktop.
Save jaekwon/4751779 to your computer and use it in GitHub Desktop.
# Add custom shader
# http://www.html5rocks.com/en/tutorials/webgl/shaders/
vertexShader = """
// create a shared variable for the
// VS and FS containing the normal
varying vec3 vNormal;
void main() {
// set the vNormal value with
// the attribute value passed
// in by Three.js
vNormal = normal;
gl_Position = projectionMatrix *
modelViewMatrix *
vec4(position,1.0);
}
"""
fragmentShader = """
// same name and type as VS
varying vec3 vNormal;
void main() {
// calc the dot product and clamp
// 0 -> 1 rather than -1 -> 1
vec3 light = vec3(0.5,0.2,1.0);
// ensure it's normalized
light = normalize(light);
// calculate the dot product of
// the light to the vertex normal
float dProd = max(0.0, dot(vNormal, light));
// feed into our frag colour
gl_FragColor = vec4(dProd, dProd, dProd, 1.0);
}
"""
shaderMaterial = new THREE.ShaderMaterial({vertexShader, fragmentShader})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment