Nuxt.js 是一个基于 Vue.js 的服务端渲染应用程序框架,而 Three.js 是一个用于创建 3D 图形的 JavaScript 库。将它们结合起来使用比较麻烦,需要做一些特殊的配置工作。
以下是使用 Nuxt.js 和 Three.js 创建 3D 场景的一些步骤:
- 安装 Three.js
使用 npm 或 yarn 安装 Three.js:
npm install three
# 或
yarn add three
- 创建一个 Three.js 场景
在 pages 文件夹中创建一个新的 threejs.vue 文件,并添加以下代码:
<template>
<div ref="threejsContainer"></div>
</template>
<script>
import * as THREE from 'three';
export default {
name: 'ThreeJS',
data() {
return {
// Three.js 变量
scene: null,
camera: null,
renderer: null,
// 场景元素
mesh: null
};
},
methods: {
init() {
// 创建场景
this.scene = new THREE.Scene();
// 创建相机
this.camera = new THREE.PerspectiveCamera(
75,
window.innerWidth / window.innerHeight,
0.1,
1000
);
this.camera.position.z = 5;
// 创建渲染器
this.renderer = new THREE.WebGLRenderer();
this.renderer.setSize(window.innerWidth, window.innerHeight);
// 向容器添加渲染器的 DOM 元素
this.$refs.threejsContainer.appendChild(this.renderer.domElement);
// 创建几何体和材质
const geometry = new THREE.BoxGeometry(1, 1, 1);
const material = new THREE.MeshBasicMaterial({
color: 0x00ff00
});
// 创建网格
this.mesh = new THREE.Mesh(geometry, material);
this.scene.add(this.mesh);
},
animate() {
// 循环动画
requestAnimationFrame(this.animate.bind(this));
// 旋转几何体
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>
该组件在 data 中定义了一些 Three.js 变量和场景元素。在 init 方法中,它创建了一个场景、相机和渲染器,并向容器添加了渲染器的 DOM 元素。接着它创建了一个几何体和材质,然后用这些元素创建一个网格并将其加入场景。在 animate 方法中,它使用 requestAnimationFrame 函数循环渲染场景。
- 在 Nuxt.js 中注册 Three.js
在 nuxt.config.js 文件中添加以下代码:
import VuetifyLoaderPlugin from 'vuetify-loader/lib/plugin';
export default {
plugins: [
{ src: '~/plugins/three.js', ssr: false }
],
build: {
plugins: [
new VuetifyLoaderPlugin(),
]
}
};
这将注册 Three.js,使其可以在 Vue 文件中使用。
- 在 Vue 文件中使用 Three.js
在需要使用 Three.js 的 Vue 组件中,您需要使用以下代码:
import * as THREE from 'three';
这将允许您使用所有 Three.js 的函数和类。
最后,在需要使用 Three.js 的 Vue 组件中,您可以通过以下方式使用 Three.js 场景:
<template>
<div ref="threejsContainer"></div>
</template>
<script>
import * as THREE from 'three';
import { OrbitControls } from 'three/examples/jsm/controls/OrbitControls.js';
export default {
data() {
return {
scene: null,
camera: null,
renderer: null,
controls: null,
mesh: null
};
},
mounted() {
// 创建场景
this.scene = new THREE.Scene();
// 创建相机和渲染器
this.camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);
this.camera.position.z = 5;
this.renderer = new THREE.WebGLRenderer();
this.renderer.setSize(window.innerWidth, window.innerHeight);
// 向容器添加渲染器的 DOM 元素
this.$refs.threejsContainer.appendChild(this.renderer.domElement);
// 添加控制器
this.controls = new OrbitControls(this.camera, this.renderer.domElement);
// 创建几何体和材质
const geometry = new THREE.BoxGeometry(1, 1, 1);
const material = new THREE.MeshBasicMaterial({ color: 0x00ff00 });
// 创建网格
this.mesh = new THREE.Mesh(geometry, material);
this.scene.add(this.mesh);
// 循环动画
requestAnimationFrame(this.animate);
},
methods: {
animate() {
// 循环渲染
requestAnimationFrame(this.animate.bind(this));
// 旋转几何体
this.mesh.rotation.x += 0.01;
this.mesh.rotation.y += 0.01;
// 渲染场景
this.renderer.render(this.scene, this.camera);
}
}
};
</script>
在这个例子中,我们使用 OrbitControls 来控制相机,并在 mounted 钩子中创建了场景、相机和渲染器。我们向容器添加了渲染器的 DOM 元素,并创建了控制器,用于使用鼠标控制相机。我们创建了几何体和材质,并用它们创建了一个网格。最后,我们在 animate 方法中循环渲染场景。