webgl+three.js,学习笔记,调用摄像头,做成纹理

1、谷歌浏览器
(1)错误信息:Only secure origins are allowed (see: https://goo.gl/Y0ZkNV).(仅支持https访问)
(2)如果用localhost访问,没有这种错误

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() {
                // 环境光
                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标签,输出  
                    video.play(); // 向PeerConnection中加入需要发送的流

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


                texture = new THREE.VideoTexture( video );//可以直接把视频设为材质
                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>
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值