screenSpaceReflection.fragment.js 4.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. // Do not edit.
  2. import { ShaderStore } from "../Engines/shaderStore.js";
  3. const name = "screenSpaceReflectionPixelShader";
  4. const shader = `uniform sampler2D textureSampler;
  5. #ifdef SSR_SUPPORTED
  6. uniform sampler2D reflectivitySampler;uniform sampler2D normalSampler;uniform sampler2D positionSampler;
  7. #endif
  8. uniform mat4 view;uniform mat4 projection;uniform float stepSize;uniform float strength;uniform float threshold;uniform float roughnessFactor;uniform float reflectionSpecularFalloffExponent;varying vec2 vUV;
  9. #ifdef SSR_SUPPORTED
  10. struct ReflectionInfo {vec3 color;vec4 coords;};/**
  11. * According to specular,see https:
  12. */
  13. vec3 fresnelSchlick(float cosTheta,vec3 F0)
  14. {return F0+(1.0-F0)*pow(1.0-cosTheta,5.0);}
  15. /**
  16. * Once the pixel's coordinates has been found,let's adjust (smooth) a little bit
  17. * by sampling multiple reflection pixels.
  18. */
  19. ReflectionInfo smoothReflectionInfo(vec3 dir,vec3 hitCoord)
  20. {ReflectionInfo info;info.color=vec3(0.0);vec4 projectedCoord;float sampledDepth;for(int i=0; i<SMOOTH_STEPS; i++)
  21. {projectedCoord=projection*vec4(hitCoord,1.0);projectedCoord.xy/=projectedCoord.w;projectedCoord.xy=0.5*projectedCoord.xy+vec2(0.5);sampledDepth=(view*texture2D(positionSampler,projectedCoord.xy)).z;float depth=sampledDepth-hitCoord.z;dir*=0.5;if(depth>0.0)
  22. hitCoord-=dir;else
  23. hitCoord+=dir;info.color+=texture2D(textureSampler,projectedCoord.xy).rgb;}
  24. projectedCoord=projection*vec4(hitCoord,1.0);projectedCoord.xy/=projectedCoord.w;projectedCoord.xy=0.5*projectedCoord.xy+vec2(0.5);info.coords=vec4(projectedCoord.xy,sampledDepth,1.0);info.color+=texture2D(textureSampler,projectedCoord.xy).rgb;info.color/=float(SMOOTH_STEPS+1);return info;}
  25. /**
  26. * Tests the given world position (hitCoord) according to the given reflection vector (dir)
  27. * until it finds a collision (means that depth is enough close to say "it's the pixel to sample!").
  28. */
  29. ReflectionInfo getReflectionInfo(vec3 dir,vec3 hitCoord)
  30. {ReflectionInfo info;vec4 projectedCoord;float sampledDepth;dir*=stepSize;for(int i=0; i<REFLECTION_SAMPLES; i++)
  31. {hitCoord+=dir;projectedCoord=projection*vec4(hitCoord,1.0);projectedCoord.xy/=projectedCoord.w;projectedCoord.xy=0.5*projectedCoord.xy+vec2(0.5);sampledDepth=(view*texture2D(positionSampler,projectedCoord.xy)).z;float depth=sampledDepth-hitCoord.z;
  32. #ifdef RIGHT_HANDED_SCENE
  33. depth*=-1.0;
  34. #endif
  35. if(((depth-dir.z)<threshold) && depth<=0.0)
  36. {
  37. #ifdef ENABLE_SMOOTH_REFLECTIONS
  38. return smoothReflectionInfo(dir,hitCoord);
  39. #else
  40. info.color=texture2D(textureSampler,projectedCoord.xy).rgb;info.coords=vec4(projectedCoord.xy,sampledDepth,0.0);return info;
  41. #endif
  42. }}
  43. info.color=texture2D(textureSampler,projectedCoord.xy).rgb;info.coords=vec4(projectedCoord.xy,sampledDepth,0.0);return info;}
  44. vec3 hash(vec3 a)
  45. {a=fract(a*0.8);a+=dot(a,a.yxz+19.19);return fract((a.xxy+a.yxx)*a.zyx);}
  46. #endif
  47. void main()
  48. {
  49. #ifdef SSR_SUPPORTED
  50. vec4 albedoFull=texture2D(textureSampler,vUV);vec3 albedo=albedoFull.rgb;float spec=texture2D(reflectivitySampler,vUV).r;if (spec==0.0) {gl_FragColor=albedoFull;return;}
  51. vec3 normal=(texture2D(normalSampler,vUV)).xyz;vec3 position=(view*texture2D(positionSampler,vUV)).xyz;vec3 reflected=normalize(reflect(normalize(position),normalize(normal)));float roughness=1.0-texture2D(reflectivitySampler,vUV).a;vec3 jitt=mix(vec3(0.0),hash(position),roughness)*roughnessFactor;ReflectionInfo info=getReflectionInfo(jitt+reflected,position);vec2 dCoords=smoothstep(0.2,0.6,abs(vec2(0.5,0.5)-info.coords.xy));float screenEdgefactor=clamp(1.0-(dCoords.x+dCoords.y),0.0,1.0);vec3 F0=vec3(0.04);F0 =mix(F0,albedo,spec);vec3 fresnel=fresnelSchlick(max(dot(normalize(normal),normalize(position)),0.0),F0);
  52. #ifdef RIGHT_HANDED_SCENE
  53. reflected.z*=-1.0;
  54. #endif
  55. float reflectionMultiplier=clamp(pow(spec*strength,reflectionSpecularFalloffExponent)*screenEdgefactor*reflected.z,0.0,0.9);float albedoMultiplier=1.0-reflectionMultiplier;vec3 SSR=info.color*fresnel;gl_FragColor=vec4((albedo*albedoMultiplier)+(SSR*reflectionMultiplier),albedoFull.a);
  56. #else
  57. gl_FragColor=texture2D(textureSampler,vUV);
  58. #endif
  59. }
  60. `;
  61. // Sideeffect
  62. ShaderStore.ShadersStore[name] = shader;
  63. /** @internal */
  64. export const screenSpaceReflectionPixelShader = { name, shader };
  65. //# sourceMappingURL=screenSpaceReflection.fragment.js.map