vue3项目中使用three.js
在Vue 3项目中使用Three.js,你可以按照以下步骤操作:
- 安装Three.js:
npm install three
- 创建一个Three.js组件:
<template>
<div ref="threeContainer"></div>
</template>
<script>
import * as THREE from 'three';
import { OrbitControls } from 'three/examples/jsm/controls/OrbitControls.js';
export default {
name: 'ThreeJsComponent',
mounted() {
this.initThreeJs();
this.animate();
},
methods: {
initThreeJs() {
const scene = new THREE.Scene();
const camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);
const renderer = new THREE.WebGLRenderer();
renderer.setSize(window.innerWidth, window.innerHeight);
this.$refs.threeContainer.appendChild(renderer.domElement);
const geometry = new THREE.BoxGeometry();
const material = new THREE.MeshBasicMaterial({ color: 0x00ff00 });
const cube = new THREE.Mesh(geometry, material);
scene.add(cube);
camera.position.z = 5;
const controls = new OrbitControls(camera, renderer.domElement);
controls.enableDamping = true;
this.scene = scene;
this.camera = camera;
this.renderer = renderer;
this.controls = controls;
},
animate() {
requestAnimationFrame(this.animate);
this.renderer.render(this.scene, this.camera);
this.controls.update();
}
}
};
</script>
- 在Vue组件中引入并使用这个Three.js组件。
确保你的Three.js版本与你的项目依赖兼容,并且适合你的Vue 3项目。上面的代码是一个基本的Three.js场景设置,你可以根据自己的需求添加更多的3D对象和交互功能。
评论已关闭