Advertisement
Guest User

Untitled

a guest
Jun 30th, 2015
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.87 KB | None | 0 0
  1. uniform sampler2D baseTexture;
  2. uniform sampler2D normalTexture;
  3. uniform sampler2D useNormalmap;
  4.  
  5. uniform vec4 skyBgColor;
  6. uniform float fogDistance;
  7. uniform vec3 eyePosition;
  8.  
  9. varying vec3 vPosition;
  10. varying vec3 worldPosition;
  11. varying float area_enable_parallax;
  12.  
  13. varying vec3 eyeVec;
  14. varying vec3 tsEyeVec;
  15. varying vec3 lightVec;
  16. varying vec3 tsLightVec;
  17.  
  18. bool normalTexturePresent = false;
  19.  
  20. const float e = 2.718281828459;
  21. const float BS = 10.0;
  22.  
  23. float intensity (vec3 color)
  24. {
  25. return (color.r + color.g + color.b) / 3.0;
  26. }
  27.  
  28. float get_rgb_height (vec2 uv)
  29. {
  30. return intensity(texture2D(baseTexture,uv).rgb);
  31. }
  32.  
  33. vec4 get_normal_map(vec2 uv)
  34. {
  35. vec4 bump = texture2D(normalTexture, uv).rgba;
  36. bump.xyz = normalize(bump.xyz * 2.0 - 1.0);
  37. return bump;
  38. }
  39.  
  40. float find_intersection(vec2 dp, vec2 ds)
  41. {
  42. int linear_steps = 10;
  43. int binary_steps = 5;
  44. float depth_step = 0.1; // = 1.0 / linear_steps
  45. float size = depth_step;
  46. float depth = 1.0;
  47. float best_depth = 1.0;
  48. for (int i = 0 ; i < linear_steps ; i++) {
  49. vec4 t = texture2D(normalTexture, dp + ds * depth);
  50. if (best_depth > 0.05)
  51. if (depth >= t.a)
  52. best_depth = depth;
  53. depth -= size;
  54. }
  55. depth = best_depth - size;
  56. for (int i = 0 ; i < binary_steps ; i++) {
  57. size *= 0.5;
  58. vec4 t = texture2D(normalTexture, dp + ds * depth);
  59. if (depth >= t.a) {
  60. best_depth = depth;
  61. depth -= 2.0 * size;
  62. }
  63. depth += size;
  64. }
  65. return best_depth;
  66. }
  67.  
  68. float find_intersectionRGB(vec2 dp, vec2 ds) {
  69. int iterations = 24;
  70. float depth_step = 0.041666667; // = 1.0 / iterations;
  71. float depth = 1.0;
  72. for (int i = 0 ; i < iterations ; i++) {
  73. float h = get_rgb_height(dp + ds * depth);
  74. if (h >= depth)
  75. break;
  76. depth -= depth_step;
  77. }
  78. return depth;
  79. }
  80.  
  81. void main (void)
  82. {
  83. vec3 color;
  84. vec4 bump;
  85. vec2 uv = gl_TexCoord[0].st;
  86. bool use_normalmap = false;
  87.  
  88. #if USE_NORMALMAPS == 1
  89. if (texture2D(useNormalmap,vec2(1.0, 1.0)).r > 0.0) {
  90. normalTexturePresent = true;
  91. }
  92. #endif
  93.  
  94. #ifdef ENABLE_PARALLAX_OCCLUSION
  95. vec3 eyeRay = normalize(tsEyeVec);
  96. #if PARALLAX_OCCLUSION_MODE == 0
  97. // Parallax occlusion with slope information
  98. if (normalTexturePresent && area_enable_parallax > 0.0) {
  99. float scale = PARALLAX_OCCLUSION_SCALE / PARALLAX_OCCLUSION_ITERATIONS;
  100. float bias = PARALLAX_OCCLUSION_BIAS / PARALLAX_OCCLUSION_ITERATIONS;
  101. for(int i = 0; i < PARALLAX_OCCLUSION_ITERATIONS; i++) {
  102. vec4 normal = texture2D(normalTexture, uv.xy);
  103. float h = normal.a * scale - bias;
  104. uv += h * normal.z * eyeRay.xy;
  105. }
  106. #endif
  107. #if PARALLAX_OCCLUSION_MODE == 1
  108. // Relief mapping
  109. if (normalTexturePresent && area_enable_parallax > 0.0) {
  110. vec2 ds = eyeRay.xy * PARALLAX_OCCLUSION_SCALE;
  111. float dist = find_intersection(uv, ds);
  112. uv += dist * ds;
  113. #endif
  114. } else if (area_enable_parallax > 0.0) {
  115. vec2 ds = eyeRay.xy * PARALLAX_OCCLUSION_SCALE;
  116. float dist = find_intersectionRGB(uv, ds);
  117. uv += dist * ds;
  118. }
  119. #endif
  120.  
  121. #if USE_NORMALMAPS == 1
  122. if (normalTexturePresent) {
  123. bump = get_normal_map(uv);
  124. use_normalmap = true;
  125. }
  126. #endif
  127.  
  128. if (GENERATE_NORMALMAPS == 1 && use_normalmap == false) {
  129. float tl = get_rgb_height(vec2(uv.x - SAMPLE_STEP, uv.y + SAMPLE_STEP));
  130. float t = get_rgb_height(vec2(uv.x - SAMPLE_STEP, uv.y - SAMPLE_STEP));
  131. float tr = get_rgb_height(vec2(uv.x + SAMPLE_STEP, uv.y + SAMPLE_STEP));
  132. float r = get_rgb_height(vec2(uv.x + SAMPLE_STEP, uv.y));
  133. float br = get_rgb_height(vec2(uv.x + SAMPLE_STEP, uv.y - SAMPLE_STEP));
  134. float b = get_rgb_height(vec2(uv.x, uv.y - SAMPLE_STEP));
  135. float bl = get_rgb_height(vec2(uv.x -SAMPLE_STEP, uv.y - SAMPLE_STEP));
  136. float l = get_rgb_height(vec2(uv.x - SAMPLE_STEP, uv.y));
  137. float dX = (tr + 2.0 * r + br) - (tl + 2.0 * l + bl);
  138. float dY = (bl + 2.0 * b + br) - (tl + 2.0 * t + tr);
  139. bump = vec4(normalize(vec3 (dX, dY, NORMALMAPS_STRENGTH)), 1.0);
  140. use_normalmap = true;
  141. }
  142.  
  143. vec4 base = texture2D(baseTexture, uv).rgba;
  144.  
  145. #ifdef ENABLE_BUMPMAPPING
  146. if (use_normalmap) {
  147. vec3 L = normalize(lightVec);
  148. vec3 E = normalize(eyeVec);
  149. float specular = pow(clamp(dot(reflect(L, bump.xyz), E), 0.0, 1.0), 1.0);
  150. float diffuse = dot(-E,bump.xyz);
  151. color = (diffuse + 0.1 * specular) * base.rgb;
  152. } else {
  153. color = base.rgb;
  154. }
  155. #else
  156. color = base.rgb;
  157. #endif
  158.  
  159. #if MATERIAL_TYPE == TILE_MATERIAL_LIQUID_TRANSPARENT || MATERIAL_TYPE == TILE_MATERIAL_LIQUID_OPAQUE
  160. float alpha = gl_Color.a;
  161. vec4 col = vec4(color.rgb, alpha);
  162. col *= gl_Color;
  163. if (fogDistance != 0.0) {
  164. float d = max(0.0, min(vPosition.z / fogDistance * 1.5 - 0.6, 1.0));
  165. alpha = mix(alpha, 0.0, d);
  166. }
  167. gl_FragColor = vec4(col.rgb, alpha);
  168. #else
  169. vec4 col = vec4(color.rgb, base.a);
  170. col *= gl_Color;
  171. if (fogDistance != 0.0) {
  172. float d = max(0.0, min(vPosition.z / fogDistance * 1.5 - 0.6, 1.0));
  173. col = mix(col, skyBgColor, d);
  174. }
  175. gl_FragColor = vec4(col.rgb, base.a);
  176. #endif
  177. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement