three.js实现3D全景(vue2)

下包

npm i three

引入

import * as THREE from "three";
import { OrbitControls } from "three/examples/jsm/controls/OrbitControls.js";

调用方法(有部分数据要存在data里)

    createThree() {
      // 初始化场景
      this.scene = new THREE.Scene();

      // 初始化相机
      this.camera = new THREE.PerspectiveCamera(
        90,
        document.body.clientWidth / document.body.clientHeight,
        0.1,
        100
      );
      this.camera.position.set(0, 0, 0.1);
      // 初始化渲染器
      this.renderer = new THREE.WebGLRenderer();
      this.renderer.setSize(window.innerWidth, window.innerHeight);
      document
        .getElementById("container")
        .appendChild(this.renderer.domElement);
      // 镜头控制器
      var controls = new OrbitControls(this.camera, this.renderer.domElement);
      // //添加3D物体

      // 用立方体实现
      // this.useBox();
      // 用全景图实现
      this.useSphere();

      // 调用启动渲染帧;
      this.loop();
    },
    loop() {
      requestAnimationFrame(this.loop);
      this.renderer.render(this.scene, this.camera);
    },
    useBox() {
      var materials = [];
      var texture_left = new THREE.TextureLoader().load(
        require("../assets/scene_left.png")
      );
      materials.push(new THREE.MeshBasicMaterial({ map: texture_left }));
      var texture_right = new THREE.TextureLoader().load(
        require("../assets/scene_right.png")
      );
      materials.push(new THREE.MeshBasicMaterial({ map: texture_right }));

      var texture_top = new THREE.TextureLoader().load(
        require("../assets/scene_top.png")
      );
      materials.push(new THREE.MeshBasicMaterial({ map: texture_top }));

      var texture_bottom = new THREE.TextureLoader().load(
        require("../assets/scene_bottom.png")
      );
      materials.push(new THREE.MeshBasicMaterial({ map: texture_bottom }));

      var texture_front = new THREE.TextureLoader().load(
        require("../assets/scene_front.png")
      );
      materials.push(new THREE.MeshBasicMaterial({ map: texture_front }));

      var texture_back = new THREE.TextureLoader().load(
        require("../assets/scene_back.png")
      );
      materials.push(new THREE.MeshBasicMaterial({ map: texture_back }));

      var box = new THREE.Mesh(new THREE.BoxGeometry(1, 1, 1), materials);
      // 用线框模式大家可以看得清楚是个立方体而不是矩形
      box.material.wireframe = true;
      this.scene.add(box);
      // 把图片翻转
      box.geometry.scale(1, 1, -1);

      // 设置相机位置
      this.camera.position.set(0, 0, 0.01);
    },
    useSphere() {
      let sphereGeometry = new THREE.SphereGeometry(1, 50, 50);
      let texture = new THREE.TextureLoader().load(
        require("../assets/scene.jpeg")
      );
      let sphereMaterial = new THREE.MeshBasicMaterial({ map: texture });
      let sphere = new THREE.Mesh(sphereGeometry, sphereMaterial);
      // sphere.material.wireframe = true;
      this.scene.add(sphere);
      sphere.geometry.scale(1, 1, -1);
      this.camera.position.set(0, 0, 0.01);
    },

可以用两种方法:立方体(6张图)和球体(全景图)

都是把图贴到物体上,再把图片翻转,把相机移到物体中间即可。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
可以按照以下步骤来实现全景视频: 1. 安装three.jsvue-threejs插件: ``` npm install three vue-threejs --save ``` 2. 在Vue组件中引入three.jsvue-threejs插件: ```javascript import * as THREE from 'three' import { VueThreeJS, Object3D } from 'vue-threejs' ``` 3. 在组件中定义场景、相机和渲染器: ```javascript export default { components: { VueThreeJS }, data () { return { scene: new THREE.Scene(), camera: new THREE.PerspectiveCamera(90, window.innerWidth / window.innerHeight, 0.1, 1000), renderer: new THREE.WebGLRenderer() } } } ``` 4. 在mounted钩子中初始化渲染器并将其添加到DOM中: ```javascript mounted () { this.renderer.setSize(window.innerWidth, window.innerHeight) this.$refs.container.appendChild(this.renderer.domElement) } ``` 5. 加载全景视频并创建球体展示: ```javascript const texture = new THREE.VideoTexture(video) texture.minFilter = THREE.LinearFilter texture.format = THREE.RGBFormat const sphereGeometry = new THREE.SphereGeometry(500, 60, 40) const sphereMaterial = new THREE.MeshBasicMaterial({ map: texture }) const sphere = new THREE.Mesh(sphereGeometry, sphereMaterial) sphere.material.side = THREE.BackSide this.scene.add(sphere) ``` 6. 创建灯光: ```javascript const ambientLight = new THREE.AmbientLight(0xffffff) this.scene.add(ambientLight) const directionalLight = new THREE.DirectionalLight(0xffffff, 0.5) directionalLight.position.set(0, 1, 0).normalize() this.scene.add(directionalLight) ``` 7. 在animate函数中更新场景和相机: ```javascript animate () { requestAnimationFrame(this.animate) this.renderer.render(this.scene, this.camera) } ``` 完整代码示例: ```javascript <template> <div ref="container" style="width: 100%; height: 100%;"></div> </template> <script> import * as THREE from 'three' import { VueThreeJS, Object3D } from 'vue-threejs' export default { components: { VueThreeJS }, data () { return { scene: new THREE.Scene(), camera: new THREE.PerspectiveCamera(90, window.innerWidth / window.innerHeight, 0.1, 1000), renderer: new THREE.WebGLRenderer() } }, mounted () { this.renderer.setSize(window.innerWidth, window.innerHeight) this.$refs.container.appendChild(this.renderer.domElement) const video = document.createElement('video') video.src = 'path/to/video.mp4' video.autoplay = true video.loop = true const texture = new THREE.VideoTexture(video) texture.minFilter = THREE.LinearFilter texture.format = THREE.RGBFormat const sphereGeometry = new THREE.SphereGeometry(500, 60, 40) const sphereMaterial = new THREE.MeshBasicMaterial({ map: texture }) const sphere = new THREE.Mesh(sphereGeometry, sphereMaterial) sphere.material.side = THREE.BackSide this.scene.add(sphere) const ambientLight = new THREE.AmbientLight(0xffffff) this.scene.add(ambientLight) const directionalLight = new THREE.DirectionalLight(0xffffff, 0.5) directionalLight.position.set(0, 1, 0).normalize() this.scene.add(directionalLight) this.camera.position.set(0, 0, 0.1) this.animate() }, methods: { animate () { requestAnimationFrame(this.animate) this.renderer.render(this.scene, this.camera) } } } </script> ```
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值