LitParticle.hlsl 4.9 KB

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