1 precision mediump float;
3 uniform sampler2D sTexture;
6 uniform vec2 uObjectFadeStart;
7 uniform vec2 uObjectFadeEnd;
8 uniform float uAlphaScale;
9 uniform float uBlurTexCoordScale;
10 uniform float uNumSamples;
11 uniform float uRecipNumSamples;
12 uniform float uRecipNumSamplesMinusOne;
15 varying vec2 vModelSpaceCenterToPos;
16 varying vec2 vScreenSpaceVelocityVector;
18 varying vec2 vTexCoord;
22 // calculate an alpha value that will fade the object towards its extremities, we need this to avoid an unsightly hard edge between color values of
23 // the blurred object and the unblurred background. Use smoothstep also to hide any hard edges (discontinuities) in rate of change of this alpha gradient
24 vec2 centerToPixel = abs(vModelSpaceCenterToPos);
25 vec2 fadeToEdges = smoothstep(0.0, 1.0, 1.0 - ((centerToPixel - uObjectFadeStart) / (uObjectFadeEnd - uObjectFadeStart)));
26 float fadeToEdgesScale = fadeToEdges.x * fadeToEdges.y * uAlphaScale; // apply global scaler
27 fadeToEdgesScale = mix(1.0, fadeToEdgesScale, vSpeed);// fade proportional to speed, so opaque when at rest
29 // scale velocity vector by user requirements
30 vec2 velocity = vScreenSpaceVelocityVector * uBlurTexCoordScale;
32 // standard actor texel
33 vec4 colActor = texture2D(sTexture, vTexCoord);
35 // blurred actor - gather texture samples from the actor texture in the direction of motion
36 vec4 col = colActor * uRecipNumSamples;
37 for(float i = 1.0; i < uNumSamples; i += 1.0)
39 float t = i * uRecipNumSamplesMinusOne;
40 col += texture2D(sTexture, vTexCoord + (velocity * t)) * uRecipNumSamples;
42 gl_FragColor = mix(colActor, col, vSpeed); // lerp blurred and non-blurred actor based on speed of motion
43 gl_FragColor.a = fadeToEdgesScale;//colActor.a * fadeToEdgesScale; // fade blurred actor to its edges based on speed of motion
44 gl_FragColor *= uColor;