bump2.frag 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. /*
  2. Normal - normal texture
  3. Base_Height - Base texture with height map in alpha channel
  4. */
  5. uniform sampler2D Normal;
  6. uniform sampler2D base_tex;
  7. uniform sampler2D Base_Height; // height in alpha ch.
  8. varying vec3 g_lightVec;
  9. varying vec3 g_viewVec;
  10. uniform vec3 cBumpSize;// = 0.02 * vec2 (2.0, -1.0);
  11. void main()
  12. {
  13. float LightAttenuation = clamp(1.0 - dot(g_lightVec, g_lightVec), 0.0, 1.0);
  14. vec3 lightVec = normalize(g_lightVec);
  15. vec3 viewVec = normalize(g_viewVec);
  16. float height = texture2D(Base_Height, gl_TexCoord[0].xy).r;
  17. height = height * cBumpSize.x + cBumpSize.y;
  18. vec2 newUV = gl_TexCoord[0].xy + viewVec.xy * height;
  19. vec4 color_base = texture2D(base_tex,newUV);
  20. vec3 bump = texture2D(Normal, newUV.xy).rgb * 2.0 - 1.0;
  21. bump = normalize(bump);
  22. //vec4 base = texture2D(Base_Height, newUV.xy);
  23. float base = texture2D(Base_Height, newUV.xy).r;
  24. float diffuse = clamp(dot(lightVec, bump), 0.0, 1.0);
  25. float specular = pow(clamp(dot(reflect(-viewVec, bump), lightVec), 0.0, 1.0), 16.0);
  26. gl_FragColor = color_base * gl_LightSource[0].diffuse
  27. * (diffuse * base + 0.7 * specular)
  28. * LightAttenuation;
  29. }