【可视化学习】05-Three.js物体
发表于:2023-05-16 |

掌握几何体顶点_UV_法向属性

基本概念:

顶点(position)

几何体通过顶点连接构成

法向量(normal)

几何体的垂直线,常用于光影控制

uv

相当于将几何体展开部分,常用于贴图等

BufferGeometry设置顶点创建矩形

让我们先将常用的配置加上

常规配置

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
import * as THREE from "three";
// 导入轨道控制器
import { OrbitControls } from "three/examples/jsm/controls/OrbitControls"
// 1.创建场景
const scene = new THREE.Scene()

// 2.创建相机
const camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000)

// 设置相机位置
camera.position.set(0, 0, 10)

// 添加相机
scene.add(camera)

// 初始化渲染器
const renderer = new THREE.WebGLRenderer();

//设置渲染的尺寸大小
renderer.setSize(window.innerWidth, window.innerHeight);

// 将webgl渲染的canvas内容添加到body上
document.body.appendChild(renderer.domElement);

//当前是平面的,让平面立体起来,使用控制器controls
// 创建轨道控制器
const controls = new OrbitControls(camera, renderer.domElement);
controls.enableDamping=true
// 添加坐标轴辅助器
const axesHelper = new THREE.AxesHelper(5);
scene.add(axesHelper)

// 持续渲染,让相机场景动起来
function render() {
controls.update()
// 使用渲染器通过相机将场景渲染进来
renderer.render(scene, camera);
// 渲染下一帧的时候就会调用render函数
requestAnimationFrame(render)
}

render()

// 监听画面的变化,更新渲染的画面
window.addEventListener("resize", () => {
// console.log("画面变化了")
// 更新摄像头
camera.aspect = window.innerWidth / window.innerHeight;
// 更新摄像机的投影矩阵
camera.updateProjectionMatrix();
// 更新渲染器
renderer.setSize(window.innerWidth, window.innerHeight);

// 设置渲染器的像素比
renderer.setPixelRatio(window.devicePixelRatio)
})

添加物体

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
// 创建几何体
const geometry = new THREE.BufferGeometry();

// 设置顶点
const vertices=new Float32Array([
-1.0,-1.0,1.0,
1.0,-1.0,1.0,
1.0,1.0,1.0,
1.0,1.0,1.0,
-1.0,1.0,1.0,
-1.0,-1.0,1.0
])
// 设置顶点位置,3个为一组
geometry.setAttribute('position',new THREE.BufferAttribute(vertices,3))
// 设置材质
const material = new THREE.MeshBasicMaterial({ color: 0x00ff00 });

// 生成物体
const mesh=new THREE.Mesh(geometry,material)

// 将物体添加到场景中
scene.add(mesh)

效果图

生成炫酷的三角形

保持基本配置不要变,我们修改下生成物体的代码即可

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
// 创建几何体
for (let i = 0; i < 50; i++) {
// 每一个三角形,需要3个顶点,每个顶点需要3个值
const geometry = new THREE.BufferGeometry();
// 告诉几何体有几个顶点
const positionArray = new Float32Array(9);
// 点的位置在-5到5之间
for (let j = 0; j < 9; j++) {
positionArray[j] = Math.random() * 10 - 5;
}
geometry.setAttribute("position", new THREE.BufferAttribute(positionArray, 3))
// 设置随机颜色
let color = new THREE.Color(Math.random(), Math.random(), Math.random())
// 设置材质,增加透明度
const material = new THREE.MeshBasicMaterial({ color: color, transparent: true, opacity: 0.5 });
// 根据几何体和材质创建物体
const mesh = new THREE.Mesh(geometry, material)
scene.add(mesh)
}

效果图

了解常用的网格几何体(还有很多,具体可以看文档)

立方缓冲几何体BoxGeometry

圆形缓冲几何体CircleGeometry

可以用来做三角形,圆形,圆环,扇形,多边形等

圆锥缓冲几何体ConeGeometry

圆柱缓冲几何体CylinderGeometry

多面体缓冲几何体DodecahedronGeometry

边缘几何体EdgesGeometry

二十面体缓冲几何体IcosahedronGeometry

球体缓冲几何体SphereGeometry

正四面体缓冲几何体TetrahedronGeometry

文字缓冲几何体TextGeometry

圆环缓冲几何体TorusGeometry

圆环扭结缓冲几何体TorusKnotGeometry

立方体缓冲几何体BoxBufferGeometry

上一篇:
【可视化学习】06-Three.js材质和纹理
下一篇:
【可视化学习】04-Three.js的开发入门与调试配置