|
| 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 | + useBeat, |
| 12 | +} from "@/packages/use-shader-fx/src"; |
| 13 | +import { FxMaterial, FxMaterialProps } from "./FxMaterial"; |
| 14 | +import { useFBX } from "@react-three/drei"; |
| 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: 0.8, |
| 35 | + saturation: 0.8, |
| 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.max(Math.random(), 0.2), |
| 47 | + Math.max(Math.random(), 0.2) |
| 48 | + ); |
| 49 | + CONFIG.colorStrata.laminateDetail = new THREE.Vector2( |
| 50 | + Math.random() * 8, |
| 51 | + Math.random() * 8 |
| 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.noiseIntensity = Math.random() * 10; |
| 63 | + }, |
| 64 | + save: () => {}, |
| 65 | +}; |
| 66 | + |
| 67 | +const setConfig = (key: "marble" | "colorStrata" | "hsv") => { |
| 68 | + return { |
| 69 | + ...CONFIG[key], |
| 70 | + }; |
| 71 | +}; |
| 72 | + |
| 73 | +// function FunKun() { |
| 74 | +// let fbx = useFBX("/funkun.fbx"); |
| 75 | +// return <primitive object={fbx} scale={0.1} />; |
| 76 | +// } |
| 77 | + |
| 78 | +export const Playground = () => { |
| 79 | + const ref = useRef<FxMaterialProps>(); |
| 80 | + |
| 81 | + useEffect(() => { |
| 82 | + CONFIG.random(); |
| 83 | + }, []); |
| 84 | + |
| 85 | + const { size, viewport } = useThree(); |
| 86 | + const [updateNoise, setNoise, { output: noise }] = useNoise({ |
| 87 | + size, |
| 88 | + dpr: viewport.dpr, |
| 89 | + }); |
| 90 | + const [updateColorStrata, setColorStrata, { output: colorStrata }] = |
| 91 | + useColorStrata({ size, dpr: viewport.dpr }); |
| 92 | + const [updateMarble, setMarble, { output: marble }] = useMarble({ |
| 93 | + size, |
| 94 | + dpr: viewport.dpr, |
| 95 | + }); |
| 96 | + const [updateHSV, setHSV, { output: hsv }] = useHSV({ |
| 97 | + size, |
| 98 | + dpr: viewport.dpr, |
| 99 | + }); |
| 100 | + |
| 101 | + setNoise({ |
| 102 | + scale: 1000, |
| 103 | + warpOctaves: 1, |
| 104 | + noiseOctaves: 1, |
| 105 | + fbmOctaves: 1, |
| 106 | + timeStrength: 1, |
| 107 | + }); |
| 108 | + |
| 109 | + setMarble({ |
| 110 | + ...setConfig("marble"), |
| 111 | + timeStrength: 0.5, |
| 112 | + }); |
| 113 | + |
| 114 | + setColorStrata({ |
| 115 | + ...setConfig("colorStrata"), |
| 116 | + timeStrength: new THREE.Vector2(0, 0), |
| 117 | + }); |
| 118 | + |
| 119 | + setHSV({ |
| 120 | + ...setConfig("hsv"), |
| 121 | + texture: colorStrata, |
| 122 | + }); |
| 123 | + |
| 124 | + const beting = useBeat(110); |
| 125 | + const hashMemo = useRef(0); |
| 126 | + |
| 127 | + useFrame((props) => { |
| 128 | + const { beat, hash } = beting(props.clock); |
| 129 | + if (hash !== hashMemo.current) { |
| 130 | + hashMemo.current = hash; |
| 131 | + CONFIG.random(); |
| 132 | + } |
| 133 | + updateNoise(props); |
| 134 | + updateColorStrata(props, { |
| 135 | + ...(setConfig("colorStrata") as any), |
| 136 | + }); |
| 137 | + updateHSV(props, { |
| 138 | + ...(setConfig("hsv") as any), |
| 139 | + }); |
| 140 | + updateMarble(props, { |
| 141 | + ...(setConfig("marble") as any), |
| 142 | + beat: beat, |
| 143 | + }); |
| 144 | + ref.current!.u_noiseIntensity = CONFIG.noiseIntensity; |
| 145 | + }); |
| 146 | + |
| 147 | + return ( |
| 148 | + // <mesh> |
| 149 | + // <ambientLight /> |
| 150 | + // <pointLight position={[10, 10, 10]} /> |
| 151 | + // <FunKun /> |
| 152 | + // </mesh> |
| 153 | + <mesh> |
| 154 | + <planeGeometry args={[2, 2]} /> |
| 155 | + <fxMaterial |
| 156 | + key={FxMaterial.key} |
| 157 | + u_noise={marble} |
| 158 | + u_grain={noise} |
| 159 | + u_colorStrata={hsv} |
| 160 | + ref={ref} |
| 161 | + /> |
| 162 | + </mesh> |
| 163 | + ); |
| 164 | +}; |
0 commit comments