记录在vue中使用three.js

试了很多次,终于找到一个可以正确在vue中使用three.js的方法。我觉得主要的问题是出在vue的版本上。

我刚开始尝试用过vue init webpack ***的方式创建vue项目,但是创建完的项目一加入three运行就报错,所有我改用vue create ** 创建项目。我刚开始创建项目选择的是vue3,创建完的项目加入three运行不报错但是引用three的其他组件又报错,所以改选择vue2。选择vue2发现在创建vue项目的时候就报错,最后找到报错的原因,是由于我npm的版本过高,降低npm的版本就好了。
步骤如下
1、npm的版本过高,先降低npm的版本。

npm install npm@6.14.10 -g

2、创建项目,选择vue2,其他选择看个人喜好

vue create demo

3、进入项目文件夹,添加three到项目中

npm install --save three 

4、可以正常的书学代码,例子:

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


<script>
import * as THREE from 'three'
import { OrbitControls } from 'three/examples/jsm/controls/OrbitControls'


export default {
  name: 'City',
  data () {
    return {
      camera:null,
      scene:null,
      renderer: null,
    
      width:'',
      height:'',

    }
  },
  methods: {
    
      init() {
        
    
          this.initThree();
          this.initScene();
          this.initCamera();
          this.initPlant();        
 
        //添加坐标轴
        var axesHelper = new THREE.AxesHelper;
        this.scene.add(axesHelper);
      },
      initThree:function(){
          
          let container = document.getElementById('container');
          this.width=container.clientWidth;
          this.height=container.clientHeight;
        this.renderer = new THREE.WebGLRenderer({antialias: true})
        this.renderer.setSize(this.width, this.height)
        container.appendChild(this.renderer.domElement)
      },
      initScene:function(){
        this.scene = new THREE.Scene();
        this.scene.add(new THREE.AmbientLight(0x3c3c3c));//环境光
      },
      initCamera:function(){
        this.camera = new THREE.PerspectiveCamera(45,this.width / this.height,0.25, 1000);
        this.camera.position.set( -12.69085400727962,  120.82969958208948,  -69.97533778224751);
        
      },
      //插入地板
      initPlant:function(){
        var geometry1 = new THREE.BoxGeometry(80, 1, 80);
        var matarial1 = new THREE.MeshPhongMaterial( { 
          color: '#ffffff'
            // map: THREE.ImageUtils.loadTexture('img/3d66Model-593580-files-10.jpg') ,
            // emissive:'blue',
        } );
        var cube = new THREE.Mesh(geometry1, matarial1);
        cube.position.set(20,0,10);
        this.scene.add(cube);
      },
     
      animate: function () {

        //镜头移动
        var controls =  new OrbitControls(this.camera,this.renderer.domElement);
        controls.target.set(0,-0.2,-0.2);
        controls.update();
		requestAnimationFrame(this.animate)
        this.renderer.render(this.scene, this.camera);
      }
   },
   mounted () {
      this.init()
      this.animate()
    }
}
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
  #container {
    height:500px;
  }
</style>

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Vue使用Three.js VR全景图,你可以按照以下步骤进行: 1. 首先,确保你已经安装了VueThree.js的依赖包。你可以使用npm或yarn来安装它们。 2. 在Vue的组件,导入Three.js所需的库和组件: ```javascript import * as THREE from 'three'; import { VRButton } from 'three/examples/jsm/webxr/VRButton.js'; ``` 3. 创建一个Vue组件,并在其定义一个方法来初始化Three.js场景: ```javascript export default { mounted() { this.initScene(); }, methods: { initScene() { // 创建场景 const scene = new THREE.Scene(); // 创建相机 const camera = new THREE.PerspectiveCamera( 75, window.innerWidth / window.innerHeight, 0.1, 1000 ); // 创建渲染器 const renderer = new THREE.WebGLRenderer({ antialias: true }); renderer.setSize(window.innerWidth, window.innerHeight); document.body.appendChild(renderer.domElement); // 添加VR按钮 document.body.appendChild(VRButton.createButton(renderer)); // 创建全景图 const textureLoader = new THREE.TextureLoader(); const texture = textureLoader.load('path/to/your/panorama-image.jpg'); const sphereGeometry = new THREE.SphereGeometry(500, 60, 40); const sphereMaterial = new THREE.MeshBasicMaterial({ map: texture }); const sphere = new THREE.Mesh(sphereGeometry, sphereMaterial); sphere.position.set(0, 0, 0); scene.add(sphere); // 更新相机和场景 const animate = () => { requestAnimationFrame(animate); sphere.rotation.y += 0.01; renderer.render(scene, camera); }; animate(); }, }, }; ``` 4. 在Vue模板使用这个组件: ```html <template> <div id="app"> <canvas id="vr-canvas"></canvas> </div> </template> ``` 这样,你就可以在Vue使用Three.js VR全景图了。当用户点击VR按钮时,全屏显示全景图,并支持通过移动设备或VR眼镜进行交互。你可以适配不同的全景图、修改相机参数等来满足你的需求。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值