|
1 | 1 | import * as PP from "postprocessing"
|
2 |
| -import { $, Input, Resolution, Snippet, Vec2, Vec3 } from "shader-composer" |
3 |
| -import { |
4 |
| - InputColor, |
5 |
| - PostProcessingEffectMaster, |
6 |
| - ShaderComposerEffect |
7 |
| -} from "shader-composer-postprocessing" |
8 | 2 | import { usePostProcessingEffect } from "../lib/usePostProcessingEffect"
|
9 | 3 |
|
10 |
| -export type TiltShiftEffectProps = ConstructorParameters< |
11 |
| - typeof TiltShiftEffectImpl |
12 |
| ->[0] |
13 |
| - |
14 |
| -export const TiltShiftEffect = (props: TiltShiftEffectProps) => { |
15 |
| - usePostProcessingEffect(() => new TiltShiftEffectImpl(props), props) |
| 4 | +export const TiltShiftEffect = ( |
| 5 | + props: ConstructorParameters<typeof PP.TiltShiftEffect>[0] |
| 6 | +) => { |
| 7 | + usePostProcessingEffect(() => new PP.TiltShiftEffect(props), props) |
16 | 8 | return null
|
17 | 9 | }
|
18 |
| - |
19 |
| -export type TiltShiftEffectArgs = { |
20 |
| - blendFunction?: PP.BlendFunction |
21 |
| -} |
22 |
| - |
23 |
| -const random = Snippet( |
24 |
| - (random) => $` |
25 |
| - float ${random}(vec3 scale, float seed) { |
26 |
| - /* use the fragment position for a different seed per-pixel */ |
27 |
| - return fract(sin(dot(gl_FragCoord.xyz + seed, scale)) * 43758.5453 + seed); |
28 |
| - } |
29 |
| -` |
30 |
| -) |
31 |
| - |
32 |
| -const TiltShiftUnit = ( |
33 |
| - inputColor: Input<"vec3">, |
34 |
| - start: Input<"vec2">, |
35 |
| - end: Input<"vec2">, |
36 |
| - delta: Input<"vec2">, |
37 |
| - blurRadius: Input<"float">, |
38 |
| - gradientRadius: Input<"float">, |
39 |
| - sampleCount: Input<"float"> |
40 |
| -) => |
41 |
| - Vec3(inputColor, { |
42 |
| - fragment: { |
43 |
| - body: $` |
44 |
| - vec3 color = vec3(0.0); |
45 |
| - float total = 0.0; |
46 |
| - vec2 startPixel = vec2(${start}.x * ${Resolution}.x, ${start}.y * ${Resolution}.y); |
47 |
| - vec2 endPixel = vec2(${end}.x * ${Resolution}.x, ${end}.y * ${Resolution}.y); |
48 |
| -
|
49 |
| - /* randomize the lookup values to hide the fixed number of samples */ |
50 |
| - float offset = ${random}(vec3(12.9898, 78.233, 151.7182), 0.0); |
51 |
| -
|
52 |
| - vec2 normal = normalize(vec2(startPixel.y - endPixel.y, endPixel.x - startPixel.x)); |
53 |
| - float radius = smoothstep(0.0, 1.0, |
54 |
| - abs(dot(uv * ${Resolution} - startPixel, normal)) / ${gradientRadius}) * ${blurRadius}; |
55 |
| -
|
56 |
| - float firstSample = ${sampleCount} / -2.0; |
57 |
| - float lastSample = ${sampleCount} / 2.0; |
58 |
| -
|
59 |
| - // for (float t = -30.0; t <= 30.0; t++) { |
60 |
| - for (float t = firstSample; t <= lastSample; t++) { |
61 |
| - float percent = (t + offset - 0.5) / lastSample; |
62 |
| - float weight = 1.0 - abs(percent); |
63 |
| -
|
64 |
| - vec4 sample_t = texture2D(inputBuffer, uv + ${delta} / ${Resolution} * percent * radius); |
65 |
| -
|
66 |
| - /* switch to pre-multiplied alpha to correctly blur transparent images */ |
67 |
| - sample_t.rgb *= sample_t.a; |
68 |
| -
|
69 |
| - color += vec3(sample_t) * weight; |
70 |
| - total += weight; |
71 |
| - } |
72 |
| -
|
73 |
| - value = color / total; |
74 |
| -
|
75 |
| - /* switch back from pre-multiplied alpha */ |
76 |
| - // value.rgb /= value.a + 0.00001; |
77 |
| - ` |
78 |
| - } |
79 |
| - }) |
80 |
| - |
81 |
| -export class TiltShiftEffectImpl extends ShaderComposerEffect { |
82 |
| - constructor({ |
83 |
| - blendFunction = PP.BlendFunction.NORMAL |
84 |
| - }: TiltShiftEffectArgs) { |
85 |
| - super({ |
86 |
| - blendFunction, |
87 |
| - root: PostProcessingEffectMaster({ |
88 |
| - color: TiltShiftUnit( |
89 |
| - InputColor, |
90 |
| - Vec2([0, 0.5]), |
91 |
| - Vec2([1, 0.5]), |
92 |
| - Vec2([1, 1]), |
93 |
| - 10, |
94 |
| - 400, |
95 |
| - 40 |
96 |
| - ) |
97 |
| - }) |
98 |
| - }) |
99 |
| - } |
100 |
| -} |
0 commit comments