Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 30 additions & 10 deletions src/scene/mesh.js
Original file line number Diff line number Diff line change
Expand Up @@ -1072,41 +1072,61 @@ 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]];

const base = this.primitive[RENDERSTYLE_SOLID].base;
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;
const i2 = srcIndices[j + offsets[k][1]] + baseVertex;
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,
Expand Down