|
| 1 | +import { useCallback, useMemo } from "react"; |
| 2 | +import * as THREE from "three"; |
| 3 | +import { useMesh } from "./useMesh"; |
| 4 | +import { useCamera } from "../../utils/useCamera"; |
| 5 | +import { RootState } from "@react-three/fiber"; |
| 6 | +import { useSingleFBO } from "../../utils/useSingleFBO"; |
| 7 | +import { setUniform } from "../../utils/setUniforms"; |
| 8 | +import { HooksProps, HooksReturn } from "../types"; |
| 9 | +import { useParams } from "../../utils/useParams"; |
| 10 | + |
| 11 | +export type MarbleParams = { |
| 12 | + pattern?: number; |
| 13 | + complexity?: number; |
| 14 | + complexityAttenuation?: number; |
| 15 | + iterations?: number; |
| 16 | + timeStrength?: number; |
| 17 | + scale?: number; |
| 18 | + /** you can get into the rhythm ♪ , default:null */ |
| 19 | + beat?: number | null; |
| 20 | +}; |
| 21 | + |
| 22 | +export type MarbleObject = { |
| 23 | + scene: THREE.Scene; |
| 24 | + material: THREE.Material; |
| 25 | + camera: THREE.Camera; |
| 26 | + renderTarget: THREE.WebGLRenderTarget; |
| 27 | + output: THREE.Texture; |
| 28 | +}; |
| 29 | + |
| 30 | +export const MARBLE_PARAMS: MarbleParams = { |
| 31 | + pattern: 0, |
| 32 | + complexity: 2, |
| 33 | + complexityAttenuation: 0.2, |
| 34 | + iterations: 8, |
| 35 | + timeStrength: 0.2, |
| 36 | + scale: 0.002, |
| 37 | + beat: null, |
| 38 | +}; |
| 39 | + |
| 40 | +/** |
| 41 | + * @link https://github.com/takuma-hmng8/use-shader-fx#usage |
| 42 | + */ |
| 43 | +export const useMarble = ({ |
| 44 | + size, |
| 45 | + dpr, |
| 46 | + samples = 0, |
| 47 | +}: HooksProps): HooksReturn<MarbleParams, MarbleObject> => { |
| 48 | + const scene = useMemo(() => new THREE.Scene(), []); |
| 49 | + const material = useMesh(scene); |
| 50 | + const camera = useCamera(size); |
| 51 | + const [renderTarget, updateRenderTarget] = useSingleFBO({ |
| 52 | + scene, |
| 53 | + camera, |
| 54 | + size, |
| 55 | + dpr, |
| 56 | + samples, |
| 57 | + }); |
| 58 | + |
| 59 | + const [params, setParams] = useParams<MarbleParams>(MARBLE_PARAMS); |
| 60 | + |
| 61 | + const updateFx = useCallback( |
| 62 | + (props: RootState, updateParams?: MarbleParams) => { |
| 63 | + const { gl, clock } = props; |
| 64 | + updateParams && setParams(updateParams); |
| 65 | + |
| 66 | + setUniform(material, "u_pattern", params.pattern!); |
| 67 | + setUniform(material, "u_complexity", params.complexity!); |
| 68 | + setUniform( |
| 69 | + material, |
| 70 | + "u_complexityAttenuation", |
| 71 | + params.complexityAttenuation! |
| 72 | + ); |
| 73 | + setUniform(material, "u_iterations", params.iterations!); |
| 74 | + setUniform(material, "u_timeStrength", params.timeStrength!); |
| 75 | + setUniform(material, "u_scale", params.scale!); |
| 76 | + |
| 77 | + setUniform(material, "u_time", params.beat ?? clock.getElapsedTime()); |
| 78 | + |
| 79 | + return updateRenderTarget(gl); |
| 80 | + }, |
| 81 | + [updateRenderTarget, material, setParams, params] |
| 82 | + ); |
| 83 | + |
| 84 | + return [ |
| 85 | + updateFx, |
| 86 | + setParams, |
| 87 | + { |
| 88 | + scene: scene, |
| 89 | + material: material, |
| 90 | + camera: camera, |
| 91 | + renderTarget: renderTarget, |
| 92 | + output: renderTarget.texture, |
| 93 | + }, |
| 94 | + ]; |
| 95 | +}; |
0 commit comments