Threejs——360°jpg全景环绕

Three——360°图片全景环绕

可以去这个网址找一张作为全景的图片HDRS
在这里插入图片描述
三个步骤:

  1. 创建一个圆
  2. 让相机视角在圆的内部
  3. 将圆贴上贴图
/*
 * @Author: SouthernWind 
 * @Date: 2023-06-14 16:38:59 
 * @Last Modified by: Mr.Jia
 * @Last Modified time: 2023-06-28 15:29:07
 */

<template>
  <div class="container" ref="container"></div>
</template>

<script setup>
import * as THREE from "three";
// 轨道
import { OrbitControls } from "three/examples/jsm/controls/OrbitControls";
import { GLTFLoader } from "three/addons/loaders/GLTFLoader.js";
import { GUI } from "three/addons/libs/lil-gui.module.min.js";
import { ref, reactive, onMounted } from "vue";
// 三个必备的参数
let scene, camera, renderer, controls;

onMounted(() => {
  let container = document.querySelector(".container");
  const { clientWidth, clientHeight } = container;
  console.log(clientHeight);
  // 首先需要获取场景,这里公共方法放在init函数中
  const init = () => {
    scene = new THREE.Scene();
    // 给相机设置一个背景
    scene.background = new THREE.Color(0xaaaaaa);
    camera = new THREE.PerspectiveCamera(
      90,
      clientWidth / clientHeight,
      1,
      3000
    );
    camera.position.set(200, 200, 200);
    camera.lookAt(0, 0, 0);

    // 渲染器
    renderer = new THREE.WebGLRenderer({
      antialias: true,
    });
    // 渲染多大的地方
    renderer.setSize(clientWidth, clientHeight);
    container.appendChild(renderer.domElement);
    addBox();
    console.log("查看当前屏幕设备像素比", window.devicePixelRatio);
  };
  init();
  function addBox() {
    const geometry = new THREE.SphereGeometry(2000, 500, 500);
    geometry.scale(-1, 1, 1);
    const texture = new THREE.TextureLoader().load(
      new URL(`../assets/img/7/hj1.jpg`, import.meta.url).href
    );
    const material = new THREE.MeshBasicMaterial({ map: texture });
    const mesh = new THREE.Mesh(geometry, material);
    scene.add(mesh);
  }

  // 相机控件
  const control = () => {
    controls = new OrbitControls(camera, renderer.domElement);
    controls.addEventListener("change", function () {});
  };
  control();

  // 光源
  const linght = () => {

  };
  linght();
  // 渲染循环
  const render = () => {
    requestAnimationFrame(render);
    renderer.render(scene, camera);
  };
  render();
  window.addEventListener("resize", () => {
    // 更新摄像头
    // camera.aspect = window.innerWidth / window.innerHeight;
    // 相机参数更新
    camera.updateProjectionMatrix();
    renderer.setSize(window.innerWidth, window.innerHeight);
  });
});
</script>

<style>
.container {
  width: 100%;
  height: 100vh;
  position: relative;
  z-index: 1;
  /* background: #ff5810; */
}
.btn {
  position: absolute;
  top: 0;
  left: 0;
  z-index: 99;
}
</style>


核心代码:

    const geometry = new THREE.SphereGeometry(2000, 500, 500);
    geometry.scale(-1, 1, 1);
    const texture = new THREE.TextureLoader().load(
      new URL(`../assets/img/7/hj1.jpg`, import.meta.url).href
    );
    const material = new THREE.MeshBasicMaterial({ map: texture });
    const mesh = new THREE.Mesh(geometry, material);
    scene.add(mesh);

geometry.scale(-1, 1, 1);这里的-1其实就是取反,看到内部的效果。
实际效果如下:
请添加图片描述

还有的方法可以通过正方体拼成六面形成一个360度全景效果
在这里插入图片描述
xyz代表的就是坐标位置
可以借助这个工具将一张图拆分成六面matheowis
在这里插入图片描述
选择拆分的格式
在这里插入图片描述

 var materials = [
        loadTexture( 'images/skybox/px.jpg' ), // right
        loadTexture( 'images/skybox/nx.jpg' ), // left
 
        /* pano生成的顶部图, 底部图, 在ps中先将py逆时针处理90度 */
        loadTexture( 'images/skybox/py.jpg' ), // top
 
        /* pano生成的顶部图, 底部图, 在ps中先将ny顺时针处理90度 */
        loadTexture( 'images/skybox/ny.jpg' ), // bottom
 
        loadTexture( 'images/skybox/pz.jpg' ), // back
        loadTexture( 'images/skybox/nz.jpg' )  // front
 
    ];
    mesh = new THREE.Mesh( new THREE.BoxGeometry( 300, 300, 300, 7, 7, 7 ), materials );
    mesh.scale.x = - 1;
    scene.add( mesh );
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Southern Wind

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值