LitParticle.hlsl 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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. float3 iNormal : NORMAL,
  8. float2 iTexCoord : TEXCOORD0,
  9. #ifdef VERTEXCOLOR
  10. float4 iColor : COLOR0,
  11. #endif
  12. #ifdef SKINNED
  13. float4 iBlendWeights : BLENDWEIGHT,
  14. int4 iBlendIndices : BLENDINDICES,
  15. #endif
  16. #ifdef INSTANCED
  17. float4x3 iModelInstance : TEXCOORD2,
  18. #endif
  19. #ifdef BILLBOARD
  20. float2 iSize : TEXCOORD1,
  21. #endif
  22. out float2 oTexCoord : TEXCOORD0,
  23. #ifdef HEIGHTFOG
  24. out float3 oWorldPos : TEXCOORD8,
  25. #endif
  26. #if PERPIXEL
  27. out float4 oLightVec : TEXCOORD1,
  28. #ifdef SPOTLIGHT
  29. out float4 oSpotPos : TEXCOORD2,
  30. #endif
  31. #ifdef POINTLIGHT
  32. out float3 oCubeMaskVec : TEXCOORD2,
  33. #endif
  34. #else
  35. out float4 oVertexLight : TEXCOORD1,
  36. #endif
  37. #ifdef VERTEXCOLOR
  38. out float4 oColor : COLOR0,
  39. #endif
  40. out float4 oPos : POSITION)
  41. {
  42. float4x3 modelMatrix = iModelMatrix;
  43. float3 worldPos = GetWorldPos(modelMatrix);
  44. oPos = GetClipPos(worldPos);
  45. oTexCoord = GetTexCoord(iTexCoord);
  46. #ifdef HEIGHTFOG
  47. oWorldPos = worldPos;
  48. #endif
  49. #ifdef VERTEXCOLOR
  50. oColor = iColor;
  51. #endif
  52. #ifdef PERPIXEL
  53. // Per-pixel forward lighting
  54. float4 projWorldPos = float4(worldPos, 1.0);
  55. #ifdef DIRLIGHT
  56. oLightVec = float4(cLightDir, GetDepth(oPos));
  57. #else
  58. oLightVec = float4((cLightPos.xyz - worldPos) * cLightPos.w, GetDepth(oPos));
  59. #endif
  60. #ifdef SPOTLIGHT
  61. // Spotlight projection: transform from world space to projector texture coordinates
  62. oSpotPos = mul(projWorldPos, cLightMatrices[0]);
  63. #endif
  64. #ifdef POINTLIGHT
  65. oCubeMaskVec = mul(oLightVec.xyz, (float3x3)cLightMatrices[0]);
  66. #endif
  67. #else
  68. // Ambient & per-vertex lighting
  69. oVertexLight = float4(GetAmbient(GetZonePos(worldPos)), GetDepth(oPos));
  70. #ifdef NUMVERTEXLIGHTS
  71. for (int i = 0; i < NUMVERTEXLIGHTS; ++i)
  72. oVertexLight.rgb += GetVertexLightVolumetric(i, worldPos) * cVertexLights[i * 3].rgb;
  73. #endif
  74. #endif
  75. }
  76. void PS(float2 iTexCoord : TEXCOORD0,
  77. #ifdef HEIGHTFOG
  78. float3 iWorldPos : TEXCOORD8,
  79. #endif
  80. #ifdef PERPIXEL
  81. float4 iLightVec : TEXCOORD1,
  82. #ifdef SPOTLIGHT
  83. float4 iSpotPos : TEXCOORD2,
  84. #endif
  85. #ifdef CUBEMASK
  86. float3 iCubeMaskVec : TEXCOORD2,
  87. #endif
  88. #else
  89. float4 iVertexLight : TEXCOORD1,
  90. #endif
  91. #ifdef VERTEXCOLOR
  92. float4 iColor : COLOR0,
  93. #endif
  94. out float4 oColor : COLOR0)
  95. {
  96. // Get material diffuse albedo
  97. #ifdef DIFFMAP
  98. float4 diffInput = tex2D(sDiffMap, iTexCoord);
  99. #ifdef ALPHAMASK
  100. if (diffInput.a < 0.5)
  101. discard;
  102. #endif
  103. float4 diffColor = cMatDiffColor * diffInput;
  104. #else
  105. float4 diffColor = cMatDiffColor;
  106. #endif
  107. #ifdef VERTEXCOLOR
  108. diffColor *= iColor;
  109. #endif
  110. #if PERPIXEL
  111. // Per-pixel forward lighting
  112. float3 lightColor;
  113. float3 finalColor;
  114. float diff;
  115. diff = GetDiffuseVolumetric(iLightVec.xyz);
  116. #if defined(SPOTLIGHT)
  117. lightColor = iSpotPos.w > 0.0 ? tex2Dproj(sLightSpotMap, iSpotPos).rgb * cLightColor.rgb : 0.0;
  118. #elif defined(CUBEMASK)
  119. lightColor = texCUBE(sLightCubeMap, iCubeMaskVec).rgb * cLightColor.rgb;
  120. #else
  121. lightColor = cLightColor.rgb;
  122. #endif
  123. #ifdef HEIGHTFOG
  124. float fogFactor = GetHeightFogFactor(iLightVec.w, iWorldPos.y);
  125. #else
  126. float fogFactor = GetFogFactor(iLightVec.w);
  127. #endif
  128. finalColor = diff * lightColor * diffColor.rgb;
  129. oColor = float4(GetLitFog(finalColor, fogFactor), diffColor.a);
  130. #else
  131. // Ambient & per-vertex lighting
  132. float3 finalColor = iVertexLight.rgb * diffColor.rgb;
  133. #ifdef HEIGHTFOG
  134. float fogFactor = GetHeightFogFactor(iVertexLight.a, iWorldPos.y);
  135. #else
  136. float fogFactor = GetFogFactor(iVertexLight.a);
  137. #endif
  138. oColor = float4(GetFog(finalColor, fogFactor), diffColor.a);
  139. #endif
  140. }