fluidRenderingRender.fragment.js 4.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. // Do not edit.
  2. import { ShaderStore } from "../Engines/shaderStore.js";
  3. const name = "fluidRenderingRenderPixelShader";
  4. const shader = `/* disable_uniformity_analysis */
  5. #define IOR 1.333
  6. #define ETA 1.0/IOR
  7. #define F0 0.02
  8. uniform sampler2D textureSampler;uniform sampler2D depthSampler;
  9. #ifdef FLUIDRENDERING_DIFFUSETEXTURE
  10. uniform sampler2D diffuseSampler;
  11. #else
  12. uniform vec3 diffuseColor;
  13. #endif
  14. #ifdef FLUIDRENDERING_FIXED_THICKNESS
  15. uniform float thickness;uniform sampler2D bgDepthSampler;
  16. #else
  17. uniform float minimumThickness;uniform sampler2D thicknessSampler;
  18. #endif
  19. #ifdef FLUIDRENDERING_ENVIRONMENT
  20. uniform samplerCube reflectionSampler;
  21. #endif
  22. #if defined(FLUIDRENDERING_DEBUG) && defined(FLUIDRENDERING_DEBUG_TEXTURE)
  23. uniform sampler2D debugSampler;
  24. #endif
  25. uniform mat4 viewMatrix;uniform mat4 projectionMatrix;uniform mat4 invProjectionMatrix;uniform vec2 texelSize;uniform vec3 dirLight;uniform float cameraFar;uniform float density;uniform float refractionStrength;uniform float fresnelClamp;uniform float specularPower;varying vec2 vUV;vec3 computeViewPosFromUVDepth(vec2 texCoord,float depth) {vec4 ndc;ndc.xy=texCoord*2.0-1.0;
  26. #ifdef FLUIDRENDERING_RHS
  27. ndc.z=-projectionMatrix[2].z+projectionMatrix[3].z/depth;
  28. #else
  29. ndc.z=projectionMatrix[2].z+projectionMatrix[3].z/depth;
  30. #endif
  31. ndc.w=1.0;vec4 eyePos=invProjectionMatrix*ndc;eyePos.xyz/=eyePos.w;return eyePos.xyz;}
  32. vec3 getViewPosFromTexCoord(vec2 texCoord) {float depth=textureLod(depthSampler,texCoord,0.).x;return computeViewPosFromUVDepth(texCoord,depth);}
  33. void main(void) {vec2 texCoord=vUV;
  34. #if defined(FLUIDRENDERING_DEBUG) && defined(FLUIDRENDERING_DEBUG_TEXTURE)
  35. vec4 color=texture2D(debugSampler,texCoord);
  36. #ifdef FLUIDRENDERING_DEBUG_DEPTH
  37. glFragColor=vec4(color.rgb/vec3(2.0),1.);if (color.r>0.999 && color.g>0.999) {glFragColor=texture2D(textureSampler,texCoord);}
  38. #else
  39. glFragColor=vec4(color.rgb,1.);if (color.r<0.001 && color.g<0.001 && color.b<0.001) {glFragColor=texture2D(textureSampler,texCoord);}
  40. #endif
  41. return;
  42. #endif
  43. vec2 depthVel=textureLod(depthSampler,texCoord,0.).rg;float depth=depthVel.r;
  44. #ifndef FLUIDRENDERING_FIXED_THICKNESS
  45. float thickness=texture2D(thicknessSampler,texCoord).x;
  46. #else
  47. float bgDepth=texture2D(bgDepthSampler,texCoord).x;float depthNonLinear=projectionMatrix[2].z+projectionMatrix[3].z/depth;depthNonLinear=depthNonLinear*0.5+0.5;
  48. #endif
  49. vec4 backColor=texture2D(textureSampler,texCoord);
  50. #ifndef FLUIDRENDERING_FIXED_THICKNESS
  51. if (depth>=cameraFar || depth<=0. || thickness<=minimumThickness) {
  52. #else
  53. if (depth>=cameraFar || depth<=0. || bgDepth<=depthNonLinear) {
  54. #endif
  55. glFragColor=backColor;return;}
  56. vec3 viewPos=computeViewPosFromUVDepth(texCoord,depth);vec3 ddx=getViewPosFromTexCoord(texCoord+vec2(texelSize.x,0.))-viewPos;vec3 ddy=getViewPosFromTexCoord(texCoord+vec2(0.,texelSize.y))-viewPos;vec3 ddx2=viewPos-getViewPosFromTexCoord(texCoord+vec2(-texelSize.x,0.));if (abs(ddx.z)>abs(ddx2.z)) {ddx=ddx2;}
  57. vec3 ddy2=viewPos-getViewPosFromTexCoord(texCoord+vec2(0.,-texelSize.y));if (abs(ddy.z)>abs(ddy2.z)) {ddy=ddy2;}
  58. vec3 normal=normalize(cross(ddy,ddx));
  59. #ifdef FLUIDRENDERING_RHS
  60. normal=-normal;
  61. #endif
  62. #ifndef WEBGPU
  63. if(isnan(normal.x) || isnan(normal.y) || isnan(normal.z) || isinf(normal.x) || isinf(normal.y) || isinf(normal.z)) {normal=vec3(0.,0.,-1.);}
  64. #endif
  65. #if defined(FLUIDRENDERING_DEBUG) && defined(FLUIDRENDERING_DEBUG_SHOWNORMAL)
  66. glFragColor=vec4(normal*0.5+0.5,1.0);return;
  67. #endif
  68. vec3 rayDir=normalize(viewPos);
  69. #ifdef FLUIDRENDERING_DIFFUSETEXTURE
  70. vec3 diffuseColor=textureLod(diffuseSampler,texCoord,0.0).rgb;
  71. #endif
  72. vec3 lightDir=normalize(vec3(viewMatrix*vec4(-dirLight,0.)));vec3 H =normalize(lightDir-rayDir);float specular=pow(max(0.0,dot(H,normal)),specularPower);
  73. #ifdef FLUIDRENDERING_DEBUG_DIFFUSERENDERING
  74. float diffuse =max(0.0,dot(lightDir,normal))*1.0;glFragColor=vec4(vec3(0.1) /*ambient*/+vec3(0.42,0.50,1.00)*diffuse+vec3(0,0,0.2)+specular,1.);return;
  75. #endif
  76. vec3 refractionDir=refract(rayDir,normal,ETA);vec4 transmitted=textureLod(textureSampler,vec2(texCoord+refractionDir.xy*thickness*refractionStrength),0.0);vec3 transmittance=exp(-density*thickness*(1.0-diffuseColor));
  77. vec3 refractionColor=transmitted.rgb*transmittance;
  78. #ifdef FLUIDRENDERING_ENVIRONMENT
  79. vec3 reflectionDir=reflect(rayDir,normal);vec3 reflectionColor=(textureCube(reflectionSampler,reflectionDir).rgb);float fresnel=clamp(F0+(1.0-F0)*pow(1.0-dot(normal,-rayDir),5.0),0.,fresnelClamp);vec3 finalColor=mix(refractionColor,reflectionColor,fresnel)+specular;
  80. #else
  81. vec3 finalColor=refractionColor+specular;
  82. #endif
  83. #ifdef FLUIDRENDERING_VELOCITY
  84. float velocity=depthVel.g;finalColor=mix(finalColor,vec3(1.0),smoothstep(0.3,1.0,velocity/6.0));
  85. #endif
  86. glFragColor=vec4(finalColor,transmitted.a);}
  87. `;
  88. // Sideeffect
  89. ShaderStore.ShadersStore[name] = shader;
  90. /** @internal */
  91. export const fluidRenderingRenderPixelShader = { name, shader };
  92. //# sourceMappingURL=fluidRenderingRender.fragment.js.map