TerrainBlend.glsl 6.1 KB

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