TerrainBlend.hlsl 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. #include "Uniforms.hlsl"
  2. #include "Samplers.hlsl"
  3. #include "Transform.hlsl"
  4. #include "ScreenPos.hlsl"
  5. #include "Lighting.hlsl"
  6. #include "Fog.hlsl"
  7. #ifndef D3D11
  8. // D3D9 uniforms and samplers
  9. #ifdef COMPILEVS
  10. uniform float2 cDetailTiling;
  11. #else
  12. sampler2D sWeightMap0 : register(s0);
  13. sampler2D sDetailMap1 : register(s1);
  14. sampler2D sDetailMap2 : register(s2);
  15. sampler2D sDetailMap3 : register(s3);
  16. #endif
  17. #else
  18. // D3D11 constant buffers and samplers
  19. #ifdef COMPILEVS
  20. cbuffer CustomVS : register(b6)
  21. {
  22. float2 cDetailTiling;
  23. }
  24. #else
  25. Texture2D tWeightMap0 : register(t0);
  26. Texture2D tDetailMap1 : register(t1);
  27. Texture2D tDetailMap2 : register(t2);
  28. Texture2D tDetailMap3 : register(t3);
  29. SamplerState sWeightMap0 : register(s0);
  30. SamplerState sDetailMap1 : register(s1);
  31. SamplerState sDetailMap2 : register(s2);
  32. SamplerState sDetailMap3 : register(s3);
  33. #endif
  34. #endif
  35. void VS(float4 iPos : POSITION,
  36. float3 iNormal : NORMAL,
  37. float2 iTexCoord : TEXCOORD0,
  38. #ifdef SKINNED
  39. float4 iBlendWeights : BLENDWEIGHT,
  40. int4 iBlendIndices : BLENDINDICES,
  41. #endif
  42. #ifdef INSTANCED
  43. float4x3 iModelInstance : TEXCOORD4,
  44. #endif
  45. #if defined(BILLBOARD) || defined(DIRBILLBOARD)
  46. float2 iSize : TEXCOORD1,
  47. #endif
  48. #if defined(TRAILFACECAM) || defined(TRAILBONE)
  49. float4 iTangent : TANGENT,
  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. #if defined(D3D11) && defined(CLIPPLANE)
  70. out float oClip : SV_CLIPDISTANCE0,
  71. #endif
  72. out float4 oPos : OUTPOSITION)
  73. {
  74. float4x3 modelMatrix = iModelMatrix;
  75. float3 worldPos = GetWorldPos(modelMatrix);
  76. oPos = GetClipPos(worldPos);
  77. oNormal = GetWorldNormal(modelMatrix);
  78. oWorldPos = float4(worldPos, GetDepth(oPos));
  79. oTexCoord = GetTexCoord(iTexCoord);
  80. oDetailTexCoord = cDetailTiling * oTexCoord;
  81. #if defined(D3D11) && defined(CLIPPLANE)
  82. oClip = dot(oPos, cClipPlane);
  83. #endif
  84. #ifdef PERPIXEL
  85. // Per-pixel forward lighting
  86. float4 projWorldPos = float4(worldPos.xyz, 1.0);
  87. #ifdef SHADOW
  88. // Shadow projection: transform from world space to shadow space
  89. GetShadowPos(projWorldPos, oNormal, oShadowPos);
  90. #endif
  91. #ifdef SPOTLIGHT
  92. // Spotlight projection: transform from world space to projector texture coordinates
  93. oSpotPos = mul(projWorldPos, cLightMatrices[0]);
  94. #endif
  95. #ifdef POINTLIGHT
  96. oCubeMaskVec = mul(worldPos - cLightPos.xyz, (float3x3)cLightMatrices[0]);
  97. #endif
  98. #else
  99. // Ambient & per-vertex lighting
  100. oVertexLight = GetAmbient(GetZonePos(worldPos));
  101. #ifdef NUMVERTEXLIGHTS
  102. for (int i = 0; i < NUMVERTEXLIGHTS; ++i)
  103. oVertexLight += GetVertexLight(i, worldPos, oNormal) * cVertexLights[i * 3].rgb;
  104. #endif
  105. oScreenPos = GetScreenPos(oPos);
  106. #endif
  107. }
  108. void PS(float2 iTexCoord : TEXCOORD0,
  109. float3 iNormal : TEXCOORD1,
  110. float4 iWorldPos : TEXCOORD2,
  111. float2 iDetailTexCoord : TEXCOORD3,
  112. #ifdef PERPIXEL
  113. #ifdef SHADOW
  114. float4 iShadowPos[NUMCASCADES] : TEXCOORD4,
  115. #endif
  116. #ifdef SPOTLIGHT
  117. float4 iSpotPos : TEXCOORD5,
  118. #endif
  119. #ifdef POINTLIGHT
  120. float3 iCubeMaskVec : TEXCOORD5,
  121. #endif
  122. #else
  123. float3 iVertexLight : TEXCOORD4,
  124. float4 iScreenPos : TEXCOORD5,
  125. #endif
  126. #if defined(D3D11) && defined(CLIPPLANE)
  127. float iClip : SV_CLIPDISTANCE0,
  128. #endif
  129. #ifdef PREPASS
  130. out float4 oDepth : OUTCOLOR1,
  131. #endif
  132. #ifdef DEFERRED
  133. out float4 oAlbedo : OUTCOLOR1,
  134. out float4 oNormal : OUTCOLOR2,
  135. out float4 oDepth : OUTCOLOR3,
  136. #endif
  137. out float4 oColor : OUTCOLOR0)
  138. {
  139. // Get material diffuse albedo
  140. float3 weights = Sample2D(WeightMap0, iTexCoord).rgb;
  141. float sumWeights = weights.r + weights.g + weights.b;
  142. weights /= sumWeights;
  143. float4 diffColor = cMatDiffColor * (
  144. weights.r * Sample2D(DetailMap1, iDetailTexCoord) +
  145. weights.g * Sample2D(DetailMap2, iDetailTexCoord) +
  146. weights.b * Sample2D(DetailMap3, iDetailTexCoord)
  147. );
  148. // Get material specular albedo
  149. float3 specColor = cMatSpecColor.rgb;
  150. // Get normal
  151. float3 normal = normalize(iNormal);
  152. // Get fog factor
  153. #ifdef HEIGHTFOG
  154. float fogFactor = GetHeightFogFactor(iWorldPos.w, iWorldPos.y);
  155. #else
  156. float fogFactor = GetFogFactor(iWorldPos.w);
  157. #endif
  158. #if defined(PERPIXEL)
  159. // Per-pixel forward lighting
  160. float3 lightDir;
  161. float3 lightColor;
  162. float3 finalColor;
  163. float diff = GetDiffuse(normal, iWorldPos.xyz, lightDir);
  164. #ifdef SHADOW
  165. diff *= GetShadow(iShadowPos, iWorldPos.w);
  166. #endif
  167. #if defined(SPOTLIGHT)
  168. lightColor = iSpotPos.w > 0.0 ? Sample2DProj(LightSpotMap, iSpotPos).rgb * cLightColor.rgb : 0.0;
  169. #elif defined(CUBEMASK)
  170. lightColor = SampleCube(LightCubeMap, iCubeMaskVec).rgb * cLightColor.rgb;
  171. #else
  172. lightColor = cLightColor.rgb;
  173. #endif
  174. #ifdef SPECULAR
  175. float spec = GetSpecular(normal, cCameraPosPS - iWorldPos.xyz, lightDir, cMatSpecColor.a);
  176. finalColor = diff * lightColor * (diffColor.rgb + spec * specColor * cLightColor.a);
  177. #else
  178. finalColor = diff * lightColor * diffColor.rgb;
  179. #endif
  180. #ifdef AMBIENT
  181. finalColor += cAmbientColor.rgb * diffColor.rgb;
  182. finalColor += cMatEmissiveColor;
  183. oColor = float4(GetFog(finalColor, fogFactor), diffColor.a);
  184. #else
  185. oColor = float4(GetLitFog(finalColor, fogFactor), diffColor.a);
  186. #endif
  187. #elif defined(PREPASS)
  188. // Fill light pre-pass G-Buffer
  189. float specPower = cMatSpecColor.a / 255.0;
  190. oColor = float4(normal * 0.5 + 0.5, specPower);
  191. oDepth = iWorldPos.w;
  192. #elif defined(DEFERRED)
  193. // Fill deferred G-buffer
  194. float specIntensity = specColor.g;
  195. float specPower = cMatSpecColor.a / 255.0;
  196. float3 finalColor = iVertexLight * diffColor.rgb;
  197. oColor = float4(GetFog(finalColor, fogFactor), 1.0);
  198. oAlbedo = fogFactor * float4(diffColor.rgb, specIntensity);
  199. oNormal = float4(normal * 0.5 + 0.5, specPower);
  200. oDepth = iWorldPos.w;
  201. #else
  202. // Ambient & per-vertex lighting
  203. float3 finalColor = iVertexLight * diffColor.rgb;
  204. #ifdef MATERIAL
  205. // Add light pre-pass accumulation result
  206. // Lights are accumulated at half intensity. Bring back to full intensity now
  207. float4 lightInput = 2.0 * Sample2DProj(LightBuffer, iScreenPos);
  208. float3 lightSpecColor = lightInput.a * (lightInput.rgb / GetIntensity(lightInput.rgb));
  209. finalColor += lightInput.rgb * diffColor.rgb + lightSpecColor * specColor;
  210. #endif
  211. oColor = float4(GetFog(finalColor, fogFactor), diffColor.a);
  212. #endif
  213. }