From 992a52ec9b8ff2c096c8dc4f7f4f4bc995331963 Mon Sep 17 00:00:00 2001 From: ZTIF Date: Wed, 17 Dec 2025 20:25:18 +0100 Subject: [PATCH] Support PLY files with additional elements (e.g., SHARP camera data) This patch adds support for PLY files that contain additional elements beyond the standard vertex data, such as camera intrinsic/extrinsic matrices embedded by tools like Apple's SHARP. Changes: - Parse properties only within vertex elements, ignoring others - Limit DataView to exact vertex data size to prevent bounds errors - Maintain full backward compatibility with standard PLY files Fixes issues with PLY files from SHARP and other tools that embed camera or metadata elements in the PLY format. --- main.js | 30 +++++++++++++++++++++--------- 1 file changed, 21 insertions(+), 9 deletions(-) diff --git a/main.js b/main.js index 8f7f527..0b9ba6e 100644 --- a/main.js +++ b/main.js @@ -493,21 +493,33 @@ function createWorker(self) { ushort: "getUint16", uchar: "getUint8", }; - for (let prop of header - .slice(0, header_end_index) - .split("\n") - .filter((k) => k.startsWith("property "))) { - const [p, type, name] = prop.split(" "); - const arrayType = TYPE_MAP[type] || "getInt8"; - types[name] = arrayType; - offsets[name] = row_offset; - row_offset += parseInt(arrayType.replace(/[^\d]/g, "")) / 8; + // Only process properties for vertex elements (skip camera data) + let inVertexElement = false; + for (let line of header.slice(0, header_end_index).split("\n")) { + if (line.startsWith("element vertex")) { + inVertexElement = true; + continue; + } + if (line.startsWith("element ")) { + inVertexElement = false; + continue; + } + if (line.startsWith("property ") && inVertexElement) { + const [p, type, name] = line.split(" "); + const arrayType = TYPE_MAP[type] || "getInt8"; + types[name] = arrayType; + offsets[name] = row_offset; + row_offset += parseInt(arrayType.replace(/[^\d]/g, "")) / 8; + } } console.log("Bytes per row", row_offset, types, offsets); + // Calculate vertex data size and create DataView only for vertex data + const vertexDataSize = vertexCount * row_offset; let dataView = new DataView( inputBuffer, header_end_index + header_end.length, + vertexDataSize ); let row = 0; const attrs = new Proxy(