1 line
20 KiB
Plaintext
1 line
20 KiB
Plaintext
|
|
{"version":3,"file":"PLYExporter.cjs","sources":["../../src/exporters/PLYExporter.ts"],"sourcesContent":["import { BufferGeometry, Matrix3, Mesh, Object3D, Vector3 } from 'three'\n\n/**\n * https://github.com/gkjohnson/ply-exporter-js\n *\n * Usage:\n * const exporter = new PLYExporter();\n *\n * // second argument is a list of options\n * exporter.parse(mesh, data => console.log(data), { binary: true, excludeAttributes: [ 'color' ], littleEndian: true });\n *\n * Format Definition:\n * http://paulbourke.net/dataformats/ply/\n */\n\nexport interface PLYExporterOptions {\n binary?: boolean\n excludeAttributes?: string[]\n littleEndian?: boolean\n}\n\nclass PLYExporter {\n public parse(\n object: Object3D,\n onDone: ((res: string) => void) | undefined,\n options: PLYExporterOptions,\n ): string | ArrayBuffer | null {\n if (onDone && typeof onDone === 'object') {\n console.warn(\n 'THREE.PLYExporter: The options parameter is now the third argument to the \"parse\" function. See the documentation for the new API.',\n )\n options = onDone\n onDone = undefined\n }\n\n // Default options\n const defaultOptions = {\n binary: false,\n excludeAttributes: [], // normal, uv, color, index\n littleEndian: false,\n }\n\n options = Object.assign(defaultOptions, options)\n\n const excludeAttributes = options.excludeAttributes\n let includeNormals = false\n let includeColors = false\n let includeUVs = false\n\n // count the vertices, check which properties are used,\n // and cache the BufferGeometry\n let vertexCount = 0\n let faceCount = 0\n object.traverse(function (child) {\n if (child instanceof Mesh && child.isMesh) {\n const mesh = child\n const geometry = mesh.geometry\n\n if (!geometry.isBufferGeometry) {\n throw new Error('THREE.PLYExporter: Geometry is not of type THREE.BufferGeometry.')\n }\n\n const vertices = geometry.getAttribute('position')\n const normals = geometry.getAttribute('normal')\n const uvs = geometry.getAttribute('uv')\n const colors = geometry.getAttribute('color')\n const indices = geometry.getIndex()\n\n if (vertices === undefined) {\n return\n }\n\n vertexCount += vertices.count\n faceCount += indices ? indices.count / 3 : vertices.count / 3\n\n if (normals !== undefined) includeNormals = true\n\n if (uvs !== undefined) includeUVs = true\n\n if (colors !== undefined) includeColors = true\n }\n })\n\n const includeIndices = excludeAttributes?.indexOf('index') === -1\n includeNormals = includeNormals && excludeAttributes?.indexOf('normal') === -1\n includeColors = includeColors && excludeAttributes?.indexOf('color') === -1\n includeUVs = includeUVs && excludeAttributes?.indexOf('uv') === -1\n\n if (includeIndices && faceCount !== Math.floor(faceCount)) {\n // point cloud meshes will not have an index array and may not have a\n // number of vertices that is divisble by 3 (and therefore representable\n // as triangles)\n console.error(\n 'PLYExporter: Failed to generate a valid PLY file with triangle indices because the ' +\n 'number of indices is not divisible by 3.',\n )\n\n return null\n }\n\n const indexByteCount = 4\n\n let header =\n 'ply\\n' +\n `format ${\n options.binary ? (options.littleEndian ? 'binary_little_endian' : 'binary_big_endian') : 'ascii'\n } 1.0\\n` +\n `element vertex ${vertexCount}\\n` +\n // position\n 'property float x\\n' +\n 'property float y\\n' +\n 'property float z\\n'\n\n if (includeNormals) {\n // normal\n header += 'property float nx\\n' + 'property float ny\\n' + 'property float nz\\n'\n }\n\n if (includeUVs) {\n // uvs\n header += 'property float s\\n' + 'property float t\\n'\n }\n\n if (includeColors) {\n // colors\n header += 'property uchar red\\n' + 'property uchar gree
|