threejs之将PlaneGeometry转换为BufferGeometry
threejs之将PlaneGeometry转换为BufferGeometry
用途:将PlaneGeometry转换为BufferGeometry后,对BufferGeometry的标记面数组操作,达成使用边界切割PlaneGeometry
const material = new THREE.MeshBasicMaterial({
color: 0x00ff00,
side: THREE.DoubleSide,
transparent: true,
opacity: 0.5,
wireframe: true
});
// 创建PlaneGeometry
let widthLength = 4
let heightLength = 5
const geometry1 = new THREE.PlaneGeometry(
30,
30,
widthLength,
heightLength,
);
// const mesh1 = new THREE.Mesh(geometry1, material);
// scene.add(mesh1);
// 数据处理 生成BufferGeometry的点位标记面数组
let arr = []
for (let i = 0; i < heightLength; i++) {
for (let j = 0; j < widthLength; j++) {
arr.push((i*(widthLength+1)+j))
arr.push((i*(widthLength+1)+j+1))
arr.push((i+1)*(widthLength+1)+j)
arr.push((i*(widthLength+1)+j+1))
arr.push((i+1)*(widthLength+1)+j)
arr.push((i+1)*(widthLength+1)+j+1)
}
}
// 创建BufferGeometry
const geometry = new THREE.BufferGeometry();
// 将PlaneGeometry的点直接放入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,2,2,2,2,2,0,2,0,0]);
const vertices = new Float32Array(geometry1.attributes.position.array);
const verticesBuffer = new THREE.BufferAttribute(vertices, 3);
geometry.setAttribute('position', verticesBuffer);
// 将点位标记面数组赋值
// const indices = new Uint16Array([0, 1, 2, 0, 2, 3]);
const indices = new Uint16Array(arr);
geometry.setIndex(new THREE.BufferAttribute(indices, 1));
const mesh = new THREE.Mesh(geometry, material);
scene.add(mesh);
。。。 钻研不易,转载请注明出处

浙公网安备 33010602011771号