background.fragment.fx 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  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. vec3 reflectionColor = vec3(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).rgb;
  129. #else
  130. float lodReflectionNormalized = clamp(reflectionLOD, 0., 1.);
  131. float lodReflectionNormalizedDoubled = lodReflectionNormalized * 2.0;
  132. vec3 reflectionSpecularMid = sampleReflection(reflectionSampler, reflectionCoords).rgb;
  133. if(lodReflectionNormalizedDoubled < 1.0){
  134. reflectionColor = mix(
  135. sampleReflection(reflectionSamplerHigh, reflectionCoords).rgb,
  136. reflectionSpecularMid,
  137. lodReflectionNormalizedDoubled
  138. );
  139. } else {
  140. reflectionColor = mix(
  141. reflectionSpecularMid,
  142. sampleReflection(reflectionSamplerLow, reflectionCoords).rgb,
  143. lodReflectionNormalizedDoubled - 1.0
  144. );
  145. }
  146. #endif
  147. #else
  148. vec4 reflectionSample = sampleReflection(reflectionSampler, reflectionCoords);
  149. reflectionColor = reflectionSample.rgb;
  150. #endif
  151. #ifdef GAMMAREFLECTION
  152. reflectionColor = toLinearSpace(reflectionColor.rgb);
  153. #endif
  154. #ifdef REFLECTIONBGR
  155. reflectionColor = reflectionColor.bgr;
  156. #endif
  157. // _____________________________ Levels _____________________________________
  158. reflectionColor *= vReflectionInfos.x;
  159. #endif
  160. // _____________________________ Diffuse Information _______________________________
  161. vec3 diffuseColor = vec3(1., 1., 1.);
  162. float finalAlpha = alpha;
  163. #ifdef DIFFUSE
  164. vec4 diffuseMap = texture2D(diffuseSampler, vDiffuseUV);
  165. #ifdef GAMMADIFFUSE
  166. diffuseMap.rgb = toLinearSpace(diffuseMap.rgb);
  167. #endif
  168. // _____________________________ Levels _____________________________________
  169. diffuseMap.rgb *= vDiffuseInfos.y;
  170. #ifdef DIFFUSEHASALPHA
  171. finalAlpha *= diffuseMap.a;
  172. #endif
  173. diffuseColor = diffuseMap.rgb;
  174. #endif
  175. // _____________________________ MIX ________________________________________
  176. #ifdef REFLECTIONFRESNEL
  177. vec3 colorBase = diffuseColor;
  178. #else
  179. vec3 colorBase = reflectionColor * diffuseColor;
  180. #endif
  181. colorBase = max(colorBase, 0.0);
  182. // ___________________________ COMPOSE _______________________________________
  183. #ifdef USERGBCOLOR
  184. vec3 finalColor = colorBase;
  185. #else
  186. vec3 finalColor = colorBase.r * vPrimaryColor.rgb * vPrimaryColor.a;
  187. finalColor += colorBase.g * vSecondaryColor.rgb * vSecondaryColor.a;
  188. finalColor += colorBase.b * vTertiaryColor.rgb * vTertiaryColor.a;
  189. #endif
  190. // ___________________________ FRESNELS _______________________________________
  191. #ifdef REFLECTIONFRESNEL
  192. vec3 reflectionAmount = vReflectionControl.xxx;
  193. vec3 reflectionReflectance0 = vReflectionControl.yyy;
  194. vec3 reflectionReflectance90 = vReflectionControl.zzz;
  195. float VdotN = dot(normalize(vEyePosition), normalW);
  196. vec3 planarReflectionFresnel = fresnelSchlickEnvironmentGGX(clamp(VdotN, 0.0, 1.0), reflectionReflectance0, reflectionReflectance90, 1.0);
  197. reflectionAmount *= planarReflectionFresnel;
  198. #ifdef REFLECTIONFALLOFF
  199. float reflectionDistanceFalloff = 1.0 - clamp(length(vPositionW.xyz - vBackgroundCenter) * vReflectionControl.w, 0.0, 1.0);
  200. reflectionDistanceFalloff *= reflectionDistanceFalloff;
  201. reflectionAmount *= reflectionDistanceFalloff;
  202. #endif
  203. finalColor = mix(finalColor, reflectionColor, clamp(reflectionAmount, 0., 1.));
  204. #endif
  205. #ifdef OPACITYFRESNEL
  206. float viewAngleToFloor = dot(normalW, normalize(vEyePosition - vBackgroundCenter));
  207. // Fade out the floor plane as the angle between the floor and the camera tends to 0 (starting from startAngle)
  208. const float startAngle = 0.1;
  209. float fadeFactor = clamp(viewAngleToFloor/startAngle, 0.0, 1.0);
  210. finalAlpha *= fadeFactor * fadeFactor;
  211. #endif
  212. // ___________________________ SHADOWS _______________________________________
  213. #ifdef SHADOWINUSE
  214. finalColor = mix(finalColor * shadowLevel, finalColor, globalShadow);
  215. #endif
  216. // ___________________________ FINALIZE _______________________________________
  217. vec4 color = vec4(finalColor, finalAlpha);
  218. #include<fogFragment>
  219. #ifdef IMAGEPROCESSINGPOSTPROCESS
  220. // Sanitize output incase invalid normals or tangents have caused div by 0 or undefined behavior
  221. // this also limits the brightness which helpfully reduces over-sparkling in bloom (native handles this in the bloom blur shader)
  222. color.rgb = clamp(color.rgb, 0., 30.0);
  223. #else
  224. // Alway run even to ensure going back to gamma space.
  225. color = applyImageProcessing(color);
  226. #endif
  227. #ifdef PREMULTIPLYALPHA
  228. // Convert to associative (premultiplied) format if needed.
  229. color.rgb *= color.a;
  230. #endif
  231. #ifdef NOISE
  232. color.rgb += dither(vPositionW.xy, 0.5);
  233. color = max(color, 0.0);
  234. #endif
  235. gl_FragColor = color;
  236. }