|
| 1 | +import { useCallback, useMemo } from "react"; |
| 2 | +import * as THREE from "three"; |
| 3 | +import { useMesh } from "./useMesh"; |
| 4 | +import { RootState } from "@react-three/fiber"; |
| 5 | +import { useCamera } from "../../utils/useCamera"; |
| 6 | +import { useSingleFBO } from "../../utils/useSingleFBO"; |
| 7 | +import { setUniform } from "../../utils/setUniforms"; |
| 8 | +import { useParams } from "../../utils/useParams"; |
| 9 | +import { HooksProps, HooksReturn } from "../types"; |
| 10 | + |
| 11 | +export type CosPaletteParams = { |
| 12 | + /** color1, default:rgb(50%, 50%, 50%) */ |
| 13 | + color1?: THREE.Color; |
| 14 | + /** color2, default:rgb(50%, 50%, 50%) */ |
| 15 | + color2?: THREE.Color; |
| 16 | + /** color3, default:rgb(100%, 100%, 100%) */ |
| 17 | + color3?: THREE.Color; |
| 18 | + /** color4, default:rgb(0%, 10%, 20%) */ |
| 19 | + color4?: THREE.Color; |
| 20 | + /** texture to be used as a palette */ |
| 21 | + texture?: THREE.Texture; |
| 22 | + /** weight of the rgb, default:THREE.Vector3(1.0,0.0,0.0) */ |
| 23 | + rgbWeight?: THREE.Vector3; |
| 24 | +}; |
| 25 | + |
| 26 | +export type ColorPaletteObject = { |
| 27 | + scene: THREE.Scene; |
| 28 | + material: THREE.Material; |
| 29 | + camera: THREE.Camera; |
| 30 | + renderTarget: THREE.WebGLRenderTarget; |
| 31 | + output: THREE.Texture; |
| 32 | +}; |
| 33 | + |
| 34 | +export const COSPALETTE_PARAMS: CosPaletteParams = { |
| 35 | + texture: new THREE.Texture(), |
| 36 | + color1: new THREE.Color().set(0.5,0.5,0.5), |
| 37 | + color2: new THREE.Color().set(0.5,0.5,0.5), |
| 38 | + color3: new THREE.Color().set(1,1,1), |
| 39 | + color4: new THREE.Color().set(0,0.1,0.2), |
| 40 | + rgbWeight: new THREE.Vector3(0.299,0.587,0.114), |
| 41 | +}; |
| 42 | + |
| 43 | +/** |
| 44 | + * @link https://github.com/takuma-hmng8/use-shader-fx#usage |
| 45 | + */ |
| 46 | +export const useCosPalette = ({ |
| 47 | + size, |
| 48 | + dpr, |
| 49 | + samples = 0, |
| 50 | +}: HooksProps): HooksReturn<CosPaletteParams, ColorPaletteObject> => { |
| 51 | + const scene = useMemo(() => new THREE.Scene(), []); |
| 52 | + const material = useMesh(scene); |
| 53 | + const camera = useCamera(size); |
| 54 | + const [renderTarget, updateRenderTarget] = useSingleFBO({ |
| 55 | + scene, |
| 56 | + camera, |
| 57 | + size, |
| 58 | + dpr, |
| 59 | + samples, |
| 60 | + }); |
| 61 | + |
| 62 | + const [params, setParams] = useParams<CosPaletteParams>(COSPALETTE_PARAMS); |
| 63 | + |
| 64 | + const updateFx = useCallback( |
| 65 | + (props: RootState, updateParams?: CosPaletteParams) => { |
| 66 | + const { gl } = props; |
| 67 | + |
| 68 | + updateParams && setParams(updateParams); |
| 69 | + |
| 70 | + setUniform(material, "uTexture", params.texture!); |
| 71 | + setUniform(material, "uColor1", params.color1!); |
| 72 | + setUniform(material, "uColor2", params.color2!); |
| 73 | + setUniform(material, "uColor3", params.color3!); |
| 74 | + setUniform(material, "uColor4", params.color4!); |
| 75 | + setUniform(material, "uRgbWeight", params.rgbWeight!); |
| 76 | + |
| 77 | + return updateRenderTarget(gl); |
| 78 | + }, |
| 79 | + [updateRenderTarget, material, setParams, params] |
| 80 | + ); |
| 81 | + |
| 82 | + return [ |
| 83 | + updateFx, |
| 84 | + setParams, |
| 85 | + { |
| 86 | + scene: scene, |
| 87 | + material: material, |
| 88 | + camera: camera, |
| 89 | + renderTarget: renderTarget, |
| 90 | + output: renderTarget.texture, |
| 91 | + }, |
| 92 | + ]; |
| 93 | +}; |
0 commit comments