TerrainBlend.glsl 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. #include "Uniforms.glsl"
  2. #include "Samplers.glsl"
  3. #include "Transform.glsl"
  4. #include "ScreenPos.glsl"
  5. #include "Lighting.glsl"
  6. #include "Fog.glsl"
  7. varying vec2 vTexCoord;
  8. #ifndef GL_ES
  9. varying vec2 vDetailTexCoord;
  10. #else
  11. varying mediump vec2 vDetailTexCoord;
  12. #endif
  13. varying vec3 vNormal;
  14. varying vec4 vWorldPos;
  15. #ifdef PERPIXEL
  16. #ifdef SHADOW
  17. #ifndef GL_ES
  18. varying vec4 vShadowPos[NUMCASCADES];
  19. #else
  20. varying highp vec4 vShadowPos[NUMCASCADES];
  21. #endif
  22. #endif
  23. #ifdef SPOTLIGHT
  24. varying vec4 vSpotPos;
  25. #endif
  26. #ifdef POINTLIGHT
  27. varying vec3 vCubeMaskVec;
  28. #endif
  29. #else
  30. varying vec3 vVertexLight;
  31. varying vec4 vScreenPos;
  32. #ifdef ENVCUBEMAP
  33. varying vec3 vReflectionVec;
  34. #endif
  35. #if defined(LIGHTMAP) || defined(AO)
  36. varying vec2 vTexCoord2;
  37. #endif
  38. #endif
  39. uniform sampler2D sWeightMap0;
  40. uniform sampler2D sDetailMap1;
  41. uniform sampler2D sDetailMap2;
  42. uniform sampler2D sDetailMap3;
  43. #ifndef GL_ES
  44. uniform vec2 cDetailTiling;
  45. #else
  46. uniform mediump vec2 cDetailTiling;
  47. #endif
  48. void VS()
  49. {
  50. mat4 modelMatrix = iModelMatrix;
  51. vec3 worldPos = GetWorldPos(modelMatrix);
  52. gl_Position = GetClipPos(worldPos);
  53. vNormal = GetWorldNormal(modelMatrix);
  54. vWorldPos = vec4(worldPos, GetDepth(gl_Position));
  55. vTexCoord = GetTexCoord(iTexCoord);
  56. vDetailTexCoord = cDetailTiling * vTexCoord;
  57. #ifdef PERPIXEL
  58. // Per-pixel forward lighting
  59. vec4 projWorldPos = vec4(worldPos, 1.0);
  60. #ifdef SHADOW
  61. // Shadow projection: transform from world space to shadow space
  62. for (int i = 0; i < NUMCASCADES; i++)
  63. vShadowPos[i] = GetShadowPos(i, vNormal, projWorldPos);
  64. #endif
  65. #ifdef SPOTLIGHT
  66. // Spotlight projection: transform from world space to projector texture coordinates
  67. vSpotPos = projWorldPos * cLightMatrices[0];
  68. #endif
  69. #ifdef POINTLIGHT
  70. vCubeMaskVec = (worldPos - cLightPos.xyz) * mat3(cLightMatrices[0][0].xyz, cLightMatrices[0][1].xyz, cLightMatrices[0][2].xyz);
  71. #endif
  72. #else
  73. // Ambient & per-vertex lighting
  74. #if defined(LIGHTMAP) || defined(AO)
  75. // If using lightmap, disregard zone ambient light
  76. // If using AO, calculate ambient in the PS
  77. vVertexLight = vec3(0.0, 0.0, 0.0);
  78. vTexCoord2 = iTexCoord1;
  79. #else
  80. vVertexLight = GetAmbient(GetZonePos(worldPos));
  81. #endif
  82. #ifdef NUMVERTEXLIGHTS
  83. for (int i = 0; i < NUMVERTEXLIGHTS; ++i)
  84. vVertexLight += GetVertexLight(i, worldPos, vNormal) * cVertexLights[i * 3].rgb;
  85. #endif
  86. vScreenPos = GetScreenPos(gl_Position);
  87. #ifdef ENVCUBEMAP
  88. vReflectionVec = worldPos - cCameraPos;
  89. #endif
  90. #endif
  91. }
  92. void PS()
  93. {
  94. // Get material diffuse albedo
  95. vec3 weights = texture2D(sWeightMap0, vTexCoord).rgb;
  96. float sumWeights = weights.r + weights.g + weights.b;
  97. weights /= sumWeights;
  98. vec4 diffColor = cMatDiffColor * (
  99. weights.r * texture2D(sDetailMap1, vDetailTexCoord) +
  100. weights.g * texture2D(sDetailMap2, vDetailTexCoord) +
  101. weights.b * texture2D(sDetailMap3, vDetailTexCoord)
  102. );
  103. // Get material specular albedo
  104. vec3 specColor = cMatSpecColor.rgb;
  105. // Get normal
  106. vec3 normal = normalize(vNormal);
  107. // Get fog factor
  108. #ifdef HEIGHTFOG
  109. float fogFactor = GetHeightFogFactor(vWorldPos.w, vWorldPos.y);
  110. #else
  111. float fogFactor = GetFogFactor(vWorldPos.w);
  112. #endif
  113. #if defined(PERPIXEL)
  114. // Per-pixel forward lighting
  115. vec3 lightColor;
  116. vec3 lightDir;
  117. vec3 finalColor;
  118. float diff = GetDiffuse(normal, vWorldPos.xyz, lightDir);
  119. #ifdef SHADOW
  120. diff *= GetShadow(vShadowPos, vWorldPos.w);
  121. #endif
  122. #if defined(SPOTLIGHT)
  123. lightColor = vSpotPos.w > 0.0 ? texture2DProj(sLightSpotMap, vSpotPos).rgb * cLightColor.rgb : vec3(0.0, 0.0, 0.0);
  124. #elif defined(CUBEMASK)
  125. lightColor = textureCube(sLightCubeMap, vCubeMaskVec).rgb * cLightColor.rgb;
  126. #else
  127. lightColor = cLightColor.rgb;
  128. #endif
  129. #ifdef SPECULAR
  130. float spec = GetSpecular(normal, cCameraPosPS - vWorldPos.xyz, lightDir, cMatSpecColor.a);
  131. finalColor = diff * lightColor * (diffColor.rgb + spec * specColor * cLightColor.a);
  132. #else
  133. finalColor = diff * lightColor * diffColor.rgb;
  134. #endif
  135. #ifdef AMBIENT
  136. finalColor += cAmbientColor.rgb * diffColor.rgb;
  137. finalColor += cMatEmissiveColor;
  138. gl_FragColor = vec4(GetFog(finalColor, fogFactor), diffColor.a);
  139. #else
  140. gl_FragColor = vec4(GetLitFog(finalColor, fogFactor), diffColor.a);
  141. #endif
  142. #elif defined(PREPASS)
  143. // Fill light pre-pass G-Buffer
  144. float specPower = cMatSpecColor.a / 255.0;
  145. gl_FragData[0] = vec4(normal * 0.5 + 0.5, specPower);
  146. gl_FragData[1] = vec4(EncodeDepth(vWorldPos.w), 0.0);
  147. #elif defined(DEFERRED)
  148. // Fill deferred G-buffer
  149. float specIntensity = specColor.g;
  150. float specPower = cMatSpecColor.a / 255.0;
  151. gl_FragData[0] = vec4(GetFog(vVertexLight * diffColor.rgb, fogFactor), 1.0);
  152. gl_FragData[1] = fogFactor * vec4(diffColor.rgb, specIntensity);
  153. gl_FragData[2] = vec4(normal * 0.5 + 0.5, specPower);
  154. gl_FragData[3] = vec4(EncodeDepth(vWorldPos.w), 0.0);
  155. #else
  156. // Ambient & per-vertex lighting
  157. vec3 finalColor = vVertexLight * diffColor.rgb;
  158. #ifdef MATERIAL
  159. // Add light pre-pass accumulation result
  160. // Lights are accumulated at half intensity. Bring back to full intensity now
  161. vec4 lightInput = 2.0 * texture2DProj(sLightBuffer, vScreenPos);
  162. vec3 lightSpecColor = lightInput.a * lightInput.rgb / max(GetIntensity(lightInput.rgb), 0.001);
  163. finalColor += lightInput.rgb * diffColor.rgb + lightSpecColor * specColor;
  164. #endif
  165. gl_FragColor = vec4(GetFog(finalColor, fogFactor), diffColor.a);
  166. #endif
  167. }