Skip to content

Commit ae9036f

Browse files
authored
Merge pull request #97 from FunTechInc/v1.1.15
v1.1.15
2 parents c8c7cba + 5db74fb commit ae9036f

File tree

71 files changed

+2451
-1536
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

71 files changed

+2451
-1536
lines changed

.storybook/stories/UseMotionBlur.tsx

+116
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
import * as THREE from "three";
2+
import * as React from "react";
3+
import { useFrame, extend, useThree, createPortal } from "@react-three/fiber";
4+
import { FxMaterial, FxMaterialProps } from "../../utils/fxMaterial";
5+
import GUI from "lil-gui";
6+
import { useGUI } from "../../utils/useGUI";
7+
import { useMotionBlur, useSingleFBO } from "../../packages/use-shader-fx/src";
8+
import {
9+
MotionBlurParams,
10+
MOTIONBLUR_PARAMS,
11+
} from "../../packages/use-shader-fx/src/fxs/effects/useMotionBlur";
12+
import { OrbitControls } from "@react-three/drei";
13+
14+
extend({ FxMaterial });
15+
16+
const CONFIG: MotionBlurParams = structuredClone(MOTIONBLUR_PARAMS);
17+
const setGUI = (gui: GUI) => {
18+
gui.add(CONFIG, "strength", 0, 0.99, 0.01);
19+
};
20+
const setConfig = () => {
21+
return {
22+
...CONFIG,
23+
} as MotionBlurParams;
24+
};
25+
26+
export const UseMotionBlur = (args: MotionBlurParams) => {
27+
const updateGUI = useGUI(setGUI);
28+
29+
const fxRef = React.useRef<FxMaterialProps>();
30+
const { size, viewport, camera } = useThree();
31+
const [updateMotionBlur, setMotionBlur] = useMotionBlur({
32+
size,
33+
dpr: viewport.dpr,
34+
});
35+
36+
// This scene is rendered offscreen
37+
const offscreenScene = React.useMemo(() => new THREE.Scene(), []);
38+
39+
// create FBO for offscreen rendering
40+
const [boxView, updateRenderTarget] = useSingleFBO({
41+
scene: offscreenScene,
42+
camera,
43+
size,
44+
dpr: viewport.dpr,
45+
samples: 4,
46+
});
47+
48+
setMotionBlur({
49+
texture: boxView.texture,
50+
});
51+
52+
useFrame((props) => {
53+
updateRenderTarget(props.gl);
54+
const fx = updateMotionBlur(props, {
55+
strength: setConfig().strength,
56+
});
57+
fxRef.current!.u_fx = fx;
58+
updateGUI();
59+
});
60+
61+
return (
62+
<>
63+
{createPortal(
64+
<mesh>
65+
<ambientLight intensity={Math.PI} />
66+
<spotLight
67+
position={[10, 10, 10]}
68+
angle={0.15}
69+
penumbra={1}
70+
decay={0}
71+
intensity={Math.PI}
72+
/>
73+
<pointLight
74+
position={[-10, -10, -10]}
75+
decay={0}
76+
intensity={Math.PI}
77+
/>
78+
<Box position={[-1.5, 0, 0]} />
79+
<Box position={[1.5, 0, 0]} />
80+
</mesh>,
81+
offscreenScene
82+
)}
83+
<mesh>
84+
<planeGeometry args={[2, 2]} />
85+
<fxMaterial key={FxMaterial.key} ref={fxRef} />
86+
</mesh>
87+
<OrbitControls />
88+
</>
89+
);
90+
};
91+
92+
function Box(props: any) {
93+
// This reference will give us direct access to the mesh
94+
const meshRef = React.useRef<THREE.Mesh>();
95+
// Set up state for the hovered and active state
96+
const [hovered, setHover] = React.useState(false);
97+
const [active, setActive] = React.useState(false);
98+
// Subscribe this component to the render-loop, rotate the mesh every frame
99+
useFrame((state, delta) => {
100+
meshRef.current!.rotation.x += delta;
101+
meshRef.current!.rotation.y -= delta;
102+
});
103+
// Return view, these are regular three.js elements expressed in JSX
104+
return (
105+
<mesh
106+
{...props}
107+
ref={meshRef}
108+
scale={active ? 2 : 1.5}
109+
onClick={(event) => setActive(!active)}
110+
onPointerOver={(event) => setHover(true)}
111+
onPointerOut={(event) => setHover(false)}>
112+
<boxGeometry args={[1, 1, 1]} />
113+
<meshStandardMaterial color={hovered ? "hotpink" : "orange"} />
114+
</mesh>
115+
);
116+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import * as React from "react";
2+
import type { StoryObj } from "@storybook/react";
3+
import { setArgTypes } from "../utils/setArgTypes";
4+
import { Setup } from "../utils/Setup";
5+
import type { Meta } from "@storybook/react";
6+
import {
7+
MotionBlurParams,
8+
MOTIONBLUR_PARAMS,
9+
} from "../../packages/use-shader-fx/src/fxs/effects/useMotionBlur";
10+
import { UseMotionBlur } from "./UseMotionBlur";
11+
12+
const meta = {
13+
title: "effects/useMotionBlur",
14+
component: UseMotionBlur,
15+
tags: ["autodocs"],
16+
decorators: [(storyFn: any) => <Setup>{storyFn()}</Setup>],
17+
} satisfies Meta<typeof UseMotionBlur>;
18+
19+
export default meta;
20+
type Story = StoryObj<typeof meta>;
21+
22+
const storySetting = {
23+
args: MOTIONBLUR_PARAMS,
24+
argTypes: setArgTypes<MotionBlurParams>(MOTIONBLUR_PARAMS),
25+
};
26+
27+
export const Default: Story = {
28+
render: (args) => <UseMotionBlur {...args} />,
29+
...storySetting,
30+
};

app/_home/Playground.tsx

+2-17
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import * as THREE from "three";
22
import { useMemo, useRef } from "react";
33
import { useFrame, useThree, extend } from "@react-three/fiber";
44
import {
5-
useNoise,
65
useColorStrata,
76
useMarble,
87
useHSV,
@@ -92,10 +91,6 @@ export const Playground = ({
9291
const { size, viewport } = useThree();
9392
const funkun = useVideoTexture("/FT_Ch02-comp.mp4");
9493

95-
const [updateNoise, setNoise, { output: noise }] = useNoise({
96-
size,
97-
dpr: viewport.dpr,
98-
});
9994
const [updateColorStrata, setColorStrata, { output: colorStrata }] =
10095
useColorStrata({ size, dpr: viewport.dpr });
10196
const [updateMarble, setMarble, { output: marble }] = useMarble({
@@ -108,22 +103,15 @@ export const Playground = ({
108103
});
109104
const [updateBrush, setBrush, { output: brush }] = useBrush({
110105
size,
111-
dpr: viewport.dpr,
106+
dpr: 0.05,
112107
});
113108
const [updateCover, setCover, { output: cover }] = useCoverTexture({
114109
size,
115-
dpr: viewport.dpr,
110+
dpr: 0.1,
116111
});
117112

118113
useMemo(() => {
119114
CONFIG.random();
120-
setNoise({
121-
scale: 1000,
122-
warpOctaves: 1,
123-
noiseOctaves: 1,
124-
fbmOctaves: 1,
125-
timeStrength: 1,
126-
});
127115

128116
setMarble({
129117
...setConfig("marble"),
@@ -145,7 +133,6 @@ export const Playground = ({
145133
});
146134

147135
setBrush({
148-
map: noise,
149136
texture: cover,
150137
mapIntensity: 0.35,
151138
radius: 0.2,
@@ -169,7 +156,6 @@ export const Playground = ({
169156
hashMemo.current = hash;
170157
CONFIG.random();
171158
}
172-
updateNoise(props);
173159
updateColorStrata(props, {
174160
...(setConfig("colorStrata") as ColorStrataParams),
175161
});
@@ -194,7 +180,6 @@ export const Playground = ({
194180
<fxMaterial
195181
key={FxMaterial.key}
196182
u_noise={marble}
197-
u_grain={noise}
198183
u_colorStrata={hsv}
199184
u_brush={brush}
200185
ref={ref}

app/_home/main.frag

+2-5
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,19 @@ varying vec2 vUv;
33
uniform sampler2D u_noise;
44
uniform float u_noiseIntensity;
55
uniform sampler2D u_colorStrata;
6-
uniform sampler2D u_grain;
76
uniform sampler2D u_brush;
87

98
void main() {
109

1110
vec2 uv = vUv;
12-
vec4 grain = texture2D(u_grain, uv);
1311
vec4 noise = texture2D(u_noise, uv);
1412
vec4 brush = texture2D(u_brush, uv);
15-
16-
uv += grain.rg;
13+
1714
uv += noise.rg * u_noiseIntensity;
1815
vec4 colorStrata = texture2D(u_colorStrata,uv);
1916

2017
vec4 mixColor = mix(colorStrata, brush, brush.a);
2118

2219
gl_FragColor = mixColor;
2320
gl_FragColor.a = 1.0;
24-
}
21+
}

app/funtech/FxMaterial.tsx

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import * as THREE from "three";
2+
import { shaderMaterial } from "@react-three/drei";
3+
import frag from "./main.frag";
4+
5+
declare global {
6+
namespace JSX {
7+
interface IntrinsicElements {
8+
fxMaterial: any;
9+
}
10+
}
11+
}
12+
13+
export type FxMaterialProps = {
14+
u_fx: THREE.Texture;
15+
u_time: number;
16+
u_floor: number;
17+
u_contrast: number;
18+
u_brightness: number;
19+
u_saturation: number;
20+
u_noiseStrength: number;
21+
u_floorStrength: THREE.Vector2;
22+
};
23+
24+
export const FxMaterial = shaderMaterial(
25+
{
26+
u_fx: new THREE.Texture(),
27+
u_time: 0,
28+
u_floor: 8,
29+
u_contrast: 1,
30+
u_brightness: 1,
31+
u_saturation: 1,
32+
u_noiseStrength: 0.3,
33+
u_floorStrength: new THREE.Vector2(0.2, 0.8),
34+
},
35+
36+
`
37+
varying vec2 vUv;
38+
void main() {
39+
vUv = uv;
40+
gl_Position = vec4(position, 1.0);
41+
}
42+
`,
43+
frag
44+
);

0 commit comments

Comments
 (0)