Skip to content

Commit d3959f3

Browse files
committed
Use TiltShiftEffect from PP (#338)
1 parent a3b891e commit d3959f3

File tree

5 files changed

+135
-102
lines changed

5 files changed

+135
-102
lines changed

.changeset/shaggy-carrots-rescue.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"render-composer": patch
3+
---
4+
5+
**Changed:** `<TiltShiftEffect>` now uses the tilt shift effect implementation provided by `postprocessing` 6.29.0.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@
4848
"@types/three": "^0.144.0",
4949
"jest": "^28.1.3",
5050
"jest-environment-jsdom": "^29.1.2",
51-
"postprocessing": "^6.28.7",
51+
"postprocessing": "^6.29.0",
5252
"react": "^18.2.0",
5353
"react-dom": "^18.2.0",
5454
"rimraf": "^3.0.2",

packages/render-composer/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
},
3535
"peerDependencies": {
3636
"@react-three/fiber": ">=8.5.0",
37-
"postprocessing": ">=6.28.5",
37+
"postprocessing": ">=6.29.0",
3838
"react": ">=18.2.0",
3939
"react-dom": ">=18.2.0",
4040
"three": ">=0.143.0"
Lines changed: 4 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -1,100 +1,9 @@
11
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"
82
import { usePostProcessingEffect } from "../lib/usePostProcessingEffect"
93

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)
168
return null
179
}
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-
}

pnpm-lock.yaml

Lines changed: 124 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)