background.fragment.js 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. // Do not edit.
  2. import { ShaderStore } from "../Engines/shaderStore.js";
  3. import "./ShadersInclude/backgroundFragmentDeclaration.js";
  4. import "./ShadersInclude/backgroundUboDeclaration.js";
  5. import "./ShadersInclude/helperFunctions.js";
  6. import "./ShadersInclude/reflectionFunction.js";
  7. import "./ShadersInclude/imageProcessingDeclaration.js";
  8. import "./ShadersInclude/lightFragmentDeclaration.js";
  9. import "./ShadersInclude/lightUboDeclaration.js";
  10. import "./ShadersInclude/lightsFragmentFunctions.js";
  11. import "./ShadersInclude/shadowsFragmentFunctions.js";
  12. import "./ShadersInclude/imageProcessingFunctions.js";
  13. import "./ShadersInclude/logDepthDeclaration.js";
  14. import "./ShadersInclude/clipPlaneFragmentDeclaration.js";
  15. import "./ShadersInclude/fogFragmentDeclaration.js";
  16. import "./ShadersInclude/clipPlaneFragment.js";
  17. import "./ShadersInclude/lightFragment.js";
  18. import "./ShadersInclude/logDepthFragment.js";
  19. import "./ShadersInclude/fogFragment.js";
  20. const name = "backgroundPixelShader";
  21. const shader = `#ifdef TEXTURELODSUPPORT
  22. #extension GL_EXT_shader_texture_lod : enable
  23. #endif
  24. precision highp float;
  25. #include<__decl__backgroundFragment>
  26. #include<helperFunctions>
  27. varying vec3 vPositionW;
  28. #ifdef MAINUV1
  29. varying vec2 vMainUV1;
  30. #endif
  31. #ifdef MAINUV2
  32. varying vec2 vMainUV2;
  33. #endif
  34. #ifdef NORMAL
  35. varying vec3 vNormalW;
  36. #endif
  37. #ifdef DIFFUSE
  38. #if DIFFUSEDIRECTUV==1
  39. #define vDiffuseUV vMainUV1
  40. #elif DIFFUSEDIRECTUV==2
  41. #define vDiffuseUV vMainUV2
  42. #else
  43. varying vec2 vDiffuseUV;
  44. #endif
  45. uniform sampler2D diffuseSampler;
  46. #endif
  47. #ifdef REFLECTION
  48. #ifdef REFLECTIONMAP_3D
  49. #define sampleReflection(s,c) textureCube(s,c)
  50. uniform samplerCube reflectionSampler;
  51. #ifdef TEXTURELODSUPPORT
  52. #define sampleReflectionLod(s,c,l) textureCubeLodEXT(s,c,l)
  53. #else
  54. uniform samplerCube reflectionSamplerLow;uniform samplerCube reflectionSamplerHigh;
  55. #endif
  56. #else
  57. #define sampleReflection(s,c) texture2D(s,c)
  58. uniform sampler2D reflectionSampler;
  59. #ifdef TEXTURELODSUPPORT
  60. #define sampleReflectionLod(s,c,l) texture2DLodEXT(s,c,l)
  61. #else
  62. uniform samplerCube reflectionSamplerLow;uniform samplerCube reflectionSamplerHigh;
  63. #endif
  64. #endif
  65. #ifdef REFLECTIONMAP_SKYBOX
  66. varying vec3 vPositionUVW;
  67. #else
  68. #if defined(REFLECTIONMAP_EQUIRECTANGULAR_FIXED) || defined(REFLECTIONMAP_MIRROREDEQUIRECTANGULAR_FIXED)
  69. varying vec3 vDirectionW;
  70. #endif
  71. #endif
  72. #include<reflectionFunction>
  73. #endif
  74. #ifndef FROMLINEARSPACE
  75. #define FROMLINEARSPACE;
  76. #endif
  77. #ifndef SHADOWONLY
  78. #define SHADOWONLY;
  79. #endif
  80. #include<imageProcessingDeclaration>
  81. #include<__decl__lightFragment>[0..maxSimultaneousLights]
  82. #include<lightsFragmentFunctions>
  83. #include<shadowsFragmentFunctions>
  84. #include<imageProcessingFunctions>
  85. #ifdef LOGARITHMICDEPTH
  86. #extension GL_EXT_frag_depth : enable
  87. #endif
  88. #include<logDepthDeclaration>
  89. #include<clipPlaneFragmentDeclaration>
  90. #include<fogFragmentDeclaration>
  91. #ifdef REFLECTIONFRESNEL
  92. #define FRESNEL_MAXIMUM_ON_ROUGH 0.25
  93. vec3 fresnelSchlickEnvironmentGGX(float VdotN,vec3 reflectance0,vec3 reflectance90,float smoothness)
  94. {float weight=mix(FRESNEL_MAXIMUM_ON_ROUGH,1.0,smoothness);return reflectance0+weight*(reflectance90-reflectance0)*pow5(saturate(1.0-VdotN));}
  95. #endif
  96. #ifdef PROJECTED_GROUND
  97. float diskIntersectWithBackFaceCulling(vec3 ro,vec3 rd,vec3 c,float r) {float d=rd.y;if(d>0.0) { return 1e6; }
  98. vec3 o=ro-c;float t=-o.y/d;vec3 q=o+rd*t;return (dot(q,q)<r*r) ? t : 1e6;}
  99. float sphereIntersect(vec3 ro,vec3 rd,float ra) {float b=dot(ro,rd);float c=dot(ro,ro)-ra*ra;float h=b*b-c;if(h<0.0) { return -1.0; }
  100. h=sqrt(h);return-b+h;}
  101. vec3 project(vec3 viewDirectionW,vec3 eyePosition) {float radius=projectedGroundInfos.x;float height=projectedGroundInfos.y;vec3 camDir=-viewDirectionW;float skySphereDistance=sphereIntersect(eyePosition,camDir,radius);vec3 skySpherePositionW=eyePosition+camDir*skySphereDistance;vec3 p=normalize(skySpherePositionW);eyePosition.y-=height;float sIntersection=sphereIntersect(eyePosition,p,radius);vec3 h=vec3(0.0,-height,0.0);float dIntersection=diskIntersectWithBackFaceCulling(eyePosition,p,h,radius);p=(eyePosition+min(sIntersection,dIntersection)*p);return p;}
  102. #endif
  103. #define CUSTOM_FRAGMENT_DEFINITIONS
  104. void main(void) {
  105. #define CUSTOM_FRAGMENT_MAIN_BEGIN
  106. #include<clipPlaneFragment>
  107. vec3 viewDirectionW=normalize(vEyePosition.xyz-vPositionW);
  108. #ifdef NORMAL
  109. vec3 normalW=normalize(vNormalW);
  110. #else
  111. vec3 normalW=vec3(0.0,1.0,0.0);
  112. #endif
  113. float shadow=1.;float globalShadow=0.;float shadowLightCount=0.;float aggShadow=0.;float numLights=0.;
  114. #include<lightFragment>[0..maxSimultaneousLights]
  115. #ifdef SHADOWINUSE
  116. globalShadow/=shadowLightCount;
  117. #else
  118. globalShadow=1.0;
  119. #endif
  120. #ifndef BACKMAT_SHADOWONLY
  121. vec4 reflectionColor=vec4(1.,1.,1.,1.);
  122. #ifdef REFLECTION
  123. #ifdef PROJECTED_GROUND
  124. vec3 reflectionVector=project(viewDirectionW,vEyePosition.xyz);reflectionVector=vec3(reflectionMatrix*vec4(reflectionVector,1.));
  125. #else
  126. vec3 reflectionVector=computeReflectionCoords(vec4(vPositionW,1.0),normalW);
  127. #endif
  128. #ifdef REFLECTIONMAP_OPPOSITEZ
  129. reflectionVector.z*=-1.0;
  130. #endif
  131. #ifdef REFLECTIONMAP_3D
  132. vec3 reflectionCoords=reflectionVector;
  133. #else
  134. vec2 reflectionCoords=reflectionVector.xy;
  135. #ifdef REFLECTIONMAP_PROJECTION
  136. reflectionCoords/=reflectionVector.z;
  137. #endif
  138. reflectionCoords.y=1.0-reflectionCoords.y;
  139. #endif
  140. #ifdef REFLECTIONBLUR
  141. float reflectionLOD=vReflectionInfos.y;
  142. #ifdef TEXTURELODSUPPORT
  143. reflectionLOD=reflectionLOD*log2(vReflectionMicrosurfaceInfos.x)*vReflectionMicrosurfaceInfos.y+vReflectionMicrosurfaceInfos.z;reflectionColor=sampleReflectionLod(reflectionSampler,reflectionCoords,reflectionLOD);
  144. #else
  145. float lodReflectionNormalized=saturate(reflectionLOD);float lodReflectionNormalizedDoubled=lodReflectionNormalized*2.0;vec4 reflectionSpecularMid=sampleReflection(reflectionSampler,reflectionCoords);if(lodReflectionNormalizedDoubled<1.0){reflectionColor=mix(
  146. sampleReflection(reflectionSamplerHigh,reflectionCoords),
  147. reflectionSpecularMid,
  148. lodReflectionNormalizedDoubled
  149. );} else {reflectionColor=mix(
  150. reflectionSpecularMid,
  151. sampleReflection(reflectionSamplerLow,reflectionCoords),
  152. lodReflectionNormalizedDoubled-1.0
  153. );}
  154. #endif
  155. #else
  156. vec4 reflectionSample=sampleReflection(reflectionSampler,reflectionCoords);reflectionColor=reflectionSample;
  157. #endif
  158. #ifdef RGBDREFLECTION
  159. reflectionColor.rgb=fromRGBD(reflectionColor);
  160. #endif
  161. #ifdef GAMMAREFLECTION
  162. reflectionColor.rgb=toLinearSpace(reflectionColor.rgb);
  163. #endif
  164. #ifdef REFLECTIONBGR
  165. reflectionColor.rgb=reflectionColor.bgr;
  166. #endif
  167. reflectionColor.rgb*=vReflectionInfos.x;
  168. #endif
  169. vec3 diffuseColor=vec3(1.,1.,1.);float finalAlpha=alpha;
  170. #ifdef DIFFUSE
  171. vec4 diffuseMap=texture2D(diffuseSampler,vDiffuseUV);
  172. #ifdef GAMMADIFFUSE
  173. diffuseMap.rgb=toLinearSpace(diffuseMap.rgb);
  174. #endif
  175. diffuseMap.rgb*=vDiffuseInfos.y;
  176. #ifdef DIFFUSEHASALPHA
  177. finalAlpha*=diffuseMap.a;
  178. #endif
  179. diffuseColor=diffuseMap.rgb;
  180. #endif
  181. #ifdef REFLECTIONFRESNEL
  182. vec3 colorBase=diffuseColor;
  183. #else
  184. vec3 colorBase=reflectionColor.rgb*diffuseColor;
  185. #endif
  186. colorBase=max(colorBase,0.0);
  187. #ifdef USERGBCOLOR
  188. vec3 finalColor=colorBase;
  189. #else
  190. #ifdef USEHIGHLIGHTANDSHADOWCOLORS
  191. vec3 mainColor=mix(vPrimaryColorShadow.rgb,vPrimaryColor.rgb,colorBase);
  192. #else
  193. vec3 mainColor=vPrimaryColor.rgb;
  194. #endif
  195. vec3 finalColor=colorBase*mainColor;
  196. #endif
  197. #ifdef REFLECTIONFRESNEL
  198. vec3 reflectionAmount=vReflectionControl.xxx;vec3 reflectionReflectance0=vReflectionControl.yyy;vec3 reflectionReflectance90=vReflectionControl.zzz;float VdotN=dot(normalize(vEyePosition.xyz),normalW);vec3 planarReflectionFresnel=fresnelSchlickEnvironmentGGX(saturate(VdotN),reflectionReflectance0,reflectionReflectance90,1.0);reflectionAmount*=planarReflectionFresnel;
  199. #ifdef REFLECTIONFALLOFF
  200. float reflectionDistanceFalloff=1.0-saturate(length(vPositionW.xyz-vBackgroundCenter)*vReflectionControl.w);reflectionDistanceFalloff*=reflectionDistanceFalloff;reflectionAmount*=reflectionDistanceFalloff;
  201. #endif
  202. finalColor=mix(finalColor,reflectionColor.rgb,saturate(reflectionAmount));
  203. #endif
  204. #ifdef OPACITYFRESNEL
  205. float viewAngleToFloor=dot(normalW,normalize(vEyePosition.xyz-vBackgroundCenter));const float startAngle=0.1;float fadeFactor=saturate(viewAngleToFloor/startAngle);finalAlpha*=fadeFactor*fadeFactor;
  206. #endif
  207. #ifdef SHADOWINUSE
  208. finalColor=mix(finalColor*shadowLevel,finalColor,globalShadow);
  209. #endif
  210. vec4 color=vec4(finalColor,finalAlpha);
  211. #else
  212. vec4 color=vec4(vPrimaryColor.rgb,(1.0-clamp(globalShadow,0.,1.))*alpha);
  213. #endif
  214. #include<logDepthFragment>
  215. #include<fogFragment>
  216. #ifdef IMAGEPROCESSINGPOSTPROCESS
  217. #if !defined(SKIPFINALCOLORCLAMP)
  218. color.rgb=clamp(color.rgb,0.,30.0);
  219. #endif
  220. #else
  221. color=applyImageProcessing(color);
  222. #endif
  223. #ifdef PREMULTIPLYALPHA
  224. color.rgb*=color.a;
  225. #endif
  226. #ifdef NOISE
  227. color.rgb+=dither(vPositionW.xy,0.5);color=max(color,0.0);
  228. #endif
  229. gl_FragColor=color;
  230. #define CUSTOM_FRAGMENT_MAIN_END
  231. }
  232. `;
  233. // Sideeffect
  234. ShaderStore.ShadersStore[name] = shader;
  235. /** @internal */
  236. export const backgroundPixelShader = { name, shader };
  237. //# sourceMappingURL=background.fragment.js.map