webgl+three.js,學(xué)習(xí)筆記,調(diào)用攝像頭,做成紋理(用視屏做紋理)

1、谷歌瀏覽器
(1)錯(cuò)誤信息:Only secure origins are allowed (see: https://goo.gl/Y0ZkNV).(僅支持https訪問(wèn))
(2)如果用localhost訪問(wèn),沒(méi)有這種錯(cuò)誤

2、ie/edge瀏覽器支持

3、只注解了主體部分,其他的再第一篇文章里有

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title>Three框架</title>
        <script src="js/three.js"></script>
        <style type="text/css">
            div#canvas-frame {
                border: none;
                cursor: pointer;
                width: 100%;
                height: 600px;
                background-color: #EEEEEE;
            }
            .booth {
                position: absolute;
            }
        </style>
        <script>
            var renderer;
            function initThree() {
                width = document.getElementById('canvas-frame').clientWidth;
                height = document.getElementById('canvas-frame').clientHeight;
                
                renderer = new THREE.WebGLRenderer({antialias : true});
                renderer.setSize(width, height);
                document.getElementById('canvas-frame').appendChild(renderer.domElement);
                renderer.setClearColor(0xFFFFFF, 1.0);
            }

            var camera;
            function initCamera() {
                camera = new THREE.PerspectiveCamera(45, width / height, 1, 10000);
                camera.position.z = 100;
            }

            var scene;
            function initScene() {
                scene = new THREE.Scene();
            }

            var light;
            function initLight() {
                // 環(huán)境光
                light = new THREE.AmbientLight(0xffffff);
                light.position.set(100, 100, 200);
                scene.add(light);
            }


            var mesh;
            var geometry;
            var material;
            
            function initObject() {
                scene = new THREE.Scene();
                geometry = new THREE.PlaneGeometry( 80, 50);
               
                var video = document.getElementById('video');
                var vendorUrl = window.URL || window.webkitURL;
                var texture;
                

                navigator.getMedia = navigator.getUserMedia ||
                                     navagator.webkitGetUserMedia ||
                                     navigator.mozGetUserMedia ||
                                     navigator.msGetUserMedia ||
                                     (navigator.mediaDevices && navigator.mediaDevices.getUserMedia);
                navigator.getMedia({
                    video: true,  // 攝像頭
                    audio: false  // 音頻
                }, function(strem){
                    //console.log(">>>>>>>>>>>"+strem); // 獲取到視頻流
                    video.src = vendorUrl.createObjectURL(strem); // 綁定到video標(biāo)簽,輸出  
                    video.play(); // 向PeerConnection中加入需要發(fā)送的流

                }, function(error) {
                    console.log(error);
                });


                texture = new THREE.VideoTexture( video );//可以直接把視頻設(shè)為材質(zhì)
                texture.minFilter = THREE.LinearFilter;
                texture.magFilter = THREE.LinearFilter;
                texture.format = THREE.RGBFormat;

                material = new THREE.MeshPhongMaterial( { map: texture , side: THREE.DoubleSide} );
                mesh = new THREE.Mesh(geometry,  material);
                scene.add(mesh);

            }



            function threeStart() {
                initObject();
                initThree();
                initCamera();
                initLight();
                animation();
            }

            function animation() {
                requestAnimationFrame(animation);
                renderer.render(scene, camera);
            }


        </script>
    </head>

    <body onload="threeStart();">
        <div id="canvas-frame">
            <div class="booth">
                <video id="video" width="400" height="300"></video>
                <!-- <canvas id='canvas' width='400' height='300'></canvas> -->
            </div>
        </div>
       
    </body>
</html>
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Spring Cloud為開(kāi)發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見(jiàn)模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,596評(píng)論 19 139
  • Android 自定義View的各種姿勢(shì)1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 179,119評(píng)論 25 709
  • afinalAfinal是一個(gè)android的ioc,orm框架 https://github.com/yangf...
    passiontim閱讀 15,879評(píng)論 2 45
  • 一、概念(載錄于:http://www.cnblogs.com/EricaMIN1987_IT/p/3837436...
    yuantao123434閱讀 8,742評(píng)論 6 152
  • 每個(gè)人都平等地每天擁有全新的24小時(shí)即86400秒,不多不少,日日清日日新,用不完沒(méi)得存,不夠用沒(méi)得多。如果時(shí)間賬...
    成舒閱讀 1,143評(píng)論 0 5

友情鏈接更多精彩內(nèi)容