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>