Skip to content

Commit 9fe6adf

Browse files
author
takuma-hmng8
committed
README
1 parent 9e1c888 commit 9fe6adf

Some content is hidden

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

52 files changed

+367
-1208
lines changed

README.md

Lines changed: 109 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,54 @@ For details on each FX, please refer to Storybook
66
👉 [Storybook](https://use-shader-fx-stories.vercel.app/) 👈
77

88
```bash
9-
npm install @hmng8/use-shader-fx
9+
npm install @funtech-inc/use-shader-fx
1010
```
1111

12+
# Hooks Index
13+
14+
### FXs
15+
16+
<table>
17+
<tr>
18+
<th><strong>effects</strong></th>
19+
<td><a href="">useSimpleBlur</a>, <a href="">useWave</a></td>
20+
</tr>
21+
22+
<tr>
23+
<th><strong>interactions</strong></th>
24+
<td><a href="">useBrush</a>, <a href="">useFluid</a>, <a href="">useRipple</a></td>
25+
</tr>
26+
27+
<tr>
28+
<th><strong>misc</strong></th>
29+
<td><a href="">useChromaKey</a></td>
30+
</tr>
31+
32+
<tr>
33+
<th><strong>noises</strong></th>
34+
<td><a href="">useColorStrata</a>, <a href="">useMarble</a>, <a href="">useNoise</a></td>
35+
</tr>
36+
37+
<tr>
38+
<th><strong>utils</strong></th>
39+
<td><a href="">useAlphaBlending</a>, <a href="">useBlending</a>, <a href="">useBrightnessPicker</a>, <a href="">useCoverTexture</a>, <a href="">useDuoTone</a>, <a href="">useFxBlending</a>, <a href="">useFxTexture</a>, <a href="">useHSV</a></td>
40+
</tr>
41+
42+
<tr>
43+
<th><strong>misc</strong></th>
44+
<td><a href="">useBeat</a>, <a href="">useFPSLimiter</a>, <a href="">usePointer</a>, <a href="">useDomSyncer</a></td>
45+
</tr>
46+
</table>
47+
48+
### Misc
49+
50+
<table>
51+
<tr>
52+
<th><strong>misc</strong></th>
53+
<td><a href="#usebeat">useBeat</a>, <a href="#usefpslimiter">useFPSLimiter</a>, <a href="#usepointer">usePointer</a>, <a href="#usedomsyncer">useDomSyncer</a></td>
54+
</tr>
55+
</table>
56+
1257
# Usage
1358

1459
From each `fxHooks`, you can receive [`updateFx`, `setParams`, `fxObject`] in array format. The `config` is an object, which varies for each Hook, containing details such as `size`,`dpr` and `samples`.
@@ -304,17 +349,6 @@ Generates and returns a `THREE.OrthographicCamera`.
304349
const camera = useCamera(size);
305350
```
306351

307-
## usePointer
308-
309-
When given the `pointer` vector2 from r3f's `RootState`, it generates an update function that returns {currentPointer, prevPointer, diffPointer, isVelocityUpdate, velocity}.
310-
311-
```js
312-
const updatePointer = usePointer();
313-
314-
const { currentPointer, prevPointer, diffPointer, isVelocityUpdate, velocity } =
315-
updatePointer(pointer);
316-
```
317-
318352
## useResolution
319353

320354
This hook returns `resolution`. If `dpr` isn't set (or set to false), dpr won't be multiplied.
@@ -359,7 +393,7 @@ const [renderTargets, copyTexture] = useCopyTexture(UseFboProps, length);
359393
copyTexture(gl, index); // return texture
360394
```
361395

362-
# Other Hooks
396+
# Misc
363397

364398
## useDomSyncer
365399

@@ -460,3 +494,65 @@ type DomSyncerParams = {
460494
```
461495

462496
`updateKey` : Because DOM rendering and React updates occur asynchronously, there may be a lag between updating dependent arrays and setting DOM arrays. That's what the Key is for. If the dependent array is updated but the Key is not, the loop will skip and return an empty texture. By updating the timing key when DOM acquisition is complete, you can perfectly synchronize DOM and Mesh updates.
497+
498+
## usePointer
499+
500+
When given the `pointer` vector2 from r3f's `RootState`, it generates an update function that returns {currentPointer, prevPointer, diffPointer, isVelocityUpdate, velocity}.
501+
You can also add `lerp` (0~1, lerp intensity (0 to less than 1) , default: 0)
502+
503+
```js
504+
const updatePointer = usePointer(lerp);
505+
506+
const { currentPointer, prevPointer, diffPointer, isVelocityUpdate, velocity } =
507+
updatePointer(pointer);
508+
```
509+
510+
You can override the pointer process by passing `pointerValues` to `updateFx` in the `useFrame`.
511+
512+
```ts
513+
useFrame((props) => {
514+
const pointerValues = updatePointer(props.pointer);
515+
updateBrush(props, {
516+
pointerValues: pointerValues,
517+
});
518+
});
519+
```
520+
521+
## useBeat
522+
523+
Time-sensitive hooks such as `useNoise` and `useMarble` accept `beat`.
524+
The second argument can be `easing`.
525+
easing functions are referenced from https://github.com/ai/easings.net , default : "easeOutQuart"
526+
527+
```ts
528+
const beting = useBeat(bpm, "easeOutQuad");
529+
useFrame((props) => {
530+
const { beat, hash } = beting(props.clock);
531+
updateMarble(props, {
532+
beat: beat,
533+
});
534+
});
535+
```
536+
537+
```ts
538+
type BeatValues = {
539+
beat: number;
540+
floor: number;
541+
fract: number;
542+
/** unique hash specific to the beat */
543+
hash: number;
544+
};
545+
```
546+
547+
## useFPSLimiter
548+
549+
Allows you to skip FX that do not need to be processed at 60 FPS.
550+
551+
```ts
552+
const limiter = useFPSLimiter(30);
553+
useFrame((props) => {
554+
if (!limiter(props.clock)) {
555+
return;
556+
}
557+
});
558+
```

app/ShaderFx.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ export const ShaderFx = ({
2222
return;
2323
}
2424
console.log(`dpr:${dpr}`);
25-
setDpr(Math.round((0.5 + 1.0 * factor) * 10) / 10);
25+
setDpr(Math.round((1.0 + 1.0 * factor) * 10) / 10);
2626
}}>
2727
<Suspense fallback={null}>{children}</Suspense>
2828
{/* <Perf position={"bottom-left"} minimal={false} /> */}

app/comingsoon/FxMaterial.tsx renamed to app/_home/FxMaterial.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ declare global {
1313
export type FxMaterialProps = {
1414
u_noise: THREE.Texture;
1515
u_colorStrata: THREE.Texture;
16+
u_brush: THREE.Texture;
1617
u_grain: THREE.Texture;
1718
u_noiseIntensity: number;
1819
};
@@ -21,6 +22,7 @@ export const FxMaterial = shaderMaterial(
2122
{
2223
u_noise: new THREE.Texture(),
2324
u_colorStrata: new THREE.Texture(),
25+
u_brush: new THREE.Texture(),
2426
u_grain: new THREE.Texture(),
2527
u_noiseIntensity: 1,
2628
},

app/comingsoon/Playground.tsx renamed to app/_home/Playground.tsx

Lines changed: 72 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
"use client";
2-
31
import * as THREE from "three";
42
import { useEffect, useRef } from "react";
53
import { useFrame, useThree, extend } from "@react-three/fiber";
@@ -9,9 +7,14 @@ import {
97
useMarble,
108
useHSV,
119
useBeat,
10+
useBrush,
11+
useCoverTexture,
12+
usePointer,
13+
useFPSLimiter,
1214
} from "@/packages/use-shader-fx/src";
15+
1316
import { FxMaterial, FxMaterialProps } from "./FxMaterial";
14-
import { useFBX } from "@react-three/drei";
17+
import { useVideoTexture } from "@react-three/drei";
1518

1619
extend({ FxMaterial });
1720

@@ -40,7 +43,10 @@ const CONFIG = {
4043
CONFIG.marble.complexity = Math.random() * 10;
4144
CONFIG.marble.complexityAttenuation = Math.random();
4245
CONFIG.marble.scale = Math.random() * 0.001;
43-
CONFIG.colorStrata.laminateLayer = Math.random() * 100;
46+
CONFIG.colorStrata.laminateLayer = Math.max(
47+
Math.floor(Math.random() * 5),
48+
1
49+
);
4450
CONFIG.colorStrata.scale = Math.max(Math.random(), 0.1);
4551
CONFIG.colorStrata.laminateInterval = new THREE.Vector2(
4652
Math.max(Math.random(), 0.2),
@@ -59,9 +65,8 @@ const CONFIG = {
5965
Math.random(),
6066
Math.random()
6167
);
62-
CONFIG.noiseIntensity = Math.random() * 10;
68+
CONFIG.noiseIntensity = Math.random() * 20;
6369
},
64-
save: () => {},
6570
};
6671

6772
const setConfig = (key: "marble" | "colorStrata" | "hsv") => {
@@ -70,19 +75,11 @@ const setConfig = (key: "marble" | "colorStrata" | "hsv") => {
7075
};
7176
};
7277

73-
// function FunKun() {
74-
// let fbx = useFBX("/funkun.fbx");
75-
// return <primitive object={fbx} scale={0.1} />;
76-
// }
77-
78-
export const Playground = () => {
78+
export const Playground = ({ bpm }: { bpm: number }) => {
7979
const ref = useRef<FxMaterialProps>();
80-
81-
useEffect(() => {
82-
CONFIG.random();
83-
}, []);
84-
8580
const { size, viewport } = useThree();
81+
const funkun = useVideoTexture("/FT_Ch02.mp4");
82+
8683
const [updateNoise, setNoise, { output: noise }] = useNoise({
8784
size,
8885
dpr: viewport.dpr,
@@ -97,34 +94,66 @@ export const Playground = () => {
9794
size,
9895
dpr: viewport.dpr,
9996
});
100-
101-
setNoise({
102-
scale: 1000,
103-
warpOctaves: 1,
104-
noiseOctaves: 1,
105-
fbmOctaves: 1,
106-
timeStrength: 1,
97+
const [updateBrush, setBrush, { output: brush }] = useBrush({
98+
size,
99+
dpr: viewport.dpr,
107100
});
108-
109-
setMarble({
110-
...setConfig("marble"),
111-
timeStrength: 0.5,
101+
const [updateCover, setCover, { output: cover }] = useCoverTexture({
102+
size,
103+
dpr: viewport.dpr,
112104
});
113105

114-
setColorStrata({
115-
...setConfig("colorStrata"),
116-
timeStrength: new THREE.Vector2(0, 0),
117-
});
106+
useEffect(() => {
107+
CONFIG.random();
108+
setNoise({
109+
scale: 1000,
110+
warpOctaves: 1,
111+
noiseOctaves: 1,
112+
fbmOctaves: 1,
113+
timeStrength: 1,
114+
});
118115

119-
setHSV({
120-
...setConfig("hsv"),
121-
texture: colorStrata,
122-
});
116+
setMarble({
117+
...setConfig("marble"),
118+
timeStrength: 0.5,
119+
iterations: 3,
120+
});
121+
122+
setColorStrata({
123+
...setConfig("colorStrata"),
124+
timeStrength: new THREE.Vector2(0, 0),
125+
});
126+
127+
setHSV({
128+
...setConfig("hsv"),
129+
texture: colorStrata,
130+
});
131+
132+
setCover({
133+
texture: funkun,
134+
textureResolution: new THREE.Vector2(1280, 720),
135+
});
136+
137+
setBrush({
138+
map: noise,
139+
texture: cover,
140+
mapIntensity: 0.3,
141+
radius: 0.2,
142+
dissipation: 0.9,
143+
isCursor: true,
144+
});
145+
// eslint-disable-next-line react-hooks/exhaustive-deps
146+
}, []);
123147

124-
const beting = useBeat(110);
148+
const beting = useBeat(bpm, "easeOutQuad");
149+
const updatePointer = usePointer(0.8);
150+
const limiter = useFPSLimiter(40);
125151
const hashMemo = useRef(0);
126152

127153
useFrame((props) => {
154+
if (!limiter(props.clock)) {
155+
return;
156+
}
128157
const { beat, hash } = beting(props.clock);
129158
if (hash !== hashMemo.current) {
130159
hashMemo.current = hash;
@@ -141,22 +170,23 @@ export const Playground = () => {
141170
...(setConfig("marble") as any),
142171
beat: beat,
143172
});
173+
updateCover(props);
174+
const pointerValues = updatePointer(props.pointer);
175+
updateBrush(props, {
176+
pointerValues: pointerValues,
177+
});
144178
ref.current!.u_noiseIntensity = CONFIG.noiseIntensity;
145179
});
146180

147181
return (
148-
// <mesh>
149-
// <ambientLight />
150-
// <pointLight position={[10, 10, 10]} />
151-
// <FunKun />
152-
// </mesh>
153182
<mesh>
154183
<planeGeometry args={[2, 2]} />
155184
<fxMaterial
156185
key={FxMaterial.key}
157186
u_noise={marble}
158187
u_grain={noise}
159188
u_colorStrata={hsv}
189+
u_brush={brush}
160190
ref={ref}
161191
/>
162192
</mesh>

0 commit comments

Comments
 (0)