|
| 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 | +} from "@/packages/use-shader-fx/src"; |
| 12 | +import { FxMaterial, FxMaterialProps } from "./FxMaterial"; |
| 13 | +import GUI from "lil-gui"; |
| 14 | +import { useGUI } from "@/utils/useGUI"; |
| 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: 1.5, |
| 35 | + saturation: 0.4, |
| 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.random(), |
| 47 | + Math.random() |
| 48 | + ); |
| 49 | + CONFIG.colorStrata.laminateDetail = new THREE.Vector2( |
| 50 | + Math.random(), |
| 51 | + Math.random() |
| 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.hsv.brightness = Math.max(Math.random() * 3, 0.1); |
| 63 | + CONFIG.hsv.saturation = Math.random() * 3; |
| 64 | + CONFIG.noiseIntensity = Math.random() * 10; |
| 65 | + }, |
| 66 | + save: () => {}, |
| 67 | +}; |
| 68 | +const setGUI = (gui: GUI) => { |
| 69 | + gui.add(CONFIG, "random").name("Randomize"); |
| 70 | + gui.add(CONFIG, "save").name("Save"); |
| 71 | +}; |
| 72 | + |
| 73 | +const setConfig = (key: "marble" | "colorStrata" | "hsv") => { |
| 74 | + return { |
| 75 | + ...CONFIG[key], |
| 76 | + }; |
| 77 | +}; |
| 78 | + |
| 79 | +function useDownloadCanvas() { |
| 80 | + const { gl } = useThree(); |
| 81 | + const downloadImage = (filename = "image.png") => { |
| 82 | + const image = gl.domElement.toDataURL("image/png"); |
| 83 | + const link = document.createElement("a"); |
| 84 | + link.download = filename; |
| 85 | + link.href = image; |
| 86 | + link.click(); |
| 87 | + link.remove(); |
| 88 | + }; |
| 89 | + return downloadImage; |
| 90 | +} |
| 91 | + |
| 92 | +export const Playground = () => { |
| 93 | + const ref = useRef<FxMaterialProps>(); |
| 94 | + |
| 95 | + const saveImage = useDownloadCanvas(); |
| 96 | + useEffect(() => { |
| 97 | + CONFIG.save = saveImage; |
| 98 | + CONFIG.random(); |
| 99 | + }, [saveImage]); |
| 100 | + useGUI(setGUI); |
| 101 | + |
| 102 | + const { size, viewport } = useThree(); |
| 103 | + const [updateNoise, setNoise, { output: noise }] = useNoise({ |
| 104 | + size, |
| 105 | + dpr: viewport.dpr, |
| 106 | + }); |
| 107 | + const [updateColorStrata, setColorStrata, { output: colorStrata }] = |
| 108 | + useColorStrata({ size, dpr: viewport.dpr }); |
| 109 | + const [updateMarble, setMarble, { output: marble }] = useMarble({ |
| 110 | + size, |
| 111 | + dpr: viewport.dpr, |
| 112 | + }); |
| 113 | + const [updateHSV, setHSV, { output: hsv }] = useHSV({ |
| 114 | + size, |
| 115 | + dpr: viewport.dpr, |
| 116 | + }); |
| 117 | + |
| 118 | + setNoise({ |
| 119 | + scale: 1000, |
| 120 | + warpOctaves: 1, |
| 121 | + noiseOctaves: 1, |
| 122 | + fbmOctaves: 1, |
| 123 | + timeStrength: 0, |
| 124 | + }); |
| 125 | + |
| 126 | + setMarble({ |
| 127 | + ...setConfig("marble"), |
| 128 | + timeStrength: 0, |
| 129 | + }); |
| 130 | + |
| 131 | + setColorStrata({ |
| 132 | + ...setConfig("colorStrata"), |
| 133 | + timeStrength: new THREE.Vector2(0, 0), |
| 134 | + }); |
| 135 | + |
| 136 | + setHSV({ |
| 137 | + ...setConfig("hsv"), |
| 138 | + texture: colorStrata, |
| 139 | + }); |
| 140 | + |
| 141 | + useFrame((props) => { |
| 142 | + updateNoise(props); |
| 143 | + updateColorStrata(props, { |
| 144 | + ...(setConfig("colorStrata") as any), |
| 145 | + }); |
| 146 | + updateHSV(props, { |
| 147 | + ...(setConfig("hsv") as any), |
| 148 | + }); |
| 149 | + updateMarble(props, { |
| 150 | + ...(setConfig("marble") as any), |
| 151 | + }); |
| 152 | + ref.current!.u_noiseIntensity = CONFIG.noiseIntensity; |
| 153 | + }); |
| 154 | + |
| 155 | + return ( |
| 156 | + <mesh> |
| 157 | + <planeGeometry args={[2, 2]} /> |
| 158 | + <fxMaterial |
| 159 | + key={FxMaterial.key} |
| 160 | + u_noise={marble} |
| 161 | + u_grain={noise} |
| 162 | + u_colorStrata={hsv} |
| 163 | + ref={ref} |
| 164 | + /> |
| 165 | + </mesh> |
| 166 | + ); |
| 167 | +}; |
0 commit comments