|
| 1 | +"use client"; |
| 2 | + |
| 3 | +import * as THREE from "three"; |
| 4 | +import { useEffect, useMemo } from "react"; |
| 5 | +import { useFrame, useThree, extend, useLoader } from "@react-three/fiber"; |
| 6 | +import { |
| 7 | + useCreateWobble3D, |
| 8 | + Wobble3DParams, |
| 9 | + WOBBLE3D_PARAMS, |
| 10 | + useCreateMorphParticles, |
| 11 | + MorphParticlesParams, |
| 12 | + MORPHPARTICLES_PARAMS, |
| 13 | +} from "@/packages/use-shader-fx/src"; |
| 14 | +import { FxMaterial } from "./FxMaterial"; |
| 15 | +import GUI from "lil-gui"; |
| 16 | +import { useGUI } from "@/utils/useGUI"; |
| 17 | +import { OrbitControls, Environment } from "@react-three/drei"; |
| 18 | + |
| 19 | +extend({ FxMaterial }); |
| 20 | + |
| 21 | +const WOBBLE_CONFIG: Wobble3DParams = { |
| 22 | + ...structuredClone(WOBBLE3D_PARAMS), |
| 23 | + color0: new THREE.Color(0x000000), |
| 24 | + color1: new THREE.Color(0x000000), |
| 25 | + color2: new THREE.Color(0x000000), |
| 26 | + color3: new THREE.Color(0x000000), |
| 27 | + wobbleStrength: 0.35, |
| 28 | + wobbleTimeFrequency: 0.2, |
| 29 | + warpStrength: 0.2, |
| 30 | + colorMix: 0.3, |
| 31 | + chromaticAberration: 0.05, |
| 32 | + anisotropicBlur: 0.2, |
| 33 | + distortion: 0.3, |
| 34 | + distortionScale: 0.5, |
| 35 | + temporalDistortion: 0.3, |
| 36 | +}; |
| 37 | + |
| 38 | +const MATERIAL_CONFIG: THREE.MeshPhysicalMaterialParameters = { |
| 39 | + iridescence: 0.1, |
| 40 | + metalness: 0.0, |
| 41 | + roughness: 0.0, |
| 42 | + transmission: 2, |
| 43 | + thickness: 1.2, |
| 44 | + transparent: true, |
| 45 | +}; |
| 46 | + |
| 47 | +const PARTICLE_CONFIG: MorphParticlesParams = { |
| 48 | + ...structuredClone(MORPHPARTICLES_PARAMS), |
| 49 | + blurAlpha: 0.01, |
| 50 | + blurRadius: 0.6, |
| 51 | + pointSize: 0.4, |
| 52 | + sizeRandomIntensity: 1, |
| 53 | + sizeRandomMax: 2.5, |
| 54 | + sizeRandomMin: 0.8, |
| 55 | + sizeRandomTimeFrequency: 1, |
| 56 | + color0: new THREE.Color(0x000000), |
| 57 | + color1: new THREE.Color(0x000000), |
| 58 | + color2: new THREE.Color(0x000000), |
| 59 | + color3: new THREE.Color(0x000000), |
| 60 | + wobbleStrength: 0.4, |
| 61 | + warpStrength: 2.6, |
| 62 | + wobblePositionFrequency: 0.4, |
| 63 | + wobbleTimeFrequency: 0.4, |
| 64 | + warpTimeFrequency: 0.2, |
| 65 | +}; |
| 66 | + |
| 67 | +const setGUI = (gui: GUI) => { |
| 68 | + const wobble = gui.addFolder("Wobble3D"); |
| 69 | + wobble.addColor(WOBBLE_CONFIG, "color0"); |
| 70 | + wobble.addColor(WOBBLE_CONFIG, "color1"); |
| 71 | + wobble.addColor(WOBBLE_CONFIG, "color2"); |
| 72 | + wobble.addColor(WOBBLE_CONFIG, "color3"); |
| 73 | + wobble.add(WOBBLE_CONFIG, "wobbleStrength", 0, 10, 0.01); |
| 74 | + wobble.add(WOBBLE_CONFIG, "wobblePositionFrequency", 0, 10, 0.01); |
| 75 | + wobble.add(WOBBLE_CONFIG, "wobbleTimeFrequency", 0, 10, 0.01); |
| 76 | + wobble.add(WOBBLE_CONFIG, "warpStrength", 0, 10, 0.01); |
| 77 | + wobble.add(WOBBLE_CONFIG, "warpPositionFrequency", 0, 10, 0.01); |
| 78 | + wobble.add(WOBBLE_CONFIG, "warpTimeFrequency", 0, 10, 0.01); |
| 79 | + wobble.add(WOBBLE_CONFIG, "wobbleShine", 0, 5, 0.01); |
| 80 | + wobble.add(WOBBLE_CONFIG, "samples", 0, 10, 1); |
| 81 | + wobble.add(WOBBLE_CONFIG, "colorMix", 0, 1, 0.01); |
| 82 | + wobble.add(WOBBLE_CONFIG, "chromaticAberration", 0, 10, 0.01); |
| 83 | + wobble.add(WOBBLE_CONFIG, "anisotropicBlur", 0, 10, 0.01); |
| 84 | + wobble.add(WOBBLE_CONFIG, "distortion", 0, 10, 0.01); |
| 85 | + wobble.add(WOBBLE_CONFIG, "distortionScale", 0, 10, 0.01); |
| 86 | + wobble.add(WOBBLE_CONFIG, "temporalDistortion", 0, 10, 0.01); |
| 87 | + |
| 88 | + const mpm = gui.addFolder("MeshPhysicalMaterial"); |
| 89 | + mpm.add(MATERIAL_CONFIG, "iridescence", 0, 1, 0.01); |
| 90 | + mpm.add(MATERIAL_CONFIG, "metalness", 0, 1, 0.01); |
| 91 | + mpm.add(MATERIAL_CONFIG, "roughness", 0, 1, 0.01); |
| 92 | + mpm.add(MATERIAL_CONFIG, "transmission", 0, 10, 0.01); |
| 93 | + mpm.add(MATERIAL_CONFIG, "thickness", 0, 10, 0.01); |
| 94 | + |
| 95 | + const particle = gui.addFolder("MorphParticles"); |
| 96 | + particle.add(PARTICLE_CONFIG, "blurAlpha", 0, 1, 0.01); |
| 97 | + particle.add(PARTICLE_CONFIG, "blurRadius", 0, 2, 0.01); |
| 98 | + particle.add(PARTICLE_CONFIG, "pointSize", 0.01, 2, 0.01); |
| 99 | + particle.addColor(PARTICLE_CONFIG, "color0"); |
| 100 | + particle.addColor(PARTICLE_CONFIG, "color1"); |
| 101 | + particle.addColor(PARTICLE_CONFIG, "color2"); |
| 102 | + particle.addColor(PARTICLE_CONFIG, "color3"); |
| 103 | + particle.add(PARTICLE_CONFIG, "wobbleStrength", 0, 10, 0.01); |
| 104 | + particle.add(PARTICLE_CONFIG, "wobblePositionFrequency", 0, 10, 0.01); |
| 105 | + particle.add(PARTICLE_CONFIG, "wobbleTimeFrequency", 0, 10, 0.01); |
| 106 | + particle.add(PARTICLE_CONFIG, "warpStrength", 0, 10, 0.01); |
| 107 | + particle.add(PARTICLE_CONFIG, "warpPositionFrequency", 0, 10, 0.01); |
| 108 | + particle.add(PARTICLE_CONFIG, "warpTimeFrequency", 0, 10, 0.01); |
| 109 | + particle.add(PARTICLE_CONFIG, "displacementIntensity", 0, 10, 0.01); |
| 110 | + particle.add(PARTICLE_CONFIG, "displacementColorIntensity", 0, 40, 0.01); |
| 111 | + particle.add(PARTICLE_CONFIG, "sizeRandomIntensity", 0, 10, 0.01); |
| 112 | + particle.add(PARTICLE_CONFIG, "sizeRandomTimeFrequency", 0, 3, 0.01); |
| 113 | + particle.add(PARTICLE_CONFIG, "sizeRandomMin", 0, 1, 0.01); |
| 114 | + particle.add(PARTICLE_CONFIG, "sizeRandomMax", 1, 2, 0.01); |
| 115 | + particle.add(PARTICLE_CONFIG, "divergence", -2, 2, 0.1); |
| 116 | + return gui; |
| 117 | +}; |
| 118 | +const setParticleConfig = () => { |
| 119 | + return { |
| 120 | + ...PARTICLE_CONFIG, |
| 121 | + } as MorphParticlesParams; |
| 122 | +}; |
| 123 | +const setWobbleConfig = () => { |
| 124 | + return { |
| 125 | + ...WOBBLE_CONFIG, |
| 126 | + } as Wobble3DParams; |
| 127 | +}; |
| 128 | + |
| 129 | +export const Playground = () => { |
| 130 | + useGUI(setGUI); |
| 131 | + const [noise] = useLoader(THREE.TextureLoader, ["/noise.jpg"]); |
| 132 | + const { size, viewport } = useThree(); |
| 133 | + const [updateWobble, wobble] = useCreateWobble3D({ |
| 134 | + baseMaterial: THREE.MeshPhysicalMaterial, |
| 135 | + geometry: useMemo(() => new THREE.IcosahedronGeometry(2.4, 10), []), |
| 136 | + materialParameters: MATERIAL_CONFIG, |
| 137 | + }); |
| 138 | + const [updateParticle, particles] = useCreateMorphParticles({ |
| 139 | + size, |
| 140 | + dpr: viewport.dpr, |
| 141 | + geometry: useMemo(() => new THREE.IcosahedronGeometry(0.8, 10), []), |
| 142 | + }); |
| 143 | + useEffect(() => { |
| 144 | + const particleMat = particles.points.material as THREE.ShaderMaterial; |
| 145 | + particleMat.blending = THREE.NormalBlending; |
| 146 | + }, [noise, updateParticle, particles.points.material]); |
| 147 | + |
| 148 | + useFrame((props) => { |
| 149 | + updateWobble(props, { |
| 150 | + ...setWobbleConfig(), |
| 151 | + }); |
| 152 | + const mat = wobble.mesh.material as THREE.MeshPhysicalMaterial; |
| 153 | + mat.iridescence = MATERIAL_CONFIG.iridescence!; |
| 154 | + mat.metalness = MATERIAL_CONFIG.metalness!; |
| 155 | + mat.roughness = MATERIAL_CONFIG.roughness!; |
| 156 | + mat.transmission = MATERIAL_CONFIG.transmission!; |
| 157 | + mat.thickness = MATERIAL_CONFIG.thickness!; |
| 158 | + updateParticle(props, { |
| 159 | + ...setParticleConfig(), |
| 160 | + alphaMap: noise, |
| 161 | + }); |
| 162 | + }); |
| 163 | + |
| 164 | + return ( |
| 165 | + <mesh> |
| 166 | + <OrbitControls /> |
| 167 | + <Environment files={"/snowpark.exr"} background={true} /> |
| 168 | + <primitive object={wobble.mesh} /> |
| 169 | + <primitive object={particles.points} /> |
| 170 | + </mesh> |
| 171 | + ); |
| 172 | +}; |
0 commit comments