Vue结合3D技术实现VR看房效果

效果图: 

1:首先创建一个vue的基础项目
vue create demo
2:启动并打开项目

3。项目中安装threejs

npm install three

4.安装完成再组件如下导入:

import * as THREE from 'three'

5。在template模块中创建一个div,并绑定一个class

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

其次,需要给html,body,默认的容器#app这三类元素设定宽高(必须)
6:在data中定义全局变量

  data() {
    return {
      //相机
      camera: '',
      //场景
      scene: '',
      //网格
      mesh: '',
      //渲染器
      renderer: '',
      //几何体
      geometry: '',
      //材质
      material: '',
      //引入的图片资源
      roomImg: 'https://kh-qg.oss-cn-hangzhou.aliyuncs.com/img/3d-img/img1.jpg',
      //图片的下标
      index: 1,
      //用来控制音频是否播放的标记
      musicFlag: false,
      //引入的音频资源
      mp3,
      //是否全屏
      screenBool: true,
      //音频对象
      audio: '',
      //控制旋转
      rotateBool: true,
      //图片的总数
      sum: 0,
      //纹理加载器
      textureLoader: '',
      //渲染函数
      render: ''
    }
  },

7:在methods中写入方法

methods: {
    //初始化页面数据
    init() {
      let _this = this
      /*----------------------------渲染数据的位置------------------------------*/
      //1、找到渲染数据的位置
      let container = document.getElementsByClassName('container')[0]
      /*-------------------------------场景对象---------------------------------*/
      //2、创建一个场景
      _this.scene = new THREE.Scene()
      //3、创建一个几何
      _this.geometry = new THREE.SphereGeometry(25, 50, 50)
      //4、材质
      _this.material = new THREE.MeshBasicMaterial({
        color: 0xffffff,
        side: THREE.BackSide
      })
      //5、创建网格对象
      _this.mesh = new THREE.Mesh(_this.geometry, _this.material)
      //6、把材质放到场景中
      _this.scene.add(_this.mesh)
      /*-------------------------------纹理对象---------------------------------*/
      //7、纹理加载对象
      _this.textureLoader = new THREE.TextureLoader()
      //8、音频监听器
      let listener = new THREE.AudioListener()
      //9、音频对象
      _this.audio = new THREE.Audio(listener)
      //10、执行资源加载
      let texture = _this.textureLoader.load(_this.roomImg, function() {
        let audioLoader = new THREE.AudioLoader()
        audioLoader.load(_this.mp3, function(AudioBuffer) {
          _this.musicFlag = 0
          _this.audio.setBuffer(AudioBuffer)
          _this.audio.setLoop(true)
          _this.audio.setVolume(0.3)
          //音乐停止
          _this.audio.pause()
        })
        //资源加载结束,开始渲染操作
        _this.render()
      })
      //11、设置材质
      _this.mesh.material.map = texture
      /*-------------------------------设置相机---------------------------------*/
      let width = container.clientWidth
      let height = container.clientHeight
      let k = width / height
      //12、创建相机对象
      _this.camer
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值