Skip to content

Commit 539a7b3

Browse files
author
takuma-hmng8
committed
add useBeat
1 parent b715da1 commit 539a7b3

File tree

12 files changed

+381
-17
lines changed

12 files changed

+381
-17
lines changed

app/ShaderFx.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ export const ShaderFx = ({ children }: { children: React.ReactNode }) => {
1212
<PerformanceMonitor
1313
factor={1}
1414
onChange={({ factor }) => {
15-
console.log(`dpr:${dpr}`);
16-
// setDpr(Math.round((0.5 + 1.0 * factor) * 10) / 10);
15+
// console.log(`dpr:${dpr}`);
16+
setDpr(Math.round((0.5 + 1.0 * factor) * 10) / 10);
1717
}}>
1818
<Suspense fallback={null}>{children}</Suspense>
1919
<Perf position={"bottom-left"} minimal={false} />

app/playground/Playground.tsx

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,7 @@ extend({ FxMaterial });
2929
export const Playground = () => {
3030
const ref = useRef<FxMaterialProps>();
3131

32-
const video = useVideoTexture("/glitch.mov");
33-
const sample = useTexture("/test.jpg");
32+
const video = useVideoTexture("/gorilla.mov");
3433

3534
const { size, viewport } = useThree();
3635

@@ -42,12 +41,12 @@ export const Playground = () => {
4241
set({
4342
texture: video,
4443
textureResolution: new THREE.Vector2(1920, 1080),
45-
keyColor: new THREE.Color(0x000000),
46-
similarity: 0.01,
47-
spill: 0.1,
44+
keyColor: new THREE.Color(0x71f998),
45+
similarity: 0.2,
46+
spill: 0.2,
4847
smoothness: 0.1,
49-
contrast: 1.8,
50-
gamma: 0.7,
48+
contrast: 1,
49+
gamma: 1,
5150
brightness: 0.0,
5251
});
5352

app/useBeat/FxMaterial.tsx

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import * as THREE from "three";
2+
import { shaderMaterial } from "@react-three/drei";
3+
import fragment 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_time: number;
15+
u_resolution: THREE.Vector2;
16+
u_fx: THREE.Texture;
17+
u_noise: THREE.Texture;
18+
u_noiseIntensity: number;
19+
};
20+
21+
export const FxMaterial = shaderMaterial(
22+
{
23+
u_time: 0,
24+
u_resolution: new THREE.Vector2(0, 0),
25+
u_fx: new THREE.Texture(),
26+
u_noise: new THREE.Texture(),
27+
u_noiseIntensity: 0,
28+
},
29+
30+
`
31+
varying vec2 vUv;
32+
void main() {
33+
vUv = uv;
34+
gl_Position = vec4(position, 1.0);
35+
}
36+
`,
37+
fragment
38+
);

app/useBeat/Playground.tsx

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
"use client";
2+
3+
import * as THREE from "three";
4+
import { useEffect, useRef } from "react";
5+
import { useFrame, useThree, extend } from "@react-three/fiber";
6+
import {
7+
useNoise,
8+
useFluid,
9+
useFxBlending,
10+
useColorStrata,
11+
useBrightnessPicker,
12+
useChromaKey,
13+
useBeat,
14+
useFxTexture,
15+
} from "@/packages/use-shader-fx/src";
16+
import {
17+
NoiseParams,
18+
NOISE_PARAMS,
19+
} from "@/packages/use-shader-fx/src/hooks/useNoise";
20+
import {
21+
ColorStrataParams,
22+
COLORSTRATA_PARAMS,
23+
} from "@/packages/use-shader-fx/src/hooks/useColorStrata";
24+
import GUI from "lil-gui";
25+
import { useGUI } from "@/utils/useGUI";
26+
import { FxMaterial, FxMaterialProps } from "./FxMaterial";
27+
import { useTexture, useVideoTexture } from "@react-three/drei";
28+
import { Console } from "console";
29+
30+
extend({ FxMaterial });
31+
32+
export const Playground = () => {
33+
const ref = useRef<FxMaterialProps>();
34+
35+
const { size, viewport } = useThree();
36+
const video = useVideoTexture("/gorilla.mov");
37+
const glitch = useVideoTexture("/glitch.mov");
38+
39+
const [updateNoise, set, { output: noise }] = useNoise({
40+
size,
41+
dpr: viewport.dpr,
42+
});
43+
44+
const updateBeat = useBeat(157);
45+
46+
useFrame((props) => {
47+
const { beat, fract, hash } = updateBeat(props.clock);
48+
updateNoise(props, {
49+
beat: beat,
50+
});
51+
ref.current!.u_noiseIntensity =
52+
hash > 0.5 ? hash * fract : hash * fract * -1;
53+
ref.current!.u_fx = hash > 0.5 ? glitch : video;
54+
});
55+
56+
return (
57+
<mesh>
58+
<planeGeometry args={[2, 2]} />
59+
<fxMaterial key={FxMaterial.key} u_noise={noise} ref={ref} />
60+
</mesh>
61+
);
62+
};

app/useBeat/main.frag

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
precision highp float;
2+
varying vec2 vUv;
3+
uniform sampler2D u_fx;
4+
uniform sampler2D u_noise;
5+
uniform float u_noiseIntensity;
6+
7+
void main() {
8+
vec2 uv = vUv;
9+
10+
vec4 noise = texture2D(u_noise, uv);
11+
12+
uv += noise.rg * u_noiseIntensity;
13+
14+
vec4 fx = texture2D(u_fx, uv);
15+
16+
gl_FragColor = fx;
17+
}

app/useBeat/page.tsx

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { ShaderFx } from "../ShaderFx";
2+
import { Playground } from "./Playground";
3+
4+
export default function Page() {
5+
return (
6+
<div style={{ width: "100%" }}>
7+
<div
8+
style={{
9+
position: "fixed",
10+
width: "100%",
11+
height: "100svh",
12+
pointerEvents: "none",
13+
}}>
14+
<ShaderFx>
15+
<Playground />
16+
</ShaderFx>
17+
</div>
18+
</div>
19+
);
20+
}

packages/use-shader-fx/src/hooks/useChromaKey/index.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@ export type ChromaKeyParams = {
1515
textureResolution?: THREE.Vector2;
1616
/** key color for chromakey processing , default: THREE.Color(0x00ff00) */
1717
keyColor?: THREE.Color;
18-
/** If the similarity with the key color exceeds this value, it becomes transparent. , default: 0.01 */
18+
/** If the similarity with the key color exceeds this value, it becomes transparent. , default: 0.2 */
1919
similarity?: number;
20-
/** smoothness , default : 0.01 */
20+
/** smoothness , default : 0.1 */
2121
smoothness?: number;
22-
/** spill , default : 0.01 */
22+
/** spill , default : 0.2 */
2323
spill?: number;
2424
/** tone correction , default : THREE.Vector4(1.0, 1.0, 1.0, 1.0) */
2525
color?: THREE.Vector4;
@@ -43,9 +43,9 @@ export const CHROMAKEY_PARAMS: ChromaKeyParams = {
4343
texture: new THREE.Texture(),
4444
textureResolution: new THREE.Vector2(0, 0),
4545
keyColor: new THREE.Color(0x00ff00),
46-
similarity: 0.01,
47-
smoothness: 0.01,
48-
spill: 0.01,
46+
similarity: 0.2,
47+
smoothness: 0.1,
48+
spill: 0.2,
4949
color: new THREE.Vector4(1.0, 1.0, 1.0, 1.0),
5050
contrast: 1.0,
5151
brightness: 0.0,

packages/use-shader-fx/src/hooks/useChromaKey/shader/main.frag

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ float getChromeDist(vec3 texColor){
2929

3030
float getBoxFilteredChromaDist(vec3 rgb, vec2 uv)
3131
{
32-
vec2 pixel_size = vec2(1.,1.);
32+
vec2 pixel_size = vec2(1.) / u_textureResolution;
3333
vec2 h_pixel_size = pixel_size / 2.0;
3434
vec2 point_0 = vec2(pixel_size.x, h_pixel_size.y);
3535
vec2 point_1 = vec2(h_pixel_size.x, -pixel_size.y);

packages/use-shader-fx/src/hooks/useNoise/index.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ export type NoiseParams = {
2323
warpDirection?: THREE.Vector2;
2424
/** strength of domain warping , default:8.0 */
2525
warpStrength?: number;
26+
/** you can get into the rhythm ♪ , default:null */
27+
beat?: number | null;
2628
};
2729

2830
export type NoiseObject = {
@@ -41,6 +43,7 @@ export const NOISE_PARAMS: NoiseParams = {
4143
warpOctaves: 2,
4244
warpDirection: new THREE.Vector2(2.0, 2.0),
4345
warpStrength: 8.0,
46+
beat: null,
4447
};
4548

4649
/**
@@ -80,7 +83,7 @@ export const useNoise = ({
8083
setUniform(material, "warpDirection", params.warpDirection!);
8184
setUniform(material, "warpStrength", params.warpStrength!);
8285

83-
setUniform(material, "uTime", clock.getElapsedTime());
86+
setUniform(material, "uTime", params.beat ?? clock.getElapsedTime());
8487

8588
return updateRenderTarget(gl);
8689
},

packages/use-shader-fx/src/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,4 @@ export { usePointer } from "./utils/usePointer";
2727
export { useResolution } from "./utils/useResolution";
2828
export { useSingleFBO } from "./utils/useSingleFBO";
2929
export { useCopyTexture } from "./utils/useCopyTexture";
30+
export { useBeat } from "./utils/useBeat";

0 commit comments

Comments
 (0)