100 Three.js使用VideoTexture实现视频Video更新纹理
Posted 暮志未晚Webgl
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了100 Three.js使用VideoTexture实现视频Video更新纹理相关的知识,希望对你有一定的参考价值。
案例查看地址:http://www.wjceo.com/blog/threejs/2018-05-10/163.html
简介
既然我们可以使用canvas
元素创建纹理,Three.js也给我们提供了可以实现video
创建纹理的方法。我们通过Three.js提供的VideoTexture
来实现创建视频纹理。
案例实现
- 首先还是需要有一个video标签,我们将video放到左下角:
<video id="video" autoplay controls >
<source src="/lib/textures/sintel.ogv" type='video/ogg; codecs="theora, vorbis"'>
<source src="/lib/textures/sintel.mp4" type='video/mp4; codecs="avc1.42E01E, mp4a.40.2"'>
</video>
- 然后我们通过video标签创建一个
VideoTexture
并赋值给material
的map
即可:
//获取到video对象
var video = document.querySelector("#video");
//通过video对象实例化纹理
var texture = new THREE.VideoTexture(video);
texture.wrapS = texture.wrapT = THREE.ClampToEdgeWrapping;
texture.minFilter = THREE.LinearFilter;
material = new THREE.MeshBasicMaterial( map: texture );
在上面,我们设置了wrapS
和wrapT
平铺,因为视频的格式一般都不会是标准的纹理尺寸。
案例代码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Threejs使用canvas更新纹理</title>
<style type="text/css">
html, body
margin: 0;
height: 100%;
canvas
display: block;
#video
position: fixed;
left:0;
bottom:0;
</style>
</head>
<body onload="draw();">
<video id="video" autoplay controls >
<source src="/lib/textures/sintel.ogv" type='video/ogg; codecs="theora, vorbis"'>
<source src="/lib/textures/sintel.mp4" type='video/mp4; codecs="avc1.42E01E, mp4a.40.2"'>
</video>
</body>
<script src="https://cdn.bootcss.com/three.js/91/three.min.js"></script>
<script src="/lib/js/controls/OrbitControls.js"></script>
<script src="https://cdn.bootcss.com/stats.js/r17/Stats.min.js"></script>
<script src="https://cdn.bootcss.com/dat-gui/0.7.1/dat.gui.min.js"></script>
<script src="/lib/js/Detector.js"></script>
<script>
var renderer, camera, scene, gui, light, stats, controls;
function initRender()
renderer = new THREE.WebGLRenderer(antialias: true);
renderer.setPixelRatio(window.devicePixelRatio);
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.setClearColor(0xeeeeee);
renderer.shadowMap.enabled = true;
//告诉渲染器需要阴影效果
document.body.appendChild(renderer.domElement);
function initCamera()
camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 0.1, 1000);
camera.position.set(0, 0, 15 );
function initScene()
scene = new THREE.Scene();
//初始化dat.GUI简化试验流程
function initGui()
//声明一个保存需求修改的相关数据的对象
gui =
;
var datGui = new dat.GUI();
//将设置属性添加到gui当中,gui.add(对象,属性,最小值,最大值)
function initLight()
scene.add(new THREE.AmbientLight(0x444444));
light = new THREE.DirectionalLight(0xffffff);
light.position.set(0, 20, 20 );
light.castShadow = true;
light.shadow.camera.top = 10;
light.shadow.camera.bottom = -10;
light.shadow.camera.left = -10;
light.shadow.camera.right = 10;
//告诉平行光需要开启阴影投射
light.castShadow = true;
scene.add(light);
function initModel()
//辅助工具
var helper = new THREE.AxesHelper(50);
scene.add(helper);
//添加立方体
var geometry = new THREE.BoxBufferGeometry( 10, 5, 5 );
//获取到video对象
var video = document.querySelector("#video");
//通过video对象实例化纹理
var texture = new THREE.VideoTexture(video);
texture.wrapS = texture.wrapT = THREE.ClampToEdgeWrapping;
texture.minFilter = THREE.LinearFilter;
material = new THREE.MeshBasicMaterial( map: texture );
scene.add(new THREE.Mesh(geometry, material));
//初始化性能插件
function initStats()
stats = new Stats();
document.body.appendChild(stats.dom);
function initControls()
controls = new THREE.OrbitControls(camera, renderer.domElement);
//设置控制器的中心点
//controls.target.set( 0, 5, 0 );
// 如果使用animate方法时,将此函数删除
//controls.addEventListener( 'change', render );
// 使动画循环使用时阻尼或自转 意思是否有惯性
controls.enableDamping = true;
//动态阻尼系数 就是鼠标拖拽旋转灵敏度
//controls.dampingFactor = 0.25;
//是否可以缩放
controls.enableZoom = true;
//是否自动旋转
controls.autoRotate = false;
controls.autoRotateSpeed = 0.5;
//设置相机距离原点的最远距离
controls.minDistance = 1;
//设置相机距离原点的最远距离
controls.maxDistance = 2000;
//是否开启右键拖拽
controls.enablePan = true;
function render()
//窗口变动触发的函数
function onWindowResize()
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
function animate()
//更新控制器
render();
//更新性能插件
stats.update();
controls.update();
renderer.render(scene, camera);
requestAnimationFrame(animate);
function draw()
//兼容性判断
if (!Detector.webgl) Detector.addGetWebGLMessage();
initGui();
initRender();
initScene();
initCamera();
initLight();
initModel();
initControls();
initStats();
animate();
window.onresize = onWindowResize;
</script>
</html>
以上是关于100 Three.js使用VideoTexture实现视频Video更新纹理的主要内容,如果未能解决你的问题,请参考以下文章
Three.js中的CubeGeometry VS BoxGeometry?