attribute vec4 a_Position; attribute vec3 a_Normal; attribute vec3 a_TangentX; attribute vec2 a_TexCoord; varying vec3 v_Position; varying vec2 v_TexCoord; varying vec3 v_TangentX; varying vec3 v_Normal; uniform sampler2D u_Sampler0; uniform sampler2D u_Sampler1; uniform float u_Displace; uniform vec3 u_LightColor; uniform vec3 u_LightDirection; uniform vec3 u_LightAmbient; uniform float u_MaterialEmit; uniform float u_MaterialSpecularExponent; uniform float u_MaterialBump; uniform mat4 u_MMatrix; uniform mat4 u_VMatrix; uniform mat4 u_PMatrix; uniform mat4 u_NMatrix; //////////////// void main() { float displaceVal = (texture2D(u_Sampler1, a_TexCoord).x - 0.5) * u_Displace; vec4 posDisplaced = a_Position + vec4(a_Normal, 0.0) * displaceVal; vec4 v = u_VMatrix * u_MMatrix * posDisplaced; vec4 n = u_NMatrix * vec4(a_Normal, 0); vec4 t = u_MMatrix * vec4(a_TangentX, 0); gl_Position = u_PMatrix * v; v_TexCoord = a_TexCoord; v_Normal = normalize(n.xyz); v_TangentX = normalize(t.xyz); v_Position = v.xyz / v.w; } ////////////////// // blinn-phong specular reflection vec3 blinnphong(vec3 viewdir, vec3 normal, vec3 lightdir, vec3 lightcol, vec3 matspecular, float matspecexp) { vec3 halfdir = normalize(viewdir + lightdir); float specAng = max(dot(halfdir, normal),0.0); float specular = pow(specAng, matspecexp); return matspecular * lightcol * specular; } void main() { vec2 texcoord = v_TexCoord * 0.48 + vec2(0.01, 0.01); // look up texture values vec3 matDiffuse = texture2D(u_Sampler0, texcoord + vec2(0.0,0.0)).xyz; vec3 matSpecular = texture2D(u_Sampler0, texcoord + vec2(0.5,0.0)).xyz; vec3 matNormal = texture2D(u_Sampler0, texcoord + vec2(0.0,0.5)).xyz; vec3 matEmit = texture2D(u_Sampler0, texcoord + vec2(0.5,0.5)).xyz; // re-normalize bump value ([0,1] => [-1,1]) matNormal = matNormal * 2.0 - vec3(1.0, 1.0, 1.0); // compute other tangent vec3 tany = normalize(cross(v_Normal, v_TangentX)); // rotate the normal vec3 norm_rot = normalize(v_Normal * matNormal.z + v_TangentX * matNormal.x + tany * matNormal.y); // control the bump amount vec3 norm = (norm_rot * u_MaterialBump) + (v_Normal * (1.0 - u_MaterialBump)); vec3 viewdir = normalize(-v_Position); float nDotL = max(dot(u_LightDirection, norm), 0.0); vec3 diffuse = u_LightColor * matDiffuse * nDotL; vec3 ambient = u_LightAmbient * matDiffuse; vec3 emit = matEmit * u_MaterialEmit; vec3 specular = blinnphong(viewdir, norm, u_LightDirection, u_LightColor, matSpecular, u_MaterialSpecularExponent) * 2.0; gl_FragColor = vec4(diffuse + ambient + emit + specular, 1.0); }