Hi all, I’m trying to create a WallGeometry (like in Cesium; I have a pet project called Cesium + Three), but I have my geometry crashing when updating for some reason. What is the problem? I do everything in this class:
export class WallGeometry extends BufferGeometry {
constructor(vertices) {
super();
if (vertices.length % 6 !== 0) {
throw new Error('Vertices array length must be a multiple of 6');
}
const positions = [];
const indices = [];
const baseVerticesCount = vertices.length / 6;
for (let i = 0; i < baseVerticesCount; i++) {
const bottomX = vertices[i * 6];
const bottomY = vertices[i * 6 + 1];
const bottomZ = vertices[i * 6 + 2];
const topX = vertices[i * 6 + 3];
const topY = vertices[i * 6 + 4];
const topZ = vertices[i * 6 + 5];
positions.push(bottomX, bottomY, bottomZ);
positions.push(topX, topY, topZ);
}
for (let i = 0; i < baseVerticesCount - 1; i++) {
const bottomLeft = i * 2;
const topLeft = bottomLeft + 1;
const bottomRight = bottomLeft + 2;
const topRight = bottomRight + 1;
indices.push(bottomLeft, topLeft, bottomRight);
indices.push(topLeft, topRight, bottomRight);
}
const bottomLeft = (baseVerticesCount - 1) * 2;
const topLeft = bottomLeft + 1;
const bottomRight = 0;
const topRight = 1;
indices.push(bottomLeft, topLeft, bottomRight);
indices.push(topLeft, topRight, bottomRight);
this.setIndex(indices);
this.setAttribute('position', new Float32BufferAttribute(positions, 3));
this.computeVertexNormals();
}
updateGeometry(newPoints) {
const newVertices = [];
console.log(newPoints, 'AAAAAAAAAAAAAAAAAAAAAAAAAAAAA')
for (let i = 0; i < newPoints.length; i += 2) {
const upper = newPoints[i];
const lower = newPoints[i + 1];
newVertices.push(upper.L, upper.B, upper.H);
newVertices.push(lower.L, lower.B, 0);
}
const newCartesianVertices = fromDegreesToCartesian3(newVertices);
const positionAttribute = this.getAttribute('position');
const currentVertices = positionAttribute.array;
const indexAttribute = this.getIndex();
const currentIndices = indexAttribute.array;
const combinedVertices = new Float32Array(currentVertices.length + newPoints.length);
combinedVertices.set(currentVertices);
combinedVertices.set(newPoints, currentVertices.length);
const baseIndex = currentVertices.length / 3;
const newIndices = [];
const baseVerticesCount = newPoints.length / 3 / 2;
for (let i = 0; i < baseVerticesCount - 1; i++) {
const bottomLeft = baseIndex + i * 2;
const topLeft = bottomLeft + 1;
const bottomRight = bottomLeft + 2;
const topRight = bottomRight + 1;
newIndices.push(bottomLeft, topLeft, bottomRight);
newIndices.push(topLeft, topRight, bottomRight);
}
const bottomLeft = baseIndex + (baseVerticesCount - 1) * 2;
const topLeft = bottomLeft + 1;
const bottomRight = baseIndex;
const topRight = bottomRight + 1;
newIndices.push(bottomLeft, topLeft, bottomRight);
newIndices.push(topLeft, topRight, bottomRight);
const combinedIndices = new Uint16Array(currentIndices.length + newIndices.length);
combinedIndices.set(currentIndices);
combinedIndices.set(newIndices, currentIndices.length);
this.setAttribute('position', new Float32BufferAttribute(combinedVertices, 3));
this.setIndex(new Uint16BufferAttribute(combinedIndices, 1));
this.attributes.position.needsUpdate = true;
this.index.needsUpdate = true;
this.computeVertexNormals();
this.needsUpdate = true;
}
}
vertices and newPoints format data: [L,B,H,L,B,0]. They have already been converted with Cartesian3.fromDegrees.