background.fragment.fx 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. #ifdef TEXTURELODSUPPORT
  2. #extension GL_EXT_shader_texture_lod : enable
  3. #endif
  4. precision highp float;
  5. #include<__decl__backgroundFragment>
  6. #define RECIPROCAL_PI2 0.15915494
  7. // Constants
  8. uniform vec3 vEyePosition;
  9. // Input
  10. varying vec3 vPositionW;
  11. #ifdef MAINUV1
  12. varying vec2 vMainUV1;
  13. #endif
  14. #ifdef MAINUV2
  15. varying vec2 vMainUV2;
  16. #endif
  17. #ifdef NORMAL
  18. varying vec3 vNormalW;
  19. #endif
  20. #ifdef DIFFUSE
  21. #if DIFFUSEDIRECTUV == 1
  22. #define vDiffuseUV vMainUV1
  23. #elif DIFFUSEDIRECTUV == 2
  24. #define vDiffuseUV vMainUV2
  25. #else
  26. varying vec2 vDiffuseUV;
  27. #endif
  28. uniform sampler2D diffuseSampler;
  29. #endif
  30. // Reflection
  31. #ifdef REFLECTION
  32. #ifdef REFLECTIONMAP_3D
  33. #define sampleReflection(s, c) textureCube(s, c)
  34. uniform samplerCube reflectionSampler;
  35. #ifdef TEXTURELODSUPPORT
  36. #define sampleReflectionLod(s, c, l) textureCubeLodEXT(s, c, l)
  37. #else
  38. uniform samplerCube reflectionSamplerLow;
  39. uniform samplerCube reflectionSamplerHigh;
  40. #endif
  41. #else
  42. #define sampleReflection(s, c) texture2D(s, c)
  43. uniform sampler2D reflectionSampler;
  44. #ifdef TEXTURELODSUPPORT
  45. #define sampleReflectionLod(s, c, l) texture2DLodEXT(s, c, l)
  46. #else
  47. uniform samplerCube reflectionSamplerLow;
  48. uniform samplerCube reflectionSamplerHigh;
  49. #endif
  50. #endif
  51. #ifdef REFLECTIONMAP_SKYBOX
  52. varying vec3 vPositionUVW;
  53. #else
  54. #if defined(REFLECTIONMAP_EQUIRECTANGULAR_FIXED) || defined(REFLECTIONMAP_MIRROREDEQUIRECTANGULAR_FIXED)
  55. varying vec3 vDirectionW;
  56. #endif
  57. #endif
  58. #include<reflectionFunction>
  59. #endif
  60. // Forces linear space for image processing
  61. #ifndef FROMLINEARSPACE
  62. #define FROMLINEARSPACE;
  63. #endif
  64. // Prevent expensive light computations
  65. #ifndef SHADOWONLY
  66. #define SHADOWONLY;
  67. #endif
  68. #include<imageProcessingDeclaration>
  69. // Lights
  70. #include<__decl__lightFragment>[0..maxSimultaneousLights]
  71. #include<helperFunctions>
  72. #include<lightsFragmentFunctions>
  73. #include<shadowsFragmentFunctions>
  74. #include<imageProcessingFunctions>
  75. #include<clipPlaneFragmentDeclaration>
  76. // Fog
  77. #include<fogFragmentDeclaration>
  78. #ifdef REFLECTIONFRESNEL
  79. #define FRESNEL_MAXIMUM_ON_ROUGH 0.25
  80. vec3 fresnelSchlickEnvironmentGGX(float VdotN, vec3 reflectance0, vec3 reflectance90, float smoothness)
  81. {
  82. // Schlick fresnel approximation, extended with basic smoothness term so that rough surfaces do not approach reflectance90 at grazing angle
  83. float weight = mix(FRESNEL_MAXIMUM_ON_ROUGH, 1.0, smoothness);
  84. return reflectance0 + weight * (reflectance90 - reflectance0) * pow(clamp(1.0 - VdotN, 0., 1.), 5.0);
  85. }
  86. #endif
  87. void main(void) {
  88. #include<clipPlaneFragment>
  89. vec3 viewDirectionW = normalize(vEyePosition - vPositionW);
  90. // _____________________________ Normal Information ______________________________
  91. #ifdef NORMAL
  92. vec3 normalW = normalize(vNormalW);
  93. #else
  94. vec3 normalW = vec3(0.0, 1.0, 0.0);
  95. #endif
  96. // _____________________________ Light Information _______________________________
  97. float shadow = 1.;
  98. float globalShadow = 0.;
  99. float shadowLightCount = 0.;
  100. #include<lightFragment>[0..maxSimultaneousLights]
  101. #ifdef SHADOWINUSE
  102. globalShadow /= shadowLightCount;
  103. #else
  104. globalShadow = 1.0;
  105. #endif
  106. // _____________________________ REFLECTION ______________________________________
  107. vec4 reflectionColor = vec4(1., 1., 1., 1.);
  108. #ifdef REFLECTION
  109. vec3 reflectionVector = computeReflectionCoords(vec4(vPositionW, 1.0), normalW);
  110. #ifdef REFLECTIONMAP_OPPOSITEZ
  111. reflectionVector.z *= -1.0;
  112. #endif
  113. // _____________________________ 2D vs 3D Maps ________________________________
  114. #ifdef REFLECTIONMAP_3D
  115. vec3 reflectionCoords = reflectionVector;
  116. #else
  117. vec2 reflectionCoords = reflectionVector.xy;
  118. #ifdef REFLECTIONMAP_PROJECTION
  119. reflectionCoords /= reflectionVector.z;
  120. #endif
  121. reflectionCoords.y = 1.0 - reflectionCoords.y;
  122. #endif
  123. #ifdef REFLECTIONBLUR
  124. float reflectionLOD = vReflectionInfos.y;
  125. #ifdef TEXTURELODSUPPORT
  126. // Apply environment convolution scale/offset filter tuning parameters to the mipmap LOD selection
  127. reflectionLOD = reflectionLOD * log2(vReflectionMicrosurfaceInfos.x) * vReflectionMicrosurfaceInfos.y + vReflectionMicrosurfaceInfos.z;
  128. reflectionColor = sampleReflectionLod(reflectionSampler, reflectionCoords, reflectionLOD);
  129. #else
  130. float lodReflectionNormalized = clamp(reflectionLOD, 0., 1.);
  131. float lodReflectionNormalizedDoubled = lodReflectionNormalized * 2.0;
  132. vec4 reflectionSpecularMid = sampleReflection(reflectionSampler, reflectionCoords);
  133. if(lodReflectionNormalizedDoubled < 1.0){
  134. reflectionColor = mix(
  135. sampleReflection(reflectionSamplerHigh, reflectionCoords),
  136. reflectionSpecularMid,
  137. lodReflectionNormalizedDoubled
  138. );
  139. } else {
  140. reflectionColor = mix(
  141. reflectionSpecularMid,
  142. sampleReflection(reflectionSamplerLow, reflectionCoords),
  143. lodReflectionNormalizedDoubled - 1.0
  144. );
  145. }
  146. #endif
  147. #else
  148. vec4 reflectionSample = sampleReflection(reflectionSampler, reflectionCoords);
  149. reflectionColor = reflectionSample;
  150. #endif
  151. #ifdef RGBDREFLECTION
  152. reflectionColor.rgb = fromRGBD(reflectionColor);
  153. #endif
  154. #ifdef GAMMAREFLECTION
  155. reflectionColor.rgb = toLinearSpace(reflectionColor.rgb);
  156. #endif
  157. #ifdef REFLECTIONBGR
  158. reflectionColor.rgb = reflectionColor.bgr;
  159. #endif
  160. // _____________________________ Levels _____________________________________
  161. reflectionColor.rgb *= vReflectionInfos.x;
  162. #endif
  163. // _____________________________ Diffuse Information _______________________________
  164. vec3 diffuseColor = vec3(1., 1., 1.);
  165. float finalAlpha = alpha;
  166. #ifdef DIFFUSE
  167. vec4 diffuseMap = texture2D(diffuseSampler, vDiffuseUV);
  168. #ifdef GAMMADIFFUSE
  169. diffuseMap.rgb = toLinearSpace(diffuseMap.rgb);
  170. #endif
  171. // _____________________________ Levels _____________________________________
  172. diffuseMap.rgb *= vDiffuseInfos.y;
  173. #ifdef DIFFUSEHASALPHA
  174. finalAlpha *= diffuseMap.a;
  175. #endif
  176. diffuseColor = diffuseMap.rgb;
  177. #endif
  178. // _____________________________ MIX ________________________________________
  179. #ifdef REFLECTIONFRESNEL
  180. vec3 colorBase = diffuseColor;
  181. #else
  182. vec3 colorBase = reflectionColor.rgb * diffuseColor;
  183. #endif
  184. colorBase = max(colorBase, 0.0);
  185. // ___________________________ COMPOSE _______________________________________
  186. #ifdef USERGBCOLOR
  187. vec3 finalColor = colorBase;
  188. #else
  189. #ifdef USEHIGHLIGHTANDSHADOWCOLORS
  190. vec3 mainColor = mix(vPrimaryColorShadow.rgb, vPrimaryColor.rgb, colorBase);
  191. #else
  192. vec3 mainColor = vPrimaryColor.rgb;
  193. #endif
  194. vec3 finalColor = colorBase * mainColor;
  195. #endif
  196. // ___________________________ FRESNELS _______________________________________
  197. #ifdef REFLECTIONFRESNEL
  198. vec3 reflectionAmount = vReflectionControl.xxx;
  199. vec3 reflectionReflectance0 = vReflectionControl.yyy;
  200. vec3 reflectionReflectance90 = vReflectionControl.zzz;
  201. float VdotN = dot(normalize(vEyePosition), normalW);
  202. vec3 planarReflectionFresnel = fresnelSchlickEnvironmentGGX(clamp(VdotN, 0.0, 1.0), reflectionReflectance0, reflectionReflectance90, 1.0);
  203. reflectionAmount *= planarReflectionFresnel;
  204. #ifdef REFLECTIONFALLOFF
  205. float reflectionDistanceFalloff = 1.0 - clamp(length(vPositionW.xyz - vBackgroundCenter) * vReflectionControl.w, 0.0, 1.0);
  206. reflectionDistanceFalloff *= reflectionDistanceFalloff;
  207. reflectionAmount *= reflectionDistanceFalloff;
  208. #endif
  209. finalColor = mix(finalColor, reflectionColor.rgb, clamp(reflectionAmount, 0., 1.));
  210. #endif
  211. #ifdef OPACITYFRESNEL
  212. float viewAngleToFloor = dot(normalW, normalize(vEyePosition - vBackgroundCenter));
  213. // Fade out the floor plane as the angle between the floor and the camera tends to 0 (starting from startAngle)
  214. const float startAngle = 0.1;
  215. float fadeFactor = clamp(viewAngleToFloor/startAngle, 0.0, 1.0);
  216. finalAlpha *= fadeFactor * fadeFactor;
  217. #endif
  218. // ___________________________ SHADOWS _______________________________________
  219. #ifdef SHADOWINUSE
  220. finalColor = mix(finalColor * shadowLevel, finalColor, globalShadow);
  221. #endif
  222. // ___________________________ FINALIZE _______________________________________
  223. vec4 color = vec4(finalColor, finalAlpha);
  224. #include<fogFragment>
  225. #ifdef IMAGEPROCESSINGPOSTPROCESS
  226. // Sanitize output incase invalid normals or tangents have caused div by 0 or undefined behavior
  227. // this also limits the brightness which helpfully reduces over-sparkling in bloom (native handles this in the bloom blur shader)
  228. color.rgb = clamp(color.rgb, 0., 30.0);
  229. #else
  230. // Alway run even to ensure going back to gamma space.
  231. color = applyImageProcessing(color);
  232. #endif
  233. #ifdef PREMULTIPLYALPHA
  234. // Convert to associative (premultiplied) format if needed.
  235. color.rgb *= color.a;
  236. #endif
  237. #ifdef NOISE
  238. color.rgb += dither(vPositionW.xy, 0.5);
  239. color = max(color, 0.0);
  240. #endif
  241. gl_FragColor = color;
  242. }