11"use client" ;
22
33import * as THREE from "three" ;
4- import { useCallback , useEffect , useMemo , useRef } from "react" ;
5- import {
6- useFrame ,
7- useThree ,
8- extend ,
9- useLoader ,
10- Size ,
11- } from "@react-three/fiber" ;
4+ import { useEffect } from "react" ;
5+ import { useFrame , useThree , extend } from "@react-three/fiber" ;
126import {
137 useBeat ,
14- useFluid ,
15- usePointer ,
168 useCreateWobble3D ,
179 Wobble3DParams ,
1810 WOBBLE3D_PARAMS ,
11+ useWobble3D ,
1912} from "@/packages/use-shader-fx/src" ;
20- import { FxMaterial , FxMaterialProps } from "./FxMaterial" ;
13+ import { FxMaterial } from "./FxMaterial" ;
2114import GUI from "lil-gui" ;
2215import { useGUI } from "@/utils/useGUI" ;
23- import { CONFIG as HomeConfig } from "../_home/Playground" ;
24- import {
25- OrbitControls ,
26- useGLTF ,
27- Environment ,
28- MeshTransmissionMaterial ,
29- } from "@react-three/drei" ;
30- import CustomShaderMaterial from "three-custom-shader-material/vanilla" ;
31- import wobbleVertexShader from "./vert.glsl" ;
32- import wobbleFragmentShader from "./frag.glsl" ;
33- import { mergeVertices } from "three-stdlib" ;
16+ import { OrbitControls , Environment } from "@react-three/drei" ;
3417
3518extend ( { FxMaterial } ) ;
3619
37- const CONFIG : Wobble3DParams = structuredClone ( WOBBLE3D_PARAMS ) ;
20+ const CONFIG : Wobble3DParams = {
21+ ...structuredClone ( WOBBLE3D_PARAMS ) ,
22+ color0 : new THREE . Color ( 0x1adb91 ) ,
23+ color1 : new THREE . Color ( 0xdbff57 ) ,
24+ color2 : new THREE . Color ( 0xdf6bff ) ,
25+ color3 : new THREE . Color ( 0x9eaeff ) ,
26+ wobbleStrength : 0.8 ,
27+ wobbleTimeFrequency : 0.4 ,
28+ warpStrength : 0.2 ,
29+ colorMix : 0.6 ,
30+ chromaticAberration : 0.8 ,
31+ anisotropicBlur : 0.2 ,
32+ distortion : 2 ,
33+ distortionScale : 0.8 ,
34+ temporalDistortion : 0.3 ,
35+ } ;
36+
37+ const MATERIAL_CONFIG : THREE . MeshPhysicalMaterialParameters = {
38+ iridescence : 1 ,
39+ metalness : 0.0 ,
40+ roughness : 0.0 ,
41+ transmission : 2 ,
42+ thickness : 1 ,
43+ transparent : true ,
44+ } ;
45+
3846const setGUI = ( gui : GUI ) => {
3947 gui . addColor ( CONFIG , "color0" ) ;
4048 gui . addColor ( CONFIG , "color1" ) ;
@@ -46,7 +54,20 @@ const setGUI = (gui: GUI) => {
4654 gui . add ( CONFIG , "warpStrength" , 0 , 10 , 0.01 ) ;
4755 gui . add ( CONFIG , "warpPositionFrequency" , 0 , 10 , 0.01 ) ;
4856 gui . add ( CONFIG , "warpTimeFrequency" , 0 , 10 , 0.01 ) ;
57+ gui . add ( CONFIG , "wobbleShine" , 0 , 5 , 0.01 ) ;
58+ gui . add ( CONFIG , "samples" , 0 , 10 , 1 ) ;
4959 gui . add ( CONFIG , "colorMix" , 0 , 1 , 0.01 ) ;
60+ gui . add ( CONFIG , "chromaticAberration" , 0 , 10 , 0.01 ) ;
61+ gui . add ( CONFIG , "anisotropicBlur" , 0 , 10 , 0.01 ) ;
62+ gui . add ( CONFIG , "distortion" , 0 , 10 , 0.01 ) ;
63+ gui . add ( CONFIG , "distortionScale" , 0 , 10 , 0.01 ) ;
64+ gui . add ( CONFIG , "temporalDistortion" , 0 , 10 , 0.01 ) ;
65+ const mpm = gui . addFolder ( "MeshPhysicalMaterial" ) ;
66+ mpm . add ( MATERIAL_CONFIG , "iridescence" , 0 , 1 , 0.01 ) ;
67+ mpm . add ( MATERIAL_CONFIG , "metalness" , 0 , 1 , 0.01 ) ;
68+ mpm . add ( MATERIAL_CONFIG , "roughness" , 0 , 1 , 0.01 ) ;
69+ mpm . add ( MATERIAL_CONFIG , "transmission" , 0 , 10 , 0.01 ) ;
70+ mpm . add ( MATERIAL_CONFIG , "thickness" , 0 , 10 , 0.01 ) ;
5071 return gui ;
5172} ;
5273const setConfig = ( ) => {
@@ -55,210 +76,72 @@ const setConfig = () => {
5576 } as Wobble3DParams ;
5677} ;
5778
58- /*===============================================
59- TODO :
60- - geometryはbufferでなんでも許容する
61- - まずはMeshPhysicalMaterialで、シェーダをカスタムコンパイルする
62- - いまの機能全部
63- - colorは4色線形保管
64- - displacement
65- - これは頂点を操作する必要があるからあり
66- - depthMaterial
67- - たぶん陰がwobbleにならないのを、これで解決してる感じだな。hookから吐き出したい
68- - たぶんmaterialは何でも受け付けられる。
69- - csmと同様、ある種のmaterialの時にだけshaderを対応させればいい
70-
71- v mapMixっていうuniformのつくろっと
72- v デフォルトの色とカスタムしたカラーのmix率
73- - meshTransmissionMaterialの拡張版としよう
74- - なんか透過したときに、rgb shiftしてシャボン玉みたいにできる感じ
75-
76- - chromatic aberation
77- - anisotropicBlur
78- - distortion
79- - distortionScale
80- - temporalDistortion
81- ===============================================*/
82-
8379export const Playground = ( ) => {
8480 const updateGUI = useGUI ( setGUI ) ;
85- const { size, viewport, scene : rootScene , camera } = useThree ( ) ;
86- const [ funkun , funkunAlpha ] = useLoader ( THREE . TextureLoader , [
87- "/funkun.jpg" ,
88- "/funkun-alpha.jpg" ,
89- ] ) ;
90- const [ updateFluid , setFluid , { output : fluid } ] = useFluid ( {
91- size,
92- dpr : viewport . dpr ,
93- } ) ;
81+
82+ /*===============================================
83+ example of FBO
84+ ===============================================*/
85+ // const { size, viewport, camera } = useThree();
86+ // const [update, _, { output, scene }] = useWobble3D({
87+ // size,
88+ // dpr: viewport.dpr,
89+ // camera,
90+ // });
91+ // useEffect(() => {
92+ // const light = new THREE.AmbientLight(0xffffff, 2);
93+ // scene.add(light);
94+ // }, [scene]);
95+ // useFrame((props) => update(props));
96+
97+ /*===============================================
98+ example of primitive
99+ ===============================================*/
94100 const [ updateWobble , wobble ] = useCreateWobble3D ( {
95- scene : false ,
96- // geometry: new THREE.SphereGeometry(1, 32, 32),
97- // baseMaterial: THREE.MeshToonMaterial,
98- materialParameters : {
99- // metalness: 0.4,
100- // roughness: 0.5,
101- // color: "#ffffff",
102- transmission : 1.5 ,
103- // ior: 1.5,
104- thickness : 4 ,
105- transparent : true ,
106- // wireframe: false,
107- // side: THREE.DoubleSide,
108- map : fluid ,
109- } ,
101+ baseMaterial : THREE . MeshPhysicalMaterial ,
102+ materialParameters : { ...MATERIAL_CONFIG } ,
110103 } ) ;
111104
112105 useEffect ( ( ) => {
113- wobble . mesh . geometry = mergeVertices ( wobble . mesh . geometry ) ;
114- wobble . mesh . geometry . computeTangents ( ) ;
115- // wobble.mesh.customDepthMaterial = depthMaterial;
106+ wobble . mesh . customDepthMaterial = wobble . depthMaterial ;
116107 wobble . mesh . receiveShadow = true ;
117108 wobble . mesh . castShadow = true ;
118109 } , [ wobble ] ) ;
119110
120- const updatePointer = usePointer ( ) ;
121- const refPointer = useRef ( new THREE . Vector2 ( 0 , 0 ) ) ;
122- const handlePointerMove = ( e : any ) => {
123- if ( ! e ?. pointer ) {
124- return ;
125- }
126- refPointer . current = e . pointer ;
127- } ;
111+ const beat = useBeat ( 140 , "easeInOutBack" ) ;
128112
129113 useFrame ( ( props ) => {
130114 updateWobble ( props , {
131115 ...setConfig ( ) ,
132- // colorMix: 0,
133- } ) ;
134- updateFluid ( props , {
135- pointerValues : updatePointer ( refPointer . current ) ,
116+ beat : beat ( props . clock ) . beat ,
136117 } ) ;
118+ const mat = wobble . mesh . material as THREE . MeshPhysicalMaterial ;
119+ mat . iridescence = MATERIAL_CONFIG . iridescence ! ;
120+ mat . metalness = MATERIAL_CONFIG . metalness ! ;
121+ mat . roughness = MATERIAL_CONFIG . roughness ! ;
122+ mat . transmission = MATERIAL_CONFIG . transmission ! ;
123+ mat . thickness = MATERIAL_CONFIG . thickness ! ;
137124 updateGUI ( ) ;
138125 } ) ;
139126
140127 return (
141128 < mesh >
142- < ambientLight />
143- < directionalLight scale = { [ 2 , 2 , 2 ] } />
144- < OrbitControls />
145- < Environment preset = "warehouse" background = { true } />
146- < primitive
147- onPointerMove = { handlePointerMove }
148- object = { wobble . mesh }
149- position = { [ 0 , 0 , 0 ] }
129+ < directionalLight
130+ color = { "white" }
131+ position = { [ 0.25 , 2 , 3 ] }
132+ intensity = { 2 }
133+ castShadow
150134 />
151- { /* <icosahedronGeometry args={[2, 50]} />
152- <MeshTransmissionMaterial
153- transmission={1}
154- samples={4}
155- backside
156- thickness={4}
157- // chromaticAberration={10.5}
158- // anisotropicBlur={0}
159- // distortion={10}
160- distortionScale={10}
161- temporalDistortion={10}
162- /> */ }
135+ < OrbitControls />
136+ < Environment preset = "warehouse" background />
137+ < primitive object = { wobble . mesh } />
138+ < mesh receiveShadow position = { [ 0 , - 4 , - 6 ] } >
139+ < planeGeometry args = { [ 15 , 15 , 15 ] } />
140+ < meshStandardMaterial />
141+ </ mesh >
142+ { /* <OrbitControls />
143+ <planeGeometry args={[2, 2]} />
144+ <fxMaterial key={FxMaterial.key} u_fx={output} /> */ }
163145 </ mesh >
164146 ) ;
165147} ;
166-
167- // export const Playground = () => {
168- // const { size, viewport, scene: rootScene, camera } = useThree();
169-
170- // // camera.position.set(0, 0, 2);
171-
172- // // const funkun = useGLTF("/funkun.glb");
173- // // const model = useGLTF(
174- // // "https://vazxmixjsiawhamofees.supabase.co/storage/v1/object/public/models/suzanne-high-poly/model.gltf"
175- // // );
176-
177- // // console.log(model);
178-
179- // const [updateFluid, setFluid, fluid] = useFluid({ size, dpr: viewport.dpr });
180-
181- // const mesh = useMemo(() => {
182- // const material = new CustomShaderMaterial({
183- // // CSM
184- // baseMaterial: THREE.MeshPhysicalMaterial,
185- // vertexShader: wobbleVertexShader,
186- // fragmentShader: wobbleFragmentShader,
187- // uniforms: uniforms,
188- // silent: true,
189-
190- // // MeshPhysicalMaterial
191- // metalness: 0,
192- // roughness: 0,
193- // color: "#ffffff",
194- // transmission: 1,
195- // ior: 1.2,
196- // thickness: 1.5,
197- // transparent: true,
198- // wireframe: false,
199- // });
200- // const depthMaterial = new CustomShaderMaterial({
201- // // CSM
202- // baseMaterial: THREE.MeshDepthMaterial,
203- // vertexShader: wobbleVertexShader,
204- // uniforms: uniforms,
205- // silent: true,
206-
207- // // MeshDepthMaterial
208- // depthPacking: THREE.RGBADepthPacking,
209- // });
210- // let geometry = new THREE.IcosahedronGeometry(
211- // 2.5,
212- // 50
213- // ) as THREE.BufferGeometry;
214-
215- // geometry = mergeVertices(geometry);
216- // geometry.computeTangents();
217-
218- // const wobble = new THREE.Mesh(geometry, material);
219- // wobble.customDepthMaterial = depthMaterial;
220- // wobble.receiveShadow = true;
221- // wobble.castShadow = true;
222- // return wobble;
223- // }, []);
224-
225- // const beat = useBeat(140);
226- // const updatePointer = usePointer();
227-
228- // const refPointer = useRef(new THREE.Vector2(0, 0));
229- // const handlePointerMove = (e: any) =>
230- // (refPointer.current = e.uv.multiplyScalar(2).subScalar(1));
231-
232- // useFrame((props) => {
233- // const b = beat(props.clock);
234- // updateFluid(props);
235- // mesh.material.uniforms.uTime.value = b.beat;
236- // mesh.material.uniforms.uFx.value = updateFluid(props, {
237- // pointerValues: updatePointer(refPointer.current),
238- // });
239- // });
240-
241- // return (
242- // <mesh>
243- // <ambientLight />
244- // <directionalLight />
245- // <OrbitControls />
246- // {/* <Environment preset="city" /> */}
247- // {/* <Environment
248- // preset="city"
249- // background={true} // can be true, false or "only" (which only sets the background) (default: false)
250- // blur={0} // blur factor between 0 and 1 (default: 0, only works with three 0.146 and up)
251- // // files={["px.png", "nx.png", "py.png", "ny.png", "pz.png", "nz.png"]}
252- // // path="/"
253- // // preset={null}
254- // // scene={undefined} // adds the ability to pass a custom THREE.Scene, can also be a ref
255- // // encoding={undefined} // adds the ability to pass a custom THREE.TextureEncoding (default: THREE.sRGBEncoding for an array of files and THREE.LinearEncoding for a single texture)
256- // /> */ }
257- // <primitive
258- // onPointerMove={handlePointerMove}
259- // object={mesh}
260- // position={[0, 0, 0]}
261- // />
262- // </mesh>
263- // );
264- // };
0 commit comments