|
| 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 ChromaKeyParams = { |
| 12 | + /** Process this texture with chroma key , default:THREE.Texture */ |
| 13 | + texture?: THREE.Texture; |
| 14 | + /** texture resolution , default:THREE.Vector2(0, 0) */ |
| 15 | + textureResolution?: THREE.Vector2; |
| 16 | + /** key color for chromakey processing , default: THREE.Color(0x00ff00) */ |
| 17 | + keyColor?: THREE.Color; |
| 18 | + /** If the similarity with the key color exceeds this value, it becomes transparent. , default: 0.01 */ |
| 19 | + similarity?: number; |
| 20 | + /** smoothness , default : 0.01 */ |
| 21 | + smoothness?: number; |
| 22 | + /** spill , default : 0.01 */ |
| 23 | + spill?: number; |
| 24 | + /** tone correction , default : THREE.Vector4(1.0, 1.0, 1.0, 1.0) */ |
| 25 | + color?: THREE.Vector4; |
| 26 | + /** contrast , default : 1.0 */ |
| 27 | + contrast?: number; |
| 28 | + /** brightness , default : 0.0 */ |
| 29 | + brightness?: number; |
| 30 | + /** gamma correction , default : 1.0 */ |
| 31 | + gamma?: number; |
| 32 | +}; |
| 33 | + |
| 34 | +export type ChromaKeyObject = { |
| 35 | + scene: THREE.Scene; |
| 36 | + material: THREE.Material; |
| 37 | + camera: THREE.Camera; |
| 38 | + renderTarget: THREE.WebGLRenderTarget; |
| 39 | + output: THREE.Texture; |
| 40 | +}; |
| 41 | + |
| 42 | +export const CHROMAKEY_PARAMS: ChromaKeyParams = { |
| 43 | + texture: new THREE.Texture(), |
| 44 | + textureResolution: new THREE.Vector2(0, 0), |
| 45 | + keyColor: new THREE.Color(0x00ff00), |
| 46 | + similarity: 0.01, |
| 47 | + smoothness: 0.01, |
| 48 | + spill: 0.01, |
| 49 | + color: new THREE.Vector4(1.0, 1.0, 1.0, 1.0), |
| 50 | + contrast: 1.0, |
| 51 | + brightness: 0.0, |
| 52 | + gamma: 1.0, |
| 53 | +}; |
| 54 | + |
| 55 | +/** |
| 56 | + * @link https://github.com/takuma-hmng8/use-shader-fx#usage |
| 57 | + */ |
| 58 | +export const useChromaKey = ({ |
| 59 | + size, |
| 60 | + dpr, |
| 61 | + samples = 0, |
| 62 | +}: HooksProps): HooksReturn<ChromaKeyParams, ChromaKeyObject> => { |
| 63 | + const scene = useMemo(() => new THREE.Scene(), []); |
| 64 | + const material = useMesh({ scene, size, dpr }); |
| 65 | + const camera = useCamera(size); |
| 66 | + const [renderTarget, updateRenderTarget] = useSingleFBO({ |
| 67 | + scene, |
| 68 | + camera, |
| 69 | + size, |
| 70 | + dpr, |
| 71 | + samples, |
| 72 | + }); |
| 73 | + |
| 74 | + const [params, setParams] = useParams<ChromaKeyParams>(CHROMAKEY_PARAMS); |
| 75 | + |
| 76 | + const updateFx = useCallback( |
| 77 | + (props: RootState, updateParams?: ChromaKeyParams) => { |
| 78 | + const { gl } = props; |
| 79 | + updateParams && setParams(updateParams); |
| 80 | + |
| 81 | + setUniform(material, "u_texture", params.texture!); |
| 82 | + setUniform(material, "u_textureResolution", params.textureResolution!); |
| 83 | + setUniform(material, "u_keyColor", params.keyColor!); |
| 84 | + setUniform(material, "u_similarity", params.similarity!); |
| 85 | + setUniform(material, "u_smoothness", params.smoothness!); |
| 86 | + setUniform(material, "u_spill", params.spill!); |
| 87 | + setUniform(material, "u_color", params.color!); |
| 88 | + setUniform(material, "u_contrast", params.contrast!); |
| 89 | + setUniform(material, "u_brightness", params.brightness!); |
| 90 | + setUniform(material, "u_gamma", params.gamma!); |
| 91 | + |
| 92 | + return updateRenderTarget(gl); |
| 93 | + }, |
| 94 | + [updateRenderTarget, material, setParams, params] |
| 95 | + ); |
| 96 | + |
| 97 | + return [ |
| 98 | + updateFx, |
| 99 | + setParams, |
| 100 | + { |
| 101 | + scene: scene, |
| 102 | + material: material, |
| 103 | + camera: camera, |
| 104 | + renderTarget: renderTarget, |
| 105 | + output: renderTarget.texture, |
| 106 | + }, |
| 107 | + ]; |
| 108 | +}; |
0 commit comments