PBRTerrainBlend.hlsl 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. #include "Uniforms.hlsl"
  2. #include "Samplers.hlsl"
  3. #include "Constants.hlsl"
  4. #include "Transform.hlsl"
  5. #include "ScreenPos.hlsl"
  6. #include "Lighting.hlsl"
  7. #include "Fog.hlsl"
  8. #include "PBR.hlsl"
  9. #include "IBL.hlsl"
  10. #ifndef D3D11
  11. // D3D9 uniforms and samplers
  12. #ifdef COMPILEVS
  13. uniform float2 cDetailTiling;
  14. #else
  15. sampler2D sWeightMap0 : register(s0);
  16. sampler2D sDetailMap1 : register(s1);
  17. sampler2D sDetailMap2 : register(s2);
  18. sampler2D sDetailMap3 : register(s3);
  19. #endif
  20. #else
  21. // D3D11 constant buffers and samplers
  22. #ifdef COMPILEVS
  23. cbuffer CustomVS : register(b6)
  24. {
  25. float2 cDetailTiling;
  26. }
  27. #else
  28. Texture2D tWeightMap0 : register(t0);
  29. Texture2D tDetailMap1 : register(t1);
  30. Texture2D tDetailMap2 : register(t2);
  31. Texture2D tDetailMap3 : register(t3);
  32. SamplerState sWeightMap0 : register(s0);
  33. SamplerState sDetailMap1 : register(s1);
  34. SamplerState sDetailMap2 : register(s2);
  35. SamplerState sDetailMap3 : register(s3);
  36. #endif
  37. #endif
  38. void VS(float4 iPos : POSITION,
  39. float3 iNormal : NORMAL,
  40. float2 iTexCoord : TEXCOORD0,
  41. #ifdef SKINNED
  42. float4 iBlendWeights : BLENDWEIGHT,
  43. int4 iBlendIndices : BLENDINDICES,
  44. #endif
  45. #ifdef INSTANCED
  46. float4x3 iModelInstance : TEXCOORD4,
  47. #endif
  48. #if defined(BILLBOARD) || defined(DIRBILLBOARD)
  49. float2 iSize : TEXCOORD1,
  50. #endif
  51. out float2 oTexCoord : TEXCOORD0,
  52. out float3 oNormal : TEXCOORD1,
  53. out float4 oWorldPos : TEXCOORD2,
  54. out float2 oDetailTexCoord : TEXCOORD3,
  55. #ifdef PERPIXEL
  56. #ifdef SHADOW
  57. out float4 oShadowPos[NUMCASCADES] : TEXCOORD4,
  58. #endif
  59. #ifdef SPOTLIGHT
  60. out float4 oSpotPos : TEXCOORD5,
  61. #endif
  62. #ifdef POINTLIGHT
  63. out float3 oCubeMaskVec : TEXCOORD5,
  64. #endif
  65. #else
  66. out float3 oVertexLight : TEXCOORD4,
  67. out float4 oScreenPos : TEXCOORD5,
  68. #endif
  69. #ifdef VERTEXCOLOR
  70. out float4 oColor : COLOR0,
  71. #endif
  72. #if defined(D3D11) && defined(CLIPPLANE)
  73. out float oClip : SV_CLIPDISTANCE0,
  74. #endif
  75. out float4 oPos : OUTPOSITION)
  76. {
  77. const float4x3 modelMatrix = iModelMatrix;
  78. const float3 worldPos = GetWorldPos(modelMatrix);
  79. oPos = GetClipPos(worldPos);
  80. oNormal = GetWorldNormal(modelMatrix);
  81. oWorldPos = float4(worldPos, GetDepth(oPos));
  82. oTexCoord = GetTexCoord(iTexCoord);
  83. oDetailTexCoord = cDetailTiling * oTexCoord;
  84. #if defined(D3D11) && defined(CLIPPLANE)
  85. oClip = dot(oPos, cClipPlane);
  86. #endif
  87. #ifdef PERPIXEL
  88. // Per-pixel forward lighting
  89. const float4 projWorldPos = float4(worldPos.xyz, 1.0);
  90. #ifdef SHADOW
  91. // Shadow projection: transform from world space to shadow space
  92. GetShadowPos(projWorldPos, oNormal, oShadowPos);
  93. #endif
  94. #ifdef SPOTLIGHT
  95. // Spotlight projection: transform from world space to projector texture coordinates
  96. oSpotPos = mul(projWorldPos, cLightMatrices[0]);
  97. #endif
  98. #ifdef POINTLIGHT
  99. oCubeMaskVec = mul(worldPos - cLightPos.xyz, (float3x3)cLightMatrices[0]);
  100. #endif
  101. #else
  102. oVertexLight = GetAmbient(GetZonePos(worldPos));
  103. #ifdef NUMVERTEXLIGHTS
  104. for (int i = 0; i < NUMVERTEXLIGHTS; ++i)
  105. oVertexLight += GetVertexLight(i, worldPos, oNormal) * cVertexLights[i * 3].rgb;
  106. #endif
  107. oScreenPos = GetScreenPos(oPos);
  108. #endif
  109. }
  110. void PS(
  111. float2 iTexCoord : TEXCOORD0,
  112. float3 iNormal : TEXCOORD1,
  113. float4 iWorldPos : TEXCOORD2,
  114. float2 iDetailTexCoord : TEXCOORD3,
  115. #ifdef PERPIXEL
  116. #ifdef SHADOW
  117. float4 iShadowPos[NUMCASCADES] : TEXCOORD4,
  118. #endif
  119. #ifdef SPOTLIGHT
  120. float4 iSpotPos : TEXCOORD5,
  121. #endif
  122. #ifdef POINTLIGHT
  123. float3 iCubeMaskVec : TEXCOORD5,
  124. #endif
  125. #else
  126. float3 iVertexLight : TEXCOORD4,
  127. float4 iScreenPos : TEXCOORD5,
  128. #endif
  129. #ifdef VERTEXCOLOR
  130. float4 iColor : COLOR0,
  131. #endif
  132. #if defined(D3D11) && defined(CLIPPLANE)
  133. float iClip : SV_CLIPDISTANCE0,
  134. #endif
  135. #ifdef PREPASS
  136. out float4 oDepth : OUTCOLOR1,
  137. #endif
  138. #ifdef DEFERRED
  139. out float4 oAlbedo : OUTCOLOR1,
  140. out float4 oNormal : OUTCOLOR2,
  141. out float4 oDepth : OUTCOLOR3,
  142. #ifndef D3D11
  143. float2 iFragPos : VPOS,
  144. #else
  145. float4 iFragPos : SV_Position,
  146. #endif
  147. #endif
  148. out float4 oColor : OUTCOLOR0)
  149. {
  150. // Get material diffuse albedo
  151. float3 weights = Sample2D(WeightMap0, iTexCoord).rgb;
  152. float sumWeights = weights.r + weights.g + weights.b;
  153. weights /= sumWeights;
  154. float4 diffColor = cMatDiffColor * (
  155. weights.r * Sample2D(DetailMap1, iDetailTexCoord) +
  156. weights.g * Sample2D(DetailMap2, iDetailTexCoord) +
  157. weights.b * Sample2D(DetailMap3, iDetailTexCoord)
  158. );
  159. // Get material specular albedo
  160. #ifdef METALLIC // METALNESS
  161. float4 roughMetalSrc = Sample2D(RoughMetalFresnel, iTexCoord.xy);
  162. float roughness = roughMetalSrc.r + cRoughness;
  163. float metalness = roughMetalSrc.g + cMetallic;
  164. #else
  165. float roughness = cRoughness;
  166. float metalness = cMetallic;
  167. #endif
  168. roughness *= roughness;
  169. roughness = clamp(roughness, ROUGHNESS_FLOOR, 1.0);
  170. metalness = clamp(metalness, METALNESS_FLOOR, 1.0);
  171. float3 specColor = lerp(0.08 * cMatSpecColor.rgb, diffColor.rgb, metalness);
  172. diffColor.rgb = diffColor.rgb - diffColor.rgb * metalness; // Modulate down the diffuse
  173. // Get normal
  174. const float3 normal = normalize(iNormal);
  175. // Get fog factor
  176. #ifdef HEIGHTFOG
  177. const float fogFactor = GetHeightFogFactor(iWorldPos.w, iWorldPos.y);
  178. #else
  179. const float fogFactor = GetFogFactor(iWorldPos.w);
  180. #endif
  181. #if defined(PERPIXEL)
  182. // Per-pixel forward lighting
  183. float3 lightDir;
  184. float3 lightColor;
  185. float3 finalColor;
  186. float atten = 1;
  187. #if defined(DIRLIGHT)
  188. atten = GetAtten(normal, iWorldPos.xyz, lightDir);
  189. #elif defined(SPOTLIGHT)
  190. atten = GetAttenSpot(normal, iWorldPos.xyz, lightDir);
  191. #else
  192. atten = GetAttenPoint(normal, iWorldPos.xyz, lightDir);
  193. #endif
  194. float shadow = 1.0;
  195. #ifdef SHADOW
  196. shadow *= GetShadow(iShadowPos, iWorldPos.w);
  197. #endif
  198. #if defined(SPOTLIGHT)
  199. lightColor = iSpotPos.w > 0.0 ? Sample2DProj(LightSpotMap, iSpotPos).rgb * cLightColor.rgb : 0.0;
  200. #elif defined(CUBEMASK)
  201. lightColor = SampleCube(LightCubeMap, iCubeMaskVec).rgb * cLightColor.rgb;
  202. #else
  203. lightColor = cLightColor.rgb;
  204. #endif
  205. const float3 toCamera = normalize(cCameraPosPS - iWorldPos.xyz);
  206. const float3 lightVec = normalize(lightDir);
  207. const float ndl = clamp((dot(normal, lightVec)), M_EPSILON, 1.0);
  208. float3 BRDF = GetBRDF(iWorldPos.xyz, lightDir, lightVec, toCamera, normal, roughness, diffColor.rgb, specColor);
  209. finalColor.rgb = BRDF * lightColor * (atten * shadow) / M_PI;
  210. #ifdef AMBIENT
  211. finalColor += cAmbientColor.rgb * diffColor.rgb;
  212. finalColor += cMatEmissiveColor;
  213. oColor = float4(GetFog(finalColor, fogFactor), diffColor.a);
  214. #else
  215. oColor = float4(GetLitFog(finalColor, fogFactor), diffColor.a);
  216. #endif
  217. #elif defined(DEFERRED)
  218. // Fill deferred G-buffer
  219. const float3 spareData = 0; // Can be used to pass more data to deferred renderer
  220. oColor = float4(specColor, spareData.r);
  221. oAlbedo = float4(diffColor.rgb, spareData.g);
  222. oNormal = float4(normalize(normal) * roughness, spareData.b);
  223. oDepth = iWorldPos.w;
  224. #else
  225. // Ambient & per-vertex lighting
  226. float3 finalColor = iVertexLight * diffColor.rgb;
  227. #ifdef MATERIAL
  228. // Add light pre-pass accumulation result
  229. // Lights are accumulated at half intensity. Bring back to full intensity now
  230. float4 lightInput = 2.0 * Sample2DProj(LightBuffer, iScreenPos);
  231. float3 lightSpecColor = lightInput.a * lightInput.rgb / max(GetIntensity(lightInput.rgb), 0.001);
  232. finalColor += lightInput.rgb * diffColor.rgb + lightSpecColor * specColor;
  233. #endif
  234. const float3 toCamera = normalize(iWorldPos.xyz - cCameraPosPS);
  235. const float3 reflection = normalize(reflect(toCamera, normal));
  236. float3 cubeColor = iVertexLight.rgb;
  237. #ifdef IBL
  238. const float3 iblColor = ImageBasedLighting(reflection, normal, toCamera, diffColor, specColor, roughness, cubeColor);
  239. finalColor += iblColor;
  240. #endif
  241. finalColor += cMatEmissiveColor;
  242. oColor = float4(GetFog(finalColor, fogFactor), diffColor.a);
  243. #endif
  244. }