@react-three/postprocessing 3.0.4 → 3.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (114) hide show
  1. package/LICENSE +21 -21
  2. package/README.md +80 -81
  3. package/dist/EffectComposer.d.ts +14 -6
  4. package/dist/EffectGroup.d.ts +9 -0
  5. package/dist/Selection.d.ts +8 -8
  6. package/dist/createEffectComponent.d.ts +30 -0
  7. package/dist/effects/ASCII.d.ts +29 -7
  8. package/dist/effects/Autofocus.d.ts +8 -7
  9. package/dist/effects/Bloom.d.ts +10 -5
  10. package/dist/effects/BrightnessContrast.d.ts +15 -5
  11. package/dist/effects/ChromaticAberration.d.ts +30 -7
  12. package/dist/effects/ColorAverage.d.ts +7 -8
  13. package/dist/effects/ColorDepth.d.ts +8 -5
  14. package/dist/effects/Depth.d.ts +13 -5
  15. package/dist/effects/DepthOfField.d.ts +7 -18
  16. package/dist/effects/DotScreen.d.ts +15 -5
  17. package/dist/effects/FXAA.d.ts +11 -5
  18. package/dist/effects/Glitch.d.ts +17 -29
  19. package/dist/effects/GodRays.d.ts +7 -19
  20. package/dist/effects/Grid.d.ts +8 -8
  21. package/dist/effects/HueSaturation.d.ts +15 -5
  22. package/dist/effects/LUT.d.ts +4 -3
  23. package/dist/effects/LensFlare.d.ts +52 -11
  24. package/dist/effects/Noise.d.ts +8 -5
  25. package/dist/effects/Outline.d.ts +9 -25
  26. package/dist/effects/Pixelation.d.ts +3 -2
  27. package/dist/effects/Ramp.d.ts +58 -10
  28. package/dist/effects/SMAA.d.ts +10 -5
  29. package/dist/effects/SSAO.d.ts +7 -30
  30. package/dist/effects/ScanlineEffect.d.ts +13 -5
  31. package/dist/effects/SelectiveBloom.d.ts +5 -10
  32. package/dist/effects/Sepia.d.ts +13 -5
  33. package/dist/effects/ShockWave.d.ts +12 -5
  34. package/dist/effects/Texture.d.ts +7 -8
  35. package/dist/effects/TiltShift.d.ts +10 -5
  36. package/dist/effects/TiltShift2.d.ts +44 -8
  37. package/dist/effects/ToneMapping.d.ts +9 -7
  38. package/dist/effects/Vignette.d.ts +19 -5
  39. package/dist/effects/Water.d.ts +17 -5
  40. package/dist/index.d.ts +15 -11
  41. package/dist/index.js +1841 -45
  42. package/dist/index.js.map +1 -1
  43. package/dist/passes/DepthPicking.d.ts +11 -0
  44. package/dist/{effects → passes}/N8AO.d.ts +5 -2
  45. package/dist/util.d.ts +19 -17
  46. package/dist/wrapEffect.d.ts +12 -0
  47. package/package.json +74 -67
  48. package/src/EffectComposer.tsx +379 -200
  49. package/src/EffectGroup.tsx +74 -0
  50. package/src/Selection.tsx +86 -46
  51. package/src/createEffectComponent.tsx +71 -0
  52. package/src/effects/ASCII.tsx +183 -135
  53. package/src/effects/Autofocus.tsx +122 -153
  54. package/src/effects/Bloom.tsx +34 -6
  55. package/src/effects/BrightnessContrast.tsx +7 -4
  56. package/src/effects/ChromaticAberration.tsx +22 -5
  57. package/src/effects/ColorAverage.tsx +4 -15
  58. package/src/effects/ColorDepth.tsx +25 -4
  59. package/src/effects/Depth.tsx +6 -4
  60. package/src/effects/DepthOfField.tsx +109 -89
  61. package/src/effects/DotScreen.tsx +7 -4
  62. package/src/effects/FXAA.tsx +6 -4
  63. package/src/effects/Glitch.tsx +32 -40
  64. package/src/effects/GodRays.tsx +104 -16
  65. package/src/effects/Grid.tsx +27 -21
  66. package/src/effects/HueSaturation.tsx +7 -4
  67. package/src/effects/LUT.tsx +25 -26
  68. package/src/effects/LensFlare.tsx +765 -611
  69. package/src/effects/Noise.tsx +16 -4
  70. package/src/effects/Outline.tsx +84 -117
  71. package/src/effects/Pixelation.tsx +20 -15
  72. package/src/effects/Ramp.tsx +239 -150
  73. package/src/effects/SMAA.tsx +20 -4
  74. package/src/effects/SSAO.tsx +152 -41
  75. package/src/effects/ScanlineEffect.tsx +7 -7
  76. package/src/effects/SelectiveBloom.tsx +98 -126
  77. package/src/effects/Sepia.tsx +6 -4
  78. package/src/effects/ShockWave.tsx +32 -4
  79. package/src/effects/Texture.tsx +28 -23
  80. package/src/effects/TiltShift.tsx +32 -4
  81. package/src/effects/TiltShift2.tsx +156 -90
  82. package/src/effects/ToneMapping.tsx +19 -6
  83. package/src/effects/Vignette.tsx +7 -4
  84. package/src/effects/Water.tsx +50 -35
  85. package/src/index.ts +43 -40
  86. package/src/passes/DepthPicking.tsx +63 -0
  87. package/src/passes/N8AO.tsx +105 -0
  88. package/src/tests/Autofocus.test.tsx +38 -0
  89. package/src/tests/Bloom.test.tsx +76 -0
  90. package/src/tests/ChromaticAberration.test.tsx +81 -0
  91. package/src/tests/ColorDepth.test.tsx +71 -0
  92. package/src/tests/DepthOfField.test.tsx +130 -0
  93. package/src/tests/DepthPicking.test.tsx +250 -0
  94. package/src/tests/EffectComposer.test.tsx +1404 -0
  95. package/src/tests/EffectGroup.test.tsx +337 -0
  96. package/src/tests/Glitch.test.tsx +56 -0
  97. package/src/tests/GodRays.test.tsx +101 -0
  98. package/src/tests/Grid.test.tsx +34 -0
  99. package/src/tests/LUT.test.tsx +64 -0
  100. package/src/tests/N8AO.test.tsx +92 -0
  101. package/src/tests/Outline.test.tsx +211 -0
  102. package/src/tests/SSAO.test.tsx +140 -0
  103. package/src/tests/Selection.test.tsx +324 -0
  104. package/src/tests/SelectiveBloom.test.tsx +166 -0
  105. package/src/tests/ShockWave.test.tsx +95 -0
  106. package/src/tests/TiltShift.test.tsx +76 -0
  107. package/src/tests/createEffectComponent.test.tsx +286 -0
  108. package/src/tests/effects.smoke.test.tsx +238 -0
  109. package/src/tests/test-utils.tsx +96 -0
  110. package/src/tests/wrapEffect.test.tsx +209 -0
  111. package/src/util.tsx +221 -55
  112. package/src/wrapEffect.tsx +123 -0
  113. package/src/EffectComposer.test.tsx +0 -126
  114. package/src/effects/N8AO.tsx +0 -81
@@ -1,4 +1,16 @@
1
- import { NoiseEffect, BlendFunction } from 'postprocessing'
2
- import { wrapEffect } from '../util'
3
-
4
- export const Noise = /* @__PURE__ */ wrapEffect(NoiseEffect, { blendFunction: BlendFunction.COLOR_DODGE })
1
+ import { BlendFunction, NoiseEffect } from 'postprocessing'
2
+ import type { Ref } from 'react'
3
+ import { createEffectComponent, type EffectOptions } from '../createEffectComponent'
4
+
5
+ const NoiseImpl = /* @__PURE__ */ createEffectComponent<typeof NoiseEffect, EffectOptions<typeof NoiseEffect>>(
6
+ NoiseEffect
7
+ )
8
+
9
+ export type NoiseProps = EffectOptions<typeof NoiseEffect> & {
10
+ opacity?: number
11
+ ref?: Ref<NoiseEffect>
12
+ }
13
+
14
+ export function Noise({ blendFunction = BlendFunction.COLOR_DODGE, ...props }: NoiseProps) {
15
+ return <NoiseImpl blendFunction={blendFunction} {...props} />
16
+ }
@@ -1,117 +1,84 @@
1
- import { OutlineEffect } from 'postprocessing'
2
- import { Ref, RefObject, forwardRef, useMemo, useEffect, useContext, useRef } from 'react'
3
- import { Object3D } from 'three'
4
- import { useThree } from '@react-three/fiber'
5
- import { EffectComposerContext } from '../EffectComposer'
6
- import { selectionContext } from '../Selection'
7
- import { resolveRef } from '../util'
8
-
9
- type ObjectRef = RefObject<Object3D>
10
-
11
- export type OutlineProps = ConstructorParameters<typeof OutlineEffect>[2] &
12
- Partial<{
13
- selection: Object3D | Object3D[] | ObjectRef | ObjectRef[]
14
- selectionLayer: number
15
- }>
16
-
17
- export const Outline = /* @__PURE__ */ forwardRef(function Outline(
18
- {
19
- selection = [],
20
- selectionLayer = 10,
21
- blendFunction,
22
- patternTexture,
23
- edgeStrength,
24
- pulseSpeed,
25
- visibleEdgeColor,
26
- hiddenEdgeColor,
27
- width,
28
- height,
29
- kernelSize,
30
- blur,
31
- xRay,
32
- ...props
33
- }: OutlineProps,
34
- forwardRef: Ref<OutlineEffect>
35
- ) {
36
- const invalidate = useThree((state) => state.invalidate)
37
- const { scene, camera } = useContext(EffectComposerContext)
38
-
39
- const effect = useMemo(
40
- () =>
41
- new OutlineEffect(scene, camera, {
42
- blendFunction,
43
- patternTexture,
44
- edgeStrength,
45
- pulseSpeed,
46
- visibleEdgeColor,
47
- hiddenEdgeColor,
48
- width,
49
- height,
50
- kernelSize,
51
- blur,
52
- xRay,
53
- ...props,
54
- }),
55
- // NOTE: `props` is an unstable reference, so we can't memoize it
56
- // eslint-disable-next-line react-hooks/exhaustive-deps
57
- [
58
- blendFunction,
59
- blur,
60
- camera,
61
- edgeStrength,
62
- height,
63
- hiddenEdgeColor,
64
- kernelSize,
65
- patternTexture,
66
- pulseSpeed,
67
- scene,
68
- visibleEdgeColor,
69
- width,
70
- xRay,
71
- ]
72
- )
73
-
74
- const api = useContext(selectionContext)
75
-
76
- useEffect(() => {
77
- // Do not allow array selection if declarative selection is active
78
- // TODO: array selection should probably be deprecated altogether
79
- if (!api && selection) {
80
- effect.selection.set(
81
- Array.isArray(selection) ? (selection as Object3D[]).map(resolveRef) : [resolveRef(selection) as Object3D]
82
- )
83
- invalidate()
84
- return () => {
85
- effect.selection.clear()
86
- invalidate()
87
- }
88
- }
89
- }, [effect, selection, api, invalidate])
90
-
91
- useEffect(() => {
92
- effect.selectionLayer = selectionLayer
93
- invalidate()
94
- }, [effect, invalidate, selectionLayer])
95
-
96
- const ref = useRef<OutlineEffect>(undefined)
97
- useEffect(() => {
98
- if (api && api.enabled) {
99
- if (api.selected?.length) {
100
- effect.selection.set(api.selected)
101
- invalidate()
102
- return () => {
103
- effect.selection.clear()
104
- invalidate()
105
- }
106
- }
107
- }
108
- }, [api, effect.selection, invalidate])
109
-
110
- useEffect(() => {
111
- return () => {
112
- effect.dispose()
113
- }
114
- }, [effect])
115
-
116
- return <primitive ref={forwardRef} object={effect} />
117
- })
1
+ import { OutlineEffect } from 'postprocessing'
2
+ import { Ref, RefObject, use, useEffect, useMemo } from 'react'
3
+ import { Color, Object3D, type ColorRepresentation } from 'three'
4
+ import { EffectComposerContext } from '../EffectComposer'
5
+ import { applyPierced, EMPTY_ARRAY, readPierced, useDispose, useLiveDefaults, useSelectionSync } from '../util'
6
+
7
+ type ObjectRef = RefObject<Object3D | null>
8
+
9
+ type OutlineEffectOptions = NonNullable<ConstructorParameters<typeof OutlineEffect>[2]>
10
+
11
+ export type OutlineProps = Omit<OutlineEffectOptions, 'visibleEdgeColor' | 'hiddenEdgeColor'> &
12
+ Partial<{
13
+ selection: Object3D | Object3D[] | ObjectRef | ObjectRef[]
14
+ selectionLayer: number
15
+ visibleEdgeColor: ColorRepresentation
16
+ hiddenEdgeColor: ColorRepresentation
17
+ ref?: Ref<OutlineEffect>
18
+ }>
19
+
20
+ // Every OutlineEffect option that has a real setter (verified against
21
+ // postprocessing's source) - resolutionScale/resolutionX/resolutionY are
22
+ // the only ones without one, since they only feed the internal blur pass
23
+ // at construction time. scene/camera are required constructor args, so
24
+ // OutlineEffect can't use createEffectComponent (needs `new Effect()` to
25
+ // work with zero args) - built by hand instead.
26
+ const LIVE_KEYS = [
27
+ 'patternTexture',
28
+ 'patternScale',
29
+ 'edgeStrength',
30
+ 'pulseSpeed',
31
+ 'visibleEdgeColor',
32
+ 'hiddenEdgeColor',
33
+ 'multisampling',
34
+ 'width',
35
+ 'height',
36
+ 'kernelSize',
37
+ 'blur',
38
+ 'xRay',
39
+ 'dithering',
40
+ 'blendMode-blendFunction',
41
+ ]
42
+
43
+ // The setter stores whatever it's given as-is, unlike the constructor -
44
+ // wrap in a Color here too, or a raw hex/string breaks the shader uniform.
45
+ function set(effect: OutlineEffect, key: string, value: unknown): void {
46
+ if (key === 'visibleEdgeColor' || key === 'hiddenEdgeColor') applyPierced(effect, key, new Color(value as never))
47
+ else applyPierced(effect, key, value)
48
+ }
49
+
50
+ export function Outline({
51
+ selection = EMPTY_ARRAY,
52
+ selectionLayer = 10,
53
+ blendFunction,
54
+ resolutionScale,
55
+ resolutionX,
56
+ resolutionY,
57
+ ref,
58
+ ...liveProps
59
+ }: OutlineProps) {
60
+ const { scene, camera, autoClear } = use(EffectComposerContext)
61
+
62
+ const effect = useMemo(
63
+ () => new OutlineEffect(scene, camera, { resolutionScale, resolutionX, resolutionY }),
64
+ [scene, camera, resolutionScale, resolutionX, resolutionY]
65
+ )
66
+
67
+ useEffect(() => {
68
+ if (autoClear !== false) {
69
+ console.warn('Outline requires <EffectComposer autoClear={false}> to render correctly.')
70
+ }
71
+ }, [autoClear])
72
+
73
+ useLiveDefaults(
74
+ effect,
75
+ { ...liveProps, 'blendMode-blendFunction': blendFunction } as Record<string, unknown>,
76
+ LIVE_KEYS,
77
+ readPierced,
78
+ set
79
+ )
80
+ useSelectionSync(effect, selection, selectionLayer)
81
+ useDispose(effect)
82
+
83
+ return <primitive ref={ref} object={effect} />
84
+ }
@@ -1,15 +1,20 @@
1
- import { forwardRef, useMemo, Ref } from 'react'
2
- import { PixelationEffect } from 'postprocessing'
3
-
4
- export type PixelationProps = {
5
- granularity?: number
6
- }
7
-
8
- export const Pixelation = /* @__PURE__ */ forwardRef<PixelationEffect, PixelationProps>(function Pixelation(
9
- { granularity = 5 }: PixelationProps,
10
- ref: Ref<PixelationEffect>
11
- ) {
12
- /** Because GlitchEffect granularity is not an object but a number, we have to define a custom prop "granularity" */
13
- const effect = useMemo(() => new PixelationEffect(granularity), [granularity])
14
- return <primitive ref={ref} object={effect} dispose={null} />
15
- })
1
+ import { PixelationEffect } from 'postprocessing'
2
+ import type { Ref } from 'react'
3
+ import { createEffectComponent } from '../createEffectComponent'
4
+
5
+ // PixelationEffect's sole constructor arg is a bare number, not an options
6
+ // object - granularity is a real live setter though, so it's just a normal
7
+ // prop; only the curated default (5, vs the class's own default of 30)
8
+ // needs a thin wrapper.
9
+ const PixelationImpl = /* @__PURE__ */ createEffectComponent<typeof PixelationEffect, { granularity?: number }>(
10
+ PixelationEffect
11
+ )
12
+
13
+ export type PixelationProps = {
14
+ granularity?: number
15
+ ref?: Ref<PixelationEffect>
16
+ }
17
+
18
+ export function Pixelation({ granularity = 5, ref }: PixelationProps) {
19
+ return <PixelationImpl granularity={granularity} ref={ref} />
20
+ }
@@ -1,150 +1,239 @@
1
- import { Uniform } from 'three'
2
- import { Effect } from 'postprocessing'
3
- import { wrapEffect } from '../util'
4
-
5
- const RampShader = {
6
- fragmentShader: /* glsl */ `
7
- uniform int rampType;
8
-
9
- uniform vec2 rampStart;
10
- uniform vec2 rampEnd;
11
-
12
- uniform vec4 startColor;
13
- uniform vec4 endColor;
14
-
15
- uniform float rampBias;
16
- uniform float rampGain;
17
-
18
- uniform bool rampMask;
19
- uniform bool rampInvert;
20
-
21
- float getBias(float time, float bias) {
22
- return time / (((1.0 / bias) - 2.0) * (1.0 - time) + 1.0);
23
- }
24
-
25
- float getGain(float time, float gain) {
26
- if (time < 0.5)
27
- return getBias(time * 2.0, gain) / 2.0;
28
- else
29
- return getBias(time * 2.0 - 1.0, 1.0 - gain) / 2.0 + 0.5;
30
- }
31
-
32
- void mainImage(const in vec4 inputColor, const in vec2 uv, out vec4 outputColor) {
33
- vec2 centerPixel = uv * resolution;
34
- vec2 startPixel = rampStart * resolution;
35
- vec2 endPixel = rampEnd * resolution;
36
-
37
- float rampAlpha;
38
-
39
- if (rampType == 1) {
40
- vec2 fuv = centerPixel / resolution.y;
41
- vec2 suv = startPixel / resolution.y;
42
- vec2 euv = endPixel / resolution.y;
43
-
44
- float radius = length(suv - euv);
45
- float falloff = length(fuv - suv);
46
- rampAlpha = smoothstep(0.0, radius, falloff);
47
- } else {
48
- float radius = length(startPixel - endPixel);
49
- vec2 direction = normalize(vec2(endPixel.x - startPixel.x, -(startPixel.y - endPixel.y)));
50
-
51
- float fade = dot(centerPixel - startPixel, direction);
52
- if (rampType == 2) fade = abs(fade);
53
-
54
- rampAlpha = smoothstep(0.0, 1.0, fade / radius);
55
- }
56
-
57
- rampAlpha = abs((rampInvert ? 1.0 : 0.0) - getBias(rampAlpha, rampBias) * getGain(rampAlpha, rampGain));
58
-
59
- if (rampMask) {
60
- vec4 inputBuff = texture2D(inputBuffer, uv);
61
- outputColor = mix(inputBuff, inputColor, rampAlpha);
62
- } else {
63
- outputColor = mix(startColor, endColor, rampAlpha);
64
- }
65
- }
66
- `,
67
- }
68
-
69
- export enum RampType {
70
- Linear,
71
- Radial,
72
- MirroredLinear,
73
- }
74
-
75
- export class RampEffect extends Effect {
76
- constructor({
77
- /**
78
- * Type of ramp gradient.
79
- */
80
- rampType = RampType.Linear,
81
- /**
82
- * Starting point of the ramp gradient in normalized coordinates.
83
- *
84
- * Ranges from `[0 - 1]` as `[x, y]`. Default is `[0.5, 0.5]`.
85
- */
86
- rampStart = [0.5, 0.5],
87
- /**
88
- * Ending point of the ramp gradient in normalized coordinates.
89
- *
90
- * Ranges from `[0 - 1]` as `[x, y]`. Default is `[1, 1]`
91
- */
92
- rampEnd = [1, 1],
93
- /**
94
- * Color at the starting point of the gradient.
95
- *
96
- * Default is black: `[0, 0, 0, 1]`
97
- */
98
- startColor = [0, 0, 0, 1],
99
- /**
100
- * Color at the ending point of the gradient.
101
- *
102
- * Default is white: `[1, 1, 1, 1]`
103
- */
104
- endColor = [1, 1, 1, 1],
105
- /**
106
- * Bias for the interpolation curve when both bias and gain are 0.5.
107
- *
108
- * Ranges from `[0 - 1]`. Default is `0.5`.
109
- */
110
- rampBias = 0.5,
111
- /**
112
- * Gain for the interpolation curve when both bias and gain are 0.5.
113
- *
114
- * Ranges from `[0 - 1]`. Default is `0.5`.
115
- */
116
- rampGain = 0.5,
117
- /**
118
- * When enabled, the ramp gradient is used as an effect mask, and colors are ignored.
119
- *
120
- * Default is `false`.
121
- */
122
- rampMask = false,
123
- /**
124
- * Controls whether the ramp gradient is inverted.
125
- *
126
- * When disabled, rampStart is transparent and rampEnd is opaque.
127
- *
128
- * Default is `false`.
129
- */
130
- rampInvert = false,
131
- ...params
132
- } = {}) {
133
- super('RampEffect', RampShader.fragmentShader, {
134
- ...params,
135
- uniforms: new Map<string, Uniform>([
136
- ['rampType', new Uniform(rampType)],
137
- ['rampStart', new Uniform(rampStart)],
138
- ['rampEnd', new Uniform(rampEnd)],
139
- ['startColor', new Uniform(startColor)],
140
- ['endColor', new Uniform(endColor)],
141
- ['rampBias', new Uniform(rampBias)],
142
- ['rampGain', new Uniform(rampGain)],
143
- ['rampMask', new Uniform(rampMask)],
144
- ['rampInvert', new Uniform(rampInvert)],
145
- ]),
146
- })
147
- }
148
- }
149
-
150
- export const Ramp = /* @__PURE__ */ wrapEffect(RampEffect)
1
+ import { BlendFunction, Effect } from 'postprocessing'
2
+ import type { Ref } from 'react'
3
+ import { Uniform } from 'three'
4
+ import { createEffectComponent } from '../createEffectComponent'
5
+
6
+ const RampShader = {
7
+ fragmentShader: /* glsl */ `
8
+ uniform int rampType;
9
+
10
+ uniform vec2 rampStart;
11
+ uniform vec2 rampEnd;
12
+
13
+ uniform vec4 startColor;
14
+ uniform vec4 endColor;
15
+
16
+ uniform float rampBias;
17
+ uniform float rampGain;
18
+
19
+ uniform bool rampMask;
20
+ uniform bool rampInvert;
21
+
22
+ float getBias(float time, float bias) {
23
+ return time / (((1.0 / bias) - 2.0) * (1.0 - time) + 1.0);
24
+ }
25
+
26
+ float getGain(float time, float gain) {
27
+ if (time < 0.5)
28
+ return getBias(time * 2.0, gain) / 2.0;
29
+ else
30
+ return getBias(time * 2.0 - 1.0, 1.0 - gain) / 2.0 + 0.5;
31
+ }
32
+
33
+ void mainImage(const in vec4 inputColor, const in vec2 uv, out vec4 outputColor) {
34
+ vec2 centerPixel = uv * resolution;
35
+ vec2 startPixel = rampStart * resolution;
36
+ vec2 endPixel = rampEnd * resolution;
37
+
38
+ float rampAlpha;
39
+
40
+ if (rampType == 1) {
41
+ vec2 fuv = centerPixel / resolution.y;
42
+ vec2 suv = startPixel / resolution.y;
43
+ vec2 euv = endPixel / resolution.y;
44
+
45
+ float radius = length(suv - euv);
46
+ float falloff = length(fuv - suv);
47
+ rampAlpha = smoothstep(0.0, radius, falloff);
48
+ } else {
49
+ float radius = length(startPixel - endPixel);
50
+ vec2 direction = normalize(vec2(endPixel.x - startPixel.x, -(startPixel.y - endPixel.y)));
51
+
52
+ float fade = dot(centerPixel - startPixel, direction);
53
+ if (rampType == 2) fade = abs(fade);
54
+
55
+ rampAlpha = smoothstep(0.0, 1.0, fade / radius);
56
+ }
57
+
58
+ rampAlpha = abs((rampInvert ? 1.0 : 0.0) - getBias(rampAlpha, rampBias) * getGain(rampAlpha, rampGain));
59
+
60
+ if (rampMask) {
61
+ vec4 inputBuff = texture2D(inputBuffer, uv);
62
+ outputColor = mix(inputBuff, inputColor, rampAlpha);
63
+ } else {
64
+ outputColor = mix(startColor, endColor, rampAlpha);
65
+ }
66
+ }
67
+ `,
68
+ }
69
+
70
+ export enum RampType {
71
+ Linear,
72
+ Radial,
73
+ MirroredLinear,
74
+ }
75
+
76
+ type RampTuple2 = [number, number]
77
+ type RampTuple4 = [number, number, number, number]
78
+
79
+ export class RampEffect extends Effect {
80
+ constructor({
81
+ /**
82
+ * Type of ramp gradient.
83
+ */
84
+ rampType = RampType.Linear,
85
+ /**
86
+ * Starting point of the ramp gradient in normalized coordinates.
87
+ *
88
+ * Ranges from `[0 - 1]` as `[x, y]`. Default is `[0.5, 0.5]`.
89
+ */
90
+ rampStart = [0.5, 0.5] as RampTuple2,
91
+ /**
92
+ * Ending point of the ramp gradient in normalized coordinates.
93
+ *
94
+ * Ranges from `[0 - 1]` as `[x, y]`. Default is `[1, 1]`
95
+ */
96
+ rampEnd = [1, 1] as RampTuple2,
97
+ /**
98
+ * Color at the starting point of the gradient.
99
+ *
100
+ * Default is black: `[0, 0, 0, 1]`
101
+ */
102
+ startColor = [0, 0, 0, 1] as RampTuple4,
103
+ /**
104
+ * Color at the ending point of the gradient.
105
+ *
106
+ * Default is white: `[1, 1, 1, 1]`
107
+ */
108
+ endColor = [1, 1, 1, 1] as RampTuple4,
109
+ /**
110
+ * Bias for the interpolation curve when both bias and gain are 0.5.
111
+ *
112
+ * Ranges from `[0 - 1]`. Default is `0.5`.
113
+ */
114
+ rampBias = 0.5,
115
+ /**
116
+ * Gain for the interpolation curve when both bias and gain are 0.5.
117
+ *
118
+ * Ranges from `[0 - 1]`. Default is `0.5`.
119
+ */
120
+ rampGain = 0.5,
121
+ /**
122
+ * When enabled, the ramp gradient is used as an effect mask, and colors are ignored.
123
+ *
124
+ * Default is `false`.
125
+ */
126
+ rampMask = false,
127
+ /**
128
+ * Controls whether the ramp gradient is inverted.
129
+ *
130
+ * When disabled, rampStart is transparent and rampEnd is opaque.
131
+ *
132
+ * Default is `false`.
133
+ */
134
+ rampInvert = false,
135
+ ...params
136
+ } = {}) {
137
+ super('RampEffect', RampShader.fragmentShader, {
138
+ ...params,
139
+ uniforms: new Map<string, Uniform>([
140
+ ['rampType', new Uniform(rampType)],
141
+ ['rampStart', new Uniform(rampStart)],
142
+ ['rampEnd', new Uniform(rampEnd)],
143
+ ['startColor', new Uniform(startColor)],
144
+ ['endColor', new Uniform(endColor)],
145
+ ['rampBias', new Uniform(rampBias)],
146
+ ['rampGain', new Uniform(rampGain)],
147
+ ['rampMask', new Uniform(rampMask)],
148
+ ['rampInvert', new Uniform(rampInvert)],
149
+ ]),
150
+ })
151
+ }
152
+
153
+ private u<T>(name: string): T {
154
+ return this.uniforms.get(name)!.value
155
+ }
156
+
157
+ private setU(name: string, value: unknown): void {
158
+ this.uniforms.get(name)!.value = value
159
+ }
160
+
161
+ get rampType(): RampType {
162
+ return this.u('rampType')
163
+ }
164
+ set rampType(value: RampType) {
165
+ this.setU('rampType', value)
166
+ }
167
+
168
+ get rampStart(): RampTuple2 {
169
+ return this.u('rampStart')
170
+ }
171
+ set rampStart(value: RampTuple2) {
172
+ this.setU('rampStart', value)
173
+ }
174
+
175
+ get rampEnd(): RampTuple2 {
176
+ return this.u('rampEnd')
177
+ }
178
+ set rampEnd(value: RampTuple2) {
179
+ this.setU('rampEnd', value)
180
+ }
181
+
182
+ get startColor(): RampTuple4 {
183
+ return this.u('startColor')
184
+ }
185
+ set startColor(value: RampTuple4) {
186
+ this.setU('startColor', value)
187
+ }
188
+
189
+ get endColor(): RampTuple4 {
190
+ return this.u('endColor')
191
+ }
192
+ set endColor(value: RampTuple4) {
193
+ this.setU('endColor', value)
194
+ }
195
+
196
+ get rampBias(): number {
197
+ return this.u('rampBias')
198
+ }
199
+ set rampBias(value: number) {
200
+ this.setU('rampBias', value)
201
+ }
202
+
203
+ get rampGain(): number {
204
+ return this.u('rampGain')
205
+ }
206
+ set rampGain(value: number) {
207
+ this.setU('rampGain', value)
208
+ }
209
+
210
+ get rampMask(): boolean {
211
+ return this.u('rampMask')
212
+ }
213
+ set rampMask(value: boolean) {
214
+ this.setU('rampMask', value)
215
+ }
216
+
217
+ get rampInvert(): boolean {
218
+ return this.u('rampInvert')
219
+ }
220
+ set rampInvert(value: boolean) {
221
+ this.setU('rampInvert', value)
222
+ }
223
+ }
224
+
225
+ export type RampProps = {
226
+ blendFunction?: BlendFunction
227
+ rampType?: RampType
228
+ rampStart?: RampTuple2
229
+ rampEnd?: RampTuple2
230
+ startColor?: RampTuple4
231
+ endColor?: RampTuple4
232
+ rampBias?: number
233
+ rampGain?: number
234
+ rampMask?: boolean
235
+ rampInvert?: boolean
236
+ ref?: Ref<RampEffect>
237
+ }
238
+
239
+ export const Ramp = /* @__PURE__ */ createEffectComponent<typeof RampEffect, RampProps>(RampEffect)