A THREE.JS ShaderMaterial that removes a specified color (e.g. green screen) from a video or image texture.
Shader code by https://github.com/Mugen87 on THREE.js forum. Inspired by https://github.com/hawksley/Threex.chromakey
ChromaKeyMaterial.js is in /js
directory.
Code in /ts
directory is for the demo app.
Source code is vanilla JS. Typescipt types in /types
directory are autogenerated with tsc.
Video file is from MixKit
View a live demo.
import ChromaKeyMaterial from '../js/ChromaKeyMaterial';
// Assumes basic THREE.js camera/scene/renderer setup.
const heightAspectRatio = 9 / 16;
const geometry: THREE.BufferGeometry =
new THREE.PlaneGeometry(1, heightAspectRatio);
// 0x19ae31 is the green color to key out. The last three arguments are
// similarity, smoothness, and spill, respectively and are used to fine
// tune the color key.
const greenScreenMaterial = new ChromaKeyMaterial(
'./assets/myVideo.mp4', 0x19ae31, 1920, 1080, 0.159, 0.082, 0.214);
const plane = new THREE.Mesh(geometry, greenScreenMaterial);
scene.add(plane);
To run the demo in this package:
- Clone this repo
cd
into repo root- Install with
npm install
- Run demo server with
npm run dev
- Open http://localhost:1234 + browser Dev Tools.
- Use dat.gui controls to pause/play video and fine tune color key.