Skip to content

Commit 5d3bbaa

Browse files
author
takuma-hmng8
committed
useDomSyncerの追加
1 parent c268260 commit 5d3bbaa

Some content is hidden

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

51 files changed

+307
-2422
lines changed

app/ShaderFx.tsx

+2-8
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,8 @@ import { Suspense, useState } from "react";
44
import { Canvas } from "@react-three/fiber";
55
import { Perf } from "r3f-perf";
66
import { PerformanceMonitor } from "@react-three/drei";
7-
import { Demo } from "./_demo";
87

9-
// import CreateShaderFx from "@/CreateShaderFx";
10-
11-
export const ShaderFx = () => {
8+
export const ShaderFx = ({ children }: { children: React.ReactNode }) => {
129
const [dpr, setDpr] = useState(1.0);
1310
return (
1411
<Canvas dpr={dpr}>
@@ -18,10 +15,7 @@ export const ShaderFx = () => {
1815
console.log(`dpr:${dpr}`);
1916
setDpr(Math.round((0.5 + 1.0 * factor) * 10) / 10);
2017
}}>
21-
<Suspense fallback={null}>
22-
{/* When creating fx, please rewrite to CreateShaderFx */}
23-
<Demo />
24-
</Suspense>
18+
<Suspense fallback={null}>{children}</Suspense>
2519
<Perf position={"bottom-left"} minimal={false} />
2620
</PerformanceMonitor>
2721
</Canvas>
File renamed without changes.

app/_demo/index.tsx app/_home/index.tsx

+2-2
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import {
1515

1616
extend({ FxTransparentMaterial });
1717

18-
export const Demo = () => {
18+
export const Home = () => {
1919
const updateGUI = useGUI(setGUI);
2020
const mainShaderRef = useRef<FxTransparentMaterialProps>();
2121

@@ -90,7 +90,7 @@ the simplest demo
9090
// import { useFrame, useThree } from "@react-three/fiber";
9191
// import { useFluid } from "@hmng8/use-shader-fx";
9292

93-
// export const Demo = () => {
93+
// export const Home = () => {
9494
// const ref = useRef<THREE.ShaderMaterial>(null);
9595
// const size = useThree((state) => state.size);
9696
// const dpr = useThree((state) => state.viewport.dpr);

app/domSyncer/DomSyncer.tsx

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import * as THREE from "three";
2+
import { useEffect, useLayoutEffect, useRef } from "react";
3+
import { useFrame, extend, useThree, useLoader } from "@react-three/fiber";
4+
import { FxMaterial, FxMaterialProps } from "@/utils/fxMaterial";
5+
import { useDomSyncer, useNoise } from "@/packages/use-shader-fx/src";
6+
7+
extend({ FxMaterial });
8+
9+
/*===============================================
10+
TODO*
11+
- 角丸
12+
- リサイズ
13+
- resolutionの調整
14+
===============================================*/
15+
16+
export const DomSyncer = ({ state }: { state: number }) => {
17+
const mainShaderRef = useRef<FxMaterialProps>();
18+
19+
const [momo] = useLoader(THREE.TextureLoader, ["momo.jpg"]);
20+
21+
const size = useThree((state) => state.size);
22+
const dpr = useThree((state) => state.viewport.dpr);
23+
24+
const [updateNoise] = useNoise({ size, dpr });
25+
26+
// TODO* dependencyを2引数に渡すようにする
27+
const [updateDomSyncer] = useDomSyncer({ size, dpr }, [state]);
28+
29+
const domArr = useRef<(HTMLElement | Element)[]>([]);
30+
useEffect(() => {
31+
if (state === 0) {
32+
domArr.current = [...document.querySelectorAll(".item")!];
33+
} else {
34+
domArr.current = [...document.querySelectorAll(".item2")!];
35+
}
36+
}, [state]);
37+
38+
useFrame((props) => {
39+
const noise = updateNoise(props);
40+
// TODO*ここにtextureとDOMをセットにした配列を渡す[texture, dom][]
41+
const tex = updateDomSyncer(props, {
42+
dom: domArr.current,
43+
texture: [...Array(domArr.current.length)].map((_, i) => noise),
44+
});
45+
const main = mainShaderRef.current;
46+
if (main) {
47+
main.u_fx = tex;
48+
}
49+
});
50+
51+
return (
52+
<mesh>
53+
<planeGeometry args={[2, 2]} />
54+
<fxMaterial key={FxMaterial.key} ref={mainShaderRef} />
55+
</mesh>
56+
);
57+
};

app/domSyncer/page.tsx

+72
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
"use client";
2+
3+
import { useState } from "react";
4+
import { ShaderFx } from "../ShaderFx";
5+
import { DomSyncer } from "./DomSyncer";
6+
7+
export default function Page() {
8+
const [domSwitch, setDomSwitch] = useState(0);
9+
return (
10+
<>
11+
<div
12+
style={{
13+
position: "fixed",
14+
top: 0,
15+
width: "100%",
16+
height: "100%",
17+
}}>
18+
<ShaderFx>
19+
<DomSyncer state={domSwitch} />
20+
</ShaderFx>
21+
</div>
22+
<button
23+
onClick={() => {
24+
setDomSwitch((prev) => (prev === 0 ? 1 : 0));
25+
}}
26+
style={{
27+
width: "200px",
28+
height: "200px",
29+
backgroundColor: "red",
30+
position: "fixed",
31+
bottom: 0,
32+
right: 0,
33+
}}>
34+
stateの切り替え
35+
</button>
36+
<div
37+
style={{
38+
display: "flex",
39+
flexWrap: "wrap",
40+
justifyContent: "center",
41+
gap: "40px",
42+
padding: "16px",
43+
}}>
44+
{domSwitch === 0 ? (
45+
<>
46+
{[...Array(4)].map((_, i) => (
47+
<div
48+
className="item"
49+
key={i}
50+
style={{
51+
width: "calc(50% - 40px)",
52+
height: "80vh",
53+
}}></div>
54+
))}
55+
</>
56+
) : (
57+
<>
58+
{[...Array(2)].map((_, i) => (
59+
<div
60+
className="item2"
61+
key={i}
62+
style={{
63+
width: "100%",
64+
height: "50vh",
65+
}}></div>
66+
))}
67+
</>
68+
)}
69+
</div>
70+
</>
71+
);
72+
}

app/layout.tsx

+1-9
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,7 @@ export default function RootLayout({
1919
}) {
2020
return (
2121
<html lang="en">
22-
<body
23-
className={playfair.className}
24-
style={{
25-
overflow: "hidden",
26-
height: "100svh",
27-
// background: "#f8f8f8",
28-
}}>
29-
{children}
30-
</body>
22+
<body className={playfair.className}>{children}</body>
3123
</html>
3224
);
3325
}

app/page.tsx

+7-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
1+
"use client";
2+
13
import { ShaderFx } from "./ShaderFx";
4+
import { Home } from "./_home";
25

3-
export default function Home() {
6+
export default function Page() {
47
return (
58
<div style={{ width: "100%", height: "100svh" }}>
6-
<ShaderFx />
9+
<ShaderFx>
10+
<Home />
11+
</ShaderFx>
712
<h1
813
style={{
914
fontSize: "10vw",

0 commit comments

Comments
 (0)