LitParticle.hlsl 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. #include "Uniforms.hlsl"
  2. #include "Samplers.hlsl"
  3. #include "Transform.hlsl"
  4. #include "Lighting.hlsl"
  5. #include "ScreenPos.hlsl"
  6. #include "Fog.hlsl"
  7. #if defined(COMPILEPS) && defined(SOFTPARTICLES)
  8. #ifndef D3D11
  9. // D3D9 uniform
  10. uniform float cSoftParticleFadeScale;
  11. #else
  12. // D3D11 constant buffer
  13. cbuffer CustomPS : register(b6)
  14. {
  15. float cSoftParticleFadeScale;
  16. }
  17. #endif
  18. #endif
  19. void VS(float4 iPos : POSITION,
  20. #if !defined(BILLBOARD) && !defined(TRAILFACECAM)
  21. float3 iNormal : NORMAL,
  22. #endif
  23. #ifndef NOUV
  24. float2 iTexCoord : TEXCOORD0,
  25. #endif
  26. #ifdef VERTEXCOLOR
  27. float4 iColor : COLOR0,
  28. #endif
  29. #ifdef SKINNED
  30. float4 iBlendWeights : BLENDWEIGHT,
  31. int4 iBlendIndices : BLENDINDICES,
  32. #endif
  33. #ifdef INSTANCED
  34. float4x3 iModelInstance : TEXCOORD4,
  35. #endif
  36. #if defined(BILLBOARD) || defined(DIRBILLBOARD)
  37. float2 iSize : TEXCOORD1,
  38. #endif
  39. #if defined(TRAILFACECAM) || defined(TRAILBONE)
  40. float4 iTangent : TANGENT,
  41. #endif
  42. out float2 oTexCoord : TEXCOORD0,
  43. #ifdef SOFTPARTICLES
  44. out float4 oScreenPos : TEXCOORD1,
  45. #endif
  46. out float4 oWorldPos : TEXCOORD3,
  47. #if PERPIXEL
  48. #ifdef SHADOW
  49. out float4 oShadowPos[NUMCASCADES] : TEXCOORD4,
  50. #endif
  51. #ifdef SPOTLIGHT
  52. out float4 oSpotPos : TEXCOORD5,
  53. #endif
  54. #ifdef POINTLIGHT
  55. out float3 oCubeMaskVec : TEXCOORD5,
  56. #endif
  57. #else
  58. out float3 oVertexLight : TEXCOORD4,
  59. #endif
  60. #ifdef VERTEXCOLOR
  61. out float4 oColor : COLOR0,
  62. #endif
  63. #if defined(D3D11) && defined(CLIPPLANE)
  64. out float oClip : SV_CLIPDISTANCE0,
  65. #endif
  66. out float4 oPos : OUTPOSITION)
  67. {
  68. // Define a 0,0 UV coord if not expected from the vertex data
  69. #ifdef NOUV
  70. float2 iTexCoord = float2(0.0, 0.0);
  71. #endif
  72. float4x3 modelMatrix = iModelMatrix;
  73. float3 worldPos = GetWorldPos(modelMatrix);
  74. oPos = GetClipPos(worldPos);
  75. oTexCoord = GetTexCoord(iTexCoord);
  76. oWorldPos = float4(worldPos, GetDepth(oPos));
  77. #if defined(D3D11) && defined(CLIPPLANE)
  78. oClip = dot(oPos, cClipPlane);
  79. #endif
  80. #ifdef SOFTPARTICLES
  81. oScreenPos = GetScreenPos(oPos);
  82. #endif
  83. #ifdef VERTEXCOLOR
  84. oColor = iColor;
  85. #endif
  86. #ifdef PERPIXEL
  87. // Per-pixel forward lighting
  88. float4 projWorldPos = float4(worldPos.xyz, 1.0);
  89. #ifdef SHADOW
  90. // Shadow projection: transform from world space to shadow space
  91. GetShadowPos(projWorldPos, float3(0, 0, 0), oShadowPos);
  92. #endif
  93. #ifdef SPOTLIGHT
  94. // Spotlight projection: transform from world space to projector texture coordinates
  95. oSpotPos = mul(projWorldPos, cLightMatrices[0]);
  96. #endif
  97. #ifdef POINTLIGHT
  98. oCubeMaskVec = mul(worldPos - cLightPos.xyz, (float3x3)cLightMatrices[0]);
  99. #endif
  100. #else
  101. // Ambient & per-vertex lighting
  102. oVertexLight = GetAmbient(GetZonePos(worldPos));
  103. #ifdef NUMVERTEXLIGHTS
  104. for (int i = 0; i < NUMVERTEXLIGHTS; ++i)
  105. oVertexLight += GetVertexLightVolumetric(i, worldPos) * cVertexLights[i * 3].rgb;
  106. #endif
  107. #endif
  108. }
  109. void PS(float2 iTexCoord : TEXCOORD0,
  110. #ifdef SOFTPARTICLES
  111. float4 iScreenPos: TEXCOORD1,
  112. #endif
  113. float4 iWorldPos : TEXCOORD3,
  114. #ifdef PERPIXEL
  115. #ifdef SHADOW
  116. float4 iShadowPos[NUMCASCADES] : TEXCOORD4,
  117. #endif
  118. #ifdef SPOTLIGHT
  119. float4 iSpotPos : TEXCOORD5,
  120. #endif
  121. #ifdef POINTLIGHT
  122. float3 iCubeMaskVec : TEXCOORD5,
  123. #endif
  124. #else
  125. float3 iVertexLight : TEXCOORD4,
  126. #endif
  127. #ifdef VERTEXCOLOR
  128. float4 iColor : COLOR0,
  129. #endif
  130. #if defined(D3D11) && defined(CLIPPLANE)
  131. float iClip : SV_CLIPDISTANCE0,
  132. #endif
  133. out float4 oColor : OUTCOLOR0)
  134. {
  135. // Get material diffuse albedo
  136. #ifdef DIFFMAP
  137. float4 diffInput = Sample2D(DiffMap, iTexCoord);
  138. #ifdef ALPHAMASK
  139. if (diffInput.a < 0.5)
  140. discard;
  141. #endif
  142. float4 diffColor = cMatDiffColor * diffInput;
  143. #else
  144. float4 diffColor = cMatDiffColor;
  145. #endif
  146. #ifdef VERTEXCOLOR
  147. diffColor *= iColor;
  148. #endif
  149. // Get fog factor
  150. #ifdef HEIGHTFOG
  151. float fogFactor = GetHeightFogFactor(iWorldPos.w, iWorldPos.y);
  152. #else
  153. float fogFactor = GetFogFactor(iWorldPos.w);
  154. #endif
  155. // Soft particle fade
  156. // In expand mode depth test should be off. In that case do manual alpha discard test first to reduce fill rate
  157. #ifdef SOFTPARTICLES
  158. #ifdef EXPAND
  159. if (diffColor.a < 0.01)
  160. discard;
  161. #endif
  162. float particleDepth = iWorldPos.w;
  163. float depth = Sample2DProj(DepthBuffer, iScreenPos).r;
  164. #ifdef HWDEPTH
  165. depth = ReconstructDepth(depth);
  166. #endif
  167. #ifdef EXPAND
  168. float diffZ = max(particleDepth - depth, 0.0) * (cFarClipPS - cNearClipPS);
  169. float fade = saturate(diffZ * cSoftParticleFadeScale);
  170. #else
  171. float diffZ = (depth - particleDepth) * (cFarClipPS - cNearClipPS);
  172. float fade = saturate(1.0 - diffZ * cSoftParticleFadeScale);
  173. #endif
  174. diffColor.a = max(diffColor.a - fade, 0.0);
  175. #endif
  176. #ifdef PERPIXEL
  177. // Per-pixel forward lighting
  178. float3 lightColor;
  179. float3 finalColor;
  180. float diff = GetDiffuseVolumetric(iWorldPos.xyz);
  181. #ifdef SHADOW
  182. diff *= GetShadow(iShadowPos, iWorldPos.w);
  183. #endif
  184. #if defined(SPOTLIGHT)
  185. lightColor = iSpotPos.w > 0.0 ? Sample2DProj(LightSpotMap, iSpotPos).rgb * cLightColor.rgb : 0.0;
  186. #elif defined(CUBEMASK)
  187. lightColor = texCUBE(sLightCubeMap, iCubeMaskVec).rgb * cLightColor.rgb;
  188. #else
  189. lightColor = cLightColor.rgb;
  190. #endif
  191. finalColor = diff * lightColor * diffColor.rgb;
  192. oColor = float4(GetLitFog(finalColor, fogFactor), diffColor.a);
  193. #else
  194. // Ambient & per-vertex lighting
  195. float3 finalColor = iVertexLight * diffColor.rgb;
  196. oColor = float4(GetFog(finalColor, fogFactor), diffColor.a);
  197. #endif
  198. }