以下是在 Three.js 中使用缓冲类型几何体随机生成三角形的示例代码:

// 创建一个空的缓冲几何体
var geometry = new THREE.BufferGeometry();

// 生成随机的顶点坐标
var vertices = [];
for (var i = 0; i < 3; i++) {
    vertices.push(Math.random() * 2 - 1); // x
    vertices.push(Math.random() * 2 - 1); // y
    vertices.push(Math.random() * 2 - 1); // z
}
// 将顶点坐标添加到缓冲几何体中
geometry.addAttribute('position', new THREE.Float32BufferAttribute(vertices, 3));

// 生成随机的颜色
var colors = [];
for (var i = 0; i < 3; i++) {
    colors.push(Math.random()); // r
    colors.push(Math.random()); // g
    colors.push(Math.random()); // b
}
// 将颜色添加到缓冲几何体中
geometry.addAttribute('color', new THREE.Float32BufferAttribute(colors, 3));

// 创建一个材质
var material = new THREE.MeshBasicMaterial({ vertexColors: THREE.VertexColors });

// 创建一个网格对象并添加到场景中
var mesh = new THREE.Mesh(geometry, material);
scene.add(mesh);

该代码会生成一个随机的三角形,其中顶点坐标和颜色都是随机生成的。注意,由于使用了缓冲类型几何体,需要通过Float32BufferAttribute来添加顶点坐标和颜色。最后,将几何体和材质组合成网格对象并添加到场景中。

Three.js 随机生成三角形:使用缓冲类型几何体

原文地址: https://www.cveoy.top/t/topic/mTYS 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录