The live video feed from getUserMedia is not displayed on the three.js plane as expected

I am trying to incorporate a video from getUserMedia into a plane using three.js. However, the video does not appear as expected. When I deny access to the webcam, an image is loaded and displayed instead of the video. Loading and displaying the image poses no issues. Can someone please point out where I might be going wrong?

// ----------------------------------------------
// custom variables

var PI=3.1415;

var WIDTH=window.innerWidth,
    HEIGHT=480;

var FOV=45,
    ASPECT=WIDTH/HEIGHT,
    NEAR=1,
    FAR=10000;

var camera,
    scene,
    renderer,
    video,
    backgroundTexture;

// ----------------------------------------------
// 

// onload function
function load() {
    init();
}

// ----------------------------------------------
// functions

function init() {   
    if(navigator.webkitGetUserMedia) {
        video = document.createElement('video');
        document.body.appendChild(video);
        navigator.webkitGetUserMedia({video:true},function(stream) {
                video.src=window.webkitURL.createObjectURL(stream);
                //video.src=webkitURL.createObjectURL(stream);

                video.autoplay=true;

                // create texture of the video stream
                backgroundTexture = new THREE.Texture(video);

                setupScene();
            },
            function(error){
                console.log("Faild to get a stream due to", error)
                loadAlternativlyBackground();
                setupScene();
        });


    }
    else {
        loadAlternativlyBackground();
        setupScene();
    }
}

function setupScene() {
    scene = new THREE.Scene();

    camera = new THREE.PerspectiveCamera( FOV, ASPECT, NEAR, FAR );
    camera.position.z = 500;

    //scene.add(new THREE.AmbientLight(0x555555));

    scene.add(camera);

    var material = new THREE.MeshBasicMaterial({map:backgroundTexture});

    var plane = new THREE.Mesh(new THREE.PlaneGeometry(WIDTH,HEIGHT),material);

    plane.rotation.x=PI/2;

    plane.material.depthTest = false;
    plane.material.depthWrite = false;

    scene.add(plane);

    renderer = new THREE.CanvasRenderer();
    renderer.setSize( WIDTH, HEIGHT );
    renderer.autoClear = false;
    document.body.appendChild( renderer.domElement );

    animate();
}

function animate() {
    requestAnimationFrame( animate );
    render();
}

function render() {
        if ( video.readyState === video.HAVE_ENOUGH_DATA ) {
            if ( backgroundTexture ) backgroundTexture.needsUpdate = true;
        }
    renderer.clear();
    renderer.render( scene, camera );
}

function loadAlternativlyBackground() {
    // load alternativly background
    backgroundTexture = THREE.ImageUtils.loadTexture('p/background.png');
}

Answer №1

Ensure to include the following code snippet in your render loop:

if ( video.readyState === video.HAVE_ENOUGH_DATA ) {

    if ( backgroundTexture ) backgroundTexture.needsUpdate = true;

}

Answer №2

Recently, I encountered a similar issue when working on a project and it was all because of the video size not being a power of two. I remember seeing warnings in the console of Chrome as well. You can read more about this bug here: http://code.google.com/p/chromium/issues/detail?id=100525

Could it be that you are facing the same bug?

To bypass this problem, what worked for me was rendering the video to a canvas and then using the canvas as the texture:

// Inside the init function
    canvas = $("canvas#videoCanvas")[0];
    canvasContext = canvas.getContext("2d");
// Within the render function
    canvasContext.clearRect(0, 0, canvas.width, canvas.height);
    canvasContext.drawImage(video, 0, 0, video.videoWidth, video.videoHeight);
// Instead of
    backgroundTexture = new THREE.Texture(video);
// Use
    backgroundTexture = new THREE.Texture(canvas);

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Having difficulty entering text into a "Search Input Field" that is a react component in testcafe

Struggling to input text in a "Type to search dropdown" that is a react component. While able to click on the component, typing any text seems to be an issue. Listed below is an example of the code: import { Selector } from 'testcafe'; test(&ap ...

Refreshing certain sections of a webpage without the need to refresh the entire page

If you want to understand better, it would be helpful if you could check out my website first at: The main part of the website is a stream from Own3D.tv displayed through an iframe (line 342). My objective is to have the ability to click on a specific str ...

Can the hexadecimal code for a particular color name be identified?

Similar Question: Javascript function to convert color names to hex codes Is there a way to determine the hexadecimal value of a named color that is currently being used by the browser? For example, I would like to achieve something similar ...

How to handle multiple formData input in NestJS controller

How can I create a controller in Nest.js that accepts two form-data inputs? Here is my Angular service code: public importSchema(file: File, importConfig: PreviewImportConfig): Observable<HttpEvent<SchemaParseResponse>> { const formData = ...

Issue Alert: Inconsistencies with Google Scripts spreadsheets

Objective I have been working on a script that will make consecutive calls to an API (with a JSON response) and input the data into a Spreadsheet. Issue: When I debug the script, everything runs smoothly without any major problems. However, when I try r ...

Can someone please guide me on how to transfer information from a material ui datagrid Row to a form input?

I need assistance. I have a table that holds user data and I want to create a functionality where clicking on the edit button opens a dialogue box with a form pre-filled with the user's initial data. However, I'm currently only able to pass the u ...

How to disable the onChange event in PrimeNG p-dropdown?

I'm currently utilizing PrimeNG's dropdown component. Each option in the list includes an icon that, when clicked, should trigger a specific method. Additionally, I need to execute another method when the onChange event of the dropdown is trigger ...

Creating a jQuery accordion: A step-by-step guide

I'm currently working on building a dynamic accordion using jQuery where only one element can be open at a time. When a new element is opened, the previous one should close. I've managed to make it open and hide the button when opening it, but I ...

Need help with resetting a value in an array when a button is clicked?

Using Tabulator to create a table, where clicking on a cell pushes the cell values to an array with initial value of '0'. The goal is to add a reset button that sets the values back to '0' when clicked. component.ts names = [{name: f ...

Choosing a radio button based on the stored value within a variable

When transferring a variable (active) from my route to EJS, I initially found it easy to simply display its value: <!-- Active Text Input--> <div class="form-outline mb-4"> <label for="active" class="form-label">Active</label> ...

Try using Vanilla JavaScript instead of jquery $("body").on for event delegation on child elements

How can I convert this jQuery code to Vanilla JavaScript? $( document ).ready(function() { $('body').on('click', '.f_click', function (e) { e.preventDefault(); alert("TEST"); }); }); My initi ...

Error encountered in Three.js: Import statement is not allowed outside a module context

Struggling to integrate a 3D design file (gltf) into a website using three.js, but encountering a persistent error: https://i.sstatic.net/QCQZC.png Below is the snippet of html/Javascript/CSS code causing the issue: <!DOCTYPE html> <html> ...

Exporting JSON data to CSV or XLS does not result in a saved file when using Internet Explorer

Presented below is the service I offer: angular.module('LBTable').service('exportTable', function () { function JSONToCSVConvertor(JSONData, ReportTitle, ShowLabel, fileName) { //If JSONData isn't an object, parse the ...

Despite my efforts, I am struggling to refresh the view after retrieving data from an AJAX call in AngularJS

I've been searching for a solution to this problem for quite some time now, but nothing seems to work for me. Recently, I started delving into Angular and managed to create a basic page with an ng-repeat function that iterates through an object. My ...

Having trouble retrieving a JSON Element in JavaScript using the Object's property

I am having trouble retrieving a specific JSON element from the following JSON structure: { "ACTION": "AA", "MESSAGE": "Customer: 30xxx Already Approved on 2017/01/01" } Even though I receive the data in JSON format, attempting to access data.ACTION or d ...

What steps can I take to avoid Vue.js overriding jQuery bindings within components?

After incorporating vue.js into an existing project and attaching the vue instance to the body tag with an ID of "app," everything seemed to be running smoothly. jQuery and Vue.js were cooperating well together. However, as soon as I started creating compo ...

Dealing with the error "Type 'date[]' is not assignable to type '[date?, date?]' in a React hook

I'm attempting to assign a date range but encountering an error that states: Type 'Date[]' is not assignable to type '[Date?, Date?]'. Types of property 'length' are incompatible. Type 'number' is not assignab ...

`Cannot recompile the `Product` model as it has already been compiled. Please try again

I attempted to reference my productSchema within my purchaseSchema but encountered the following error: OverwriteModelError: Cannot overwrite Product model once compiled. What steps can I take to resolve this issue? Here is my product schema: mongoose = ...

What is the best way to create a point within a mesh?

I have several meshes and I'd like to insert a point randomly within the confines of hexagon[0]. How can this be achieved? var Hexagon=new Array(); Hexagon[0] = new THREE.Mesh( HexagonExtrude[0],material[0] ); Hexagon[1] = new THREE.Mesh( HexagonExtr ...

The concept of using the `map` method within a

Hi there, I could use some assistance with a tricky issue I'm facing. My current task involves rendering a cart object that includes product names, prices, and quantities. Each product can have its own set of product options stored as an array of ob ...