Skip to content

Instantly share code, notes, and snippets.

@robashton
Created April 19, 2011 20:38
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 robashton/929593 to your computer and use it in GitHub Desktop.
Save robashton/929593 to your computer and use it in GitHub Desktop.
shader.shader
attribute vec3 aVertexPosition;
attribute vec3 aNormal;
attribute vec2 aTextureCoord;
uniform mat4 uProjection;
uniform mat4 uView;
uniform mat4 uWorld;
uniform mat3 uNormal;
uniform vec3 vLight;
uniform vec3 uViewDirection;
varying vec3 vNormal;
varying vec3 vVectorToLight;
varying vec3 vViewDirection;
varying vec2 vTextureCoord;
varying vec3 vTangent;
varying vec3 vBiTangent;
varying vec3 vHalfVector;
void main(void){
vec4 vTransformedPosition = uView * uWorld * vec4(aVertexPosition, 1.0);
// Calculate vector from light
vec3 vectorToLight = vec3(vec4(vLight.x, vLight.y, vLight.z, 1.0) - vTransformedPosition);
vectorToLight = normalize(vectorToLight);
// Transform tangent/bitangent/normal
vTangent = normalize(uNormal * vec3(1,0,0));
vNormal = normalize(uNormal * aNormal);
vBiTangent = cross(vTangent, vNormal);
// Transform the light vector into texture space
vVectorToLight = vec3(
dot(vectorToLight, vTangent),
dot(vectorToLight, vBiTangent),
dot(vectorToLight, vNormal));
vVectorToLight = normalize(vVectorToLight);
// Transform the eye into texture space
vViewDirection = vec3(
dot(uViewDirection, vTangent),
dot(uViewDirection, vBiTangent),
dot(uViewDirection, vNormal));
vViewDirection = normalize(vViewDirection);
// Transform the half vector into texture space
vHalfVector = normalize((vViewDirection + vVectorToLight) / 2.0);
vHalfVector = vec3(
dot(vHalfVector, vTangent),
dot(vHalfVector, vBiTangent),
dot(vHalfVector, vNormal));
// And finally set the texture coords + position
gl_Position = uProjection * vTransformedPosition;
vTextureCoord = aTextureCoord;
}
#ifdef GL_ES
precision highp float;
#endif
varying vec2 vTextureCoord;
varying vec3 vVectorToLight;
varying vec3 vViewDirection;
varying vec3 vNormal;
varying vec3 vTangent;
varying vec3 vBiTangent;
varying vec3 vHalfVector;
uniform sampler2D uBumpSampler;
void main(void) {
// Sample the texture to get our new normal in texture space
vec3 bumpNormal = (texture2D(uBumpSampler, vTextureCoord.st)).rgb * 2.0 - 1.0;
// Calculate diffuse against bump map
vec3 light = normalize(vVectorToLight);
float diffuse = clamp(dot(light, vNormal), 0.0, 1.0);
// Calculate specular against bump map
vec3 specular = vec3(0.0, 0.0, 1.0) * ( pow( dot(vHalfVector, vNormal), 20.0) );
// Botch phong
gl_FragColor = vec4(diffuse, diffuse, diffuse, 1.0) + vec4(specular, 1.0);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment