UnlitParticle.hlsl 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. #include "Uniforms.hlsl"
  2. #include "Samplers.hlsl"
  3. #include "Transform.hlsl"
  4. #include "ScreenPos.hlsl"
  5. #include "Fog.hlsl"
  6. #if defined(COMPILEPS) && defined(SOFTPARTICLES)
  7. #ifndef D3D11
  8. // D3D9 uniform
  9. uniform float cSoftParticleFadeScale;
  10. #else
  11. // D3D11 constant buffer
  12. cbuffer CustomPS : register(b6)
  13. {
  14. float cSoftParticleFadeScale;
  15. }
  16. #endif
  17. #endif
  18. void VS(float4 iPos : POSITION,
  19. #ifndef NOUV
  20. float2 iTexCoord : TEXCOORD0,
  21. #endif
  22. #ifdef VERTEXCOLOR
  23. float4 iColor : COLOR0,
  24. #endif
  25. #ifdef SKINNED
  26. float4 iBlendWeights : BLENDWEIGHT,
  27. int4 iBlendIndices : BLENDINDICES,
  28. #endif
  29. #ifdef INSTANCED
  30. float4x3 iModelInstance : TEXCOORD4,
  31. #endif
  32. #if defined(BILLBOARD) || defined(DIRBILLBOARD)
  33. float2 iSize : TEXCOORD1,
  34. #endif
  35. #if defined(DIRBILLBOARD) || defined(TRAILBONE)
  36. float3 iNormal : NORMAL,
  37. #endif
  38. #if defined(TRAILFACECAM) || defined(TRAILBONE)
  39. float4 iTangent : TANGENT,
  40. #endif
  41. out float2 oTexCoord : TEXCOORD0,
  42. #ifdef SOFTPARTICLES
  43. out float4 oScreenPos : TEXCOORD1,
  44. #endif
  45. out float4 oWorldPos : TEXCOORD2,
  46. #ifdef VERTEXCOLOR
  47. out float4 oColor : COLOR0,
  48. #endif
  49. #if defined(D3D11) && defined(CLIPPLANE)
  50. out float oClip : SV_CLIPDISTANCE0,
  51. #endif
  52. out float4 oPos : OUTPOSITION)
  53. {
  54. // Define a 0,0 UV coord if not expected from the vertex data
  55. #ifdef NOUV
  56. float2 iTexCoord = float2(0.0, 0.0);
  57. #endif
  58. float4x3 modelMatrix = iModelMatrix;
  59. float3 worldPos = GetWorldPos(modelMatrix);
  60. oPos = GetClipPos(worldPos);
  61. oTexCoord = GetTexCoord(iTexCoord);
  62. oWorldPos = float4(worldPos, GetDepth(oPos));
  63. #if defined(D3D11) && defined(CLIPPLANE)
  64. oClip = dot(oPos, cClipPlane);
  65. #endif
  66. #ifdef SOFTPARTICLES
  67. oScreenPos = GetScreenPos(oPos);
  68. #endif
  69. #ifdef VERTEXCOLOR
  70. oColor = iColor;
  71. #endif
  72. }
  73. void PS(float2 iTexCoord : TEXCOORD0,
  74. #ifdef SOFTPARTICLES
  75. float4 iScreenPos: TEXCOORD1,
  76. #endif
  77. float4 iWorldPos: TEXCOORD2,
  78. #ifdef VERTEXCOLOR
  79. float4 iColor : COLOR0,
  80. #endif
  81. #if defined(D3D11) && defined(CLIPPLANE)
  82. float iClip : SV_CLIPDISTANCE0,
  83. #endif
  84. out float4 oColor : OUTCOLOR0)
  85. {
  86. // Get material diffuse albedo
  87. #ifdef DIFFMAP
  88. float4 diffColor = cMatDiffColor * Sample2D(DiffMap, iTexCoord);
  89. #ifdef ALPHAMASK
  90. if (diffColor.a < 0.5)
  91. discard;
  92. #endif
  93. #else
  94. float4 diffColor = cMatDiffColor;
  95. #endif
  96. #ifdef VERTEXCOLOR
  97. diffColor *= iColor;
  98. #endif
  99. // Get fog factor
  100. #ifdef HEIGHTFOG
  101. float fogFactor = GetHeightFogFactor(iWorldPos.w, iWorldPos.y);
  102. #else
  103. float fogFactor = GetFogFactor(iWorldPos.w);
  104. #endif
  105. // Soft particle fade
  106. // In expand mode depth test should be off. In that case do manual alpha discard test first to reduce fill rate
  107. #ifdef SOFTPARTICLES
  108. #if defined(EXPAND) && !defined(ADDITIVE)
  109. if (diffColor.a < 0.01)
  110. discard;
  111. #endif
  112. float particleDepth = iWorldPos.w;
  113. float depth = Sample2DProj(DepthBuffer, iScreenPos).r;
  114. #ifdef HWDEPTH
  115. depth = ReconstructDepth(depth);
  116. #endif
  117. #ifdef EXPAND
  118. float diffZ = max(particleDepth - depth, 0.0) * (cFarClipPS - cNearClipPS);
  119. float fade = saturate(diffZ * cSoftParticleFadeScale);
  120. #else
  121. float diffZ = (depth - particleDepth) * (cFarClipPS - cNearClipPS);
  122. float fade = saturate(1.0 - diffZ * cSoftParticleFadeScale);
  123. #endif
  124. #ifndef ADDITIVE
  125. diffColor.a = max(diffColor.a - fade, 0.0);
  126. #else
  127. diffColor.rgb = max(diffColor.rgb - fade, float3(0.0, 0.0, 0.0));
  128. #endif
  129. #endif
  130. oColor = float4(GetFog(diffColor.rgb, fogFactor), diffColor.a);
  131. }