Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Video as texture with Three.js

Tags:

three.js

I am trying to create a simple rectangle with a .mp4 video as texture. As per three.js documentation(http://threejs.org/docs/#Reference/Textures/Texture) this should be straight forward.

When I am putting link of video, all I am getting is a black colored box with no texture on it. I have tested code by replacing video with a jpg image and it works fine. Can someone please explain me what I am doing wrong.

I have already seen the examples in which video is played by first linking it to a video element and then copy the frames on a canvas. I want to try the direct way as mentioned in the three.js documentation.

like image 834
Gaurav Avatar asked Aug 22 '13 14:08

Gaurav


People also ask

How do I put 3 js videos together?

const gl = { camera: new THREE. PerspectiveCamera(75, innerWidth / innerHeight, 0.01, 1000), scene: new THREE. Scene(), renderer: new THREE. WebGLRenderer(), loader: new THREE.

Does Three Js use WebGL?

js. Three. js is a cross-browser JavaScript library and application programming interface (API) used to create and display animated 3D computer graphics in a web browser using WebGL.

What can you build with three Js?

Three. js is a powerful library for creating three-dimensional models and games. With just a few lines of JavaScript, you can create anything from simple 3D patterns to photorealistic, real-time scenes. You can build simple and complex 3D geometrics, animate and move objects through a lifelike scene, and more.

What is a scene in three Js?

Scenes allow you to set up what and where is to be rendered by three. js. This is where you place objects, lights and cameras.


2 Answers

Think of video as a sequence of images. So to "play" this video on your 3D object - you'll have to pass every single frame of that sequence to your material and then update that material.

Good place to start is here: https://github.com/mrdoob/three.js/wiki/Updates

And here: http://stemkoski.github.io/Three.js/Video.html

like image 147
Alex Under Avatar answered Nov 08 '22 19:11

Alex Under


Step 1: Add a video to your HTML and "hide" it:

<video id="video" playsinline webkit-playsinline muted loop autoplay width="320" height="240" src="some-video.mp4" style="display: none;"></video>

Step 2:

//Get your video element:
const video = document.getElementById('video');

//Create your video texture:
const videoTexture = new THREE.VideoTexture(video);
const videoMaterial =  new THREE.MeshBasicMaterial( {map: videoTexture, side: THREE.FrontSide, toneMapped: false} );
//Create screen
const screen = new THREE.PlaneGeometry(1, 1);
const videoScreen = new THREE.Mesh(screen, videoMaterial);
scene.add(videoScreen);
like image 37
Haos Avatar answered Nov 08 '22 18:11

Haos