使用three.js 实现vr全景图展示,复制即可用

1.实现效果

2.代码

1.npm安装three.js

复制代码
npm install three

2.引入three.js

复制代码
import * as THREE from 'three'
import { OrbitControls } from 'three/examples/jsm/controls/OrbitControls'

3.初始化模型

javascript 复制代码
init(val) {
                this.container = document.querySelector('.container')
                // 初始场景
                this.scene = new THREE.Scene()
                // 初始化相机
                this.camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000)
                // 相机位置
                this.camera.position.z = 5
                // 初始化渲染器
                this.renderer = new THREE.WebGLRenderer()
                this.renderer.setSize(window.innerWidth, window.innerHeight)
                // 添加球体方式一:对于.hdr图片

                // 添加球体方式二:对于.jpg .png图片
                const geometry = new THREE.SphereGeometry(5, 32, 32)
                const meterial = new THREE.MeshBasicMaterial()
                // 这里图片的地址val默认位置在public文件下方,所以测试的图片直接添加在public文件夹内部即可,val格式'./images/demo1.jpg'
                let texture = new THREE.TextureLoader().load(val)
                meterial.map = texture
                this.sphere = new THREE.Mesh(geometry, meterial)
                // 改变视角,进入球体内部
                this.sphere.geometry.scale(1, 1, -1)
                this.scene.add(this.sphere)
                // 添加控制器,控制视角
                const controls = new OrbitControls(this.camera, this.container)
                // controls.enableDamping = true
                // 容器上面添加渲染器
                this.container.appendChild(this.renderer.domElement)
                // 逐帧渲染
                const circleRender = () => {
                    this.renderer.render(this.scene, this.camera)
                    requestAnimationFrame(circleRender)
                }
                circleRender()
            },

4.展示结束销毁模型

javascript 复制代码
disposeThreeJS() {
                if (this.renderer) {
                    this.renderer.domElement.remove(); // 从DOM中移除渲染器元素
                    this.renderer.dispose();
                }
                if (this.sphere && this.sphere.geometry) {
                    this.sphere.geometry.dispose();
                }
                if (this.sphere && this.sphere.material) {
                    this.sphere.material.dispose();
                }
                if (this.controls) {
                    // OrbitControls没有直接的dispose方法,但可以通过设置引用为null来释放它
                    this.controls.dispose = () => { }; // 假设你有一个空方法来标记它已被"处理"
                    this.controls = null;
                }
                // 注意:场景和相机通常不需要显式销毁,除非你有大量的资源需要清理
                // 但为了完整性,你可以将它们设置为null
                this.scene = null;
                this.camera = null;
            },

5.测试图片

相关推荐
梦想的颜色2 小时前
TypeScript 完全指南(下):从类型体操到生产级配置
前端·javascript·typescript
比昨天多敲两行3 小时前
linux 线程概念与控制
java·开发语言·jvm
huaweichenai3 小时前
php 根据每个类型的抽签范围实现抽签功能
开发语言·php
codeejun4 小时前
每日一Go-73、云原生成本优化 —— 资源限制 & 指标驱动扩容
开发语言·云原生·golang
888CC++5 小时前
如何在 C 语言中进行程序调试?
前端·javascript·算法
就叫_这个吧5 小时前
Java注解、元注解、自定义注解定义及应用
java·开发语言·注解
Sam_Deep_Thinking5 小时前
聊聊Java中的of
java·开发语言·架构
kyriewen7 小时前
我招了一个“Prompt工程师”来写前端,结果项目差点崩了
前端·javascript·面试
小新1107 小时前
从零开始 Vue.js
前端·javascript·vue.js
Delicate8 小时前
JavaScript的“变脸”艺术:类型转换戏法大揭秘
javascript