|
| 1 | +"use client"; |
| 2 | + |
| 3 | +import * 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"; |
| 12 | +import { useBeat, useFluid, usePointer } from "@/packages/use-shader-fx/src"; |
| 13 | +import { FxMaterial, FxMaterialProps } from "./FxMaterial"; |
| 14 | +import GUI from "lil-gui"; |
| 15 | +import { useGUI } from "@/utils/useGUI"; |
| 16 | +import { CONFIG as HomeConfig } from "../_home/Playground"; |
| 17 | +import { OrbitControls, useGLTF } from "@react-three/drei"; |
| 18 | +import CustomShaderMaterial from "three-custom-shader-material/vanilla"; |
| 19 | +import wobbleVertexShader from "./vert.glsl"; |
| 20 | +import wobbleFragmentShader from "./frag.glsl"; |
| 21 | +import { mergeVertices } from "three-stdlib"; |
| 22 | + |
| 23 | +extend({ FxMaterial }); |
| 24 | + |
| 25 | +const uniforms = { |
| 26 | + uTime: new THREE.Uniform(0), |
| 27 | + uPositionFrequency: new THREE.Uniform(0.5), |
| 28 | + uTimeFrequency: new THREE.Uniform(0.2), |
| 29 | + uStrength: new THREE.Uniform(0.9), |
| 30 | + uWarpPositionFrequency: new THREE.Uniform(0.2), |
| 31 | + uWarpTimeFrequency: new THREE.Uniform(0.2), |
| 32 | + uWarpStrength: new THREE.Uniform(0.2), |
| 33 | + uColorA: new THREE.Uniform(new THREE.Color("white")), |
| 34 | + uColorB: new THREE.Uniform(new THREE.Color("black")), |
| 35 | + uBaloon: new THREE.Uniform(0), |
| 36 | + uFx: new THREE.Uniform(new THREE.Texture()), |
| 37 | +}; |
| 38 | + |
| 39 | +export const Playground = () => { |
| 40 | + const { size, viewport, scene: rootScene, camera } = useThree(); |
| 41 | + |
| 42 | + // camera.position.set(0, 0, 2); |
| 43 | + |
| 44 | + // const { scene } = useGLTF( |
| 45 | + // "https://vazxmixjsiawhamofees.supabase.co/storage/v1/object/public/models/suzanne-high-poly/model.gltf" |
| 46 | + // ); |
| 47 | + |
| 48 | + const [updateFluid, setFluid, fluid] = useFluid({ size, dpr: viewport.dpr }); |
| 49 | + |
| 50 | + const mesh = useMemo(() => { |
| 51 | + const material = new CustomShaderMaterial({ |
| 52 | + // CSM |
| 53 | + baseMaterial: THREE.MeshPhysicalMaterial, |
| 54 | + vertexShader: wobbleVertexShader, |
| 55 | + fragmentShader: wobbleFragmentShader, |
| 56 | + uniforms: uniforms, |
| 57 | + silent: true, |
| 58 | + |
| 59 | + // MeshPhysicalMaterial |
| 60 | + metalness: 0.9, |
| 61 | + roughness: 0.5, |
| 62 | + color: "#ffffff", |
| 63 | + transmission: 0, |
| 64 | + ior: 1.5, |
| 65 | + thickness: 1.5, |
| 66 | + transparent: true, |
| 67 | + wireframe: false, |
| 68 | + }); |
| 69 | + const depthMaterial = new CustomShaderMaterial({ |
| 70 | + // CSM |
| 71 | + baseMaterial: THREE.MeshDepthMaterial, |
| 72 | + vertexShader: wobbleVertexShader, |
| 73 | + uniforms: uniforms, |
| 74 | + silent: true, |
| 75 | + |
| 76 | + // MeshDepthMaterial |
| 77 | + depthPacking: THREE.RGBADepthPacking, |
| 78 | + }); |
| 79 | + let geometry = new THREE.IcosahedronGeometry( |
| 80 | + 2.5, |
| 81 | + 50 |
| 82 | + ) as THREE.BufferGeometry; |
| 83 | + // let geometry = scene.children[0].geometry as THREE.BufferGeometry; |
| 84 | + |
| 85 | + geometry = mergeVertices(geometry); |
| 86 | + geometry.computeTangents(); |
| 87 | + |
| 88 | + const wobble = new THREE.Mesh(geometry, material); |
| 89 | + wobble.customDepthMaterial = depthMaterial; |
| 90 | + wobble.receiveShadow = true; |
| 91 | + wobble.castShadow = true; |
| 92 | + return wobble; |
| 93 | + }, []); |
| 94 | + |
| 95 | + const beat = useBeat(140); |
| 96 | + const updatePointer = usePointer(); |
| 97 | + |
| 98 | + const raycaster = useMemo(() => new THREE.Raycaster(), []); |
| 99 | + const rayCursor = useRef<THREE.Vector2 | null>(null); |
| 100 | + |
| 101 | + useFrame((props) => { |
| 102 | + const b = beat(props.clock); |
| 103 | + updateFluid(props); |
| 104 | + mesh.material.uniforms.uTime.value = b.beat; |
| 105 | + |
| 106 | + raycaster.setFromCamera(props.pointer, camera); |
| 107 | + const intersects = raycaster.intersectObject(mesh); |
| 108 | + if (intersects.length > 0) { |
| 109 | + const uv = intersects[0]?.uv as THREE.Vector2; |
| 110 | + if (!uv) return; |
| 111 | + rayCursor.current = uv.multiplyScalar(2).subScalar(1); |
| 112 | + } |
| 113 | + if (rayCursor.current) { |
| 114 | + mesh.material.uniforms.uFx.value = updateFluid(props, { |
| 115 | + pointerValues: updatePointer(rayCursor.current), |
| 116 | + }); |
| 117 | + } |
| 118 | + }); |
| 119 | + |
| 120 | + return ( |
| 121 | + <mesh> |
| 122 | + <ambientLight /> |
| 123 | + <directionalLight /> |
| 124 | + <OrbitControls /> |
| 125 | + <primitive object={mesh} position={[0, 0, 0]} /> |
| 126 | + </mesh> |
| 127 | + ); |
| 128 | +}; |
0 commit comments