ForwardLit.hlsl 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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 diffColor = cMatDiffColor * tex2D(sDiffMap, iTexCoord);
  134. #else
  135. float4 diffColor = cMatDiffColor;
  136. #endif
  137. #ifdef VERTEXCOLOR
  138. diffColor *= iColor;
  139. #endif
  140. #ifdef NORMALMAP
  141. float3 normal = DecodeNormal(tex2D(sNormalMap, iTexCoord));
  142. #else
  143. float3 normal = normalize(iNormal);
  144. #endif
  145. float3 lightDir;
  146. float3 lightColor;
  147. float3 finalColor;
  148. float diff;
  149. #ifdef DIRLIGHT
  150. #ifdef NORMALMAP
  151. lightDir = normalize(iLightVec.xyz);
  152. #else
  153. lightDir = iLightVec.xyz;
  154. #endif
  155. diff = GetDiffuseDir(normal, lightDir);
  156. #else
  157. diff = GetDiffusePointOrSpot(normal, iLightVec.xyz, lightDir);
  158. #endif
  159. #ifdef SHADOW
  160. #if defined(DIRLIGHT)
  161. float4 shadowPos = GetDirShadowPos(iShadowPos, iLightVec.w);
  162. diff *= saturate(GetShadow(shadowPos) + GetShadowFade(iLightVec.w));
  163. #elif defined(SPOTLIGHT)
  164. diff *= GetShadow(iShadowPos);
  165. #else
  166. diff *= GetCubeShadow(iShadowPos);
  167. #endif
  168. #endif
  169. #if defined(SPOTLIGHT)
  170. lightColor = iSpotPos.w > 0.0 ? tex2Dproj(sLightSpotMap, iSpotPos).rgb * cLightColor.rgb : 0.0;
  171. #elif defined(CUBEMASK)
  172. lightColor = texCUBE(sLightCubeMap, iCubeMaskVec).rgb * cLightColor.rgb;
  173. #else
  174. lightColor = cLightColor.rgb;
  175. #endif
  176. #ifdef SPECULAR
  177. #ifdef SPECMAP
  178. float3 specColor = cMatSpecColor.rgb * tex2D(sSpecMap, iTexCoord).g;
  179. #else
  180. float3 specColor = cMatSpecColor.rgb;
  181. #endif
  182. float spec = GetSpecular(normal, iEyeVec, lightDir, cMatSpecColor.a);
  183. finalColor = diff * lightColor * (diffColor.rgb + spec * specColor * cLightColor.a);
  184. #else
  185. finalColor = diff * lightColor * diffColor.rgb;
  186. #endif
  187. #ifdef AMBIENT
  188. finalColor += cAmbientColor * diffColor.rgb;
  189. oColor = float4(GetFog(finalColor, iLightVec.w), diffColor.a);
  190. #else
  191. oColor = float4(GetLitFog(finalColor, iLightVec.w), diffColor.a);
  192. #endif
  193. }