Skip to content

Commit

Permalink
Use TiltShiftEffect from PP (#338)
Browse files Browse the repository at this point in the history
  • Loading branch information
hmans committed Oct 19, 2022
1 parent a3b891e commit d3959f3
Show file tree
Hide file tree
Showing 5 changed files with 135 additions and 102 deletions.
5 changes: 5 additions & 0 deletions .changeset/shaggy-carrots-rescue.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"render-composer": patch
---

**Changed:** `<TiltShiftEffect>` now uses the tilt shift effect implementation provided by `postprocessing` 6.29.0.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
"@types/three": "^0.144.0",
"jest": "^28.1.3",
"jest-environment-jsdom": "^29.1.2",
"postprocessing": "^6.28.7",
"postprocessing": "^6.29.0",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"rimraf": "^3.0.2",
Expand Down
2 changes: 1 addition & 1 deletion packages/render-composer/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
},
"peerDependencies": {
"@react-three/fiber": ">=8.5.0",
"postprocessing": ">=6.28.5",
"postprocessing": ">=6.29.0",
"react": ">=18.2.0",
"react-dom": ">=18.2.0",
"three": ">=0.143.0"
Expand Down
99 changes: 4 additions & 95 deletions packages/render-composer/src/effects/TiltShiftEffect.tsx
Original file line number Diff line number Diff line change
@@ -1,100 +1,9 @@
import * as PP from "postprocessing"
import { $, Input, Resolution, Snippet, Vec2, Vec3 } from "shader-composer"
import {
InputColor,
PostProcessingEffectMaster,
ShaderComposerEffect
} from "shader-composer-postprocessing"
import { usePostProcessingEffect } from "../lib/usePostProcessingEffect"

export type TiltShiftEffectProps = ConstructorParameters<
typeof TiltShiftEffectImpl
>[0]

export const TiltShiftEffect = (props: TiltShiftEffectProps) => {
usePostProcessingEffect(() => new TiltShiftEffectImpl(props), props)
export const TiltShiftEffect = (
props: ConstructorParameters<typeof PP.TiltShiftEffect>[0]
) => {
usePostProcessingEffect(() => new PP.TiltShiftEffect(props), props)
return null
}

export type TiltShiftEffectArgs = {
blendFunction?: PP.BlendFunction
}

const random = Snippet(
(random) => $`
float ${random}(vec3 scale, float seed) {
/* use the fragment position for a different seed per-pixel */
return fract(sin(dot(gl_FragCoord.xyz + seed, scale)) * 43758.5453 + seed);
}
`
)

const TiltShiftUnit = (
inputColor: Input<"vec3">,
start: Input<"vec2">,
end: Input<"vec2">,
delta: Input<"vec2">,
blurRadius: Input<"float">,
gradientRadius: Input<"float">,
sampleCount: Input<"float">
) =>
Vec3(inputColor, {
fragment: {
body: $`
vec3 color = vec3(0.0);
float total = 0.0;
vec2 startPixel = vec2(${start}.x * ${Resolution}.x, ${start}.y * ${Resolution}.y);
vec2 endPixel = vec2(${end}.x * ${Resolution}.x, ${end}.y * ${Resolution}.y);
/* randomize the lookup values to hide the fixed number of samples */
float offset = ${random}(vec3(12.9898, 78.233, 151.7182), 0.0);
vec2 normal = normalize(vec2(startPixel.y - endPixel.y, endPixel.x - startPixel.x));
float radius = smoothstep(0.0, 1.0,
abs(dot(uv * ${Resolution} - startPixel, normal)) / ${gradientRadius}) * ${blurRadius};
float firstSample = ${sampleCount} / -2.0;
float lastSample = ${sampleCount} / 2.0;
// for (float t = -30.0; t <= 30.0; t++) {
for (float t = firstSample; t <= lastSample; t++) {
float percent = (t + offset - 0.5) / lastSample;
float weight = 1.0 - abs(percent);
vec4 sample_t = texture2D(inputBuffer, uv + ${delta} / ${Resolution} * percent * radius);
/* switch to pre-multiplied alpha to correctly blur transparent images */
sample_t.rgb *= sample_t.a;
color += vec3(sample_t) * weight;
total += weight;
}
value = color / total;
/* switch back from pre-multiplied alpha */
// value.rgb /= value.a + 0.00001;
`
}
})

export class TiltShiftEffectImpl extends ShaderComposerEffect {
constructor({
blendFunction = PP.BlendFunction.NORMAL
}: TiltShiftEffectArgs) {
super({
blendFunction,
root: PostProcessingEffectMaster({
color: TiltShiftUnit(
InputColor,
Vec2([0, 0.5]),
Vec2([1, 0.5]),
Vec2([1, 1]),
10,
400,
40
)
})
})
}
}
129 changes: 124 additions & 5 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit d3959f3

Please sign in to comment.