当前位置: 首页 > news >正文

Three.js加载360全景图片/视频

Three.js加载360全景图片/视频

效果

请添加图片描述

原理

  • 将全景图片/视频作为texture引入到three.js场景中
  • 将贴图与球形网格模型融合,将球模型当做成环境容器使用
  • 处理视频时需要以dom为载体,加载与控制视频动作
  • 每次渲染时更新当前texture,以达到视频播放效果
  • 全景图片加载有球体与正方体两种模式,区别在于是加载单张图片还是多张图片

核心方法

      // 添加VR全景图const addVrPicture = async () => {// 创建贴图const loader = new THREE.TextureLoader();const texture = await loader.load('./img/vr.jpg');texture.wrapS = THREE.RepeatWrapping;texture.repeat.x = -1;// 创建球形载体const sphereGeometry = new THREE.SphereGeometry(200, 60, 40);const sphereMaterial = new THREE.MeshBasicMaterial({ map: texture, side: THREE.BackSide });const sphere = new THREE.Mesh(sphereGeometry, sphereMaterial);scene.add(sphere);};// 添加VR全景视频const addVrVideo = async () => {// 通过Dom引入并控制视频源const video = document.createElement('video');video.src = './video/vr.mp4';video.loop = true;video.muted = true;video.autoplay = true;// 创建视频贴图const texture = new THREE.VideoTexture(video);texture.minFilter = THREE.LinearFilter;// 创建球形载体const sphereGeometry = new THREE.SphereGeometry(200, 60, 40);const sphereMaterial = new THREE.MeshBasicMaterial({ map: texture, side: THREE.BackSide });const sphere = new THREE.Mesh(sphereGeometry, sphereMaterial);scene.add(sphere);// 添加动画序列animationList.push(() => {// 更新视频纹理// 播放视频video.play();if (video.readyState === video.HAVE_ENOUGH_DATA) {texture.needsUpdate = true;}});// 调整相机视角const point = new THREE.Vector3(200, 0, 0);camera.lookAt(point);};

完整代码

<html lang="en"><head><meta charset="UTF-8" /><meta name="viewport" content="width=device-width, initial-scale=1.0" /><title>Document</title><style>* {margin: 0;padding: 0;}</style></head><body><script type="module">import * as util from './js/util.js';import * as THREE from './node_modules/three/build/three.module.js';import { creatWallByPath } from './js/effect.js';const scene = util.initScene();const stats = util.initStats();const camera = util.initCamera(-1, 0, 0);const renderer = util.initRender();const controls = util.initOrbitControls(camera, renderer);util.windowReSize(renderer, camera);util.addAxisHelper(scene, 100);util.addAmbientLight(scene);util.addDirectionalLight(scene);// 动画序列,每个渲染周期执行const animationList = [];// 添加VR全景图const addVrPicture = async () => {// 创建贴图const loader = new THREE.TextureLoader();const texture = await loader.load('./img/vr.jpg');texture.wrapS = THREE.RepeatWrapping;texture.repeat.x = -1;// 创建球形载体const sphereGeometry = new THREE.SphereGeometry(200, 60, 40);const sphereMaterial = new THREE.MeshBasicMaterial({ map: texture, side: THREE.BackSide });const sphere = new THREE.Mesh(sphereGeometry, sphereMaterial);scene.add(sphere);};// 添加VR全景视频const addVrVideo = async () => {// 通过Dom引入并控制视频源const video = document.createElement('video');video.src = './video/vr.mp4';video.loop = true;video.muted = true;video.autoplay = true;// 创建视频贴图const texture = new THREE.VideoTexture(video);texture.minFilter = THREE.LinearFilter;// 创建球形载体const sphereGeometry = new THREE.SphereGeometry(200, 60, 40);const sphereMaterial = new THREE.MeshBasicMaterial({ map: texture, side: THREE.BackSide });const sphere = new THREE.Mesh(sphereGeometry, sphereMaterial);scene.add(sphere);// 添加动画序列animationList.push(() => {// 更新视频纹理// 播放视频video.play();if (video.readyState === video.HAVE_ENOUGH_DATA) {texture.needsUpdate = true;}});// 调整相机视角const point = new THREE.Vector3(200, 0, 0);camera.lookAt(point);};const main = async () => {// 添加VR图像await addVrPicture();// 添加VR视频// await addVrVideo();};// 渲染函数const render = () => {renderer.render(scene, camera);stats.update();animationList.forEach((callback) => callback());requestAnimationFrame(render);};window.onload = () => {main();render();};</script></body>
</html>
http://www.lryc.cn/news/181303.html

相关文章:

  • 北大硕士7年嵌入式学习经验分享
  • 华为鸿蒙手表开发之动态生成二维码
  • 2023-09-28 monetdb-databae的概念和作用-分析
  • 2024级199管理类联考之数学基础(上篇)
  • RFID技术引领汽车零部件加工新时代
  • python中使用matplotlib绘图
  • Qt Creator 使用技巧
  • 来看看双阶段目标检测算法趴
  • python利用matplotlib绘图,对于中文和负号不显示,显示方框“口口”完美解决办法!!
  • 【数组及指针经典笔试题解析】
  • Transformer学习-self-attention
  • Spring Boot:利用JPA进行数据库的增改
  • 列表的增删改查和遍历
  • 获取网卡上的IP、网关及DNS信息,获取最佳路由,遍历路由表中的条目(附源码)
  • 保姆级 -- Zookeeper超详解
  • 【通意千问】大模型GitHub开源工程学习笔记(2)--使用Qwen进行推理的示例代码解析,及transformers的库使用
  • 从0开始python学习-23.selenium 常见鼠标的操作
  • 电气基础——电源、变压器、接触器、断路器、线缆
  • 步力宝科技爆款产品定位,开创智能物联网新商业
  • 凉鞋的 Unity 笔记 105. 第一个通识:编辑-测试 循环
  • Bug:elementUI样式不起作用、Vue引入组件报错not found等(Vue+ElementUI问题汇总)
  • 【大麦小米学量化】使用文心一言AI编写股票量化交易策略代码(含演示代码和进阶演示)
  • 软考 系统架构设计师系列知识点之软件架构风格(1)
  • 轮询与中断
  • 使用docker完成minio服务部署扩容备份迁移生产实践文档
  • 管道-有名管道
  • 谷歌注册手机号码无法验证
  • C语言编译与链接过程详解
  • Qt信号和槽 定时器
  • zemax对称式目镜