Skip to content

Commit 89f01c3

Browse files
author
takuma-hmng8
committed
gradation追加
1 parent 1e1e19d commit 89f01c3

File tree

39 files changed

+524
-87
lines changed

39 files changed

+524
-87
lines changed

app/ShaderFx.tsx

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,27 @@ import { Canvas } from "@react-three/fiber";
55
import { Perf } from "r3f-perf";
66
import { PerformanceMonitor } from "@react-three/drei";
77

8-
export const ShaderFx = ({ children }: { children: React.ReactNode }) => {
8+
export const ShaderFx = ({
9+
children,
10+
preserveDrawingBuffer = false,
11+
}: {
12+
children: React.ReactNode;
13+
preserveDrawingBuffer?: boolean;
14+
}) => {
915
const [dpr, setDpr] = useState(1.5);
1016
return (
11-
<Canvas dpr={dpr}>
17+
<Canvas dpr={dpr} gl={{ preserveDrawingBuffer: preserveDrawingBuffer }}>
1218
<PerformanceMonitor
1319
factor={1}
1420
onChange={({ factor }) => {
21+
if (preserveDrawingBuffer) {
22+
return;
23+
}
1524
console.log(`dpr:${dpr}`);
16-
// setDpr(Math.round((0.5 + 1.0 * factor) * 10) / 10);
25+
setDpr(Math.round((0.5 + 1.0 * factor) * 10) / 10);
1726
}}>
1827
<Suspense fallback={null}>{children}</Suspense>
19-
<Perf position={"bottom-left"} minimal={false} />
28+
{/* <Perf position={"bottom-left"} minimal={false} /> */}
2029
</PerformanceMonitor>
2130
</Canvas>
2231
);

app/domSyncer/DomSyncer.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import {
88
useFxTexture,
99
useCopyTexture,
1010
} from "@/packages/use-shader-fx/src";
11-
import { WaveParams } from "@/packages/use-shader-fx/src/fxs/utils/useWave";
11+
import { WaveParams } from "@/packages/use-shader-fx/src/fxs/effects/useWave";
1212
import gsap from "gsap";
1313

1414
extend({ FxMaterial });

app/gradation/FxMaterial.tsx

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
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_noise: THREE.Texture;
15+
u_colorStrata: THREE.Texture;
16+
u_grain: THREE.Texture;
17+
u_noiseIntensity: number;
18+
};
19+
20+
export const FxMaterial = shaderMaterial(
21+
{
22+
u_noise: new THREE.Texture(),
23+
u_colorStrata: new THREE.Texture(),
24+
u_grain: new THREE.Texture(),
25+
u_noiseIntensity: 1,
26+
},
27+
28+
`
29+
varying vec2 vUv;
30+
void main() {
31+
vUv = uv;
32+
gl_Position = vec4(position, 1.0);
33+
}
34+
`,
35+
fragment
36+
);

app/gradation/Playground.tsx

Lines changed: 167 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,167 @@
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+
useColorStrata,
9+
useMarble,
10+
useHSV,
11+
} from "@/packages/use-shader-fx/src";
12+
import { FxMaterial, FxMaterialProps } from "./FxMaterial";
13+
import GUI from "lil-gui";
14+
import { useGUI } from "@/utils/useGUI";
15+
16+
extend({ FxMaterial });
17+
18+
const CONFIG = {
19+
marble: {
20+
pattern: 10,
21+
complexity: 1.5,
22+
complexityAttenuation: 0.2,
23+
scale: 0.002,
24+
},
25+
colorStrata: {
26+
laminateLayer: 6,
27+
scale: 0.2,
28+
laminateInterval: new THREE.Vector2(0.55, 0.23),
29+
laminateDetail: new THREE.Vector2(0, 3.5),
30+
distortion: new THREE.Vector2(1.64, 4.22),
31+
colorFactor: new THREE.Vector3(0.6, 0.1, 0),
32+
},
33+
hsv: {
34+
brightness: 1.5,
35+
saturation: 0.4,
36+
},
37+
noiseIntensity: 2,
38+
random: () => {
39+
CONFIG.marble.pattern = Math.random() * 1000;
40+
CONFIG.marble.complexity = Math.random() * 10;
41+
CONFIG.marble.complexityAttenuation = Math.random();
42+
CONFIG.marble.scale = Math.random() * 0.001;
43+
CONFIG.colorStrata.laminateLayer = Math.random() * 100;
44+
CONFIG.colorStrata.scale = Math.max(Math.random(), 0.1);
45+
CONFIG.colorStrata.laminateInterval = new THREE.Vector2(
46+
Math.random(),
47+
Math.random()
48+
);
49+
CONFIG.colorStrata.laminateDetail = new THREE.Vector2(
50+
Math.random(),
51+
Math.random()
52+
);
53+
CONFIG.colorStrata.distortion = new THREE.Vector2(
54+
Math.random() * 10,
55+
Math.random() * 10
56+
);
57+
CONFIG.colorStrata.colorFactor = new THREE.Vector3(
58+
Math.random(),
59+
Math.random(),
60+
Math.random()
61+
);
62+
CONFIG.hsv.brightness = Math.max(Math.random() * 3, 0.1);
63+
CONFIG.hsv.saturation = Math.random() * 3;
64+
CONFIG.noiseIntensity = Math.random() * 10;
65+
},
66+
save: () => {},
67+
};
68+
const setGUI = (gui: GUI) => {
69+
gui.add(CONFIG, "random").name("Randomize");
70+
gui.add(CONFIG, "save").name("Save");
71+
};
72+
73+
const setConfig = (key: "marble" | "colorStrata" | "hsv") => {
74+
return {
75+
...CONFIG[key],
76+
};
77+
};
78+
79+
function useDownloadCanvas() {
80+
const { gl } = useThree();
81+
const downloadImage = (filename = "image.png") => {
82+
const image = gl.domElement.toDataURL("image/png");
83+
const link = document.createElement("a");
84+
link.download = filename;
85+
link.href = image;
86+
link.click();
87+
link.remove();
88+
};
89+
return downloadImage;
90+
}
91+
92+
export const Playground = () => {
93+
const ref = useRef<FxMaterialProps>();
94+
95+
const saveImage = useDownloadCanvas();
96+
useEffect(() => {
97+
CONFIG.save = saveImage;
98+
CONFIG.random();
99+
}, [saveImage]);
100+
useGUI(setGUI);
101+
102+
const { size, viewport } = useThree();
103+
const [updateNoise, setNoise, { output: noise }] = useNoise({
104+
size,
105+
dpr: viewport.dpr,
106+
});
107+
const [updateColorStrata, setColorStrata, { output: colorStrata }] =
108+
useColorStrata({ size, dpr: viewport.dpr });
109+
const [updateMarble, setMarble, { output: marble }] = useMarble({
110+
size,
111+
dpr: viewport.dpr,
112+
});
113+
const [updateHSV, setHSV, { output: hsv }] = useHSV({
114+
size,
115+
dpr: viewport.dpr,
116+
});
117+
118+
setNoise({
119+
scale: 1000,
120+
warpOctaves: 1,
121+
noiseOctaves: 1,
122+
fbmOctaves: 1,
123+
timeStrength: 0,
124+
});
125+
126+
setMarble({
127+
...setConfig("marble"),
128+
timeStrength: 0,
129+
});
130+
131+
setColorStrata({
132+
...setConfig("colorStrata"),
133+
timeStrength: new THREE.Vector2(0, 0),
134+
});
135+
136+
setHSV({
137+
...setConfig("hsv"),
138+
texture: colorStrata,
139+
});
140+
141+
useFrame((props) => {
142+
updateNoise(props);
143+
updateColorStrata(props, {
144+
...(setConfig("colorStrata") as any),
145+
});
146+
updateHSV(props, {
147+
...(setConfig("hsv") as any),
148+
});
149+
updateMarble(props, {
150+
...(setConfig("marble") as any),
151+
});
152+
ref.current!.u_noiseIntensity = CONFIG.noiseIntensity;
153+
});
154+
155+
return (
156+
<mesh>
157+
<planeGeometry args={[2, 2]} />
158+
<fxMaterial
159+
key={FxMaterial.key}
160+
u_noise={marble}
161+
u_grain={noise}
162+
u_colorStrata={hsv}
163+
ref={ref}
164+
/>
165+
</mesh>
166+
);
167+
};

app/gradation/main.frag

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

app/gradation/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 preserveDrawingBuffer>
15+
<Playground />
16+
</ShaderFx>
17+
</div>
18+
</div>
19+
);
20+
}

app/useBrush/Playground.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ export const Playground = () => {
8181
map: brush,
8282
});
8383

84-
const updatePointer = usePointer();
84+
const updatePointer = usePointer(0);
8585
const updateBeat = useBeat(157);
8686

8787
useFrame((props) => {

0 commit comments

Comments
 (0)