记录一下在vue3中使用threejs的过程

效果图

全屏效果图

创建一个vue3项目

1
pnpm create vue@latest

一步一步选择,创建成功后进入项目目录
执行

1
2
pnpm install
pnpm run dev

安装three.js

这一步建议开启魔法🔮,不然可能会失败

1
2
pnpm install three
pnpm install @types/three

演示代码(包括注释)

运行完成打开就是效果图的效果

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68

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

<script lang='ts' setup>
import * as THREE from 'three'
import {OrbitControls} from 'three/examples/jsm/controls/OrbitControls'
import {onMounted} from 'vue'

//创建一个三维场景
const scene = new THREE.Scene()
//添加光源
const ambient = new THREE.AmbientLight(0xffffff, 0.5),
light1 = new THREE.PointLight(0xffffff, 0.4),
light2 = new THREE.PointLight(0xffffff, 0.4)

scene.add(ambient)
light1.position.set(200, 300, 400)
scene.add(light1)
light2.position.set(-200, -300, -400)
scene.add(light2)
//创建一个透视相机
const width = window.innerWidth, height = window.innerHeight,
camera = new THREE.PerspectiveCamera(45, width / height, 1, 1000)
//设置相机位置
camera.position.set(200, 200, 200)
//设置相机方向
camera.lookAt(0, 0, 0)

//创建辅助坐标轴
const axesHelper = new THREE.AxesHelper(100)
scene.add(axesHelper)

//创建一个物体(形状)
const geometry = new THREE.BoxGeometry(100, 100, 100)
//创建材质(外观)
const material = new THREE.MeshLambertMaterial({
color: 0x00ffff,//设置材质颜色
transparent: true,//开启透明度
opacity: 0.5 //设置透明度
})
//创建一个网格模型对象
const mesh = new THREE.Mesh(geometry, material)
//把网格模型添加到三维场景
scene.add(mesh)

//创建一个WebGL渲染器
const renderer = new THREE.WebGLRenderer()
renderer.setSize(width, height)
renderer.render(scene, camera)

const controls = new OrbitControls(camera, renderer.domElement)
controls.addEventListener('change', () => {
renderer.render(scene, camera)
})
onMounted(() => {
document.getElementById('three')?.appendChild(renderer.domElement)
})
</script>

<style>
body {
margin: 0;
padding: 0;
}
</style>