LitSolid.hlsl 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  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. // With SM2 alpha-masked point light shadows become too complex, so disable specular lighting
  8. #if !defined(SM3) && defined(POINTLIGHT) && defined(SHADOW) && defined(ALPHAMASK)
  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. out float2 oTexCoord : TEXCOORD0,
  31. #ifdef HEIGHTFOG
  32. out float3 oWorldPos : TEXCOORD8,
  33. #endif
  34. #ifdef PERPIXEL
  35. out float4 oLightVec : TEXCOORD1,
  36. #ifndef NORMALMAP
  37. out float3 oNormal : TEXCOORD2,
  38. #endif
  39. #ifdef SPECULAR
  40. out float3 oEyeVec : TEXCOORD3,
  41. #endif
  42. #ifdef SHADOW
  43. out float4 oShadowPos[NUMCASCADES] : TEXCOORD4,
  44. #endif
  45. #ifdef SPOTLIGHT
  46. out float4 oSpotPos : TEXCOORD5,
  47. #endif
  48. #ifdef POINTLIGHT
  49. out float3 oCubeMaskVec : TEXCOORD5,
  50. #endif
  51. #else
  52. out float4 oVertexLight : TEXCOORD1,
  53. out float3 oNormal : TEXCOORD2,
  54. #ifdef NORMALMAP
  55. out float3 oTangent : TEXCOORD3,
  56. out float3 oBitangent : TEXCOORD4,
  57. #endif
  58. out float4 oScreenPos : TEXCOORD5,
  59. #ifdef ENVCUBEMAP
  60. out float3 oReflectionVec : TEXCOORD6,
  61. #endif
  62. #if defined(LIGHTMAP) || defined(AO)
  63. out float2 oTexCoord2 : TEXCOORD7,
  64. #endif
  65. #endif
  66. out float4 oPos : POSITION)
  67. {
  68. float4x3 modelMatrix = iModelMatrix;
  69. float3 worldPos = GetWorldPos(modelMatrix);
  70. oPos = GetClipPos(worldPos);
  71. oTexCoord = GetTexCoord(iTexCoord);
  72. #ifdef HEIGHTFOG
  73. oWorldPos = worldPos;
  74. #endif
  75. #if defined(PERPIXEL) && defined(NORMALMAP)
  76. float3 oNormal;
  77. float3 oTangent;
  78. float3 oBitangent;
  79. #endif
  80. oNormal = GetWorldNormal(modelMatrix);
  81. #ifdef NORMALMAP
  82. oTangent = GetWorldTangent(modelMatrix);
  83. oBitangent = cross(oTangent, oNormal) * iTangent.w;
  84. #endif
  85. #ifdef PERPIXEL
  86. // Per-pixel forward lighting
  87. float4 projWorldPos = float4(worldPos, 1.0);
  88. #ifdef DIRLIGHT
  89. oLightVec = float4(cLightDir, GetDepth(oPos));
  90. #else
  91. oLightVec = float4((cLightPos.xyz - worldPos) * cLightPos.w, GetDepth(oPos));
  92. #endif
  93. #ifdef SHADOW
  94. // Shadow projection: transform from world space to shadow space
  95. GetShadowPos(projWorldPos, oShadowPos);
  96. #endif
  97. #ifdef SPOTLIGHT
  98. // Spotlight projection: transform from world space to projector texture coordinates
  99. oSpotPos = mul(projWorldPos, cLightMatrices[0]);
  100. #endif
  101. #ifdef POINTLIGHT
  102. oCubeMaskVec = mul(oLightVec.xyz, (float3x3)cLightMatrices[0]);
  103. #endif
  104. #ifdef NORMALMAP
  105. float3x3 tbn = float3x3(oTangent, oBitangent, oNormal);
  106. oLightVec.xyz = mul(tbn, oLightVec.xyz);
  107. #ifdef SPECULAR
  108. oEyeVec = mul(tbn, cCameraPos - worldPos);
  109. #endif
  110. #elif defined(SPECULAR)
  111. oEyeVec = cCameraPos - worldPos;
  112. #endif
  113. #else
  114. // Ambient & per-vertex lighting
  115. #if defined(LIGHTMAP) || defined(AO)
  116. // If using lightmap, disregard zone ambient light
  117. // If using AO, calculate ambient in the PS
  118. oVertexLight = float4(0.0, 0.0, 0.0, GetDepth(oPos));
  119. oTexCoord2 = iTexCoord2;
  120. #else
  121. oVertexLight = float4(GetAmbient(GetZonePos(worldPos)), GetDepth(oPos));
  122. #endif
  123. #ifdef NUMVERTEXLIGHTS
  124. for (int i = 0; i < NUMVERTEXLIGHTS; ++i)
  125. oVertexLight.rgb += GetVertexLight(i, worldPos, oNormal) * cVertexLights[i * 3].rgb;
  126. #endif
  127. oScreenPos = GetScreenPos(oPos);
  128. #ifdef ENVCUBEMAP
  129. oReflectionVec = worldPos - cCameraPos;
  130. #endif
  131. #endif
  132. }
  133. void PS(float2 iTexCoord : TEXCOORD0,
  134. #ifdef HEIGHTFOG
  135. float3 iWorldPos : TEXCOORD8,
  136. #endif
  137. #ifdef PERPIXEL
  138. float4 iLightVec : TEXCOORD1,
  139. #ifndef NORMALMAP
  140. float3 iNormal : TEXCOORD2,
  141. #endif
  142. #ifdef SPECULAR
  143. float3 iEyeVec : TEXCOORD3,
  144. #endif
  145. #ifdef SHADOW
  146. float4 iShadowPos[NUMCASCADES] : TEXCOORD4,
  147. #endif
  148. #ifdef SPOTLIGHT
  149. float4 iSpotPos : TEXCOORD5,
  150. #endif
  151. #ifdef CUBEMASK
  152. float3 iCubeMaskVec : TEXCOORD5,
  153. #endif
  154. #else
  155. float4 iVertexLight : TEXCOORD1,
  156. float3 iNormal : TEXCOORD2,
  157. #ifdef NORMALMAP
  158. float3 iTangent : TEXCOORD3,
  159. float3 iBitangent : TEXCOORD4,
  160. #endif
  161. float4 iScreenPos : TEXCOORD5,
  162. #ifdef ENVCUBEMAP
  163. float3 iReflectionVec : TEXCOORD6,
  164. #endif
  165. #if defined(LIGHTMAP) || defined(AO)
  166. float2 iTexCoord2 : TEXCOORD7,
  167. #endif
  168. #endif
  169. #ifdef PREPASS
  170. out float4 oDepth : COLOR1,
  171. #endif
  172. #ifdef DEFERRED
  173. out float4 oAlbedo : COLOR1,
  174. out float4 oNormal : COLOR2,
  175. out float4 oDepth : COLOR3,
  176. #endif
  177. out float4 oColor : COLOR0)
  178. {
  179. // Get material diffuse albedo
  180. #ifdef DIFFMAP
  181. float4 diffInput = tex2D(sDiffMap, iTexCoord);
  182. #ifdef ALPHAMASK
  183. if (diffInput.a < 0.5)
  184. discard;
  185. #endif
  186. float4 diffColor = cMatDiffColor * diffInput;
  187. #else
  188. float4 diffColor = cMatDiffColor;
  189. #endif
  190. // Get material specular albedo
  191. #ifdef SPECMAP
  192. float3 specColor = cMatSpecColor.rgb * tex2D(sSpecMap, iTexCoord).rgb;
  193. #else
  194. float3 specColor = cMatSpecColor.rgb;
  195. #endif
  196. #if defined(PERPIXEL)
  197. // Per-pixel forward lighting
  198. float3 lightDir;
  199. float3 lightColor;
  200. float3 finalColor;
  201. float diff;
  202. #ifdef NORMALMAP
  203. float3 normal = DecodeNormal(tex2D(sNormalMap, iTexCoord));
  204. #else
  205. float3 normal = normalize(iNormal);
  206. #endif
  207. diff = GetDiffuse(normal, iLightVec.xyz, lightDir);
  208. #ifdef SHADOW
  209. diff *= GetShadow(iShadowPos, iLightVec.w);
  210. #endif
  211. #if defined(SPOTLIGHT)
  212. lightColor = iSpotPos.w > 0.0 ? tex2Dproj(sLightSpotMap, iSpotPos).rgb * cLightColor.rgb : 0.0;
  213. #elif defined(CUBEMASK)
  214. lightColor = texCUBE(sLightCubeMap, iCubeMaskVec).rgb * cLightColor.rgb;
  215. #else
  216. lightColor = cLightColor.rgb;
  217. #endif
  218. #ifdef SPECULAR
  219. float spec = GetSpecular(normal, iEyeVec, lightDir, cMatSpecColor.a);
  220. finalColor = diff * lightColor * (diffColor.rgb + spec * specColor * cLightColor.a);
  221. #else
  222. finalColor = diff * lightColor * diffColor.rgb;
  223. #endif
  224. #ifdef HEIGHTFOG
  225. float fogFactor = GetHeightFogFactor(iLightVec.w, iWorldPos.y);
  226. #else
  227. float fogFactor = GetFogFactor(iLightVec.w);
  228. #endif
  229. #ifdef AMBIENT
  230. finalColor += cAmbientColor * diffColor.rgb;
  231. finalColor += cMatEmissiveColor;
  232. oColor = float4(GetFog(finalColor, fogFactor), diffColor.a);
  233. #else
  234. oColor = float4(GetLitFog(finalColor, fogFactor), diffColor.a);
  235. #endif
  236. #elif defined(PREPASS)
  237. // Fill light pre-pass G-Buffer
  238. #ifdef NORMALMAP
  239. float3x3 tbn = float3x3(iTangent, iBitangent, iNormal);
  240. float3 normal = mul(DecodeNormal(tex2D(sNormalMap, iTexCoord.xy)), tbn);
  241. #else
  242. float3 normal = iNormal;
  243. #endif
  244. float specPower = cMatSpecColor.a / 255.0;
  245. oColor = float4(normal * 0.5 + 0.5, specPower);
  246. oDepth = iVertexLight.a;
  247. #elif defined(DEFERRED)
  248. // Fill deferred G-buffer
  249. #ifdef NORMALMAP
  250. float3x3 tbn = float3x3(iTangent, iBitangent, iNormal);
  251. float3 normal = mul(DecodeNormal(tex2D(sNormalMap, iTexCoord)), tbn);
  252. #else
  253. float3 normal = iNormal;
  254. #endif
  255. // If using SM2, light volume shader may not have instructions left to normalize the normal. Therefore do it here
  256. #if !defined(SM3) || defined(ENVCUBEMAP)
  257. normal = normalize(normal);
  258. #endif
  259. float specIntensity = specColor.g;
  260. float specPower = cMatSpecColor.a / 255.0;
  261. float3 finalColor = iVertexLight.rgb * diffColor.rgb;
  262. #ifdef AO
  263. // If using AO, the vertex light ambient is black, calculate occluded ambient here
  264. finalColor += tex2D(sEmissiveMap, iTexCoord2).rgb * cAmbientColor * diffColor.rgb;
  265. #endif
  266. #ifdef ENVCUBEMAP
  267. finalColor += cMatEnvMapColor * texCUBE(sEnvCubeMap, reflect(iReflectionVec, normal)).rgb;
  268. #endif
  269. #ifdef LIGHTMAP
  270. finalColor += tex2D(sEmissiveMap, iTexCoord2).rgb * diffColor.rgb;
  271. #endif
  272. #ifdef EMISSIVEMAP
  273. finalColor += cMatEmissiveColor * tex2D(sEmissiveMap, iTexCoord).rgb;
  274. #else
  275. finalColor += cMatEmissiveColor;
  276. #endif
  277. #ifdef HEIGHTFOG
  278. float fogFactor = GetHeightFogFactor(iVertexLight.a, iWorldPos.y);
  279. #else
  280. float fogFactor = GetFogFactor(iVertexLight.a);
  281. #endif
  282. oColor = float4(GetFog(finalColor, fogFactor), 1.0);
  283. oAlbedo = fogFactor * float4(diffColor.rgb, specIntensity);
  284. oNormal = float4(normal * 0.5 + 0.5, specPower);
  285. oDepth = iVertexLight.a;
  286. #else
  287. // Ambient & per-vertex lighting
  288. float3 finalColor = iVertexLight.rgb * diffColor.rgb;
  289. #ifdef AO
  290. // If using AO, the vertex light ambient is black, calculate occluded ambient here
  291. finalColor += tex2D(sEmissiveMap, iTexCoord2).rgb * cAmbientColor * diffColor.rgb;
  292. #endif
  293. #ifdef MATERIAL
  294. // Add light pre-pass accumulation result
  295. // Lights are accumulated at half intensity. Bring back to full intensity now
  296. float4 lightInput = 2.0 * tex2Dproj(sLightBuffer, iScreenPos);
  297. float3 lightSpecColor = lightInput.a * (lightInput.rgb / GetIntensity(lightInput.rgb));
  298. finalColor += lightInput.rgb * diffColor.rgb + lightSpecColor * specColor;
  299. #endif
  300. #ifdef ENVCUBEMAP
  301. #ifdef NORMALMAP
  302. float3x3 tbn = float3x3(iTangent, iBitangent, iNormal);
  303. float3 normal = mul(DecodeNormal(tex2D(sNormalMap, iTexCoord)), tbn);
  304. #else
  305. float3 normal = iNormal;
  306. #endif
  307. normal = normalize(normal);
  308. finalColor += cMatEnvMapColor * texCUBE(sEnvCubeMap, reflect(iReflectionVec, normal)).rgb;
  309. #endif
  310. #ifdef LIGHTMAP
  311. finalColor += tex2D(sEmissiveMap, iTexCoord2).rgb * diffColor.rgb;
  312. #endif
  313. #ifdef EMISSIVEMAP
  314. finalColor += cMatEmissiveColor * tex2D(sEmissiveMap, iTexCoord).rgb;
  315. #else
  316. finalColor += cMatEmissiveColor;
  317. #endif
  318. #ifdef HEIGHTFOG
  319. float fogFactor = GetHeightFogFactor(iVertexLight.a, iWorldPos.y);
  320. #else
  321. float fogFactor = GetFogFactor(iVertexLight.a);
  322. #endif
  323. oColor = float4(GetFog(finalColor, fogFactor), diffColor.a);
  324. #endif
  325. }