|
| 1 | +import * as React from "react"; |
| 2 | +import * as THREE from "three"; |
| 3 | +import { useFrame, useLoader, 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 { useMarble } from "../../packages/use-shader-fx/src"; |
| 8 | +import { |
| 9 | + MARBLE_PARAMS, |
| 10 | + MarbleParams, |
| 11 | +} from "../../packages/use-shader-fx/src/fxs/noises/useMarble"; |
| 12 | + |
| 13 | +extend({ FxMaterial }); |
| 14 | + |
| 15 | +const CONFIG: MarbleParams = structuredClone(MARBLE_PARAMS); |
| 16 | +const setGUI = (gui: GUI) => { |
| 17 | + gui.add(CONFIG, "pattern", 0, 1000, 1); |
| 18 | + gui.add(CONFIG, "complexity", 0, 10, 0.01); |
| 19 | + gui.add(CONFIG, "complexityAttenuation", 0, 2, 0.01); |
| 20 | + gui.add(CONFIG, "iterations", 0, 10, 1); |
| 21 | + gui.add(CONFIG, "timeStrength", 0, 2, 0.01); |
| 22 | + gui.add(CONFIG, "scale", 0, 1, 0.001); |
| 23 | +}; |
| 24 | +const setConfig = () => { |
| 25 | + return { |
| 26 | + ...CONFIG, |
| 27 | + } as MarbleParams; |
| 28 | +}; |
| 29 | + |
| 30 | +export const UseMarble = (args: MarbleParams) => { |
| 31 | + const updateGUI = useGUI(setGUI); |
| 32 | + const fxRef = React.useRef<FxMaterialProps>(); |
| 33 | + const { size, dpr } = useThree((state) => { |
| 34 | + return { size: state.size, dpr: state.viewport.dpr }; |
| 35 | + }); |
| 36 | + |
| 37 | + const [update, set, { output }] = useMarble({ size, dpr }); |
| 38 | + |
| 39 | + useFrame((props) => { |
| 40 | + update(props, { |
| 41 | + ...setConfig(), |
| 42 | + }); |
| 43 | + updateGUI(); |
| 44 | + }); |
| 45 | + |
| 46 | + return ( |
| 47 | + <mesh> |
| 48 | + <planeGeometry args={[2, 2]} /> |
| 49 | + <fxMaterial |
| 50 | + key={FxMaterial.key} |
| 51 | + u_fx={output} |
| 52 | + u_alpha={0.0} |
| 53 | + ref={fxRef} |
| 54 | + /> |
| 55 | + </mesh> |
| 56 | + ); |
| 57 | +}; |
0 commit comments