Forward.hlsl 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. #include "Common.hlsl"
  2. void VS(float4 iPos : POSITION,
  3. float3 iNormal : NORMAL,
  4. #ifdef NORMALMAP
  5. float4 iTangent : TANGENT0,
  6. #endif
  7. float2 iTexCoord : TEXCOORD0,
  8. #ifdef SKINNED
  9. float4 iBlendWeights : BLENDWEIGHT,
  10. int4 iBlendIndices : BLENDINDICES,
  11. #endif
  12. #ifdef INSTANCED
  13. float4x3 iModelInstance : TEXCOORD2,
  14. #endif
  15. #ifdef BILLBOARD
  16. float2 iSize : TEXCOORD1,
  17. #endif
  18. out float4 oWorldPos : TEXCOORD1,
  19. out float3 oNormal : TEXCOORD2,
  20. #ifdef NORMALMAP
  21. out float3 oTangent : TEXCOORD3,
  22. out float3 oBitangent : TEXCOORD4,
  23. #endif
  24. #ifdef SHADOW
  25. out float4 oShadowPos : TEXCOORD5,
  26. #endif
  27. #ifdef SPOTLIGHT
  28. out float4 oSpotPos : TEXCOORD6,
  29. #endif
  30. #ifdef VERTEXCOLOR
  31. float4 iColor : COLOR0,
  32. out float4 oColor : COLOR0,
  33. #endif
  34. out float4 oPos : POSITION,
  35. out float2 oTexCoord : TEXCOORD0)
  36. {
  37. float4 pos;
  38. #if (!defined(SKINNED)) && (!defined(INSTANCED)) && (!defined(BILLBOARD))
  39. #ifndef NORMALMAP
  40. pos = GetPositionNormal(iPos, iNormal, oPos, oNormal);
  41. #else
  42. pos = GetPositionNormalTangent(iPos, iNormal, iTangent, oPos, oNormal, oTangent);
  43. #endif
  44. #endif
  45. #ifdef SKINNED
  46. #ifndef NORMALMAP
  47. pos = GetPositionNormalSkinned(iPos, iNormal, iBlendWeights, iBlendIndices, oPos, oNormal);
  48. #else
  49. pos = GetPositionNormalTangentSkinned(iPos, iNormal, iTangent, iBlendWeights, iBlendIndices, oPos, oNormal, oTangent);
  50. #endif
  51. #endif
  52. #ifdef INSTANCED
  53. #ifndef NORMALMAP
  54. pos = GetPositionNormalInstanced(iPos, iNormal, iModelInstance, oPos, oNormal);
  55. #else
  56. pos = GetPositionNormalTangentInstanced(iPos, iNormal, iTangent, iModelInstance, oPos, oNormal, oTangent);
  57. #endif
  58. #endif
  59. #ifdef BILLBOARD
  60. pos = GetPositionBillboard(iPos, iSize, oPos);
  61. oNormal = float3(-cCameraRot[2][0], -cCameraRot[2][1], -cCameraRot[2][2]);
  62. #endif
  63. // Store adjusted world position and linear depth for light calculations
  64. oWorldPos = float4(pos.xyz - cCameraPos, GetDepth(oPos));
  65. #ifdef SHADOW
  66. // Shadow projection: transform from world space to shadow space
  67. oShadowPos = mul(pos, cShadowProj);
  68. #endif
  69. #ifdef SPOTLIGHT
  70. // Spotlight projection: transform from world space to projector texture coordinates
  71. oSpotPos = mul(pos, cSpotProj);
  72. #endif
  73. #ifdef NORMALMAP
  74. oBitangent = cross(oTangent, oNormal) * iTangent.w;
  75. #endif
  76. #ifdef VERTEXCOLOR
  77. oColor = iColor;
  78. #endif
  79. oTexCoord = GetTexCoord(iTexCoord);
  80. }
  81. void PS(float2 iTexCoord : TEXCOORD0,
  82. float4 iWorldPos : TEXCOORD1,
  83. #ifdef NORMALMAP
  84. float3 iNormal : TEXCOORD2,
  85. float3 iTangent : TEXCOORD3,
  86. float3 iBitangent : TEXCOORD4,
  87. #else
  88. float3 iNormal : TEXCOORD2,
  89. #endif
  90. #if defined(SHADOW)
  91. float4 iShadowPos : TEXCOORD5,
  92. #endif
  93. #ifdef SPOTLIGHT
  94. float4 iSpotPos : TEXCOORD6,
  95. #endif
  96. #ifdef VERTEXCOLOR
  97. float4 iColor : COLOR0,
  98. #endif
  99. out float4 oColor : COLOR0)
  100. {
  101. #ifdef DIFFMAP
  102. float4 diffColor = cMatDiffColor * tex2D(sDiffMap, iTexCoord);
  103. #else
  104. float4 diffColor = cMatDiffColor;
  105. #endif
  106. #ifdef VERTEXCOLOR
  107. diffColor *= iColor;
  108. #endif
  109. #if (!defined(VOLUMETRIC)) && ((defined(DIRLIGHT)) || (defined(POINTLIGHT)) || (defined(SPOTLIGHT)))
  110. float3 lightColor;
  111. float3 lightDir;
  112. float3 lightVec;
  113. float diff;
  114. #ifdef NORMALMAP
  115. float3x3 tbn = float3x3(iTangent, iBitangent, iNormal);
  116. float3 normal = normalize(mul(UnpackNormal(tex2D(sNormalMap, iTexCoord)), tbn));
  117. #else
  118. float3 normal = normalize(iNormal);
  119. #endif
  120. #ifdef DIRLIGHT
  121. diff = GetDiffuseDir(normal, lightDir) * GetSplitFade(iWorldPos.w);
  122. #else
  123. diff = GetDiffusePointOrSpot(normal, iWorldPos.xyz, lightDir, lightVec);
  124. #endif
  125. #ifdef SM3
  126. if (diff > 0.0)
  127. {
  128. #endif
  129. #ifdef SHADOW
  130. diff *= GetShadow(iShadowPos);
  131. #endif
  132. #ifdef SPOTLIGHT
  133. lightColor = iSpotPos.w > 0.0 ? tex2Dproj(sLightSpotMap, iSpotPos).rgb * cLightColor.rgb : 0.0;
  134. #else
  135. #ifdef CUBEMASK
  136. lightColor = texCUBE(sLightCubeMap, mul(lightVec, cLightVecRot)).rgb * cLightColor.rgb;
  137. #else
  138. lightColor = cLightColor.rgb;
  139. #endif
  140. #endif
  141. #ifdef SPECULAR
  142. #ifdef SPECMAP
  143. float specStrength = cMatSpecProperties.x * tex2D(sSpecMap, iTexCoord).g;
  144. #else
  145. float specStrength = cMatSpecProperties.x;
  146. #endif
  147. float spec = GetSpecular(normal, iWorldPos.xyz, lightDir, cMatSpecProperties.y);
  148. float3 finalColor = diff * lightColor * (diffColor.rgb + spec * specStrength * cLightColor.a);
  149. #else
  150. float3 finalColor = diff * lightColor * diffColor.rgb;
  151. #endif
  152. #ifdef AMBIENT
  153. finalColor += cAmbientColor * diffColor.rgb;
  154. oColor = float4(GetFog(finalColor, iWorldPos.w), diffColor.a);
  155. #else
  156. oColor = float4(GetLitFog(finalColor, iWorldPos.w), diffColor.a);
  157. #endif
  158. #ifdef SM3
  159. }
  160. else
  161. {
  162. #ifdef AMBIENT
  163. float3 finalColor = cAmbientColor * diffColor.rgb;
  164. oColor = float4(GetFog(finalColor, iWorldPos.w), diffColor.a);
  165. #else
  166. oColor = float4(0.0, 0.0, 0.0, diffColor.a);
  167. #endif
  168. }
  169. #endif
  170. #else
  171. #if (defined(VOLUMETRIC)) && ((defined(DIRLIGHT)) || (defined(POINTLIGHT)) || (defined(SPOTLIGHT)))
  172. float3 lightColor;
  173. float3 lightVec;
  174. float diff;
  175. #ifdef DIRLIGHT
  176. diff = GetDiffuseDirVolumetric() * GetSplitFade(iWorldPos.w);
  177. #else
  178. diff = GetDiffusePointOrSpotVolumetric(iWorldPos.xyz, lightVec);
  179. #endif
  180. #ifdef SPOTLIGHT
  181. lightColor = iSpotPos.w > 0.0 ? tex2Dproj(sLightSpotMap, iSpotPos).rgb * cLightColor.rgb : 0.0;
  182. #else
  183. #ifdef CUBEMASK
  184. lightColor = texCUBE(sLightCubeMap, mul(lightVec, cLightVecRot)).rgb * cLightColor.rgb;
  185. #else
  186. lightColor = cLightColor.rgb;
  187. #endif
  188. #endif
  189. float3 finalColor = diff * lightColor * diffColor.rgb;
  190. #ifdef AMBIENT
  191. finalColor += cAmbientColor * diffColor.rgb;
  192. oColor = float4(GetFog(finalColor, iWorldPos.w), diffColor.a);
  193. #else
  194. oColor = float4(GetLitFog(finalColor, iWorldPos.w), diffColor.a);
  195. #endif
  196. #else
  197. #ifdef UNLIT
  198. oColor = float4(GetFog(diffColor.rgb, iWorldPos.w), diffColor.a);
  199. #endif
  200. #ifdef ADDITIVE
  201. oColor = float4(GetLitFog(diffColor.rgb, iWorldPos.w), diffColor.a);
  202. #endif
  203. #ifdef AMBIENT
  204. float3 finalColor = cAmbientColor * diffColor.rgb;
  205. oColor = float4(GetFog(finalColor, iWorldPos.w), diffColor.a);
  206. #endif
  207. #endif
  208. #endif
  209. }