JSFiddle - React, Tailwind, and code Playground
HTML
<!-- Import maps polyfill -->
<!-- Remove this when import maps will be widely supported -->
<script async src="https://unpkg.com/[email protected]/dist/es-module-shims.js"></script>
<script type="importmap">
{
"imports": {
"three": "https://unpkg.com/[email protected]/build/three.module.js",
"three/addons/": "https://unpkg.com/[email protected]/examples/jsm/"
}
}
</script>
<video id="video" autoplay loop crossOrigin="anonymous" webkit-playsinline style="display:none">
<source src="//cdn.rawgit.com/mrdoob/three.js/master/examples/textures/sintel.ogv" type='video/ogg; codecs="theora, vorbis"'>
<source src="//cdn.rawgit.com/mrdoob/three.js/master/examples/textures/sintel.mp4" type='video/mp4; codecs="avc1.42E01E, mp4a.40.2"'>
</video>
JavaScript
import * as THREE from 'three';
import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
let camera, scene, renderer, box;
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
const video = document.getElementById( 'video' );
const videoTexture = new THREE.VideoTexture( video );
videoTexture.minFilter = THREE.LinearFilter;
videoTexture.magFilter = THREE.LinearFilter;
//plane
const loader = new THREE.TextureLoader();
const texture = loader.load( 'https://i.imgur.com/sJwKYvZ.jpg' );
const geometry = new THREE.PlaneGeometry();
const material = new THREE.MeshBasicMaterial( { alphaMap: texture, alphaTest: 0.5, map: videoTexture } );
const mesh = new THREE.Mesh( geometry, material );
scene.add( mesh );
// box
const boxGeometry = new THREE.BoxGeometry( 0.2, 0.2, 0.2 );
const boxMaterial = new THREE.MeshNormalMaterial();
box = new THREE.Mesh( boxGeometry, boxMaterial );
box.position.z = - 1;
scene.add( box );
renderer = new THREE.WebGLRenderer( { antialias: true } );
renderer.setPixelRatio( window.devicePixelRatio );
renderer.setSize( window.innerWidth, window.innerHeight );
document.body.appendChild( renderer.domElement );
const controls = new OrbitControls( camera, renderer.domElement );
}
function animate() {
requestAnimationFrame( animate );
box.rotation.x += 0.01;
box.rotation.y += 0.02;
renderer.render( scene, camera );
}