2d Context vs WebGL for Rendering Video

亡梦爱人 提交于 2019-12-10 17:16:08

问题


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?


回答1:


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



来源:https://stackoverflow.com/questions/36690965/2d-context-vs-webgl-for-rendering-video

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!