vue项目引入three.js实现基本的动画(含vue-cli2引入报错解决方案)

28 篇文章 2 订阅
1 篇文章 0 订阅

在这里插入图片描述
最近接到一个新的需求,再登陆页添加这样的背景效果(如上图),很显然这是three.js的,所以需要再之前的老项目中引入three.js,但是vue-cli2和vue-cli3引入后有些情况不同,下面开始吧!

安装依赖

npm i three -D
npm i three-orbit-controls -D // 控制移动的效果(放大,360度旋转,平移)

然后直接再页面中上代码,如下

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

<script>
import * as THREE from "three"; // 引入three
const OrbitControls = require("three-orbit-controls")(THREE);
export default {
  name: "index",
  data() {
    return {
      camera: null,
      scene: null,
      renderer: null,
      mesh: null,
    };
  },
  methods: {
    init() {
      let container = document.getElementById("container");
      // 创建一个相机 参数:fov、aspect、near、far
      this.camera = new THREE.PerspectiveCamera(30, 1, 0.01, 1000);
      // 相机离屏幕的距离
      this.camera.position.z = 1;
      // 创建场景
      this.scene = new THREE.Scene();
      // 创建一个立方体 参数 width、height、depth
      let geometry = new THREE.BoxGeometry(0.2, 0.2, 0.2);
      // 立方体的材质 参数为obj
      let material = new THREE.MeshNormalMaterial({ color: 0x00ff00 });
      // 将立方体和材质包添加到网格
      this.mesh = new THREE.Mesh(geometry, material);
      // 将网格添加到场景中
      this.scene.add(this.mesh);
      // 渲染器 参数为obj antialias:true 开启抗锯齿(默认为false)
      this.renderer = new THREE.WebGLRenderer({ antialias: true });
      this.renderer.setClearColor(0xb9d3ff, 1);
      // 设置渲染大小
      this.renderer.setSize(500, 500);
      this.renderer.render(this.scene, this.camera);
      container.appendChild(this.renderer.domElement);
      new OrbitControls(this.camera,this.renderer.domElement)
    },
    animate() {
      requestAnimationFrame(this.animate);
      this.mesh.rotation.x += 0.01;
      this.mesh.rotation.y += 0.01;
      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 {
  position: absolute;
  width: 100%;
  height: 100%;
}
</style>

最后展示的效果如下,便说明成功了
在这里插入图片描述
下面就随意自己秀了!!!

报错问题

在vue-cli2中,会出现以下的报错

> webpack-dev-server --inline --progress --config build/webpack.dev.conf.js

(node:5224) [DEP0111] DeprecationWarning: Access to process.binding('http_parser') is deprecated.(Use `node --trace-deprecation ...` to show where the warning was created)
 13% building modules 26/31 modules 5 active ...x=0!E:\test\thcode\t3demo\src\App.vue{ parser: "babylon" } is deprecated; we now treat it as { parser: "babel" }.
 94% asset optimization

 ERROR  Failed to compile with 1 errors 
 error  in ./node_modules/_three@0.132.2@three/build/three.module.js

Module parse failed: Unexpected token (2516:25)
You may need an appropriate loader to handle this file type.
|
|               this.texture = source.texture.clone();
|               this.texture.image = { ...this.texture.image }; // See #20328.
|
|               this.depthBuffer = source.depthBuffer;

 @ ./node_modules/babel-loader/lib!./node_modules/vue-loader/lib/selector.js?type=script&index=0!./src

大概意思就是: 您可能需要一个适当的加载器来处理这种文件类型。就是没有对应的loader来处理的问题;

解决方法
安装低版本的three —— 0.128.0 (我现在的版本是0.135.0)

//卸载three
npm uninstall three
// 安装 0.128.0版本的
npm install three@0.128.0

重启,运行,成功~~

  • 3
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Vue 2中使用Three.js创建地图可以按照以下步骤进行: 1. 首先,确保你已经安装了Vue CLI并创建了一个Vue项目。如果没有安装,请按照Vue CLI的官方文档进行安装和创建项目。 2. 安装Three.js库。可以通过以下命令使用npm进行安装: ```bash npm install three ``` 3. 在Vue组件中引入Three.js库。在你需要使用Three.js的组件中,可以通过以下方式引入: ```javascript import * as THREE from 'three'; ``` 4. 创建一个Canvas元素用于渲染Three.js场景。在你的Vue组件的模板中,添加一个Canvas元素,例如: ```html <template> <div> <canvas ref="canvas"></canvas> </div> </template> ``` 5. 在Vue组件的生命周期钩子函数中创建Three.js场景并渲染。在你的Vue组件中,可以在`mounted`钩子函数中创建Three.js场景,并在其中进行渲染。例如: ```javascript export default { mounted() { // 获取canvas元素 const canvas = this.$refs.canvas; // 创建场景 const scene = new THREE.Scene(); // 创建相机 const camera = new THREE.PerspectiveCamera( 75, window.innerWidth / window.innerHeight, 0.1, 1000 ); camera.position.z = 5; // 创建渲染器 const renderer = new THREE.WebGLRenderer({ canvas }); renderer.setSize(window.innerWidth, window.innerHeight); // 创建地图 const geometry = new THREE.BoxGeometry(1, 1, 1); const material = new THREE.MeshBasicMaterial({ color: 0x00ff00 }); const cube = new THREE.Mesh(geometry, material); scene.add(cube); // 渲染场景 function animate() { requestAnimationFrame(animate); cube.rotation.x += 0.01; cube.rotation.y += 0.01; renderer.render(scene, camera); } animate(); }, }; ``` 以上代码创建了一个简单的场景,其中添加了一个绿色的立方体,并通过动画使其旋转起来。 这只是一个简单示例,你可以根据自己的需求在Three.js中创建更复杂的地图。希望对你有所帮助!

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值