LitParticle.hlsl 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. #include "Uniforms.hlsl"
  2. #include "Samplers.hlsl"
  3. #include "Transform.hlsl"
  4. #include "Lighting.hlsl"
  5. #include "Fog.hlsl"
  6. void VS(float4 iPos : POSITION,
  7. #if !defined(BILLBOARD) && !defined(TRAILFACECAM)
  8. float3 iNormal : NORMAL,
  9. #endif
  10. #ifndef NOUV
  11. float2 iTexCoord : TEXCOORD0,
  12. #endif
  13. #ifdef VERTEXCOLOR
  14. float4 iColor : COLOR0,
  15. #endif
  16. #ifdef SKINNED
  17. float4 iBlendWeights : BLENDWEIGHT,
  18. int4 iBlendIndices : BLENDINDICES,
  19. #endif
  20. #ifdef INSTANCED
  21. float4x3 iModelInstance : TEXCOORD4,
  22. #endif
  23. #if defined(BILLBOARD) || defined(DIRBILLBOARD)
  24. float2 iSize : TEXCOORD1,
  25. #endif
  26. #if defined(TRAILFACECAM) || defined(TRAILBONE)
  27. float4 iTangent : TANGENT,
  28. #endif
  29. out float2 oTexCoord : TEXCOORD0,
  30. out float4 oWorldPos : TEXCOORD3,
  31. #if PERPIXEL
  32. #ifdef SHADOW
  33. out float4 oShadowPos[NUMCASCADES] : TEXCOORD4,
  34. #endif
  35. #ifdef SPOTLIGHT
  36. out float4 oSpotPos : TEXCOORD5,
  37. #endif
  38. #ifdef POINTLIGHT
  39. out float3 oCubeMaskVec : TEXCOORD5,
  40. #endif
  41. #else
  42. out float3 oVertexLight : TEXCOORD4,
  43. #endif
  44. #ifdef VERTEXCOLOR
  45. out float4 oColor : COLOR0,
  46. #endif
  47. #if defined(D3D11) && defined(CLIPPLANE)
  48. out float oClip : SV_CLIPDISTANCE0,
  49. #endif
  50. out float4 oPos : OUTPOSITION)
  51. {
  52. // Define a 0,0 UV coord if not expected from the vertex data
  53. #ifdef NOUV
  54. float2 iTexCoord = float2(0.0, 0.0);
  55. #endif
  56. float4x3 modelMatrix = iModelMatrix;
  57. float3 worldPos = GetWorldPos(modelMatrix);
  58. oPos = GetClipPos(worldPos);
  59. oTexCoord = GetTexCoord(iTexCoord);
  60. oWorldPos = float4(worldPos, GetDepth(oPos));
  61. #if defined(D3D11) && defined(CLIPPLANE)
  62. oClip = dot(oPos, cClipPlane);
  63. #endif
  64. #ifdef VERTEXCOLOR
  65. oColor = iColor;
  66. #endif
  67. #ifdef PERPIXEL
  68. // Per-pixel forward lighting
  69. float4 projWorldPos = float4(worldPos.xyz, 1.0);
  70. #ifdef SHADOW
  71. // Shadow projection: transform from world space to shadow space
  72. GetShadowPos(projWorldPos, float3(0, 0, 0), oShadowPos);
  73. #endif
  74. #ifdef SPOTLIGHT
  75. // Spotlight projection: transform from world space to projector texture coordinates
  76. oSpotPos = mul(projWorldPos, cLightMatrices[0]);
  77. #endif
  78. #ifdef POINTLIGHT
  79. oCubeMaskVec = mul(worldPos - cLightPos.xyz, (float3x3)cLightMatrices[0]);
  80. #endif
  81. #else
  82. // Ambient & per-vertex lighting
  83. oVertexLight = GetAmbient(GetZonePos(worldPos));
  84. #ifdef NUMVERTEXLIGHTS
  85. for (int i = 0; i < NUMVERTEXLIGHTS; ++i)
  86. oVertexLight += GetVertexLightVolumetric(i, worldPos) * cVertexLights[i * 3].rgb;
  87. #endif
  88. #endif
  89. }
  90. void PS(float2 iTexCoord : TEXCOORD0,
  91. float4 iWorldPos : TEXCOORD3,
  92. #ifdef PERPIXEL
  93. #ifdef SHADOW
  94. float4 iShadowPos[NUMCASCADES] : TEXCOORD4,
  95. #endif
  96. #ifdef SPOTLIGHT
  97. float4 iSpotPos : TEXCOORD5,
  98. #endif
  99. #ifdef POINTLIGHT
  100. float3 iCubeMaskVec : TEXCOORD5,
  101. #endif
  102. #else
  103. float3 iVertexLight : TEXCOORD4,
  104. #endif
  105. #ifdef VERTEXCOLOR
  106. float4 iColor : COLOR0,
  107. #endif
  108. #if defined(D3D11) && defined(CLIPPLANE)
  109. float iClip : SV_CLIPDISTANCE0,
  110. #endif
  111. out float4 oColor : OUTCOLOR0)
  112. {
  113. // Get material diffuse albedo
  114. #ifdef DIFFMAP
  115. float4 diffInput = Sample2D(DiffMap, iTexCoord);
  116. #ifdef ALPHAMASK
  117. if (diffInput.a < 0.5)
  118. discard;
  119. #endif
  120. float4 diffColor = cMatDiffColor * diffInput;
  121. #else
  122. float4 diffColor = cMatDiffColor;
  123. #endif
  124. #ifdef VERTEXCOLOR
  125. diffColor *= iColor;
  126. #endif
  127. // Get fog factor
  128. #ifdef HEIGHTFOG
  129. float fogFactor = GetHeightFogFactor(iWorldPos.w, iWorldPos.y);
  130. #else
  131. float fogFactor = GetFogFactor(iWorldPos.w);
  132. #endif
  133. #ifdef PERPIXEL
  134. // Per-pixel forward lighting
  135. float3 lightColor;
  136. float3 finalColor;
  137. float diff = GetDiffuseVolumetric(iWorldPos.xyz);
  138. #ifdef SHADOW
  139. diff *= GetShadow(iShadowPos, iWorldPos.w);
  140. #endif
  141. #if defined(SPOTLIGHT)
  142. lightColor = iSpotPos.w > 0.0 ? Sample2DProj(LightSpotMap, iSpotPos).rgb * cLightColor.rgb : 0.0;
  143. #elif defined(CUBEMASK)
  144. lightColor = texCUBE(sLightCubeMap, iCubeMaskVec).rgb * cLightColor.rgb;
  145. #else
  146. lightColor = cLightColor.rgb;
  147. #endif
  148. finalColor = diff * lightColor * diffColor.rgb;
  149. oColor = float4(GetLitFog(finalColor, fogFactor), diffColor.a);
  150. #else
  151. // Ambient & per-vertex lighting
  152. float3 finalColor = iVertexLight * diffColor.rgb;
  153. oColor = float4(GetFog(finalColor, fogFactor), diffColor.a);
  154. #endif
  155. }