PBRTerrainBlend.glsl 7.1 KB

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