motionBlur.fragment.js 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. // Do not edit.
  2. import { ShaderStore } from "../Engines/shaderStore.js";
  3. const name = "motionBlurPixelShader";
  4. const shader = `varying vec2 vUV;uniform sampler2D textureSampler;uniform float motionStrength;uniform float motionScale;uniform vec2 screenSize;
  5. #ifdef OBJECT_BASED
  6. uniform sampler2D velocitySampler;
  7. #else
  8. uniform sampler2D depthSampler;uniform mat4 inverseViewProjection;uniform mat4 prevViewProjection;uniform mat4 projection;
  9. #endif
  10. #define CUSTOM_FRAGMENT_DEFINITIONS
  11. void main(void)
  12. {
  13. #ifdef GEOMETRY_SUPPORTED
  14. #ifdef OBJECT_BASED
  15. vec2 texelSize=1.0/screenSize;vec4 velocityColor=texture2D(velocitySampler,vUV);velocityColor.rg=velocityColor.rg*2.0-vec2(1.0);vec2 velocity=vec2(pow(velocityColor.r,3.0),pow(velocityColor.g,3.0))*velocityColor.a;velocity*=motionScale*motionStrength;float speed=length(velocity/texelSize);int samplesCount=int(clamp(speed,1.0,SAMPLES));velocity=normalize(velocity)*texelSize;float hlim=float(-samplesCount)*0.5+0.5;vec4 result=texture2D(textureSampler,vUV);for (int i=1; i<int(SAMPLES); ++i)
  16. {if (i>=samplesCount)
  17. break;vec2 offset=vUV+velocity*(hlim+float(i));
  18. #if defined(WEBGPU)
  19. result+=texture2DLodEXT(textureSampler,offset,0.0);
  20. #else
  21. result+=texture2D(textureSampler,offset);
  22. #endif
  23. }
  24. gl_FragColor=result/float(samplesCount);gl_FragColor.a=1.0;
  25. #else
  26. vec2 texelSize=1.0/screenSize;float depth=texture2D(depthSampler,vUV).r;depth=projection[2].z+projection[3].z/depth;
  27. vec4 cpos=vec4(vUV*2.0-1.0,depth,1.0);cpos=inverseViewProjection*cpos;cpos/=cpos.w;vec4 ppos=prevViewProjection*cpos;ppos/=ppos.w;ppos.xy=ppos.xy*0.5+0.5;vec2 velocity=(ppos.xy-vUV)*motionScale*motionStrength;float speed=length(velocity/texelSize);int nSamples=int(clamp(speed,1.0,SAMPLES));vec4 result=texture2D(textureSampler,vUV);for (int i=1; i<int(SAMPLES); ++i) {if (i>=nSamples)
  28. break;vec2 offset1=vUV+velocity*(float(i)/float(nSamples-1)-0.5);
  29. #if defined(WEBGPU)
  30. result+=texture2DLodEXT(textureSampler,offset1,0.0);
  31. #else
  32. result+=texture2D(textureSampler,offset1);
  33. #endif
  34. }
  35. gl_FragColor=result/float(nSamples);
  36. #endif
  37. #else
  38. gl_FragColor=texture2D(textureSampler,vUV);
  39. #endif
  40. }
  41. `;
  42. // Sideeffect
  43. ShaderStore.ShadersStore[name] = shader;
  44. /** @internal */
  45. export const motionBlurPixelShader = { name, shader };
  46. //# sourceMappingURL=motionBlur.fragment.js.map