PBRLitSolid.glsl 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. #include "Uniforms.glsl"
  2. #include "Samplers.glsl"
  3. #include "Transform.glsl"
  4. #include "ScreenPos.glsl"
  5. #include "Lighting.glsl"
  6. #include "Constants.glsl"
  7. #include "Fog.glsl"
  8. #include "PBR.glsl"
  9. #include "IBL.glsl"
  10. #line 30010
  11. #if defined(NORMALMAP)
  12. varying vec4 vTexCoord;
  13. varying vec4 vTangent;
  14. #else
  15. varying vec2 vTexCoord;
  16. #endif
  17. varying vec3 vNormal;
  18. varying vec4 vWorldPos;
  19. #ifdef VERTEXCOLOR
  20. varying vec4 vColor;
  21. #endif
  22. #ifdef PERPIXEL
  23. #ifdef SHADOW
  24. #ifndef GL_ES
  25. varying vec4 vShadowPos[NUMCASCADES];
  26. #else
  27. varying highp vec4 vShadowPos[NUMCASCADES];
  28. #endif
  29. #endif
  30. #ifdef SPOTLIGHT
  31. varying vec4 vSpotPos;
  32. #endif
  33. #ifdef POINTLIGHT
  34. varying vec3 vCubeMaskVec;
  35. #endif
  36. #else
  37. varying vec3 vVertexLight;
  38. varying vec4 vScreenPos;
  39. #ifdef ENVCUBEMAP
  40. varying vec3 vReflectionVec;
  41. #endif
  42. #if defined(LIGHTMAP)
  43. varying vec2 vTexCoord2;
  44. #endif
  45. #endif
  46. void VS()
  47. {
  48. mat4 modelMatrix = iModelMatrix;
  49. vec3 worldPos = GetWorldPos(modelMatrix);
  50. gl_Position = GetClipPos(worldPos);
  51. vNormal = GetWorldNormal(modelMatrix);
  52. vWorldPos = vec4(worldPos, GetDepth(gl_Position));
  53. #ifdef VERTEXCOLOR
  54. vColor = iColor;
  55. #endif
  56. #if defined(NORMALMAP) || defined(DIRBILLBOARD)
  57. vec4 tangent = GetWorldTangent(modelMatrix);
  58. vec3 bitangent = cross(tangent.xyz, vNormal) * tangent.w;
  59. vTexCoord = vec4(GetTexCoord(iTexCoord), bitangent.xy);
  60. vTangent = vec4(tangent.xyz, bitangent.z);
  61. #else
  62. vTexCoord = GetTexCoord(iTexCoord);
  63. #endif
  64. #ifdef PERPIXEL
  65. // Per-pixel forward lighting
  66. vec4 projWorldPos = vec4(worldPos, 1.0);
  67. #ifdef SHADOW
  68. // Shadow projection: transform from world space to shadow space
  69. for (int i = 0; i < NUMCASCADES; i++)
  70. vShadowPos[i] = GetShadowPos(i, vNormal, projWorldPos);
  71. #endif
  72. #ifdef SPOTLIGHT
  73. // Spotlight projection: transform from world space to projector texture coordinates
  74. vSpotPos = projWorldPos * cLightMatrices[0];
  75. #endif
  76. #ifdef POINTLIGHT
  77. vCubeMaskVec = (worldPos - cLightPos.xyz) * mat3(cLightMatrices[0][0].xyz, cLightMatrices[0][1].xyz, cLightMatrices[0][2].xyz);
  78. #endif
  79. #else
  80. // Ambient & per-vertex lighting
  81. #if defined(LIGHTMAP)
  82. // If using lightmap, disregard zone ambient light
  83. vVertexLight = vec3(0.0, 0.0, 0.0);
  84. vTexCoord2 = iTexCoord1;
  85. #elif defined(AO)
  86. // If using AO, calculate ambient in the PS
  87. vVertexLight = vec3(0.0, 0.0, 0.0);
  88. #else
  89. vVertexLight = GetAmbient(GetZonePos(worldPos));
  90. #endif
  91. #ifdef NUMVERTEXLIGHTS
  92. for (int i = 0; i < NUMVERTEXLIGHTS; ++i)
  93. vVertexLight += GetVertexLight(i, worldPos, vNormal) * cVertexLights[i * 3].rgb;
  94. #endif
  95. vScreenPos = GetScreenPos(gl_Position);
  96. #ifdef ENVCUBEMAP
  97. vReflectionVec = worldPos - cCameraPos;
  98. #endif
  99. #endif
  100. }
  101. void PS()
  102. {
  103. // Get material diffuse albedo
  104. #ifdef DIFFMAP
  105. vec4 diffInput = texture2D(sDiffMap, vTexCoord.xy);
  106. #ifdef ALPHAMASK
  107. if (diffInput.a < 0.5)
  108. discard;
  109. #endif
  110. vec4 diffColor = cMatDiffColor * diffInput;
  111. #else
  112. vec4 diffColor = cMatDiffColor;
  113. #endif
  114. #ifdef VERTEXCOLOR
  115. diffColor *= vColor;
  116. #endif
  117. #ifdef METALLIC
  118. vec4 roughMetalSrc = texture2D(sSpecMap, vTexCoord.xy);
  119. float roughness = roughMetalSrc.r + cRoughness;
  120. float metalness = roughMetalSrc.g + cMetallic;
  121. #else
  122. float roughness = cRoughness;
  123. float metalness = cMetallic;
  124. #endif
  125. roughness *= roughness;
  126. roughness = clamp(roughness, ROUGHNESS_FLOOR, 1.0);
  127. metalness = clamp(metalness, METALNESS_FLOOR, 1.0);
  128. vec3 specColor = mix(0.08 * cMatSpecColor.rgb, diffColor.rgb, metalness);
  129. diffColor.rgb = diffColor.rgb - diffColor.rgb * metalness;
  130. // Get normal
  131. #if defined(NORMALMAP) || defined(DIRBILLBOARD)
  132. vec3 tangent = vTangent.xyz;
  133. vec3 bitangent = vec3(vTexCoord.zw, vTangent.w);
  134. mat3 tbn = mat3(tangent, bitangent, vNormal);
  135. #endif
  136. #ifdef NORMALMAP
  137. vec3 nn = DecodeNormal(texture2D(sNormalMap, vTexCoord.xy));
  138. //nn.rg *= 2.0;
  139. vec3 normal = normalize(tbn * nn);
  140. #else
  141. vec3 normal = normalize(vNormal);
  142. #endif
  143. // Get fog factor
  144. #ifdef HEIGHTFOG
  145. float fogFactor = GetHeightFogFactor(vWorldPos.w, vWorldPos.y);
  146. #else
  147. float fogFactor = GetFogFactor(vWorldPos.w);
  148. #endif
  149. #if defined(PERPIXEL)
  150. // Per-pixel forward lighting
  151. vec3 lightColor;
  152. vec3 lightDir;
  153. vec3 finalColor;
  154. float atten = 1;
  155. #if defined(DIRLIGHT)
  156. atten = GetAtten(normal, vWorldPos.xyz, lightDir);
  157. #elif defined(SPOTLIGHT)
  158. atten = GetAttenSpot(normal, vWorldPos.xyz, lightDir);
  159. #else
  160. atten = GetAttenPoint(normal, vWorldPos.xyz, lightDir);
  161. #endif
  162. float shadow = 1.0;
  163. #ifdef SHADOW
  164. shadow = GetShadow(vShadowPos, vWorldPos.w);
  165. #endif
  166. #if defined(SPOTLIGHT)
  167. lightColor = vSpotPos.w > 0.0 ? texture2DProj(sLightSpotMap, vSpotPos).rgb * cLightColor.rgb : vec3(0.0, 0.0, 0.0);
  168. #elif defined(CUBEMASK)
  169. lightColor = textureCube(sLightCubeMap, vCubeMaskVec).rgb * cLightColor.rgb;
  170. #else
  171. lightColor = cLightColor.rgb;
  172. #endif
  173. vec3 toCamera = normalize(cCameraPosPS - vWorldPos.xyz);
  174. vec3 lightVec = normalize(lightDir);
  175. float ndl = clamp((dot(normal, lightVec)), M_EPSILON, 1.0);
  176. vec3 BRDF = GetBRDF(vWorldPos.xyz, lightDir, lightVec, toCamera, normal, roughness, diffColor.rgb, specColor);
  177. finalColor.rgb = BRDF * lightColor * (atten * shadow) / M_PI;
  178. #ifdef AMBIENT
  179. finalColor += cAmbientColor.rgb * diffColor.rgb;
  180. finalColor += cMatEmissiveColor;
  181. gl_FragColor = vec4(GetFog(finalColor, fogFactor), diffColor.a);
  182. #else
  183. gl_FragColor = vec4(GetLitFog(finalColor, fogFactor), diffColor.a);
  184. #endif
  185. #elif defined(DEFERRED)
  186. // Fill deferred G-buffer
  187. const vec3 spareData = vec3(0,0,0); // Can be used to pass more data to deferred renderer
  188. gl_FragData[0] = vec4(specColor, spareData.r);
  189. gl_FragData[1] = vec4(diffColor.rgb, spareData.g);
  190. gl_FragData[2] = vec4(normal * roughness, spareData.b);
  191. gl_FragData[3] = vec4(EncodeDepth(vWorldPos.w), 0.0);
  192. #else
  193. // Ambient & per-vertex lighting
  194. vec3 finalColor = vVertexLight * diffColor.rgb;
  195. vec3 ambientOcclusion = vec3(1.0, 1.0, 1.0);
  196. #ifdef AO
  197. // If using AO, the vertex light ambient is black, calculate occluded ambient here
  198. ambientOcclusion = texture2D(sEmissiveMap, vTexCoord.xy).rgb;
  199. finalColor += ambientOcclusion * cAmbientColor.rgb * diffColor.rgb;
  200. #endif
  201. #ifdef MATERIAL
  202. // Add light pre-pass accumulation result
  203. // Lights are accumulated at half intensity. Bring back to full intensity now
  204. vec4 lightInput = 2.0 * texture2DProj(sLightBuffer, vScreenPos);
  205. vec3 lightSpecColor = lightInput.a * lightInput.rgb / max(GetIntensity(lightInput.rgb), 0.001);
  206. finalColor += lightInput.rgb * diffColor.rgb + lightSpecColor * specColor;
  207. #endif
  208. vec3 toCamera = normalize(vWorldPos.xyz - cCameraPosPS);
  209. vec3 reflection = normalize(reflect(toCamera, normal));
  210. vec3 cubeColor = vVertexLight.rgb;
  211. #ifdef IBL
  212. vec3 iblColor = ImageBasedLighting(reflection, normal, toCamera, diffColor.rgb, specColor.rgb, roughness, cubeColor);
  213. finalColor.rgb += iblColor * ambientOcclusion;
  214. #endif
  215. #ifdef ENVCUBEMAP
  216. finalColor += cMatEnvMapColor * textureCube(sEnvCubeMap, reflect(vReflectionVec, normal)).rgb;
  217. #endif
  218. #ifdef LIGHTMAP
  219. finalColor += texture2D(sEmissiveMap, vTexCoord2).rgb * diffColor.rgb;
  220. #endif
  221. #ifdef EMISSIVEMAP
  222. finalColor += cMatEmissiveColor * texture2D(sEmissiveMap, vTexCoord.xy).rgb;
  223. #else
  224. finalColor += cMatEmissiveColor;
  225. #endif
  226. gl_FragColor = vec4(GetFog(finalColor, fogFactor), diffColor.a);
  227. #endif
  228. }