简体   繁体   中英

2d Context vs WebGL for Rendering Video

I am currently using the CanvasRenderingContext2D.drawImage() to draw video coming from a RTC mediastream to a canvas. Unfortunately, this takes up considerable CPU resources.

Would it be more performant to do this using a WebGLRenderingContext ? (Hardware acceleration?) If yes, how exactly does one handle this, preferably without creating an in-between video element?

No need for 2d Context use WebGL for rendering video if you are already in 3d space.

"Unfortunately, this takes up considerable CPU resources" - I guest you are using special canvas to receive rtc media stream . If canvas is visible somewhere on body you double (cpu) job.

Example for video texture (this code is taken from visual-js project ) You will need a little adapt... See :

function VIDEO_TEXTURE (monitor_ ) {

    var ROOT = this;
    ROOT.LOADED = function() {};

    ROOT.video = document.getElementById( monitor_ );
    var DIV_CONTENT_STREAMS = document.getElementById( 'HOLDER_STREAMS' );

    ROOT.videoImage = document.createElement('canvas');
    ROOT.videoImage.id = monitor_ + "IMAGE_";
    ROOT.videoImage.setAttribute('width', '640px' );
    ROOT.videoImage.setAttribute('height', '480px' );
    DIV_CONTENT_STREAMS.appendChild(ROOT.videoImage);

    ROOT.videoImageContext = ROOT.videoImage.getContext( '2d' );
    ROOT.videoImageContext.fillStyle = '#0000FF';
    ROOT.videoImageContext.fillRect( 0, 0, ROOT.videoImage.width, ROOT.videoImage.height );

    ROOT.videoTexture = new THREE.Texture( ROOT.videoImage );
    ROOT.videoTexture.minFilter = THREE.LinearFilter;
    ROOT.videoTexture.magFilter = THREE.LinearFilter;

    ROOT.movieMaterial = new THREE.MeshBasicMaterial( {
        map: ROOT.videoTexture,
        overdraw: true,
        side: THREE.DoubleSide 
    });
    var movieGeometry = new THREE.PlaneGeometry( 1000, 1000, 1, 1 );
    ROOT.movieScreen = new THREE.Mesh( movieGeometry, ROOT.movieMaterial );

    ROOT.movieScreen.position.set(0, 500, 1000);
    scene.add(ROOT.movieScreen);

    ROOT.AUTO_UPDATE = function() {

        //ROOT.video.play();
        if ( ROOT.video.readyState === ROOT.video.HAVE_ENOUGH_DATA ) 
        {
            ROOT.videoImageContext.drawImage( ROOT.video, 0, 0, ROOT.videoImage.width, ROOT.videoImage.height );
            if ( ROOT.videoTexture ) 
                ROOT.videoTexture.needsUpdate = true;
        }

    };
    console.log('Video 3d canvas texture created.');
    PROGRAM.AUTO_UPDATE.push(ROOT);




}


// Usage : 

 VIDEO_MATERIAL = new VIDEO_TEXTURE(tag_element_video_rtc_remote) 

Try and see : https://maximumroulette.com/welcome/3d_slot/

webGL2 open source git project

Examples : https://maximumroulette.com/webgl2/examples.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM