ForwardLit.hlsl 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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. #ifdef NORMALMAP
  9. float4 iTangent : TANGENT0,
  10. #endif
  11. float2 iTexCoord : TEXCOORD0,
  12. #ifdef VERTEXCOLOR
  13. float4 iColor : COLOR0,
  14. #endif
  15. #ifdef SKINNED
  16. float4 iBlendWeights : BLENDWEIGHT,
  17. int4 iBlendIndices : BLENDINDICES,
  18. #endif
  19. #ifdef INSTANCED
  20. float4x3 iModelInstance : TEXCOORD2,
  21. #endif
  22. #ifdef BILLBOARD
  23. float2 iSize : TEXCOORD1,
  24. #endif
  25. out float2 oTexCoord : TEXCOORD0,
  26. out float4 oLightVec : TEXCOORD1,
  27. #ifndef NORMALMAP
  28. out float3 oNormal : TEXCOORD2,
  29. #endif
  30. #ifdef SPECULAR
  31. out float3 oEyeVec : TEXCOORD3,
  32. #endif
  33. #ifdef SHADOW
  34. #if defined(DIRLIGHT)
  35. out float4 oShadowPos[4] : TEXCOORD4,
  36. #elif defined(SPOTLIGHT)
  37. out float4 oShadowPos : TEXCOORD4,
  38. #else
  39. out float3 oShadowPos : TEXCOORD4,
  40. #endif
  41. #endif
  42. #ifdef SPOTLIGHT
  43. out float4 oSpotPos : TEXCOORD5,
  44. #endif
  45. #ifdef POINTLIGHT
  46. out float3 oCubeMaskVec : TEXCOORD5,
  47. #endif
  48. #ifdef VERTEXCOLOR
  49. out float4 oColor : COLOR0,
  50. #endif
  51. out float4 oPos : POSITION)
  52. {
  53. float4x3 modelMatrix = iModelMatrix;
  54. float3 worldPos = GetWorldPos(modelMatrix);
  55. oPos = GetClipPos(worldPos);
  56. oTexCoord = GetTexCoord(iTexCoord);
  57. #ifdef VERTEXCOLOR
  58. oColor = iColor;
  59. #endif
  60. #ifdef NORMALMAP
  61. float3 oNormal;
  62. float3 oTangent;
  63. float3 oBitangent;
  64. #endif
  65. oNormal = GetWorldNormal(modelMatrix);
  66. float4 projWorldPos = float4(worldPos, 1.0);
  67. #ifdef DIRLIGHT
  68. oLightVec = float4(cLightDir, GetDepth(oPos));
  69. #else
  70. oLightVec = float4((cLightPos.xyz - worldPos) * cLightPos.w, GetDepth(oPos));
  71. #endif
  72. #ifdef SHADOW
  73. // Shadow projection: transform from world space to shadow space
  74. #if defined(DIRLIGHT)
  75. oShadowPos[0] = mul(projWorldPos, cLightMatrices[0]);
  76. oShadowPos[1] = mul(projWorldPos, cLightMatrices[1]);
  77. oShadowPos[2] = mul(projWorldPos, cLightMatrices[2]);
  78. oShadowPos[3] = mul(projWorldPos, cLightMatrices[3]);
  79. #elif defined(SPOTLIGHT)
  80. oShadowPos = mul(projWorldPos, cLightMatrices[1]);
  81. #else
  82. oShadowPos = worldPos - cLightPos.xyz;
  83. #endif
  84. #endif
  85. #ifdef SPOTLIGHT
  86. // Spotlight projection: transform from world space to projector texture coordinates
  87. oSpotPos = mul(projWorldPos, cLightMatrices[0]);
  88. #endif
  89. #ifdef POINTLIGHT
  90. oCubeMaskVec = mul(oLightVec.xyz, (float3x3)cLightMatrices[0]);
  91. #endif
  92. #ifdef NORMALMAP
  93. oTangent = GetWorldTangent(modelMatrix);
  94. oBitangent = cross(oTangent, oNormal) * iTangent.w;
  95. float3x3 tbn = float3x3(oTangent, oBitangent, oNormal);
  96. oLightVec.xyz = mul(tbn, oLightVec.xyz);
  97. #ifdef SPECULAR
  98. oEyeVec = mul(tbn, cCameraPos - worldPos);
  99. #endif
  100. #elif defined(SPECULAR)
  101. oEyeVec = cCameraPos - worldPos;
  102. #endif
  103. }
  104. void PS(float2 iTexCoord : TEXCOORD0,
  105. float4 iLightVec : TEXCOORD1,
  106. #ifndef NORMALMAP
  107. float3 iNormal : TEXCOORD2,
  108. #endif
  109. #ifdef SPECULAR
  110. float3 iEyeVec : TEXCOORD3,
  111. #endif
  112. #ifdef SHADOW
  113. #if defined(DIRLIGHT)
  114. float4 iShadowPos[4] : TEXCOORD4,
  115. #elif defined(SPOTLIGHT)
  116. float4 iShadowPos : TEXCOORD4,
  117. #else
  118. float3 iShadowPos : TEXCOORD4,
  119. #endif
  120. #endif
  121. #ifdef SPOTLIGHT
  122. float4 iSpotPos : TEXCOORD5,
  123. #endif
  124. #ifdef CUBEMASK
  125. float3 iCubeMaskVec : TEXCOORD5,
  126. #endif
  127. #ifdef VERTEXCOLOR
  128. float4 iColor : COLOR0,
  129. #endif
  130. out float4 oColor : COLOR0)
  131. {
  132. #ifdef DIFFMAP
  133. float4 diffInput = tex2D(sDiffMap, iTexCoord);
  134. #ifdef ALPHAMASK
  135. if (diffInput.a < 0.5)
  136. discard;
  137. #endif
  138. float4 diffColor = cMatDiffColor * diffInput;
  139. #else
  140. float4 diffColor = cMatDiffColor;
  141. #endif
  142. #ifdef VERTEXCOLOR
  143. diffColor *= iColor;
  144. #endif
  145. #ifdef NORMALMAP
  146. float3 normal = DecodeNormal(tex2D(sNormalMap, iTexCoord));
  147. #else
  148. float3 normal = normalize(iNormal);
  149. #endif
  150. float3 lightDir;
  151. float3 lightColor;
  152. float3 finalColor;
  153. float diff;
  154. #ifdef DIRLIGHT
  155. #ifdef NORMALMAP
  156. lightDir = normalize(iLightVec.xyz);
  157. #else
  158. lightDir = iLightVec.xyz;
  159. #endif
  160. diff = GetDiffuseDir(normal, lightDir);
  161. #else
  162. diff = GetDiffusePointOrSpot(normal, iLightVec.xyz, lightDir);
  163. #endif
  164. #ifdef SHADOW
  165. #if defined(DIRLIGHT)
  166. float4 shadowPos = GetDirShadowPos(iShadowPos, iLightVec.w);
  167. diff *= saturate(GetShadow(shadowPos) + GetShadowFade(iLightVec.w));
  168. #elif defined(SPOTLIGHT)
  169. diff *= GetShadow(iShadowPos);
  170. #else
  171. diff *= GetCubeShadow(iShadowPos);
  172. #endif
  173. #endif
  174. #if defined(SPOTLIGHT)
  175. lightColor = iSpotPos.w > 0.0 ? tex2Dproj(sLightSpotMap, iSpotPos).rgb * cLightColor.rgb : 0.0;
  176. #elif defined(CUBEMASK)
  177. lightColor = texCUBE(sLightCubeMap, iCubeMaskVec).rgb * cLightColor.rgb;
  178. #else
  179. lightColor = cLightColor.rgb;
  180. #endif
  181. #ifdef SPECULAR
  182. #ifdef SPECMAP
  183. float3 specColor = cMatSpecColor.rgb * tex2D(sSpecMap, iTexCoord).g;
  184. #else
  185. float3 specColor = cMatSpecColor.rgb;
  186. #endif
  187. float spec = GetSpecular(normal, iEyeVec, lightDir, cMatSpecColor.a);
  188. finalColor = diff * lightColor * (diffColor.rgb + spec * specColor * cLightColor.a);
  189. #else
  190. finalColor = diff * lightColor * diffColor.rgb;
  191. #endif
  192. #ifdef AMBIENT
  193. finalColor += cAmbientColor * diffColor.rgb;
  194. oColor = float4(GetFog(finalColor, iLightVec.w), diffColor.a);
  195. #else
  196. oColor = float4(GetLitFog(finalColor, iLightVec.w), diffColor.a);
  197. #endif
  198. }