LitSolid.hlsl 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. #include "Uniforms.hlsl"
  2. #include "Samplers.hlsl"
  3. #include "Transform.hlsl"
  4. #include "ScreenPos.hlsl"
  5. #include "Lighting.hlsl"
  6. #include "Fog.hlsl"
  7. // When rendering a shadowed point light, disable specular calculations on Shader Model 2 to avoid exceeding the instruction limit
  8. #if !defined(SM3) && defined(SHADOW) && defined(POINTLIGHT)
  9. #undef SPECULAR
  10. #endif
  11. void VS(float4 iPos : POSITION,
  12. float3 iNormal : NORMAL,
  13. float2 iTexCoord : TEXCOORD0,
  14. #if defined(LIGHTMAP) || defined(AO)
  15. float2 iTexCoord2 : TEXCOORD1,
  16. #endif
  17. #ifdef NORMALMAP
  18. float4 iTangent : TANGENT,
  19. #endif
  20. #ifdef SKINNED
  21. float4 iBlendWeights : BLENDWEIGHT,
  22. int4 iBlendIndices : BLENDINDICES,
  23. #endif
  24. #ifdef INSTANCED
  25. float4x3 iModelInstance : TEXCOORD2,
  26. #endif
  27. #ifdef BILLBOARD
  28. float2 iSize : TEXCOORD1,
  29. #endif
  30. #ifndef NORMALMAP
  31. out float2 oTexCoord : TEXCOORD0,
  32. #else
  33. out float4 oTexCoord : TEXCOORD0,
  34. out float4 oTangent : TEXCOORD3,
  35. #endif
  36. out float3 oNormal : TEXCOORD1,
  37. out float4 oWorldPos : TEXCOORD2,
  38. #ifdef PERPIXEL
  39. #ifdef SHADOW
  40. out float4 oShadowPos[NUMCASCADES] : TEXCOORD4,
  41. #endif
  42. #ifdef SPOTLIGHT
  43. out float4 oSpotPos : TEXCOORD5,
  44. #endif
  45. #ifdef POINTLIGHT
  46. out float3 oCubeMaskVec : TEXCOORD5,
  47. #endif
  48. #else
  49. out float3 oVertexLight : TEXCOORD4,
  50. out float4 oScreenPos : TEXCOORD5,
  51. #ifdef ENVCUBEMAP
  52. out float3 oReflectionVec : TEXCOORD6,
  53. #endif
  54. #if defined(LIGHTMAP) || defined(AO)
  55. out float2 oTexCoord2 : TEXCOORD7,
  56. #endif
  57. #endif
  58. out float4 oPos : POSITION)
  59. {
  60. float4x3 modelMatrix = iModelMatrix;
  61. float3 worldPos = GetWorldPos(modelMatrix);
  62. oPos = GetClipPos(worldPos);
  63. oNormal = GetWorldNormal(modelMatrix);
  64. oWorldPos = float4(worldPos, GetDepth(oPos));
  65. #ifdef NORMALMAP
  66. float3 tangent = GetWorldTangent(modelMatrix);
  67. float3 bitangent = cross(tangent, oNormal) * iTangent.w;
  68. oTexCoord = float4(GetTexCoord(iTexCoord), bitangent.xy);
  69. oTangent = float4(tangent, bitangent.z);
  70. #else
  71. oTexCoord = GetTexCoord(iTexCoord);
  72. #endif
  73. #ifdef PERPIXEL
  74. // Per-pixel forward lighting
  75. float4 projWorldPos = float4(worldPos.xyz, 1.0);
  76. #ifdef SHADOW
  77. // Shadow projection: transform from world space to shadow space
  78. GetShadowPos(projWorldPos, oShadowPos);
  79. #endif
  80. #ifdef SPOTLIGHT
  81. // Spotlight projection: transform from world space to projector texture coordinates
  82. oSpotPos = mul(projWorldPos, cLightMatrices[0]);
  83. #endif
  84. #ifdef POINTLIGHT
  85. oCubeMaskVec = mul(worldPos - cLightPos.xyz, (float3x3)cLightMatrices[0]);
  86. #endif
  87. #else
  88. // Ambient & per-vertex lighting
  89. #if defined(LIGHTMAP) || defined(AO)
  90. // If using lightmap, disregard zone ambient light
  91. // If using AO, calculate ambient in the PS
  92. oVertexLight = float3(0.0, 0.0, 0.0);
  93. oTexCoord2 = iTexCoord2;
  94. #else
  95. oVertexLight = GetAmbient(GetZonePos(worldPos));
  96. #endif
  97. #ifdef NUMVERTEXLIGHTS
  98. for (int i = 0; i < NUMVERTEXLIGHTS; ++i)
  99. oVertexLight += GetVertexLight(i, worldPos, oNormal) * cVertexLights[i * 3].rgb;
  100. #endif
  101. oScreenPos = GetScreenPos(oPos);
  102. #ifdef ENVCUBEMAP
  103. oReflectionVec = worldPos - cCameraPos;
  104. #endif
  105. #endif
  106. }
  107. void PS(
  108. #ifndef NORMALMAP
  109. float2 iTexCoord : TEXCOORD0,
  110. #else
  111. float4 iTexCoord : TEXCOORD0,
  112. float4 iTangent : TEXCOORD3,
  113. #endif
  114. float3 iNormal : TEXCOORD1,
  115. float4 iWorldPos : TEXCOORD2,
  116. #ifdef PERPIXEL
  117. #ifdef SHADOW
  118. float4 iShadowPos[NUMCASCADES] : TEXCOORD4,
  119. #endif
  120. #ifdef SPOTLIGHT
  121. float4 iSpotPos : TEXCOORD5,
  122. #endif
  123. #ifdef CUBEMASK
  124. float3 iCubeMaskVec : TEXCOORD5,
  125. #endif
  126. #else
  127. float3 iVertexLight : TEXCOORD4,
  128. float4 iScreenPos : TEXCOORD5,
  129. #ifdef ENVCUBEMAP
  130. float3 iReflectionVec : TEXCOORD6,
  131. #endif
  132. #if defined(LIGHTMAP) || defined(AO)
  133. float2 iTexCoord2 : TEXCOORD7,
  134. #endif
  135. #endif
  136. #ifdef PREPASS
  137. out float4 oDepth : COLOR1,
  138. #endif
  139. #ifdef DEFERRED
  140. out float4 oAlbedo : COLOR1,
  141. out float4 oNormal : COLOR2,
  142. out float4 oDepth : COLOR3,
  143. #endif
  144. out float4 oColor : COLOR0)
  145. {
  146. // Get material diffuse albedo
  147. #ifdef DIFFMAP
  148. float4 diffInput = tex2D(sDiffMap, iTexCoord.xy);
  149. #ifdef ALPHAMASK
  150. if (diffInput.a < 0.5)
  151. discard;
  152. #endif
  153. float4 diffColor = cMatDiffColor * diffInput;
  154. #else
  155. float4 diffColor = cMatDiffColor;
  156. #endif
  157. // Get material specular albedo
  158. #ifdef SPECMAP
  159. float3 specColor = cMatSpecColor.rgb * tex2D(sSpecMap, iTexCoord.xy).rgb;
  160. #else
  161. float3 specColor = cMatSpecColor.rgb;
  162. #endif
  163. // Get normal
  164. #ifdef NORMALMAP
  165. float3x3 tbn = float3x3(iTangent.xyz, float3(iTexCoord.zw, iTangent.w), iNormal);
  166. // We may be running low on instructions on Shader Model 2, so skip normalize if necessary
  167. #if defined(SM3) || !defined(SHADOW) || !defined(SPECULAR)
  168. float3 normal = normalize(mul(DecodeNormal(tex2D(sNormalMap, iTexCoord.xy)), tbn));
  169. #else
  170. float3 normal = mul(DecodeNormal(tex2D(sNormalMap, iTexCoord.xy)), tbn);
  171. #endif
  172. #else
  173. float3 normal = normalize(iNormal);
  174. #endif
  175. // Get fog factor
  176. #ifdef HEIGHTFOG
  177. float fogFactor = GetHeightFogFactor(iWorldPos.w, iWorldPos.y);
  178. #else
  179. float fogFactor = GetFogFactor(iWorldPos.w);
  180. #endif
  181. #if defined(PERPIXEL)
  182. // Per-pixel forward lighting
  183. float3 lightDir;
  184. float3 lightColor;
  185. float3 finalColor;
  186. float diff = GetDiffuse(normal, iWorldPos.xyz, lightDir);
  187. #ifdef SHADOW
  188. diff *= GetShadow(iShadowPos, iWorldPos.w);
  189. #endif
  190. #if defined(SPOTLIGHT)
  191. lightColor = iSpotPos.w > 0.0 ? tex2Dproj(sLightSpotMap, iSpotPos).rgb * cLightColor.rgb : 0.0;
  192. #elif defined(CUBEMASK)
  193. lightColor = texCUBE(sLightCubeMap, iCubeMaskVec).rgb * cLightColor.rgb;
  194. #else
  195. lightColor = cLightColor.rgb;
  196. #endif
  197. #ifdef SPECULAR
  198. float spec = GetSpecular(normal, cCameraPosPS - iWorldPos.xyz, lightDir, cMatSpecColor.a);
  199. finalColor = diff * lightColor * (diffColor.rgb + spec * specColor * cLightColor.a);
  200. #else
  201. finalColor = diff * lightColor * diffColor.rgb;
  202. #endif
  203. #ifdef AMBIENT
  204. finalColor += cAmbientColor * diffColor.rgb;
  205. finalColor += cMatEmissiveColor;
  206. oColor = float4(GetFog(finalColor, fogFactor), diffColor.a);
  207. #else
  208. oColor = float4(GetLitFog(finalColor, fogFactor), diffColor.a);
  209. #endif
  210. #elif defined(PREPASS)
  211. // Fill light pre-pass G-Buffer
  212. float specPower = cMatSpecColor.a / 255.0;
  213. oColor = float4(normal * 0.5 + 0.5, specPower);
  214. oDepth = iWorldPos.w;
  215. #elif defined(DEFERRED)
  216. // Fill deferred G-buffer
  217. float specIntensity = specColor.g;
  218. float specPower = cMatSpecColor.a / 255.0;
  219. float3 finalColor = iVertexLight * diffColor.rgb;
  220. #ifdef AO
  221. // If using AO, the vertex light ambient is black, calculate occluded ambient here
  222. finalColor += tex2D(sEmissiveMap, iTexCoord2).rgb * cAmbientColor * diffColor.rgb;
  223. #endif
  224. #ifdef ENVCUBEMAP
  225. finalColor += cMatEnvMapColor * texCUBE(sEnvCubeMap, reflect(iReflectionVec, normal)).rgb;
  226. #endif
  227. #ifdef LIGHTMAP
  228. finalColor += tex2D(sEmissiveMap, iTexCoord2).rgb * diffColor.rgb;
  229. #endif
  230. #ifdef EMISSIVEMAP
  231. finalColor += cMatEmissiveColor * tex2D(sEmissiveMap, iTexCoord.xy).rgb;
  232. #else
  233. finalColor += cMatEmissiveColor;
  234. #endif
  235. oColor = float4(GetFog(finalColor, fogFactor), 1.0);
  236. oAlbedo = fogFactor * float4(diffColor.rgb, specIntensity);
  237. oNormal = float4(normal * 0.5 + 0.5, specPower);
  238. oDepth = iWorldPos.w;
  239. #else
  240. // Ambient & per-vertex lighting
  241. float3 finalColor = iVertexLight * diffColor.rgb;
  242. #ifdef AO
  243. // If using AO, the vertex light ambient is black, calculate occluded ambient here
  244. finalColor += tex2D(sEmissiveMap, iTexCoord2).rgb * cAmbientColor * diffColor.rgb;
  245. #endif
  246. #ifdef MATERIAL
  247. // Add light pre-pass accumulation result
  248. // Lights are accumulated at half intensity. Bring back to full intensity now
  249. float4 lightInput = 2.0 * tex2Dproj(sLightBuffer, iScreenPos);
  250. float3 lightSpecColor = lightInput.a * (lightInput.rgb / GetIntensity(lightInput.rgb));
  251. finalColor += lightInput.rgb * diffColor.rgb + lightSpecColor * specColor;
  252. #endif
  253. #ifdef ENVCUBEMAP
  254. finalColor += cMatEnvMapColor * texCUBE(sEnvCubeMap, reflect(iReflectionVec, normal)).rgb;
  255. #endif
  256. #ifdef LIGHTMAP
  257. finalColor += tex2D(sEmissiveMap, iTexCoord2).rgb * diffColor.rgb;
  258. #endif
  259. #ifdef EMISSIVEMAP
  260. finalColor += cMatEmissiveColor * tex2D(sEmissiveMap, iTexCoord.xy).rgb;
  261. #else
  262. finalColor += cMatEmissiveColor;
  263. #endif
  264. oColor = float4(GetFog(finalColor, fogFactor), diffColor.a);
  265. #endif
  266. }