当前位置: 代码网 > it编程>编程语言>Javascript > vue项目如何使用three.js实现vr360度全景图片预览

vue项目如何使用three.js实现vr360度全景图片预览

2024年05月18日 Javascript 我要评论
使用three.js实现vr360度全景图片预览当前demo使用的three.js为0.115.0版本项目中安装threenpm install three安装完成再组件如下导入import * as

使用three.js实现vr360度全景图片预览

当前demo使用的three.js为0.115.0版本

项目中安装three

npm install three

安装完成再组件如下导入

import * as three from 'three'

html文件中写入如下代码图片预览容器

代码如下:

<div ref="container" id="container"></div>

script脚本中如下代码

data() {
    return {
      sceneurl: '', // 需要预览的图片绝对路径
      camera: null,
      scene: null,
      renderer: null,
      isuserinteracting: false,
      onpointerdownpointerx: 0,
      onpointerdownpointery: 0,
      lon: 0,
      onpointerdownlon: 0,
      lat: 0,
      onpointerdownlat: 0,
      phi: 0,
      theta: 0,
      target: new three.vector3()
    }
  },
  mounted() {
  	this.init()
  },
  methods: {
    init() {
      let textureloader = new three.textureloader();

      textureloader.load(this.sceneurl, (texture) => {
        texture.mapping = three.uvmapping;

        this.initimg(texture);
        this.render();
      });
    },
    initimg(texture) {
      let container, mesh;
      // 容器宽度、高度
      let containerwidth = this.$refs.container.offsetwidth;
      let containerheight = this.$refs.container.offsetheight;
      container = document.getelementbyid('container');
      this.renderer = new three.webglrenderer({ antialias: true });
      this.renderer.setpixelratio(window.devicepixelratio);
      // this.renderer.setsize(window.innerwidth, window.innerheight);
      this.renderer.setsize(containerwidth, containerheight);
      let childs = container.childnodes;
      if (container.childnodes.length > 0) {
        container.removechild(childs[0]);
        container.appendchild(this.renderer.domelement);
      } else {
        container.appendchild(this.renderer.domelement);
      }

      this.scene = new three.scene();
      this.camera = new three.perspectivecamera(60, containerwidth  / containerheight , 1, 1000);
      mesh = new three.mesh(new three.spherebuffergeometry(500, 32, 16), new three.meshbasicmaterial({ map: texture }));
      mesh.geometry.scale(-1, 1, 1);
      this.scene.add(mesh);

      container.addeventlistener('mousedown', this.ondocumentmousedown, false);
      container.addeventlistener('mousemove', this.ondocumentmousemove, false);
      container.addeventlistener('mouseup', this.ondocumentmouseup, false);
      container.addeventlistener('mousewheel', this.ondocumentmousewheel, false);

      container.addeventlistener('touchstart', this.ondocumenttouchstart, false);
      container.addeventlistener('touchmove', this.ondocumenttouchmove, false);
    },
    ondocumentmousedown(event) {
      event.preventdefault();

      this.isuserinteracting = true;

      this.onpointerdownpointerx = event.clientx;
      this.onpointerdownpointery = event.clienty;

      this.onpointerdownlon = this.lon;
      this.onpointerdownlat = this.lat;
    },

    ondocumentmousemove(event) {
      if (this.isuserinteracting) {
        this.lon = (this.onpointerdownpointerx - event.clientx) * 0.1 + this.onpointerdownlon;
        this.lat = (event.clienty - this.onpointerdownpointery) * 0.1 + this.onpointerdownlat;
        this.render();
      }
    },

    ondocumentmouseup(event) {
      this.isuserinteracting = false;
      this.render();
    },

    ondocumentmousewheel(event) {
      this.camera.fov -= event.wheeldeltay * 0.05;
      this.camera.updateprojectionmatrix();
      event = event || window.event;
      if (event.stoppropagation) { // 这是取消冒泡
        event.stoppropagation();
      } else {
        event.cancelbubble = true;
      };
      if (event.preventdefault) { // 这是取消默认行为
        event.preventdefault();
      } else {
        event.returnvalue = false;
      };
      this.render();
    },

    ondocumenttouchstart(event) {
      if (event.touches.length == 1) {
        event.preventdefault();

        this.onpointerdownpointerx = event.touches[0].pagex;
        this.onpointerdownpointery = event.touches[0].pagey;

        this.onpointerdownlon = this.lon;
        this.onpointerdownlat = this.lat;
      }
    },

    ondocumenttouchmove(event) {
      if (event.touches.length == 1) {
        event.preventdefault();

        this.lon = (this.onpointerdownpointerx - event.touches[0].pagex) * 0.1 + this.onpointerdownlon;
        this.lat = (event.touches[0].pagey - this.onpointerdownpointery) * 0.1 + this.onpointerdownlat;

        this.render();
      }
    },
    render() {
      this.lon += 0.15;

      this.lat = math.max(-85, math.min(85, this.lat));
      this.phi = three.math.degtorad(90 - this.lat);
      this.theta = three.math.degtorad(this.lon);
      this.camera.position.x = 100 * math.sin(this.phi) * math.cos(this.theta);
      this.camera.position.y = 100 * math.cos(this.phi);
      this.camera.position.z = 100 * math.sin(this.phi) * math.sin(this.theta);

      this.camera.lookat(this.scene.position);
      this.renderer.render(this.scene, this.camera);
    }
  }

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持代码网。

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com