Three.js 视频地板教程

Three.js 视频地板教程 视频地板 ·Video Floor· ▶ 在线运行案例案例合集三维可视化功能案例threehub.cn开源仓库github地址https://github.com/z2586300277/three-cesium-examples400个案例代码:网盘链接你将学到什么OrbitControls 相机轨道交互requestAnimationFrame渲染循环与resize自适应效果说明本案例演示视频地板效果基于 WebGL 实现「视频地板」可视化效果附完整可运行源码核心用到 OrbitControls。建议先打开文首在线案例查看动态画面再对照下方源码逐步理解。核心概念OrbitControls轨道旋转缩放开enableDamping时每帧需controls.update()。实现步骤搭建 Scene / Camera / Renderer 与 OrbitControlsrAF 循环中 update 并 render代码要点import * as THREE from threeimport { OrbitControls } from three/examples/jsm/controls/OrbitControls.jsconst box document.getElementById(box)const scene new THREE.Scene()const camera new THREE.PerspectiveCamera(75, box.clientWidth / box.clientHeight, 0.1, 100000)camera.position.set(2, 2, 2)const renderer new THREE.WebGLRenderer({ antialias: true, alpha: true, logarithmicDepthBuffer: true })renderer.setPixelRatio(window.devicePixelRatio * 1.5)renderer.setSize(box.clientWidth, box.clientHeight)box.appendChild(renderer.domElement)const controls new OrbitControls(camera, renderer.domElement) controls.enableDamping truescene.add(new THREE.AxesHelper(100), new THREE.GridHelper(100, 10))const directionalLight new THREE.DirectionalLight(0xffffff, 3) directionalLight.position.set(5, 5, 5) scene.add(directionalLight)const ambientLight new THREE.AmbientLight(0xffffff, 2) scene.add(ambientLight)animate()function animate() {controls.update()requestAnimationFrame(animate)renderer.render(scene, camera)}window.onresize () {renderer.setSize(box.clientWidth, box.clientHeight)camera.aspect box.clientWidth / box.clientHeightcamera.updateProjectionMatrix()}async function createVideoPlane(url, width, height, positionY) {const video document.createElement(video) video.crossOrigin anonymous video.src url video.loop true video.muted true video.play() const texture new THREE.VideoTexture(video) const geometry new THREE.PlaneGeometry(width, height)const material new THREE.MeshStandardMaterial({ color: 0xffffff * Math.random(), // 随机颜色 alphaMap: texture, opecity: 0.5, // 透明度可调整 transparent: true, side: THREE.DoubleSide, })const mesh new THREE.Mesh(geometry, material) mesh.rotation.x -Math.PI / 2 mesh.position.set(0, positionY, 0) // 设置位置 scene.add(mesh) }createVideoPlane(FILE_HOST files/video/c1.mp4, 3, 3 , 0.01) createVideoPlane(FILE_HOST files/video/c2.mp4, 4, 4, 0) createVideoPlane(FILE_HOST files/video/c3.mp4, 5, 5, -0.01)完整源码GitHub小结本文提供视频地板完整 Three.js 源码与在线 Demo建议先运行案例再改 uniform/参数做二次实验更多 Three.js 实战案例见 three-cesium-examples 合集 与 GitHub 开源仓库