用于渲染视频的2d Context与WebGL

时间:2016-04-18 10:08:33

标签: html5 video canvas webgl

我目前正在使用CanvasRenderingContext2D.drawImage()将来自RTC媒体流的视频绘制到画布上。不幸的是,这占用了相当多的CPU资源。

使用WebGLRenderingContext执行此操作会更高效吗? (硬件加速?)如果是,一个人如何处理这个问题,最好不要创建一个中间的视频元素?

1 个答案:

答案 0 :(得分:1)

如果您已经在3d空间中,则不需要2d Context使用WebGL来渲染视频。

"不幸的是,这占用了相当多的CPU资源和#34; - 我邀请您使用特殊画布接收rtc媒体流。如果画布在身体的某个地方可见,则需要双倍(cpu)作业。

视频纹理示例(此代码取自visual-js项目)您需要稍微调整一下......参见:

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) 

试试看: https://maximumroulette.com/welcome/3d_slot/

webGL2开源git project

示例: https://maximumroulette.com/webgl2/examples.html