LitSolid.hlsl 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  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. void VS(float4 iPos : POSITION,
  8. #if !defined(BILLBOARD) && !defined(TRAILFACECAM)
  9. float3 iNormal : NORMAL,
  10. #endif
  11. #ifndef NOUV
  12. float2 iTexCoord : TEXCOORD0,
  13. #endif
  14. #ifdef VERTEXCOLOR
  15. float4 iColor : COLOR0,
  16. #endif
  17. #if defined(LIGHTMAP) || defined(AO)
  18. float2 iTexCoord2 : TEXCOORD1,
  19. #endif
  20. #if (defined(NORMALMAP) || defined(TRAILFACECAM) || defined(TRAILBONE)) && !defined(BILLBOARD) && !defined(DIRBILLBOARD)
  21. float4 iTangent : TANGENT,
  22. #endif
  23. #ifdef SKINNED
  24. float4 iBlendWeights : BLENDWEIGHT,
  25. int4 iBlendIndices : BLENDINDICES,
  26. #endif
  27. #ifdef INSTANCED
  28. float4x3 iModelInstance : TEXCOORD4,
  29. #endif
  30. #if defined(BILLBOARD) || defined(DIRBILLBOARD)
  31. float2 iSize : TEXCOORD1,
  32. #endif
  33. #ifndef NORMALMAP
  34. out float2 oTexCoord : TEXCOORD0,
  35. #else
  36. out float4 oTexCoord : TEXCOORD0,
  37. out float4 oTangent : TEXCOORD3,
  38. #endif
  39. out float3 oNormal : TEXCOORD1,
  40. out float4 oWorldPos : TEXCOORD2,
  41. #ifdef PERPIXEL
  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 float3 oVertexLight : TEXCOORD4,
  53. out float4 oScreenPos : TEXCOORD5,
  54. #ifdef ENVCUBEMAP
  55. out float3 oReflectionVec : TEXCOORD6,
  56. #endif
  57. #if defined(LIGHTMAP) || defined(AO)
  58. out float2 oTexCoord2 : TEXCOORD7,
  59. #endif
  60. #endif
  61. #ifdef VERTEXCOLOR
  62. out float4 oColor : COLOR0,
  63. #endif
  64. #if defined(D3D11) && defined(CLIPPLANE)
  65. out float oClip : SV_CLIPDISTANCE0,
  66. #endif
  67. out float4 oPos : OUTPOSITION)
  68. {
  69. // Define a 0,0 UV coord if not expected from the vertex data
  70. #ifdef NOUV
  71. float2 iTexCoord = float2(0.0, 0.0);
  72. #endif
  73. float4x3 modelMatrix = iModelMatrix;
  74. float3 worldPos = GetWorldPos(modelMatrix);
  75. oPos = GetClipPos(worldPos);
  76. oNormal = GetWorldNormal(modelMatrix);
  77. oWorldPos = float4(worldPos, GetDepth(oPos));
  78. #if defined(D3D11) && defined(CLIPPLANE)
  79. oClip = dot(oPos, cClipPlane);
  80. #endif
  81. #ifdef VERTEXCOLOR
  82. oColor = iColor;
  83. #endif
  84. #ifdef NORMALMAP
  85. float4 tangent = GetWorldTangent(modelMatrix);
  86. float3 bitangent = cross(tangent.xyz, oNormal) * tangent.w;
  87. oTexCoord = float4(GetTexCoord(iTexCoord), bitangent.xy);
  88. oTangent = float4(tangent.xyz, bitangent.z);
  89. #else
  90. oTexCoord = GetTexCoord(iTexCoord);
  91. #endif
  92. #ifdef PERPIXEL
  93. // Per-pixel forward lighting
  94. float4 projWorldPos = float4(worldPos.xyz, 1.0);
  95. #ifdef SHADOW
  96. // Shadow projection: transform from world space to shadow space
  97. GetShadowPos(projWorldPos, oNormal, oShadowPos);
  98. #endif
  99. #ifdef SPOTLIGHT
  100. // Spotlight projection: transform from world space to projector texture coordinates
  101. oSpotPos = mul(projWorldPos, cLightMatrices[0]);
  102. #endif
  103. #ifdef POINTLIGHT
  104. oCubeMaskVec = mul(worldPos - cLightPos.xyz, (float3x3)cLightMatrices[0]);
  105. #endif
  106. #else
  107. // Ambient & per-vertex lighting
  108. #if defined(LIGHTMAP) || defined(AO)
  109. // If using lightmap, disregard zone ambient light
  110. // If using AO, calculate ambient in the PS
  111. oVertexLight = float3(0.0, 0.0, 0.0);
  112. oTexCoord2 = iTexCoord2;
  113. #else
  114. oVertexLight = GetAmbient(GetZonePos(worldPos));
  115. #endif
  116. #ifdef NUMVERTEXLIGHTS
  117. for (int i = 0; i < NUMVERTEXLIGHTS; ++i)
  118. oVertexLight += GetVertexLight(i, worldPos, oNormal) * cVertexLights[i * 3].rgb;
  119. #endif
  120. oScreenPos = GetScreenPos(oPos);
  121. #ifdef ENVCUBEMAP
  122. oReflectionVec = worldPos - cCameraPos;
  123. #endif
  124. #endif
  125. }
  126. void PS(
  127. #ifndef NORMALMAP
  128. float2 iTexCoord : TEXCOORD0,
  129. #else
  130. float4 iTexCoord : TEXCOORD0,
  131. float4 iTangent : TEXCOORD3,
  132. #endif
  133. float3 iNormal : TEXCOORD1,
  134. float4 iWorldPos : TEXCOORD2,
  135. #ifdef PERPIXEL
  136. #ifdef SHADOW
  137. float4 iShadowPos[NUMCASCADES] : TEXCOORD4,
  138. #endif
  139. #ifdef SPOTLIGHT
  140. float4 iSpotPos : TEXCOORD5,
  141. #endif
  142. #ifdef POINTLIGHT
  143. float3 iCubeMaskVec : TEXCOORD5,
  144. #endif
  145. #else
  146. float3 iVertexLight : TEXCOORD4,
  147. float4 iScreenPos : TEXCOORD5,
  148. #ifdef ENVCUBEMAP
  149. float3 iReflectionVec : TEXCOORD6,
  150. #endif
  151. #if defined(LIGHTMAP) || defined(AO)
  152. float2 iTexCoord2 : TEXCOORD7,
  153. #endif
  154. #endif
  155. #ifdef VERTEXCOLOR
  156. float4 iColor : COLOR0,
  157. #endif
  158. #if defined(D3D11) && defined(CLIPPLANE)
  159. float iClip : SV_CLIPDISTANCE0,
  160. #endif
  161. #ifdef PREPASS
  162. out float4 oDepth : OUTCOLOR1,
  163. #endif
  164. #ifdef DEFERRED
  165. out float4 oAlbedo : OUTCOLOR1,
  166. out float4 oNormal : OUTCOLOR2,
  167. out float4 oDepth : OUTCOLOR3,
  168. #endif
  169. out float4 oColor : OUTCOLOR0)
  170. {
  171. // Get material diffuse albedo
  172. #ifdef DIFFMAP
  173. float4 diffInput = Sample2D(DiffMap, iTexCoord.xy);
  174. #ifdef ALPHAMASK
  175. if (diffInput.a < 0.5)
  176. discard;
  177. #endif
  178. float4 diffColor = cMatDiffColor * diffInput;
  179. #else
  180. float4 diffColor = cMatDiffColor;
  181. #endif
  182. #ifdef VERTEXCOLOR
  183. diffColor *= iColor;
  184. #endif
  185. // Get material specular albedo
  186. #ifdef SPECMAP
  187. float3 specColor = cMatSpecColor.rgb * Sample2D(SpecMap, iTexCoord.xy).rgb;
  188. #else
  189. float3 specColor = cMatSpecColor.rgb;
  190. #endif
  191. // Get normal
  192. #ifdef NORMALMAP
  193. float3x3 tbn = float3x3(iTangent.xyz, float3(iTexCoord.zw, iTangent.w), iNormal);
  194. float3 normal = normalize(mul(DecodeNormal(Sample2D(NormalMap, iTexCoord.xy)), tbn));
  195. #else
  196. float3 normal = normalize(iNormal);
  197. #endif
  198. // Get fog factor
  199. #ifdef HEIGHTFOG
  200. float fogFactor = GetHeightFogFactor(iWorldPos.w, iWorldPos.y);
  201. #else
  202. float fogFactor = GetFogFactor(iWorldPos.w);
  203. #endif
  204. #if defined(PERPIXEL)
  205. // Per-pixel forward lighting
  206. float3 lightDir;
  207. float3 lightColor;
  208. float3 finalColor;
  209. float diff = GetDiffuse(normal, iWorldPos.xyz, lightDir);
  210. #ifdef SHADOW
  211. diff *= GetShadow(iShadowPos, iWorldPos.w);
  212. #endif
  213. #if defined(SPOTLIGHT)
  214. lightColor = iSpotPos.w > 0.0 ? Sample2DProj(LightSpotMap, iSpotPos).rgb * cLightColor.rgb : 0.0;
  215. #elif defined(CUBEMASK)
  216. lightColor = SampleCube(LightCubeMap, iCubeMaskVec).rgb * cLightColor.rgb;
  217. #else
  218. lightColor = cLightColor.rgb;
  219. #endif
  220. #ifdef SPECULAR
  221. float spec = GetSpecular(normal, cCameraPosPS - iWorldPos.xyz, lightDir, cMatSpecColor.a);
  222. finalColor = diff * lightColor * (diffColor.rgb + spec * specColor * cLightColor.a);
  223. #else
  224. finalColor = diff * lightColor * diffColor.rgb;
  225. #endif
  226. #ifdef AMBIENT
  227. finalColor += cAmbientColor.rgb * diffColor.rgb;
  228. finalColor += cMatEmissiveColor;
  229. oColor = float4(GetFog(finalColor, fogFactor), diffColor.a);
  230. #else
  231. oColor = float4(GetLitFog(finalColor, fogFactor), diffColor.a);
  232. #endif
  233. #elif defined(PREPASS)
  234. // Fill light pre-pass G-Buffer
  235. float specPower = cMatSpecColor.a / 255.0;
  236. oColor = float4(normal * 0.5 + 0.5, specPower);
  237. oDepth = iWorldPos.w;
  238. #elif defined(DEFERRED)
  239. // Fill deferred G-buffer
  240. float specIntensity = specColor.g;
  241. float specPower = cMatSpecColor.a / 255.0;
  242. float3 finalColor = iVertexLight * diffColor.rgb;
  243. #ifdef AO
  244. // If using AO, the vertex light ambient is black, calculate occluded ambient here
  245. finalColor += Sample2D(EmissiveMap, iTexCoord2).rgb * cAmbientColor.rgb * diffColor.rgb;
  246. #endif
  247. #ifdef ENVCUBEMAP
  248. finalColor += cMatEnvMapColor * SampleCube(EnvCubeMap, reflect(iReflectionVec, normal)).rgb;
  249. #endif
  250. #ifdef LIGHTMAP
  251. finalColor += Sample2D(EmissiveMap, iTexCoord2).rgb * diffColor.rgb;
  252. #endif
  253. #ifdef EMISSIVEMAP
  254. finalColor += cMatEmissiveColor * Sample2D(EmissiveMap, iTexCoord.xy).rgb;
  255. #else
  256. finalColor += cMatEmissiveColor;
  257. #endif
  258. oColor = float4(GetFog(finalColor, fogFactor), 1.0);
  259. oAlbedo = fogFactor * float4(diffColor.rgb, specIntensity);
  260. oNormal = float4(normal * 0.5 + 0.5, specPower);
  261. oDepth = iWorldPos.w;
  262. #else
  263. // Ambient & per-vertex lighting
  264. float3 finalColor = iVertexLight * diffColor.rgb;
  265. #ifdef AO
  266. // If using AO, the vertex light ambient is black, calculate occluded ambient here
  267. finalColor += Sample2D(EmissiveMap, iTexCoord2).rgb * cAmbientColor.rgb * diffColor.rgb;
  268. #endif
  269. #ifdef MATERIAL
  270. // Add light pre-pass accumulation result
  271. // Lights are accumulated at half intensity. Bring back to full intensity now
  272. float4 lightInput = 2.0 * Sample2DProj(LightBuffer, iScreenPos);
  273. float3 lightSpecColor = lightInput.a * lightInput.rgb / max(GetIntensity(lightInput.rgb), 0.001);
  274. finalColor += lightInput.rgb * diffColor.rgb + lightSpecColor * specColor;
  275. #endif
  276. #ifdef ENVCUBEMAP
  277. finalColor += cMatEnvMapColor * SampleCube(EnvCubeMap, reflect(iReflectionVec, normal)).rgb;
  278. #endif
  279. #ifdef LIGHTMAP
  280. finalColor += Sample2D(EmissiveMap, iTexCoord2).rgb * diffColor.rgb;
  281. #endif
  282. #ifdef EMISSIVEMAP
  283. finalColor += cMatEmissiveColor * Sample2D(EmissiveMap, iTexCoord.xy).rgb;
  284. #else
  285. finalColor += cMatEmissiveColor;
  286. #endif
  287. oColor = float4(GetFog(finalColor, fogFactor), diffColor.a);
  288. #endif
  289. }