Using Video as a Texture in Three JS
We have discussed the textures many times on our blog and covered many articles about them in Three.js, but we have always focused on an image texture instead of a video texture. You might want to use the video texture on a geometrical shape and simulate a kind of a 3D cinema or want to map a video inside of a skybox. This will give you infinite possibilities. In this tutorial, we will focus on how to map a video on a geometrical shape and then use this script to create more cool stuff along the way. We will definitely need to load the video in the index.html file created by the Vite plugin in the project directory.
Getting started from scratch:
We will get started with the main elements of a Three js scene, including the camera, the renderer, the scene, and the object. Before doing that, we use the Vite plugin to easily create all the folders and files you need to run the Three.js code. First off, create a folder in the directory of your projects by using the following commands:
mkdir VideoTextures
cd VideoTextures
Then, inside of the your project folder, create the necessary files and folders by
simply running the Vite plugin command:
npm create vite@latest
Then enter the name of the project. You can write the name of your project as the
name. And also the package (the name is arbitrary, and you can choose anything
you want). Then select vanilla as the framework and variant. After that, enter the
following commands in the terminal. Notice that here VideoTextures is the project
folder’s name, and thus, we have changed the directory to VideoTextures. The
name depends on the name you enter in the Vite plugin :
cd VideoTextures
npm install
Afterward, you can enter the JavaScript code you want to write in the main.js file. So,
we will enter the base or template code for running every project with an animating
object, such as a sphere. Also, do not forget to install the Three.js package library
every time you create a project:
npm install three
The code :
Now, enter the following script in the main.js file:
import * as THREE from 'three';
import { Mesh } from 'three';
import { VideoTexture } from 'three';
import {OrbitControls} "/node_modules/three/examples/jsm /controls/OrbitControls.js";
import Stats from "/node_modules/three/examples/jsm/libs/stats.module.js";
const scene = new THREE.Scene();
const camera = new THREE.PerspectiveCamera(75, innerWidth /innerHeight , 0.1, 1000);
const renderer = new THREE.WebGLRenderer({
antialias : true
});
renderer.setSize(innerWidth, innerHeight);
document.body.appendChild(renderer.domElement);
const video = document.getElementById( 'video' );
const videoTexture = new THREE.VideoTexture( video );
//creating a sphere
const geometry = new THREE.BoxGeometry(8,8,8);
const material = new THREE.MeshBasicMaterial({
color:0x3268F8,
map : new THREE.VideoTexture( video ),
side : THREE.FrontSide,
toneMapped : false,
});
const sphere = new THREE.Mesh(geometry, material);
scene.add(sphere);
camera.position.z = 15;
function animate(){
requestAnimationFrame(animate);
renderer.render(scene,camera);
sphere.rotation.y += 0.003;
//videoTexture.needsUpdate = true;
};
animate();
We should also change the HTML as below:
Vite App
Notice that in the above .html script we only add the video tag in the body:
Do not forget to copy the video inside a folder called video and set the name of the
video as vid1. Now if we save the code, and enter the following command in the
terminal:
npm run dev
The above script will give the following result:And as you can see we have a video on each sides of the cube.
Conclusion
In this tutorial, we worked on a cool project: placing a video texture onto a cube. This project can open the door to so many possibilities. To make this happen, we first created a folder and pasted a video in it. Afterward, we added the video HTML tag in the index.html file and the Three.js code in the main.js file. This approach helped us develop some 3D cinema. In future tutorials, we will teach you how you can create an actual 3D cinema with 360 views.Download this Article in PDF format
Arashtad Custom Services
In Arashtad, we have gathered a professional team of developers who are working in fields such as 3D websites, 3D games, metaverses, and other types of WebGL and 3D applications as well as blockchain developemnet.