1、 安裝three
npm install --save @types/three
2、npm安裝軌道控件插件:
npm install three-orbit-controls
3、安裝加載.obj和.mtl文件的插件:
npm i --save three-obj-mtl-loader
4、 安裝渲染器插件:
npm i --save three-CSS2drender
5、在我們要用得項(xiàng)目中引入three
import * as THREE from "three";
import { FontLoader } from "three/examples/jsm/loaders/FontLoader";
import { TextGeometry } from "three/examples/jsm/geometries/TextGeometry";
注意會(huì)出現(xiàn)找不到three
解決方法是在src新建文件shims-vue.d.ts
declare module '*.vue' {
import { DefineComponent } from 'vue'
// eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/ban-types
const component: DefineComponent<{}, {}, any>
export default component
}
declare module "@types/three";
在tsconfig.json 里面加上
"include": ["src/**/*.ts", "src/**/*.vue", "src/**/*.tsx", "src/**/*.d.ts"],
完整的代碼
<script setup lang="ts">
import * as THREE from 'three'
import { OrbitControls } from 'three/examples/jsm/controls/OrbitControls'
import { onMounted } from 'vue'
//創(chuàng)建一個(gè)三維場(chǎng)景
const scene = new THREE.Scene();
//創(chuàng)建一個(gè)物體(形狀)
const geometry = new THREE.BoxGeometry(100, 100, 100);//長(zhǎng)寬高都是100的立方體
// const geometry = new THREE.SphereGeometry(60,40,40);//半徑是60的圓
//widthSegments, heightSegments:水平方向和垂直方向上分段數(shù)。widthSegments最小值為3,默認(rèn)值為8。heightSegments最小值為2,默認(rèn)值為6。
//創(chuàng)建材質(zhì)(外觀)
const material = new THREE.MeshLambertMaterial({
color: 0x0000ff,//設(shè)置材質(zhì)顏色(藍(lán)色)
// color: 0x00ff00,//(綠色)
transparent: true,//開啟透明度
opacity: 0.5 //設(shè)置透明度
});
//創(chuàng)建一個(gè)網(wǎng)格模型對(duì)象
const mesh = new THREE.Mesh(geometry, material);//網(wǎng)絡(luò)模型對(duì)象Mesh
//把網(wǎng)格模型添加到三維場(chǎng)景
scene.add(mesh);//網(wǎng)絡(luò)模型添加到場(chǎng)景中
// 添加多個(gè)模型(添加圓形)
const geometry2 = new THREE.SphereGeometry(60, 40, 40);
const material2 = new THREE.MeshLambertMaterial({
color: 0xffff00
});
const mesh2 = new THREE.Mesh(geometry2, material2); //網(wǎng)格模型對(duì)象Mesh
// mesh3.translateX(120); //球體網(wǎng)格模型沿Y軸正方向平移120
mesh2.position.set(120, 0, 0);//設(shè)置mesh3模型對(duì)象的xyz坐標(biāo)為120,0,0
scene.add(mesh2);
//添加光源 //會(huì)照亮場(chǎng)景里的全部物體(氛圍燈),前提是物體是可以接受燈光的,這種燈是無方向的,即不會(huì)有陰影。
const ambient = new THREE.AmbientLight(0xffffff, 0.4);
const light = new THREE.PointLight(0xffffff, 1);//點(diǎn)光源,color:燈光顏色,intensity:光照強(qiáng)度
scene.add(ambient);
light.position.set(200, 300, 400);
scene.add(light);
//創(chuàng)建一個(gè)透視相機(jī),窗口寬度,窗口高度
const width = window.innerWidth, height = window.innerHeight;
const camera = new THREE.PerspectiveCamera(45, width / height, 1, 1000);
//設(shè)置相機(jī)位置
camera.position.set(300, 300, 300);
//設(shè)置相機(jī)方向
camera.lookAt(0, 0, 0);
//創(chuàng)建輔助坐標(biāo)軸
const axesHelper = new THREE.AxesHelper(200);//參數(shù)200標(biāo)示坐標(biāo)系大小,可以根據(jù)場(chǎng)景大小去設(shè)置
scene.add(axesHelper);
//創(chuàng)建一個(gè)WebGL渲染器
const renderer = new THREE.WebGLRenderer()
renderer.setSize(width, height)//設(shè)置渲染區(qū)尺寸
renderer.render(scene, camera)//執(zhí)行渲染操作、指定場(chǎng)景、相機(jī)作為參數(shù)
const controls = new OrbitControls(camera, renderer.domElement)//創(chuàng)建控件對(duì)象
controls.addEventListener('change', () => {
renderer.render(scene, camera)//監(jiān)聽鼠標(biāo),鍵盤事件
})
onMounted(() => {
document.getElementById('my-three')?.appendChild(renderer.domElement)
})
</script>
<template>
<div id="my-three"></div>
</template>
<style scoped>
.read-the-docs {
color: #888;
}
</style>

image.png