screenSpaceReflection2.fragment.js 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. // Do not edit.
  2. import { ShaderStore } from "../Engines/shaderStore.js";
  3. import "./ShadersInclude/helperFunctions.js";
  4. import "./ShadersInclude/pbrBRDFFunctions.js";
  5. import "./ShadersInclude/screenSpaceRayTrace.js";
  6. const name = "screenSpaceReflection2PixelShader";
  7. const shader = `#if defined(WEBGL2) || defined(WEBGPU) || defined(NATIVE)
  8. #define TEXTUREFUNC(s,c,lod) texture2DLodEXT(s,c,lod)
  9. #define TEXTURECUBEFUNC(s,c,lod) textureLod(s,c,lod)
  10. #else
  11. #define TEXTUREFUNC(s,c,bias) texture2D(s,c,bias)
  12. #define TEXTURECUBEFUNC(s,c,bias) textureCube(s,c,bias)
  13. #endif
  14. uniform sampler2D textureSampler;varying vec2 vUV;
  15. #ifdef SSR_SUPPORTED
  16. uniform sampler2D reflectivitySampler;uniform sampler2D normalSampler;uniform sampler2D depthSampler;
  17. #ifdef SSRAYTRACE_USE_BACK_DEPTHBUFFER
  18. uniform sampler2D backDepthSampler;uniform float backSizeFactor;
  19. #endif
  20. #ifdef SSR_USE_ENVIRONMENT_CUBE
  21. uniform samplerCube envCubeSampler;
  22. #ifdef SSR_USE_LOCAL_REFLECTIONMAP_CUBIC
  23. uniform vec3 vReflectionPosition;uniform vec3 vReflectionSize;
  24. #endif
  25. #endif
  26. uniform mat4 view;uniform mat4 invView;uniform mat4 projection;uniform mat4 invProjectionMatrix;uniform mat4 projectionPixel;uniform float nearPlaneZ;uniform float stepSize;uniform float maxSteps;uniform float strength;uniform float thickness;uniform float roughnessFactor;uniform float reflectionSpecularFalloffExponent;uniform float maxDistance;uniform float selfCollisionNumSkip;uniform float reflectivityThreshold;
  27. #include<helperFunctions>
  28. #include<pbrBRDFFunctions>
  29. #include<screenSpaceRayTrace>
  30. vec3 hash(vec3 a)
  31. {a=fract(a*0.8);a+=dot(a,a.yxz+19.19);return fract((a.xxy+a.yxx)*a.zyx);}
  32. float computeAttenuationForIntersection(ivec2 hitPixel,vec2 hitUV,vec3 vsRayOrigin,vec3 vsHitPoint,vec3 reflectionVector,float maxRayDistance,float numIterations) {float attenuation=1.0;
  33. #ifdef SSR_ATTENUATE_SCREEN_BORDERS
  34. vec2 dCoords=smoothstep(0.2,0.6,abs(vec2(0.5,0.5)-hitUV.xy));attenuation*=clamp(1.0-(dCoords.x+dCoords.y),0.0,1.0);
  35. #endif
  36. #ifdef SSR_ATTENUATE_INTERSECTION_DISTANCE
  37. attenuation*=1.0-clamp(distance(vsRayOrigin,vsHitPoint)/maxRayDistance,0.0,1.0);
  38. #endif
  39. #ifdef SSR_ATTENUATE_INTERSECTION_NUMITERATIONS
  40. attenuation*=1.0-(numIterations/maxSteps);
  41. #endif
  42. #ifdef SSR_ATTENUATE_BACKFACE_REFLECTION
  43. vec3 reflectionNormal=texelFetch(normalSampler,hitPixel,0).xyz;float directionBasedAttenuation=smoothstep(-0.17,0.0,dot(reflectionNormal,-reflectionVector));attenuation*=directionBasedAttenuation;
  44. #endif
  45. return attenuation;}
  46. #endif
  47. void main()
  48. {
  49. #ifdef SSR_SUPPORTED
  50. vec4 colorFull=TEXTUREFUNC(textureSampler,vUV,0.0);vec3 color=colorFull.rgb;vec4 reflectivity=TEXTUREFUNC(reflectivitySampler,vUV,0.0);
  51. #ifndef SSR_DISABLE_REFLECTIVITY_TEST
  52. if (max(reflectivity.r,max(reflectivity.g,reflectivity.b))<=reflectivityThreshold) {
  53. #ifdef SSR_USE_BLUR
  54. gl_FragColor=vec4(0.);
  55. #else
  56. gl_FragColor=colorFull;
  57. #endif
  58. return;}
  59. #endif
  60. #ifdef SSR_INPUT_IS_GAMMA_SPACE
  61. color=toLinearSpace(color);
  62. #endif
  63. vec2 texSize=vec2(textureSize(depthSampler,0));vec3 csNormal=texelFetch(normalSampler,ivec2(vUV*texSize),0).xyz;
  64. #ifdef SSR_DECODE_NORMAL
  65. csNormal=csNormal*2.0-1.0;
  66. #endif
  67. #ifdef SSR_NORMAL_IS_IN_WORLDSPACE
  68. csNormal=(view*vec4(csNormal,0.0)).xyz;
  69. #endif
  70. float depth=texelFetch(depthSampler,ivec2(vUV*texSize),0).r;vec3 csPosition=computeViewPosFromUVDepth(vUV,depth,projection,invProjectionMatrix);vec3 csViewDirection=normalize(csPosition);vec3 csReflectedVector=reflect(csViewDirection,csNormal);
  71. #ifdef SSR_USE_ENVIRONMENT_CUBE
  72. vec3 wReflectedVector=vec3(invView*vec4(csReflectedVector,0.0));
  73. #ifdef SSR_USE_LOCAL_REFLECTIONMAP_CUBIC
  74. vec4 worldPos=invView*vec4(csPosition,1.0);wReflectedVector=parallaxCorrectNormal(worldPos.xyz,normalize(wReflectedVector),vReflectionSize,vReflectionPosition);
  75. #endif
  76. #ifdef SSR_INVERTCUBICMAP
  77. wReflectedVector.y*=-1.0;
  78. #endif
  79. #ifdef SSRAYTRACE_RIGHT_HANDED_SCENE
  80. wReflectedVector.z*=-1.0;
  81. #endif
  82. vec3 envColor=TEXTURECUBEFUNC(envCubeSampler,wReflectedVector,0.0).xyz;
  83. #ifdef SSR_ENVIRONMENT_CUBE_IS_GAMMASPACE
  84. envColor=toLinearSpace(envColor);
  85. #endif
  86. #else
  87. vec3 envColor=color;
  88. #endif
  89. float reflectionAttenuation=1.0;bool rayHasHit=false;vec2 startPixel;vec2 hitPixel;vec3 hitPoint;float numIterations;
  90. #ifdef SSRAYTRACE_DEBUG
  91. vec3 debugColor;
  92. #endif
  93. #ifdef SSR_ATTENUATE_FACING_CAMERA
  94. reflectionAttenuation*=1.0-smoothstep(0.25,0.5,dot(-csViewDirection,csReflectedVector));
  95. #endif
  96. if (reflectionAttenuation>0.0) {
  97. #ifdef SSR_USE_BLUR
  98. vec3 jitt=vec3(0.);
  99. #else
  100. float roughness=1.0-reflectivity.a;vec3 jitt=mix(vec3(0.0),hash(csPosition)-vec3(0.5),roughness)*roughnessFactor;
  101. #endif
  102. vec2 uv2=vUV*texSize;float c=(uv2.x+uv2.y)*0.25;float jitter=mod(c,1.0);
  103. rayHasHit=traceScreenSpaceRay1(
  104. csPosition,
  105. normalize(csReflectedVector+jitt),
  106. projectionPixel,
  107. depthSampler,
  108. texSize,
  109. #ifdef SSRAYTRACE_USE_BACK_DEPTHBUFFER
  110. backDepthSampler,
  111. backSizeFactor,
  112. #endif
  113. thickness,
  114. nearPlaneZ,
  115. stepSize,
  116. jitter,
  117. maxSteps,
  118. maxDistance,
  119. selfCollisionNumSkip,
  120. startPixel,
  121. hitPixel,
  122. hitPoint,
  123. numIterations
  124. #ifdef SSRAYTRACE_DEBUG
  125. ,debugColor
  126. #endif
  127. );}
  128. #ifdef SSRAYTRACE_DEBUG
  129. gl_FragColor=vec4(debugColor,1.);return;
  130. #endif
  131. vec3 F0=reflectivity.rgb;vec3 fresnel=fresnelSchlickGGX(max(dot(csNormal,-csViewDirection),0.0),F0,vec3(1.));vec3 SSR=envColor;if (rayHasHit) {vec3 reflectedColor=texelFetch(textureSampler,ivec2(hitPixel),0).rgb;
  132. #ifdef SSR_INPUT_IS_GAMMA_SPACE
  133. reflectedColor=toLinearSpace(reflectedColor);
  134. #endif
  135. reflectionAttenuation*=computeAttenuationForIntersection(ivec2(hitPixel),hitPixel/texSize,csPosition,hitPoint,csReflectedVector,maxDistance,numIterations);SSR=reflectedColor*reflectionAttenuation+(1.0-reflectionAttenuation)*envColor;}
  136. #ifndef SSR_BLEND_WITH_FRESNEL
  137. SSR*=fresnel;
  138. #endif
  139. #ifdef SSR_USE_BLUR
  140. float blur_radius=0.0;float roughness=1.0-reflectivity.a*(1.0-roughnessFactor);if (roughness>0.001) {float cone_angle=min(roughness,0.999)*3.14159265*0.5;float cone_len=distance(startPixel,hitPixel);float op_len=2.0*tan(cone_angle)*cone_len;
  141. float a=op_len;float h=cone_len;float a2=a*a;float fh2=4.0f*h*h;blur_radius=(a*(sqrt(a2+fh2)-a))/(4.0f*h);}
  142. gl_FragColor=vec4(SSR,blur_radius/255.0);
  143. #else
  144. #ifdef SSR_BLEND_WITH_FRESNEL
  145. vec3 reflectionMultiplier=clamp(pow(fresnel*strength,vec3(reflectionSpecularFalloffExponent)),0.0,1.0);
  146. #else
  147. vec3 reflectionMultiplier=clamp(pow(reflectivity.rgb*strength,vec3(reflectionSpecularFalloffExponent)),0.0,1.0);
  148. #endif
  149. vec3 colorMultiplier=1.0-reflectionMultiplier;vec3 finalColor=(color*colorMultiplier)+(SSR*reflectionMultiplier);
  150. #ifdef SSR_OUTPUT_IS_GAMMA_SPACE
  151. finalColor=toGammaSpace(finalColor);
  152. #endif
  153. gl_FragColor=vec4(finalColor,colorFull.a);
  154. #endif
  155. #else
  156. gl_FragColor=TEXTUREFUNC(textureSampler,vUV,0.0);
  157. #endif
  158. }
  159. `;
  160. // Sideeffect
  161. ShaderStore.ShadersStore[name] = shader;
  162. /** @internal */
  163. export const screenSpaceReflection2PixelShader = { name, shader };
  164. //# sourceMappingURL=screenSpaceReflection2.fragment.js.map