在 Vue 3 中使用 Three.js 创建 3D 场景

介绍

本文将介绍如何在 Vue 3 中使用 Three.js 构建一个 3D 场景。我们会使用 Vue 3 的 <script setup> 语法,并拆分不同的初始化函数,使代码结构更加清晰。

代码实现

1. 创建 Vue 组件

template 部分,我们创建一个 div 容器,并使用 ref 绑定它,以便在 setup 中操作。

css 复制代码
<template>
    <div ref="container" style="width: 100vw; height: 100vh;"></div>
</template>

2. 引入依赖和定义变量

script setup 部分,我们引入 Vue 相关的 API 和 Three.js 相关的库,并定义全局变量。

xml 复制代码
<script setup>
import { onMounted, ref } from "vue";
import * as THREE from "three";
import { OrbitControls } from "three/examples/jsm/controls/OrbitControls.js";

const container = ref(null);
let scene, camera, renderer, controls, cube, cube_mesh;

3. 初始化场景

ini 复制代码
const initScene = () => {
    scene = new THREE.Scene();
    const loader = new THREE.CubeTextureLoader();
    loader.setPath("images/");
    scene.background = loader.load([
        "6.jpg", "3.jpg", "5.jpg", "2.jpg", "4.jpg", "1.jpg"
    ]);
};

4. 初始化相机

ini 复制代码
const initCamera = () => {
    camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 2000);
    camera.position.set(700, -50, 700);
};

5. 添加光源

ini 复制代码
const initLights = () => {
    const hemLight = new THREE.HemisphereLight(0x2894ff, 0xffffbb, 50);
    scene.add(hemLight);
};

6. 创建 3D 物体

ini 复制代码
const initObjects = () => {
    const geometry = new THREE.BoxGeometry(100, 100, 100, 3, 3, 3);
    const material = new THREE.MeshBasicMaterial({ color: 0x00fff0 });
    cube = new THREE.Mesh(geometry, material);
    scene.add(cube);
    
    const material_wf = material.clone();
    cube_mesh = new THREE.Mesh(geometry, material_wf);
    scene.add(cube_mesh);
};

7. 初始化渲染器

ini 复制代码
const initRenderer = () => {
    renderer = new THREE.WebGLRenderer();
    renderer.setSize(window.innerWidth, window.innerHeight);
    container.value.appendChild(renderer.domElement);
};

8. 初始化控制器

ini 复制代码
const initControls = () => {
    controls = new OrbitControls(camera, renderer.domElement);
    controls.enableDamping = true;
    controls.enableZoom = true;
    controls.autoRotate = true;
    controls.minDistance = 10;
    controls.maxDistance = 2000;
    controls.enablePan = true;
    controls.screenSpacePanning = true;
    controls.target.set(0, 200, 0);
    controls.autoRotateSpeed = 0.5;
};

9. 动画渲染

ini 复制代码
const animate = () => {
    requestAnimationFrame(animate);
    cube.rotation.x += 0.01;
    cube.rotation.y += 0.01;
    cube_mesh.rotation.x += 0.01;
    cube_mesh.rotation.y += 0.01;
    controls.update();
    renderer.render(scene, camera);
};

10. 组件挂载后执行初始化

scss 复制代码
onMounted(() => {
    initScene();
    initCamera();
    initLights();
    initObjects();
    initRenderer();
    initControls();
    animate();
});
</script>

最后

通过 Vue 3 和 Three.js,创建 3D 场景,每个功能模块都被封装成独立的函数,方便理解和维护。

相关推荐
夏幻灵1 小时前
HTML5里最常用的十大标签
前端·html·html5
Mr Xu_1 小时前
Vue 3 中 watch 的使用详解:监听响应式数据变化的利器
前端·javascript·vue.js
未来龙皇小蓝1 小时前
RBAC前端架构-01:项目初始化
前端·架构
程序员agions1 小时前
2026年,微前端终于“死“了
前端·状态模式
万岳科技系统开发1 小时前
食堂采购系统源码库存扣减算法与并发控制实现详解
java·前端·数据库·算法
程序员猫哥_1 小时前
HTML 生成网页工具推荐:从手写代码到 AI 自动生成网页的进化路径
前端·人工智能·html
龙飞052 小时前
Systemd -systemctl - journalctl 速查表:服务管理 + 日志排障
linux·运维·前端·chrome·systemctl·journalctl
我爱加班、、2 小时前
Websocket能携带token过去后端吗
前端·后端·websocket
AAA阿giao2 小时前
从零拆解一个 React + TypeScript 的 TodoList:模块化、数据流与工程实践
前端·react.js·ui·typescript·前端框架
杨超越luckly2 小时前
HTML应用指南:利用GET请求获取中国500强企业名单,揭秘企业增长、分化与转型的新常态
前端·数据库·html·可视化·中国500强