PBRLitSolid.hlsl 11 KB

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