|
| 1 | +import * as React from "react"; |
| 2 | +import * as THREE from "three"; |
| 3 | +import { useFrame, extend, useThree } from "@react-three/fiber"; |
| 4 | +import { FxMaterial, FxMaterialProps } from "../../utils/fxMaterial"; |
| 5 | +import GUI from "lil-gui"; |
| 6 | +import { useGUI } from "../../utils/useGUI"; |
| 7 | +import { useColorStrata, useNoise } from "../../packages/use-shader-fx/src"; |
| 8 | +import { |
| 9 | + COLORSTRATA_PARAMS, |
| 10 | + ColorStrataParams, |
| 11 | +} from "../../packages/use-shader-fx/src/hooks/useColorStrata"; |
| 12 | + |
| 13 | +extend({ FxMaterial }); |
| 14 | + |
| 15 | +const CONFIG: ColorStrataParams = structuredClone(COLORSTRATA_PARAMS); |
| 16 | +const setGUI = (gui: GUI) => { |
| 17 | + gui.add(CONFIG, "laminateLayer", 0, 10, 1); |
| 18 | + const laminateInterval = gui.addFolder("laminateInterval"); |
| 19 | + laminateInterval.add(CONFIG.laminateInterval!, "x", 0, 2, 0.01); |
| 20 | + laminateInterval.add(CONFIG.laminateInterval!, "y", 0, 2, 0.01); |
| 21 | + const laminateDetail = gui.addFolder("laminateDetail"); |
| 22 | + laminateDetail.add(CONFIG.laminateDetail!, "x", 0, 10, 0.1); |
| 23 | + laminateDetail.add(CONFIG.laminateDetail!, "y", 0, 10, 0.1); |
| 24 | + const distortion = gui.addFolder("distortion"); |
| 25 | + distortion.add(CONFIG.distortion!, "x", 0, 10, 0.01); |
| 26 | + distortion.add(CONFIG.distortion!, "y", 0, 10, 0.01); |
| 27 | + const colorFactor = gui.addFolder("colorFactor"); |
| 28 | + colorFactor.add(CONFIG.colorFactor!, "x", 0, 10, 0.01); |
| 29 | + colorFactor.add(CONFIG.colorFactor!, "y", 0, 10, 0.01); |
| 30 | + colorFactor.add(CONFIG.colorFactor!, "z", 0, 10, 0.01); |
| 31 | +}; |
| 32 | +const setConfig = () => { |
| 33 | + return { |
| 34 | + ...CONFIG, |
| 35 | + } as ColorStrataParams; |
| 36 | +}; |
| 37 | + |
| 38 | +export const UseColorStrata = (args: ColorStrataParams) => { |
| 39 | + const updateGUI = useGUI(setGUI); |
| 40 | + |
| 41 | + const fxRef = React.useRef<FxMaterialProps>(); |
| 42 | + const { size, dpr } = useThree((state) => { |
| 43 | + return { size: state.size, dpr: state.viewport.dpr }; |
| 44 | + }); |
| 45 | + const [updateColorStrata] = useColorStrata({ size, dpr }); |
| 46 | + |
| 47 | + useFrame((props) => { |
| 48 | + const fx = updateColorStrata(props, { |
| 49 | + ...setConfig(), |
| 50 | + }); |
| 51 | + fxRef.current!.u_fx = fx; |
| 52 | + updateGUI(); |
| 53 | + }); |
| 54 | + |
| 55 | + return ( |
| 56 | + <mesh> |
| 57 | + <planeGeometry args={[2, 2]} /> |
| 58 | + <fxMaterial key={FxMaterial.key} ref={fxRef} /> |
| 59 | + </mesh> |
| 60 | + ); |
| 61 | +}; |
| 62 | + |
| 63 | +export const UseColorStrataWithNoise = (args: ColorStrataParams) => { |
| 64 | + const updateGUI = useGUI(setGUI); |
| 65 | + const fxRef = React.useRef<FxMaterialProps>(); |
| 66 | + const { size, dpr } = useThree((state) => { |
| 67 | + return { size: state.size, dpr: state.viewport.dpr }; |
| 68 | + }); |
| 69 | + const [updateColorStrata] = useColorStrata({ size, dpr }); |
| 70 | + const [updateNoise] = useNoise({ size, dpr }); |
| 71 | + |
| 72 | + useFrame((props) => { |
| 73 | + const noise = updateNoise(props); |
| 74 | + |
| 75 | + const fx = updateColorStrata(props, { |
| 76 | + ...setConfig(), |
| 77 | + texture: noise, |
| 78 | + }); |
| 79 | + |
| 80 | + fxRef.current!.u_fx = fx; |
| 81 | + updateGUI(); |
| 82 | + }); |
| 83 | + |
| 84 | + return ( |
| 85 | + <mesh> |
| 86 | + <planeGeometry args={[2, 2]} /> |
| 87 | + <fxMaterial key={FxMaterial.key} ref={fxRef} /> |
| 88 | + </mesh> |
| 89 | + ); |
| 90 | +}; |
0 commit comments