PBRLitSolid.glsl 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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) || defined(IBL)
  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) || defined(AO)
  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) || defined(IBL)
  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) || defined(AO)
  82. // If using lightmap, disregard zone ambient light
  83. // If using AO, calculate ambient in the PS
  84. vVertexLight = vec3(0.0, 0.0, 0.0);
  85. vTexCoord2 = iTexCoord1;
  86. #else
  87. vVertexLight = GetAmbient(GetZonePos(worldPos));
  88. #endif
  89. #ifdef NUMVERTEXLIGHTS
  90. for (int i = 0; i < NUMVERTEXLIGHTS; ++i)
  91. vVertexLight += GetVertexLight(i, worldPos, vNormal) * cVertexLights[i * 3].rgb;
  92. #endif
  93. vScreenPos = GetScreenPos(gl_Position);
  94. #ifdef ENVCUBEMAP
  95. vReflectionVec = worldPos - cCameraPos;
  96. #endif
  97. #endif
  98. }
  99. void PS()
  100. {
  101. // Get material diffuse albedo
  102. #ifdef DIFFMAP
  103. vec4 diffInput = texture2D(sDiffMap, vTexCoord.xy);
  104. #ifdef ALPHAMASK
  105. if (diffInput.a < 0.5)
  106. discard;
  107. #endif
  108. vec4 diffColor = cMatDiffColor * diffInput;
  109. #else
  110. vec4 diffColor = cMatDiffColor;
  111. #endif
  112. #ifdef VERTEXCOLOR
  113. diffColor *= vColor;
  114. #endif
  115. #ifdef METALLIC
  116. vec4 roughMetalSrc = texture2D(sSpecMap, vTexCoord.xy);
  117. float roughness = roughMetalSrc.r + cRoughness;
  118. float metalness = roughMetalSrc.g + cMetallic;
  119. #else
  120. float roughness = cRoughness;
  121. float metalness = cMetallic;
  122. #endif
  123. roughness *= roughness;
  124. roughness = clamp(roughness, ROUGHNESS_FLOOR, 1.0);
  125. metalness = clamp(metalness, METALNESS_FLOOR, 1.0);
  126. vec3 specColor = mix(0.08 * cMatSpecColor.rgb, diffColor.rgb, metalness);
  127. diffColor.rgb = diffColor.rgb - diffColor.rgb * metalness;
  128. // Get normal
  129. #if defined(NORMALMAP) || defined(DIRBILLBOARD) || defined(IBL)
  130. vec3 tangent = vTangent.xyz;
  131. vec3 bitangent = vec3(vTexCoord.zw, vTangent.w);
  132. mat3 tbn = mat3(tangent, bitangent, vNormal);
  133. #endif
  134. #ifdef NORMALMAP
  135. vec3 nn = DecodeNormal(texture2D(sNormalMap, vTexCoord.xy));
  136. //nn.rg *= 2.0;
  137. vec3 normal = normalize(tbn * nn);
  138. #else
  139. vec3 normal = normalize(vNormal);
  140. #endif
  141. // Get fog factor
  142. #ifdef HEIGHTFOG
  143. float fogFactor = GetHeightFogFactor(vWorldPos.w, vWorldPos.y);
  144. #else
  145. float fogFactor = GetFogFactor(vWorldPos.w);
  146. #endif
  147. #if defined(PERPIXEL)
  148. // Per-pixel forward lighting
  149. vec3 lightColor;
  150. vec3 lightDir;
  151. vec3 finalColor;
  152. float atten = 1;
  153. #if defined(DIRLIGHT)
  154. atten = GetAtten(normal, vWorldPos.xyz, lightDir);
  155. #elif defined(SPOTLIGHT)
  156. atten = GetAttenSpot(normal, vWorldPos.xyz, lightDir);
  157. #else
  158. atten = GetAttenPoint(normal, vWorldPos.xyz, lightDir);
  159. #endif
  160. float shadow = 1.0;
  161. #ifdef SHADOW
  162. shadow = GetShadow(vShadowPos, vWorldPos.w);
  163. #endif
  164. #if defined(SPOTLIGHT)
  165. lightColor = vSpotPos.w > 0.0 ? texture2DProj(sLightSpotMap, vSpotPos).rgb * cLightColor.rgb : vec3(0.0, 0.0, 0.0);
  166. #elif defined(CUBEMASK)
  167. lightColor = textureCube(sLightCubeMap, vCubeMaskVec).rgb * cLightColor.rgb;
  168. #else
  169. lightColor = cLightColor.rgb;
  170. #endif
  171. vec3 toCamera = normalize(cCameraPosPS - vWorldPos.xyz);
  172. vec3 lightVec = normalize(lightDir);
  173. float ndl = clamp((dot(normal, lightVec)), M_EPSILON, 1.0);
  174. vec3 BRDF = GetBRDF(vWorldPos.xyz, lightDir, lightVec, toCamera, normal, roughness, diffColor.rgb, specColor);
  175. finalColor.rgb = BRDF * lightColor * (atten * shadow) / M_PI;
  176. #ifdef AMBIENT
  177. finalColor += cAmbientColor.rgb * diffColor.rgb;
  178. finalColor += cMatEmissiveColor;
  179. gl_FragColor = vec4(GetFog(finalColor, fogFactor), diffColor.a);
  180. #else
  181. gl_FragColor = vec4(GetLitFog(finalColor, fogFactor), diffColor.a);
  182. #endif
  183. #elif defined(DEFERRED)
  184. // Fill deferred G-buffer
  185. const vec3 spareData = vec3(0,0,0); // Can be used to pass more data to deferred renderer
  186. gl_FragData[0] = vec4(specColor, spareData.r);
  187. gl_FragData[1] = vec4(diffColor.rgb, spareData.g);
  188. gl_FragData[2] = vec4(normal * roughness, spareData.b);
  189. gl_FragData[3] = vec4(EncodeDepth(vWorldPos.w), 0.0);
  190. #else
  191. // Ambient & per-vertex lighting
  192. vec3 finalColor = vVertexLight * diffColor.rgb;
  193. #ifdef AO
  194. // If using AO, the vertex light ambient is black, calculate occluded ambient here
  195. finalColor += texture2D(sEmissiveMap, vTexCoord2).rgb * cAmbientColor.rgb * diffColor.rgb;
  196. #endif
  197. #ifdef MATERIAL
  198. // Add light pre-pass accumulation result
  199. // Lights are accumulated at half intensity. Bring back to full intensity now
  200. vec4 lightInput = 2.0 * texture2DProj(sLightBuffer, vScreenPos);
  201. vec3 lightSpecColor = lightInput.a * lightInput.rgb / max(GetIntensity(lightInput.rgb), 0.001);
  202. finalColor += lightInput.rgb * diffColor.rgb + lightSpecColor * specColor;
  203. #endif
  204. vec3 toCamera = normalize(vWorldPos.xyz - cCameraPosPS);
  205. vec3 reflection = normalize(reflect(toCamera, normal));
  206. vec3 cubeColor = vVertexLight.rgb;
  207. #ifdef IBL
  208. vec3 iblColor = ImageBasedLighting(reflection, tangent, bitangent, normal, toCamera, diffColor.rgb, specColor.rgb, roughness, cubeColor);
  209. float gamma = 0.0;
  210. finalColor.rgb += iblColor;
  211. #endif
  212. #ifdef ENVCUBEMAP
  213. finalColor += cMatEnvMapColor * textureCube(sEnvCubeMap, reflect(vReflectionVec, normal)).rgb;
  214. #endif
  215. #ifdef LIGHTMAP
  216. finalColor += texture2D(sEmissiveMap, vTexCoord2).rgb * diffColor.rgb;
  217. #endif
  218. #ifdef EMISSIVEMAP
  219. finalColor += cMatEmissiveColor * texture2D(sEmissiveMap, vTexCoord.xy).rgb;
  220. #else
  221. finalColor += cMatEmissiveColor;
  222. #endif
  223. gl_FragColor = vec4(GetFog(finalColor, fogFactor), diffColor.a);
  224. #endif
  225. }