Our GLTF features an indexed BufferGeometry for all lines. Since I’d rather use Line2
than the native GL lines that GLTFLoader
uses by default, we remove all Line
elements and replace them with Line2
.
Ideally, I’d like to convert our BufferGeometry
to a LineSegmentsGeometry
like so:
function toLineSegmentsGeometry(bufferGeometry: BufferGeometry)
{
const lg = new LineSegmentsGeometry();
const a = bufferGeometry.attributes;
lg.setAttribute("position", a.position);
lg.setIndex(bufferGeometry.index);
lg.userData = bufferGeometry.userData;
return lg;
}
But for some reason, this doesn’t work, nothing is shown, neither error nor output.
The following less nice approach does work
function toLineSegmentsGeometry(bufferGeometry: BufferGeometry)
{
const lg = new LineSegmentsGeometry();
const a = bufferGeometry.attributes;
if (a.position === undefined) {
return;
}
const position = a.position;
if (!(position instanceof InterleavedBufferAttribute)) {
return;
}
const data = position.data;
if (!(data instanceof InterleavedBuffer)) {
return;
}
const stride = data.stride;
const indexAttribute = bufferGeometry.index;
if (indexAttribute === null) {
return;
}
const positions = [];
for (let index = 0; index !== indexAttribute.array.length; ++index) {
const i = indexAttribute.array[index] * stride;
const x = data.array[i];
const y = data.array[i + 1];
const z = data.array[i + 2];
positions.push(x, y, z);
}
lg.setPositions(positions);
lg.userData = bufferGeometry.userData;
return lg;
}