three.js——通过顶点和顶点索引创建集合体

本文详细介绍了如何在THREE.js中通过顶点数据、属性、材质和顶点索引创建3D几何图形,包括网格的定位与显示。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

效果图

在这里插入图片描述

1、创建顶点数据

// 创建顶点数据 每三个一个顶点 逆时针为正面
const vertices = new Float32Array([
-1.0, -1.0, 0.0, 1.0, -1.0, 0.0, 1.0, 1.0, 0.0,
1.0, 1.0, 0.0, -1.0, 1.0, 0.0, -1.0, -1.0, 0.0
])

2、创建顶点属性

// 创建顶点属性
geomentryBuff.setAttribute('position', new THREE.BufferAttribute(vertices, 3))

3、创建材质

// 创建材质
const material01 = new THREE.MeshBasicMaterial({
  color: 0x00ff00,
  // 正反面都能看到
  side: THREE.DoubleSide,
  // 线条展示
  wireframe:true
})

4、创建网格

// 创建网格
const plane = new THREE.Mesh(geomentryBuff, material01)

5、改变网格的位置并添加到场景中

// 改变网格的位置
plane.position.set(3, 0, 0)
scene.add(plane)

6、通过顶点索引创建几何图形

// 创建顶点数据:三个为一组
const vertices = new Float32Array([
  -1.0, -1.0, 0.0, 1.0, -1.0, 0.0, 1.0, 1.0, 0.0, -1.0, 1.0, 0.0
])
// 创建顶点属性
geomentryBuff.setAttribute('position', new THREE.BufferAttribute(vertices, 3))
// 创建索引
const indices = new Uint16Array([0, 1, 2, 2, 3, 0])
geomentryBuff.setIndex(new THREE.BufferAttribute(indices, 1))
// 创建材质
const material01 = new THREE.MeshBasicMaterial({
  color: 0x00ff00,
  // 正反面都能看到
  side: THREE.DoubleSide,
  // 线条展示
  wireframe:true
})
// 创建网格
const plane = new THREE.Mesh(geomentryBuff, material01)
// 改变网格的位置
plane.position.set(3, 0, 0)
scene.add(plane)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值