legacypbr.fragment.fx 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. precision mediump float;
  2. // Constants
  3. #define RECIPROCAL_PI2 0.15915494
  4. #define FRESNEL_MAXIMUM_ON_ROUGH 0.25
  5. uniform vec3 vEyePosition;
  6. uniform vec3 vAmbientColor;
  7. uniform vec4 vAlbedoColor;
  8. uniform vec3 vReflectionColor;
  9. uniform vec4 vLightRadiuses;
  10. // CUSTOM CONTROLS
  11. uniform vec4 vLightingIntensity;
  12. uniform vec4 vCameraInfos;
  13. #ifdef OVERLOADEDVALUES
  14. uniform vec4 vOverloadedIntensity;
  15. uniform vec3 vOverloadedAmbient;
  16. uniform vec3 vOverloadedAlbedo;
  17. uniform vec3 vOverloadedReflectivity;
  18. uniform vec3 vOverloadedEmissive;
  19. uniform vec3 vOverloadedReflection;
  20. uniform vec3 vOverloadedMicroSurface;
  21. #endif
  22. #ifdef OVERLOADEDSHADOWVALUES
  23. uniform vec4 vOverloadedShadowIntensity;
  24. #endif
  25. uniform vec4 vReflectivityColor;
  26. uniform vec3 vEmissiveColor;
  27. // Input
  28. varying vec3 vPositionW;
  29. #ifdef NORMAL
  30. varying vec3 vNormalW;
  31. #endif
  32. #ifdef VERTEXCOLOR
  33. varying vec4 vColor;
  34. #endif
  35. // Lights
  36. #include<lightFragmentDeclaration>[0]
  37. #include<lightFragmentDeclaration>[1]
  38. #include<lightFragmentDeclaration>[2]
  39. #include<lightFragmentDeclaration>[3]
  40. // Samplers
  41. #ifdef ALBEDO
  42. varying vec2 vAlbedoUV;
  43. uniform sampler2D albedoSampler;
  44. uniform vec2 vAlbedoInfos;
  45. #endif
  46. #ifdef AMBIENT
  47. varying vec2 vAmbientUV;
  48. uniform sampler2D ambientSampler;
  49. uniform vec2 vAmbientInfos;
  50. #endif
  51. #ifdef OPACITY
  52. varying vec2 vOpacityUV;
  53. uniform sampler2D opacitySampler;
  54. uniform vec2 vOpacityInfos;
  55. #endif
  56. #ifdef EMISSIVE
  57. varying vec2 vEmissiveUV;
  58. uniform vec2 vEmissiveInfos;
  59. uniform sampler2D emissiveSampler;
  60. #endif
  61. #ifdef LIGHTMAP
  62. varying vec2 vLightmapUV;
  63. uniform vec2 vLightmapInfos;
  64. uniform sampler2D lightmapSampler;
  65. #endif
  66. #if defined(REFLECTIVITY)
  67. varying vec2 vReflectivityUV;
  68. uniform vec2 vReflectivityInfos;
  69. uniform sampler2D reflectivitySampler;
  70. #endif
  71. #include<clipPlaneFragmentDeclaration>
  72. // PBR
  73. #include<pbrFunctions>
  74. #include<harmonicsFunctions>
  75. #include<pbrLightFunctions>
  76. void main(void) {
  77. #include<clipPlaneFragment>
  78. vec3 viewDirectionW = normalize(vEyePosition - vPositionW);
  79. // Base color
  80. vec4 surfaceAlbedo = vec4(1., 1., 1., 1.);
  81. vec3 surfaceAlbedoContribution = vAlbedoColor.rgb;
  82. // Alpha
  83. float alpha = vAlbedoColor.a;
  84. #ifdef ALBEDO
  85. surfaceAlbedo = texture2D(albedoSampler, vAlbedoUV);
  86. surfaceAlbedo = vec4(toLinearSpace(surfaceAlbedo.rgb), surfaceAlbedo.a);
  87. #ifdef ALPHATEST
  88. if (baseColor.a < 0.4)
  89. discard;
  90. #endif
  91. #ifdef ALPHAFROMALBEDO
  92. alpha *= surfaceAlbedo.a;
  93. #endif
  94. surfaceAlbedo.rgb *= vAlbedoInfos.y;
  95. #else
  96. // No Albedo texture.
  97. surfaceAlbedo.rgb = surfaceAlbedoContribution;
  98. surfaceAlbedoContribution = vec3(1., 1., 1.);
  99. #endif
  100. #ifdef VERTEXCOLOR
  101. baseColor.rgb *= vColor.rgb;
  102. #endif
  103. #ifdef OVERLOADEDVALUES
  104. surfaceAlbedo.rgb = mix(surfaceAlbedo.rgb, vOverloadedAlbedo, vOverloadedIntensity.y);
  105. #endif
  106. // Bump
  107. #ifdef NORMAL
  108. vec3 normalW = normalize(vNormalW);
  109. #else
  110. vec3 normalW = vec3(1.0, 1.0, 1.0);
  111. #endif
  112. // Ambient color
  113. vec3 ambientColor = vec3(1., 1., 1.);
  114. #ifdef AMBIENT
  115. ambientColor = texture2D(ambientSampler, vAmbientUV).rgb * vAmbientInfos.y;
  116. #ifdef OVERLOADEDVALUES
  117. ambientColor.rgb = mix(ambientColor.rgb, vOverloadedAmbient, vOverloadedIntensity.x);
  118. #endif
  119. #endif
  120. // Reflectivity map
  121. float microSurface = vReflectivityColor.a;
  122. vec3 surfaceReflectivityColor = vReflectivityColor.rgb;
  123. #ifdef OVERLOADEDVALUES
  124. surfaceReflectivityColor.rgb = mix(surfaceReflectivityColor.rgb, vOverloadedReflectivity, vOverloadedIntensity.z);
  125. #endif
  126. #ifdef REFLECTIVITY
  127. vec4 surfaceReflectivityColorMap = texture2D(reflectivitySampler, vReflectivityUV);
  128. surfaceReflectivityColor = surfaceReflectivityColorMap.rgb;
  129. surfaceReflectivityColor = toLinearSpace(surfaceReflectivityColor);
  130. #ifdef OVERLOADEDVALUES
  131. surfaceReflectivityColor = mix(surfaceReflectivityColor, vOverloadedReflectivity, vOverloadedIntensity.z);
  132. #endif
  133. #ifdef MICROSURFACEFROMREFLECTIVITYMAP
  134. microSurface = surfaceReflectivityColorMap.a;
  135. #else
  136. #ifdef MICROSURFACEAUTOMATIC
  137. microSurface = computeDefaultMicroSurface(microSurface, surfaceReflectivityColor);
  138. #endif
  139. #endif
  140. #endif
  141. #ifdef OVERLOADEDVALUES
  142. microSurface = mix(microSurface, vOverloadedMicroSurface.x, vOverloadedMicroSurface.y);
  143. #endif
  144. // Compute N dot V.
  145. float NdotV = max(0.00000000001, dot(normalW, viewDirectionW));
  146. // Adapt microSurface.
  147. microSurface = clamp(microSurface, 0., 1.) * 0.98;
  148. // Compute roughness.
  149. float roughness = clamp(1. - microSurface, 0.000001, 1.0);
  150. // Lighting
  151. vec3 lightDiffuseContribution = vec3(0., 0., 0.);
  152. #ifdef OVERLOADEDSHADOWVALUES
  153. vec3 shadowedOnlyLightDiffuseContribution = vec3(1., 1., 1.);
  154. #endif
  155. #ifdef SPECULARTERM
  156. vec3 lightSpecularContribution= vec3(0., 0., 0.);
  157. #endif
  158. float notShadowLevel = 1.; // 1 - shadowLevel
  159. float NdotL = -1.;
  160. lightingInfo info;
  161. #include<pbrLightFunctionsCall>[0]
  162. #include<pbrLightFunctionsCall>[1]
  163. #include<pbrLightFunctionsCall>[2]
  164. #include<pbrLightFunctionsCall>[3]
  165. #ifdef SPECULARTERM
  166. lightSpecularContribution *= vLightingIntensity.w;
  167. #endif
  168. #ifdef OPACITY
  169. vec4 opacityMap = texture2D(opacitySampler, vOpacityUV);
  170. #ifdef OPACITYRGB
  171. opacityMap.rgb = opacityMap.rgb * vec3(0.3, 0.59, 0.11);
  172. alpha *= (opacityMap.x + opacityMap.y + opacityMap.z)* vOpacityInfos.y;
  173. #else
  174. alpha *= opacityMap.a * vOpacityInfos.y;
  175. #endif
  176. #endif
  177. #ifdef VERTEXALPHA
  178. alpha *= vColor.a;
  179. #endif
  180. // Reflection
  181. vec3 environmentRadiance = vReflectionColor.rgb;
  182. vec3 environmentIrradiance = vReflectionColor.rgb;
  183. #ifdef OVERLOADEDVALUES
  184. environmentIrradiance = mix(environmentIrradiance, vOverloadedReflection, vOverloadedMicroSurface.z);
  185. environmentRadiance = mix(environmentRadiance, vOverloadedReflection, vOverloadedMicroSurface.z);
  186. #endif
  187. environmentRadiance *= vLightingIntensity.z;
  188. environmentIrradiance *= vLightingIntensity.z;
  189. // Compute reflection reflectivity fresnel
  190. vec3 specularEnvironmentR0 = surfaceReflectivityColor.rgb;
  191. vec3 specularEnvironmentR90 = vec3(1.0, 1.0, 1.0);
  192. vec3 specularEnvironmentReflectance = FresnelSchlickEnvironmentGGX(clamp(NdotV, 0., 1.), specularEnvironmentR0, specularEnvironmentR90, sqrt(microSurface));
  193. // Apply Energy Conservation taking in account the environment level only if the environment is present.
  194. float reflectance = max(max(surfaceReflectivityColor.r, surfaceReflectivityColor.g), surfaceReflectivityColor.b);
  195. surfaceAlbedo.rgb = (1. - reflectance) * surfaceAlbedo.rgb;
  196. environmentRadiance *= specularEnvironmentReflectance;
  197. // Emissive
  198. vec3 surfaceEmissiveColor = vEmissiveColor;
  199. #ifdef EMISSIVE
  200. vec3 emissiveColorTex = texture2D(emissiveSampler, vEmissiveUV).rgb;
  201. surfaceEmissiveColor = toLinearSpace(emissiveColorTex.rgb) * surfaceEmissiveColor * vEmissiveInfos.y;
  202. #endif
  203. #ifdef OVERLOADEDVALUES
  204. surfaceEmissiveColor = mix(surfaceEmissiveColor, vOverloadedEmissive, vOverloadedIntensity.w);
  205. #endif
  206. // Composition
  207. #ifdef EMISSIVEASILLUMINATION
  208. vec3 finalDiffuse = max(lightDiffuseContribution * surfaceAlbedoContribution + vAmbientColor, 0.0) * surfaceAlbedo.rgb;
  209. #ifdef OVERLOADEDSHADOWVALUES
  210. shadowedOnlyLightDiffuseContribution = max(shadowedOnlyLightDiffuseContribution * surfaceAlbedoContribution + vAmbientColor, 0.0) * surfaceAlbedo.rgb;
  211. #endif
  212. #else
  213. #ifdef LINKEMISSIVEWITHALBEDO
  214. vec3 finalDiffuse = max((lightDiffuseContribution + surfaceEmissiveColor) * surfaceAlbedoContribution + vAmbientColor, 0.0) * surfaceAlbedo.rgb;
  215. #ifdef OVERLOADEDSHADOWVALUES
  216. shadowedOnlyLightDiffuseContribution = max((shadowedOnlyLightDiffuseContribution + surfaceEmissiveColor) * surfaceAlbedoContribution + vAmbientColor, 0.0) * surfaceAlbedo.rgb;
  217. #endif
  218. #else
  219. vec3 finalDiffuse = max(lightDiffuseContribution * surfaceAlbedoContribution + surfaceEmissiveColor + vAmbientColor, 0.0) * surfaceAlbedo.rgb;
  220. #ifdef OVERLOADEDSHADOWVALUES
  221. shadowedOnlyLightDiffuseContribution = max(shadowedOnlyLightDiffuseContribution * surfaceAlbedoContribution + surfaceEmissiveColor + vAmbientColor, 0.0) * surfaceAlbedo.rgb;
  222. #endif
  223. #endif
  224. #endif
  225. #ifdef OVERLOADEDSHADOWVALUES
  226. finalDiffuse = mix(finalDiffuse, shadowedOnlyLightDiffuseContribution, (1.0 - vOverloadedShadowIntensity.y));
  227. #endif
  228. #ifdef SPECULARTERM
  229. vec3 finalSpecular = lightSpecularContribution * surfaceReflectivityColor;
  230. #else
  231. vec3 finalSpecular = vec3(0.0);
  232. #endif
  233. #ifdef SPECULAROVERALPHA
  234. alpha = clamp(alpha + getLuminance(finalSpecular), 0., 1.);
  235. #endif
  236. #ifdef RADIANCEOVERALPHA
  237. alpha = clamp(alpha + getLuminance(environmentRadiance), 0., 1.);
  238. #endif
  239. // Composition
  240. // Reflection already includes the environment intensity.
  241. #ifdef EMISSIVEASILLUMINATION
  242. vec4 finalColor = vec4(finalDiffuse * ambientColor * vLightingIntensity.x + surfaceAlbedo.rgb * environmentIrradiance + finalSpecular * vLightingIntensity.x + environmentRadiance + surfaceEmissiveColor * vLightingIntensity.y, alpha);
  243. #else
  244. vec4 finalColor = vec4(finalDiffuse * ambientColor * vLightingIntensity.x + surfaceAlbedo.rgb * environmentIrradiance + finalSpecular * vLightingIntensity.x + environmentRadiance, alpha);
  245. #endif
  246. finalColor = max(finalColor, 0.0);
  247. #ifdef CAMERATONEMAP
  248. finalColor.rgb = toneMaps(finalColor.rgb);
  249. #endif
  250. finalColor.rgb = toGammaSpace(finalColor.rgb);
  251. #ifdef CAMERACONTRAST
  252. finalColor = contrasts(finalColor);
  253. #endif
  254. gl_FragColor = finalColor;
  255. }