three.js实战模拟VR全景视图

文章中使用到的案例图片都来源于:Humus - Textures

里面有很多免费的资源,可以直接下载,每个资源里面都提供6个不同方位的图片,我们通过threejs稍微处理一下,就能实现以下3D效果的场景了。

html 复制代码
<template>
    <div id="view-3D"></div>
</template>

<script setup>
import {  onMounted } from "vue";
import * as THREE from 'three'
import { OrbitControls } from 'three/examples/jsm/controls/OrbitControls'

onMounted(() => {
    init()
    renderScene()
})
// 定义场景
const scene = new THREE.Scene()
// 创建一个能看场景的相机
const camare = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000)
// 定义渲染器
const renderer = new THREE.WebGLRenderer()
// 轨道控制器
let orbitControls
const init = () => {
	// 给场景设置纹理贴图
    const texture = new THREE.CubeTextureLoader().setPath('/assets/').load([
        'posx.jpg',
        'negx.jpg',
        'posy.jpg',
        'negy.jpg',
        'posz.jpg',
        'negz.jpg'
    ])
    scene.background = texture
    // 设置相机的位置
    camare.position.set(0, 0, 300)
    // 设置相机看的方向
    camare.lookAt(scene.position)
	// 设置要渲染的场景大小
    renderer.setSize(window.innerWidth, window.innerHeight)
    // 把相机添加到场景中
    scene.add(camare)
    // 在页面元素上画出元素
    document.getElementById('view-3D').appendChild(renderer.domElement)
	// 创建轨道控制器,使鼠标前后左右上下方位移动
    orbitControls = new OrbitControls(camare, renderer.domElement)
}
const renderScene = () => {
	// 创建动画刷新机制  请求再次执行渲染函数render,渲染下一帧
    requestAnimationFrame(renderScene)
    // 更新控制器
    orbitControls.update()
    // 最后一步渲染场景
    renderer.render(scene, camare)
}
</script>
相关推荐
一只爱吃糖的小羊3 小时前
React 避坑指南:“闭包陷阱“
前端·javascript·react.js
web前端进阶者3 小时前
electron-vite报错Unexpected end of JSON input
javascript·electron·json
by__csdn3 小时前
大前端:定义、演进与实践全景解析
前端·javascript·vue.js·react.js·typescript·ecmascript·动画
带刺的坐椅3 小时前
Java 低代码平台的“动态引擎”:Liquor
java·javascript·低代码·groovy·liquor
徐同保3 小时前
Electron创建demo项目和打包
前端·javascript·electron
Sherry0073 小时前
从零开始理解 JavaScript Promise:彻底搞懂异步编程
前端·javascript·promise
哆啦A梦15883 小时前
商城后台管理系统 02,商品-页面添加-弹框添加
javascript·vue.js·elementui
AAA阿giao3 小时前
从“拼字符串”到“魔法响应”:一场数据驱动页面的奇幻进化之旅
前端·javascript·vue.js
donecoding3 小时前
解决 npm 发布 403 错误:全局配置 NPM Automation Token 完整指南
前端·javascript
SakuraOnTheWay4 小时前
拆解一个由 setTimeout 引发的“页面假死”悬案
前端·javascript