2
0

opengl_fragment.glsl 1.4 KB

1234567891011121314151617181920212223242526272829303132333435
  1. uniform sampler2D baseTexture;
  2. uniform sampler2D normalTexture;
  3. uniform vec3 yawVec;
  4. varying lowp vec4 varColor;
  5. varying mediump vec2 varTexCoord;
  6. void main (void)
  7. {
  8. vec2 uv = varTexCoord.st;
  9. //texture sampling rate
  10. const float step = 1.0 / 256.0;
  11. float tl = texture2D(normalTexture, vec2(uv.x - step, uv.y + step)).r;
  12. float t = texture2D(normalTexture, vec2(uv.x, uv.y + step)).r;
  13. float tr = texture2D(normalTexture, vec2(uv.x + step, uv.y + step)).r;
  14. float r = texture2D(normalTexture, vec2(uv.x + step, uv.y )).r;
  15. float br = texture2D(normalTexture, vec2(uv.x + step, uv.y - step)).r;
  16. float b = texture2D(normalTexture, vec2(uv.x, uv.y - step)).r;
  17. float bl = texture2D(normalTexture, vec2(uv.x - step, uv.y - step)).r;
  18. float l = texture2D(normalTexture, vec2(uv.x - step, uv.y )).r;
  19. float dX = (tr + 2.0 * r + br) - (tl + 2.0 * l + bl);
  20. float dY = (bl + 2.0 * b + br) - (tl + 2.0 * t + tr);
  21. vec4 bump = vec4 (normalize(vec3 (dX, dY, 0.1)),1.0);
  22. float height = 2.0 * texture2D(normalTexture, vec2(uv.x, uv.y)).r - 1.0;
  23. vec4 base = texture2D(baseTexture, uv).rgba;
  24. vec3 L = normalize(vec3(0.0, 0.75, 1.0));
  25. float specular = pow(clamp(dot(reflect(L, bump.xyz), yawVec), 0.0, 1.0), 1.0);
  26. float diffuse = dot(yawVec, bump.xyz);
  27. vec3 color = (1.1 * diffuse + 0.05 * height + 0.5 * specular) * base.rgb;
  28. vec4 col = vec4(color.rgb, base.a);
  29. col *= varColor;
  30. gl_FragColor = vec4(col.rgb, base.a);
  31. }