three.js webcam input

HTML

<script type="importmap">
	{
		"imports": {
			"three": "https://unpkg.com/three@0.160/build/three.module.js",
      "three/addons/": "https://unpkg.com/three@0.160/examples/jsm/"
		}
	}
</script>
<video id="video" style="display:none"></video>

CSS

body {
	margin: 0px;
}

JavaScript

import * as THREE from 'three';

let camera, scene, renderer, video;


init();
animate();

function init() {

    camera = new THREE.PerspectiveCamera( 70, window.innerWidth / window.innerHeight, 0.01, 10 );
    camera.position.z = 1;

    scene = new THREE.Scene();
    
    video = document.getElementById( 'video' );
    const texture = new THREE.VideoTexture( video );

    const geometry = new THREE.PlaneGeometry();
    const material = new THREE.MeshBasicMaterial( { map: texture } );

    const mesh = new THREE.Mesh( geometry, material );
    scene.add( mesh );
    
    renderer = new THREE.WebGLRenderer( { antialias: true } );
    renderer.setSize( window.innerWidth, window.innerHeight );
    renderer.setPixelRatio( window.devicePixelRatio );
    document.body.appendChild( renderer.domElement );
    
    initWebcamInput();

}

function initWebcamInput() {

	if ( navigator.mediaDevices && navigator.mediaDevices.getUserMedia ) {
  
    navigator.mediaDevices.getUserMedia( { video: true } ).then( stream => {
    
        video.src = window.URL.createObjectURL( stream );
        video.play();
        
    } );
    
   }
   
}

function animate() {

    requestAnimationFrame( animate );
    renderer.render( scene, camera );

}