diff --git a/src/scene/mesh.js b/src/scene/mesh.js index dd15a32b235..c87bb6fbaff 100644 --- a/src/scene/mesh.js +++ b/src/scene/mesh.js @@ -1072,8 +1072,9 @@ class Mesh extends RefCountedObject { const numVertices = this.vertexBuffer.numVertices; - const lines = []; + let lines; let format; + if (this.indexBuffer.length > 0 && this.indexBuffer[0]) { const offsets = [[0, 1], [1, 2], [2, 0]]; @@ -1081,9 +1082,13 @@ class Mesh extends RefCountedObject { const count = this.primitive[RENDERSTYLE_SOLID].count; const baseVertex = this.primitive[RENDERSTYLE_SOLID].baseVertex || 0; const indexBuffer = this.indexBuffer[RENDERSTYLE_SOLID]; - const srcIndices = new typedArrayIndexFormats[indexBuffer.format](indexBuffer.storage); + const indicesArrayType = typedArrayIndexFormats[indexBuffer.format]; + const srcIndices = new indicesArrayType(indexBuffer.storage); + const tmpIndices = new indicesArrayType(count * 2); const seen = new Set(); + let len = 0; + for (let j = base; j < base + count; j += 3) { for (let k = 0; k < 3; k++) { const i1 = srcIndices[j + offsets[k][0]] + baseVertex; @@ -1091,22 +1096,37 @@ class Mesh extends RefCountedObject { const hash = (i1 > i2) ? ((i2 * numVertices) + i1) : ((i1 * numVertices) + i2); if (!seen.has(hash)) { seen.add(hash); - lines.push(i1, i2); + tmpIndices[len++] = i1; + tmpIndices[len++] = i2; } } } + + seen.clear(); + format = indexBuffer.format; + lines = tmpIndices.slice(0, len); + } else { - for (let i = 0; i < numVertices; i += 3) { - lines.push(i, i + 1, i + 1, i + 2, i + 2, i); + const safeNumVertices = numVertices - (numVertices % 3); + const count = (safeNumVertices / 3) * 6; + + format = count > 65535 ? INDEXFORMAT_UINT32 : INDEXFORMAT_UINT16; + lines = count > 65535 ? new Uint32Array(count) : new Uint16Array(count); + + let idx = 0; + + for (let i = 0; i < safeNumVertices; i += 3) { + lines[idx++] = i; + lines[idx++] = i + 1; + lines[idx++] = i + 1; + lines[idx++] = i + 2; + lines[idx++] = i + 2; + lines[idx++] = i; } - format = lines.length > 65535 ? INDEXFORMAT_UINT32 : INDEXFORMAT_UINT16; } - const wireBuffer = new IndexBuffer(this.vertexBuffer.device, format, lines.length); - const dstIndices = new typedArrayIndexFormats[wireBuffer.format](wireBuffer.storage); - dstIndices.set(lines); - wireBuffer.unlock(); + const wireBuffer = new IndexBuffer(this.vertexBuffer.device, format, lines.length, BUFFER_STATIC, lines.buffer); this.primitive[RENDERSTYLE_WIREFRAME] = { type: PRIMITIVE_LINES,