@margoengine/loaders 0.0.0-stage → 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +201 -0
- package/NOTICE +4 -0
- package/README.md +1665 -2
- package/dist/3dm.d.ts +161 -0
- package/dist/3dm.js +289 -0
- package/dist/3ds.d.ts +124 -0
- package/dist/3ds.js +392 -0
- package/dist/acclaim.d.ts +90 -0
- package/dist/acclaim.js +312 -0
- package/dist/amf.d.ts +45 -0
- package/dist/amf.js +256 -0
- package/dist/babylon.d.ts +197 -0
- package/dist/babylon.js +701 -0
- package/dist/common.d.ts +7 -0
- package/dist/common.js +6 -0
- package/dist/crease.d.ts +7 -0
- package/dist/crease.js +67 -0
- package/dist/dae-animation.d.ts +66 -0
- package/dist/dae-animation.js +266 -0
- package/dist/dae-common.d.ts +95 -0
- package/dist/dae-common.js +226 -0
- package/dist/dae-controllers.d.ts +57 -0
- package/dist/dae-controllers.js +164 -0
- package/dist/dae-kinematics.d.ts +65 -0
- package/dist/dae-kinematics.js +461 -0
- package/dist/dae-pose.d.ts +131 -0
- package/dist/dae-pose.js +381 -0
- package/dist/dae-scene.d.ts +203 -0
- package/dist/dae-scene.js +279 -0
- package/dist/dae.d.ts +166 -0
- package/dist/dae.js +644 -0
- package/dist/detect.d.ts +20 -0
- package/dist/detect.js +72 -0
- package/dist/dxf-hatch.d.ts +8 -0
- package/dist/dxf-hatch.js +346 -0
- package/dist/dxf-leaders.d.ts +59 -0
- package/dist/dxf-leaders.js +258 -0
- package/dist/dxf.d.ts +122 -0
- package/dist/dxf.js +985 -0
- package/dist/engine.d.ts +41 -0
- package/dist/engine.js +120 -0
- package/dist/font-opentype.d.ts +14 -0
- package/dist/font-opentype.js +33 -0
- package/dist/font-woff.d.ts +23 -0
- package/dist/font-woff.js +293 -0
- package/dist/font.d.ts +41 -0
- package/dist/font.js +26 -0
- package/dist/gcode.d.ts +92 -0
- package/dist/gcode.js +369 -0
- package/dist/geo-model.d.ts +84 -0
- package/dist/geo-model.js +220 -0
- package/dist/geo-mvt.d.ts +15 -0
- package/dist/geo-mvt.js +302 -0
- package/dist/geo-shp.d.ts +15 -0
- package/dist/geo-shp.js +234 -0
- package/dist/geo-xml.d.ts +6 -0
- package/dist/geo-xml.js +165 -0
- package/dist/geo.d.ts +8 -0
- package/dist/geo.js +59 -0
- package/dist/geometry.d.ts +60 -0
- package/dist/geometry.js +273 -0
- package/dist/index.d.ts +54 -0
- package/dist/index.js +28 -0
- package/dist/kmz.d.ts +78 -0
- package/dist/kmz.js +173 -0
- package/dist/las.d.ts +92 -0
- package/dist/las.js +110 -0
- package/dist/ldraw.d.ts +118 -0
- package/dist/ldraw.js +474 -0
- package/dist/load.d.ts +72 -0
- package/dist/load.js +205 -0
- package/dist/lottie.d.ts +154 -0
- package/dist/lottie.js +893 -0
- package/dist/lwo.d.ts +81 -0
- package/dist/lwo.js +454 -0
- package/dist/lzf.d.ts +13 -0
- package/dist/lzf.js +56 -0
- package/dist/materialx.d.ts +120 -0
- package/dist/materialx.js +563 -0
- package/dist/md2.d.ts +52 -0
- package/dist/md2.js +112 -0
- package/dist/mmcif.d.ts +14 -0
- package/dist/mmcif.js +246 -0
- package/dist/mmd.d.ts +244 -0
- package/dist/mmd.js +958 -0
- package/dist/mocap.d.ts +71 -0
- package/dist/mocap.js +178 -0
- package/dist/molecule-common.d.ts +4 -0
- package/dist/molecule-common.js +54 -0
- package/dist/mtl.d.ts +125 -0
- package/dist/mtl.js +550 -0
- package/dist/normals.d.ts +7 -0
- package/dist/normals.js +31 -0
- package/dist/nrrd.d.ts +70 -0
- package/dist/nrrd.js +785 -0
- package/dist/nurbs.d.ts +118 -0
- package/dist/nurbs.js +265 -0
- package/dist/obj.d.ts +145 -0
- package/dist/obj.js +462 -0
- package/dist/off.d.ts +10 -0
- package/dist/off.js +155 -0
- package/dist/outline-font.d.ts +87 -0
- package/dist/outline-font.js +110 -0
- package/dist/outline.d.ts +20 -0
- package/dist/outline.js +215 -0
- package/dist/pcd.d.ts +34 -0
- package/dist/pcd.js +258 -0
- package/dist/pdb.d.ts +67 -0
- package/dist/pdb.js +126 -0
- package/dist/ply.d.ts +35 -0
- package/dist/ply.js +392 -0
- package/dist/polygon.d.ts +9 -0
- package/dist/polygon.js +49 -0
- package/dist/prwm.d.ts +24 -0
- package/dist/prwm.js +114 -0
- package/dist/render.d.ts +32 -0
- package/dist/render.js +181 -0
- package/dist/sfnt-cff.d.ts +37 -0
- package/dist/sfnt-cff.js +749 -0
- package/dist/sfnt-layout.d.ts +24 -0
- package/dist/sfnt-layout.js +266 -0
- package/dist/sfnt-reader.d.ts +28 -0
- package/dist/sfnt-reader.js +87 -0
- package/dist/sfnt-variation.d.ts +40 -0
- package/dist/sfnt-variation.js +318 -0
- package/dist/shared/asset-url.js +82 -0
- package/dist/shared/crc32.d.ts +2 -0
- package/dist/shared/crc32.js +47 -0
- package/dist/shared/errors.js +36 -0
- package/dist/shared/inflate.js +284 -0
- package/dist/shared/normals.js +26 -0
- package/dist/shared/scene-scope.js +56 -0
- package/dist/shared/trailing-data.js +45 -0
- package/dist/stl.d.ts +64 -0
- package/dist/stl.js +309 -0
- package/dist/svg.d.ts +90 -0
- package/dist/svg.js +1007 -0
- package/dist/tds.d.ts +2 -0
- package/dist/tds.js +2 -0
- package/dist/textures.d.ts +42 -0
- package/dist/textures.js +53 -0
- package/dist/threemf-beam-reader.d.ts +19 -0
- package/dist/threemf-beam-reader.js +165 -0
- package/dist/threemf-beams.d.ts +52 -0
- package/dist/threemf-beams.js +268 -0
- package/dist/threemf-boolean-read.d.ts +10 -0
- package/dist/threemf-boolean-read.js +113 -0
- package/dist/threemf-boolean.d.ts +59 -0
- package/dist/threemf-boolean.js +20 -0
- package/dist/threemf-csg.d.ts +14 -0
- package/dist/threemf-csg.js +193 -0
- package/dist/threemf-displacement-read.d.ts +8 -0
- package/dist/threemf-displacement-read.js +213 -0
- package/dist/threemf-displacement.d.ts +71 -0
- package/dist/threemf-displacement.js +18 -0
- package/dist/threemf-evaluate.d.ts +36 -0
- package/dist/threemf-evaluate.js +86 -0
- package/dist/threemf-functions.d.ts +33 -0
- package/dist/threemf-functions.js +359 -0
- package/dist/threemf-lattice-mesh.d.ts +9 -0
- package/dist/threemf-lattice-mesh.js +115 -0
- package/dist/threemf-lattice.d.ts +139 -0
- package/dist/threemf-lattice.js +379 -0
- package/dist/threemf-levelset.d.ts +23 -0
- package/dist/threemf-levelset.js +105 -0
- package/dist/threemf-materials.d.ts +162 -0
- package/dist/threemf-materials.js +497 -0
- package/dist/threemf-mesh.d.ts +75 -0
- package/dist/threemf-mesh.js +422 -0
- package/dist/threemf-secure.d.ts +50 -0
- package/dist/threemf-secure.js +153 -0
- package/dist/threemf-tessellate.d.ts +37 -0
- package/dist/threemf-tessellate.js +277 -0
- package/dist/threemf-volume-colour.d.ts +25 -0
- package/dist/threemf-volume-colour.js +233 -0
- package/dist/threemf-volume-sample.d.ts +8 -0
- package/dist/threemf-volume-sample.js +65 -0
- package/dist/threemf-volumetric-read.d.ts +15 -0
- package/dist/threemf-volumetric-read.js +539 -0
- package/dist/threemf-volumetric.d.ts +177 -0
- package/dist/threemf-volumetric.js +28 -0
- package/dist/threemf.d.ts +170 -0
- package/dist/threemf.js +609 -0
- package/dist/tilt.d.ts +62 -0
- package/dist/tilt.js +186 -0
- package/dist/tmx.d.ts +151 -0
- package/dist/tmx.js +394 -0
- package/dist/ttf.d.ts +78 -0
- package/dist/ttf.js +568 -0
- package/dist/types.d.ts +108 -0
- package/dist/types.js +35 -0
- package/dist/usd.d.ts +2 -0
- package/dist/usd.js +2 -0
- package/dist/usd.worker.d.ts +1 -0
- package/dist/usdz.d.ts +50 -0
- package/dist/usdz.js +646 -0
- package/dist/util.d.ts +75 -0
- package/dist/util.js +241 -0
- package/dist/vdb.d.ts +111 -0
- package/dist/vdb.js +663 -0
- package/dist/vrml.d.ts +53 -0
- package/dist/vrml.js +532 -0
- package/dist/vtk-cells.d.ts +29 -0
- package/dist/vtk-cells.js +211 -0
- package/dist/vtk-legacy.d.ts +57 -0
- package/dist/vtk-legacy.js +439 -0
- package/dist/vtk-reader.d.ts +19 -0
- package/dist/vtk-reader.js +134 -0
- package/dist/vtk-xml.d.ts +96 -0
- package/dist/vtk-xml.js +578 -0
- package/dist/vtk.d.ts +13 -0
- package/dist/vtk.js +234 -0
- package/dist/woff-glyf.d.ts +17 -0
- package/dist/woff-glyf.js +215 -0
- package/dist/worker.d.ts +10 -0
- package/dist/worker.js +93 -0
- package/dist/workers/usd.js +3 -0
- package/dist/xfile.d.ts +99 -0
- package/dist/xfile.js +706 -0
- package/dist/xml.d.ts +107 -0
- package/dist/xml.js +843 -0
- package/dist/xyz.d.ts +16 -0
- package/dist/xyz.js +110 -0
- package/dist/zip.d.ts +38 -0
- package/dist/zip.js +150 -0
- package/package.json +289 -4
package/dist/ply.js
ADDED
|
@@ -0,0 +1,392 @@
|
|
|
1
|
+
import { LoaderError } from './types.js';
|
|
2
|
+
import { checkCount, latin1, limits, toBytes, triangulate, uints, TextScanner } from './util.js';
|
|
3
|
+
const F = 'PLY';
|
|
4
|
+
const TYPES = {
|
|
5
|
+
char: 'int8',
|
|
6
|
+
int8: 'int8',
|
|
7
|
+
uchar: 'uint8',
|
|
8
|
+
uint8: 'uint8',
|
|
9
|
+
short: 'int16',
|
|
10
|
+
int16: 'int16',
|
|
11
|
+
ushort: 'uint16',
|
|
12
|
+
uint16: 'uint16',
|
|
13
|
+
int: 'int32',
|
|
14
|
+
int32: 'int32',
|
|
15
|
+
uint: 'uint32',
|
|
16
|
+
uint32: 'uint32',
|
|
17
|
+
float: 'float32',
|
|
18
|
+
float32: 'float32',
|
|
19
|
+
double: 'float64',
|
|
20
|
+
float64: 'float64',
|
|
21
|
+
};
|
|
22
|
+
const SIZES = {
|
|
23
|
+
int8: 1,
|
|
24
|
+
uint8: 1,
|
|
25
|
+
int16: 2,
|
|
26
|
+
uint16: 2,
|
|
27
|
+
int32: 4,
|
|
28
|
+
uint32: 4,
|
|
29
|
+
float32: 4,
|
|
30
|
+
float64: 8,
|
|
31
|
+
};
|
|
32
|
+
const UNIT = {
|
|
33
|
+
uint8: 255,
|
|
34
|
+
uint16: 65535,
|
|
35
|
+
uint32: 4294967295,
|
|
36
|
+
int8: 127,
|
|
37
|
+
int16: 32767,
|
|
38
|
+
int32: 2147483647,
|
|
39
|
+
};
|
|
40
|
+
// Numeric type codes keep the binary row loop on one call site instead of a table of per-type closures.
|
|
41
|
+
const CODES = {
|
|
42
|
+
int8: 0,
|
|
43
|
+
uint8: 1,
|
|
44
|
+
int16: 2,
|
|
45
|
+
uint16: 3,
|
|
46
|
+
int32: 4,
|
|
47
|
+
uint32: 5,
|
|
48
|
+
float32: 6,
|
|
49
|
+
float64: 7,
|
|
50
|
+
};
|
|
51
|
+
const CODE_SIZES = [1, 1, 2, 2, 4, 4, 4, 8];
|
|
52
|
+
function read(view, offset, code, little) {
|
|
53
|
+
switch (code) {
|
|
54
|
+
case 0:
|
|
55
|
+
return view.getInt8(offset);
|
|
56
|
+
case 1:
|
|
57
|
+
return view.getUint8(offset);
|
|
58
|
+
case 2:
|
|
59
|
+
return view.getInt16(offset, little);
|
|
60
|
+
case 3:
|
|
61
|
+
return view.getUint16(offset, little);
|
|
62
|
+
case 4:
|
|
63
|
+
return view.getInt32(offset, little);
|
|
64
|
+
case 5:
|
|
65
|
+
return view.getUint32(offset, little);
|
|
66
|
+
case 6:
|
|
67
|
+
return view.getFloat32(offset, little);
|
|
68
|
+
default:
|
|
69
|
+
return view.getFloat64(offset, little);
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
const MAX_HEADER = 1 << 20;
|
|
73
|
+
const MAX_POLYGON = 65536;
|
|
74
|
+
/** Parses only the PLY header. */
|
|
75
|
+
export function parsePLYHeader(input) {
|
|
76
|
+
const bytes = toBytes(input, F);
|
|
77
|
+
if (bytes.length < 4 || latin1(bytes, 0, 3) !== 'ply' || (bytes[3] !== 10 && bytes[3] !== 13))
|
|
78
|
+
throw new LoaderError(F, 'header', 'missing "ply" magic');
|
|
79
|
+
const text = latin1(bytes, 0, Math.min(bytes.length, MAX_HEADER));
|
|
80
|
+
const match = /(^|\n)end_header[ \t]*\r?\n/.exec(text);
|
|
81
|
+
if (!match)
|
|
82
|
+
throw new LoaderError(F, bytes.length > MAX_HEADER ? 'header' : 'truncated', 'end_header not found');
|
|
83
|
+
const bodyOffset = match.index + match[0].length;
|
|
84
|
+
const lines = text
|
|
85
|
+
.slice(0, match.index)
|
|
86
|
+
.split('\n')
|
|
87
|
+
.map((line) => line.trim())
|
|
88
|
+
.filter(Boolean)
|
|
89
|
+
.slice(1);
|
|
90
|
+
let format;
|
|
91
|
+
const elements = [], comments = [];
|
|
92
|
+
for (const line of lines) {
|
|
93
|
+
const parts = line.split(/\s+/), keyword = parts[0];
|
|
94
|
+
if (keyword === 'comment' || keyword === 'obj_info') {
|
|
95
|
+
comments.push(line.slice(keyword.length).trim());
|
|
96
|
+
continue;
|
|
97
|
+
}
|
|
98
|
+
if (keyword === 'format') {
|
|
99
|
+
if (parts[1] !== 'ascii' && parts[1] !== 'binary_little_endian' && parts[1] !== 'binary_big_endian')
|
|
100
|
+
throw new LoaderError(F, 'unsupported', `unknown format "${parts[1]}"`);
|
|
101
|
+
if (parts[2] !== '1.0')
|
|
102
|
+
throw new LoaderError(F, 'unsupported', `unsupported version "${parts[2]}"`);
|
|
103
|
+
format = parts[1];
|
|
104
|
+
}
|
|
105
|
+
else if (keyword === 'element') {
|
|
106
|
+
const count = Number(parts[2]);
|
|
107
|
+
if (parts.length !== 3 || !Number.isSafeInteger(count) || count < 0)
|
|
108
|
+
throw new LoaderError(F, 'header', `invalid element line "${line.slice(0, 80)}"`);
|
|
109
|
+
elements.push({ name: parts[1], count, properties: [] });
|
|
110
|
+
}
|
|
111
|
+
else if (keyword === 'property') {
|
|
112
|
+
const element = elements[elements.length - 1];
|
|
113
|
+
if (!element)
|
|
114
|
+
throw new LoaderError(F, 'header', 'property before any element');
|
|
115
|
+
if (parts[1] === 'list') {
|
|
116
|
+
// Own keys only: `property constructor x` must not resolve Object.prototype.constructor as a type.
|
|
117
|
+
const count = Object.hasOwn(TYPES, parts[2]) ? TYPES[parts[2]] : undefined, item = Object.hasOwn(TYPES, parts[3]) ? TYPES[parts[3]] : undefined;
|
|
118
|
+
if (parts.length !== 5 || !count || !item)
|
|
119
|
+
throw new LoaderError(F, 'header', `invalid list property "${line.slice(0, 80)}"`);
|
|
120
|
+
if (count === 'float32' || count === 'float64')
|
|
121
|
+
throw new LoaderError(F, 'header', 'list counts must be integers');
|
|
122
|
+
element.properties.push({ name: parts[4], type: item, list: { count } });
|
|
123
|
+
}
|
|
124
|
+
else {
|
|
125
|
+
const type = Object.hasOwn(TYPES, parts[1]) ? TYPES[parts[1]] : undefined;
|
|
126
|
+
if (parts.length !== 3 || !type)
|
|
127
|
+
throw new LoaderError(F, 'header', `invalid property "${line.slice(0, 80)}"`);
|
|
128
|
+
element.properties.push({ name: parts[2], type });
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
else
|
|
132
|
+
comments.push(line.trim()); // Blender 2.4x wrote a bare "Created by ..." line with no comment keyword.
|
|
133
|
+
}
|
|
134
|
+
if (!format)
|
|
135
|
+
throw new LoaderError(F, 'header', 'missing format line');
|
|
136
|
+
return { format, elements, comments, bodyOffset };
|
|
137
|
+
}
|
|
138
|
+
const find = (properties, names) => {
|
|
139
|
+
for (const name of names) {
|
|
140
|
+
const index = properties.findIndex((p) => p.name === name && !p.list);
|
|
141
|
+
if (index >= 0)
|
|
142
|
+
return index;
|
|
143
|
+
}
|
|
144
|
+
return -1;
|
|
145
|
+
};
|
|
146
|
+
/**
|
|
147
|
+
* Parses an ASCII or binary (little or big endian) PLY file.
|
|
148
|
+
* Faces become a triangle fan; a file without faces is returned as a point cloud.
|
|
149
|
+
*/
|
|
150
|
+
export function parsePLY(input, options = {}) {
|
|
151
|
+
const bytes = toBytes(input, F), limit = limits(options, F), header = parsePLYHeader(bytes);
|
|
152
|
+
const vertexElement = header.elements.find((e) => e.name === 'vertex');
|
|
153
|
+
if (!vertexElement)
|
|
154
|
+
throw new LoaderError(F, 'header', 'no vertex element');
|
|
155
|
+
const vertexCount = checkCount(vertexElement.count, limit.maxVertices, F, 'vertex');
|
|
156
|
+
const faceElement = header.elements.find((e) => e.name === 'face');
|
|
157
|
+
if (faceElement)
|
|
158
|
+
checkCount(faceElement.count, limit.maxTriangles, F, 'face');
|
|
159
|
+
const elementLimit = Math.max(limit.maxVertices, limit.maxTriangles);
|
|
160
|
+
for (const element of header.elements)
|
|
161
|
+
checkCount(element.count, elementLimit, F, `"${element.name}" element`);
|
|
162
|
+
// Budget before allocation: the body must be able to hold the declared counts (an ASCII value takes at least a
|
|
163
|
+
// digit and a separator, a binary row at least its scalars and list counts), so a short file cannot claim millions.
|
|
164
|
+
const ascii = header.format === 'ascii';
|
|
165
|
+
let needed = ascii ? -1 : 0;
|
|
166
|
+
for (const element of header.elements)
|
|
167
|
+
needed +=
|
|
168
|
+
element.count *
|
|
169
|
+
(ascii
|
|
170
|
+
? 2 * element.properties.length
|
|
171
|
+
: element.properties.reduce((sum, p) => sum + SIZES[p.list ? p.list.count : p.type], 0));
|
|
172
|
+
if (needed > bytes.length - header.bodyOffset)
|
|
173
|
+
throw new LoaderError(F, 'truncated', 'body is too short for the declared element counts');
|
|
174
|
+
const props = vertexElement.properties;
|
|
175
|
+
const px = find(props, ['x']), py = find(props, ['y']), pz = find(props, ['z']);
|
|
176
|
+
if (px < 0 || py < 0 || pz < 0)
|
|
177
|
+
throw new LoaderError(F, 'header', 'vertex element needs x, y and z properties');
|
|
178
|
+
const nx = find(props, ['nx', 'normal_x']), ny = find(props, ['ny', 'normal_y']), nz = find(props, ['nz', 'normal_z']);
|
|
179
|
+
const cr = find(props, ['red', 'diffuse_red', 'r']), cg = find(props, ['green', 'diffuse_green', 'g']), cb = find(props, ['blue', 'diffuse_blue', 'b']);
|
|
180
|
+
const tu = find(props, ['s', 'u', 'texture_u', 'texture_s']), tv = find(props, ['t', 'v', 'texture_v', 'texture_t']);
|
|
181
|
+
const hasNormals = nx >= 0 && ny >= 0 && nz >= 0, hasColors = cr >= 0 && cg >= 0 && cb >= 0, hasUVs = tu >= 0 && tv >= 0;
|
|
182
|
+
const positions = new Float32Array(vertexCount * 3);
|
|
183
|
+
const normals = hasNormals ? new Float32Array(vertexCount * 3) : undefined;
|
|
184
|
+
const colors = hasColors ? new Float32Array(vertexCount * 3) : undefined;
|
|
185
|
+
const uvs = hasUVs ? new Float32Array(vertexCount * 2) : undefined;
|
|
186
|
+
const colorScale = hasColors ? [cr, cg, cb].map((i) => UNIT[props[i].type] ?? 1) : [1, 1, 1];
|
|
187
|
+
const indices = uints(faceElement ? faceElement.count * 3 : 0);
|
|
188
|
+
const row = new Float64Array(Math.max(1, ...header.elements.map((e) => e.properties.length)));
|
|
189
|
+
let polygon = new Float64Array(0);
|
|
190
|
+
const storeVertex = (i) => {
|
|
191
|
+
positions[i * 3] = row[px];
|
|
192
|
+
positions[i * 3 + 1] = row[py];
|
|
193
|
+
positions[i * 3 + 2] = row[pz];
|
|
194
|
+
if (normals) {
|
|
195
|
+
normals[i * 3] = row[nx];
|
|
196
|
+
normals[i * 3 + 1] = row[ny];
|
|
197
|
+
normals[i * 3 + 2] = row[nz];
|
|
198
|
+
}
|
|
199
|
+
if (colors) {
|
|
200
|
+
colors[i * 3] = row[cr] / colorScale[0];
|
|
201
|
+
colors[i * 3 + 1] = row[cg] / colorScale[1];
|
|
202
|
+
colors[i * 3 + 2] = row[cb] / colorScale[2];
|
|
203
|
+
}
|
|
204
|
+
if (uvs) {
|
|
205
|
+
uvs[i * 2] = row[tu];
|
|
206
|
+
uvs[i * 2 + 1] = row[tv];
|
|
207
|
+
}
|
|
208
|
+
};
|
|
209
|
+
const faceList = faceElement
|
|
210
|
+
? faceElement.properties.findIndex((p) => p.list && (p.name === 'vertex_indices' || p.name === 'vertex_index'))
|
|
211
|
+
: -1;
|
|
212
|
+
if (faceElement && faceElement.count > 0 && faceList < 0)
|
|
213
|
+
throw new LoaderError(F, 'header', 'face element has no vertex_indices list');
|
|
214
|
+
const groups = [];
|
|
215
|
+
let faceStart = 0;
|
|
216
|
+
let faceProperties = Object.create(null);
|
|
217
|
+
const faceProps = faceElement?.properties ?? [];
|
|
218
|
+
const faceRGB = [
|
|
219
|
+
['red', 'diffuse_red', 'r'],
|
|
220
|
+
['green', 'diffuse_green', 'g'],
|
|
221
|
+
['blue', 'diffuse_blue', 'b'],
|
|
222
|
+
].map((names) => find(faceProps, names));
|
|
223
|
+
const faceAlpha = find(faceProps, ['alpha', 'opacity', 'a']);
|
|
224
|
+
const storeFace = () => {
|
|
225
|
+
if (faceProps.length <= 1)
|
|
226
|
+
return;
|
|
227
|
+
for (let p = 0; p < faceProps.length; p++) {
|
|
228
|
+
if (!faceProps[p].list)
|
|
229
|
+
faceProperties[faceProps[p].name] = row[p];
|
|
230
|
+
}
|
|
231
|
+
const group = { start: faceStart, count: indices.length - faceStart, properties: faceProperties };
|
|
232
|
+
if (faceRGB.every((p) => p >= 0)) {
|
|
233
|
+
const rgba = [...faceRGB, faceAlpha].map((p) => (p < 0 ? 1 : row[p] / (UNIT[faceProps[p].type] ?? 1)));
|
|
234
|
+
if (rgba.some((v) => !Number.isFinite(v) || v < 0 || v > 1))
|
|
235
|
+
throw new LoaderError(F, 'range', 'face color must be in its declared type range');
|
|
236
|
+
group.color = rgba;
|
|
237
|
+
}
|
|
238
|
+
groups.push(group);
|
|
239
|
+
faceStart = indices.length;
|
|
240
|
+
faceProperties = Object.create(null);
|
|
241
|
+
};
|
|
242
|
+
// Turk's PLY `edge` element: vertex1 and vertex2 (and optional colour), read as line segments.
|
|
243
|
+
const edgeElement = header.elements.find((e) => e.name === 'edge'), e1 = edgeElement ? find(edgeElement.properties, ['vertex1']) : -1, e2 = edgeElement ? find(edgeElement.properties, ['vertex2']) : -1;
|
|
244
|
+
if (edgeElement &&
|
|
245
|
+
edgeElement.count > 0 &&
|
|
246
|
+
(e1 < 0 || e2 < 0 || edgeElement.properties[e1].list || edgeElement.properties[e2].list))
|
|
247
|
+
throw new LoaderError(F, 'header', 'edge element needs scalar vertex1 and vertex2 properties');
|
|
248
|
+
const lines = uints(edgeElement ? edgeElement.count * 2 : 0);
|
|
249
|
+
const storeEdge = (i) => {
|
|
250
|
+
for (const v of [row[e1], row[e2]]) {
|
|
251
|
+
if (!Number.isInteger(v) || v < 0 || v >= vertexCount)
|
|
252
|
+
throw new LoaderError(F, 'range', `edge ${i} names vertex ${v}, outside 0..${vertexCount - 1}`);
|
|
253
|
+
lines.push(v);
|
|
254
|
+
}
|
|
255
|
+
};
|
|
256
|
+
const at = (corner) => polygon[corner];
|
|
257
|
+
const checkCorners = (count) => {
|
|
258
|
+
if (!Number.isSafeInteger(count) || count < 0 || count > MAX_POLYGON)
|
|
259
|
+
throw new LoaderError(F, 'range', `invalid list length ${count}`);
|
|
260
|
+
if (count > polygon.length)
|
|
261
|
+
polygon = new Float64Array(Math.min(MAX_POLYGON, Math.max(count, polygon.length * 2, 16)));
|
|
262
|
+
};
|
|
263
|
+
if (header.format === 'ascii') {
|
|
264
|
+
const scanner = new TextScanner(latin1(bytes, header.bodyOffset), F);
|
|
265
|
+
// Runs of up to 15 digits (indices, list lengths, colors) are summed in place, which is exact. Other plain numbers
|
|
266
|
+
// go through Number; anything else (nan spellings, garbage, end of data) is read again by the scanner, which
|
|
267
|
+
// builds the error label only then.
|
|
268
|
+
const body = scanner.text;
|
|
269
|
+
const number = (name, i, suffix) => {
|
|
270
|
+
let pos = scanner.pos, code = body.charCodeAt(pos);
|
|
271
|
+
while (code === 32 || (code >= 9 && code <= 13))
|
|
272
|
+
code = body.charCodeAt(++pos);
|
|
273
|
+
let integer = 0;
|
|
274
|
+
const first = pos;
|
|
275
|
+
while (code >= 48 && code <= 57 && pos - first < 15) {
|
|
276
|
+
integer = integer * 10 + (code - 48);
|
|
277
|
+
code = body.charCodeAt(++pos);
|
|
278
|
+
}
|
|
279
|
+
if (pos > first && (pos >= body.length || code === 32 || (code >= 9 && code <= 13))) {
|
|
280
|
+
scanner.pos = pos;
|
|
281
|
+
return integer;
|
|
282
|
+
}
|
|
283
|
+
const start = scanner.pos, token = scanner.token(), value = token === null ? NaN : Number(token);
|
|
284
|
+
if (value === value)
|
|
285
|
+
return value;
|
|
286
|
+
scanner.pos = start;
|
|
287
|
+
return scanner.number(`${name} ${i}${suffix}`);
|
|
288
|
+
};
|
|
289
|
+
for (const element of header.elements) {
|
|
290
|
+
const isVertex = element === vertexElement, isFace = element === faceElement, isEdge = element === edgeElement, properties = element.properties, name = element.name;
|
|
291
|
+
for (let i = 0; i < element.count; i++) {
|
|
292
|
+
for (let p = 0; p < properties.length; p++) {
|
|
293
|
+
if (!properties[p].list) {
|
|
294
|
+
row[p] = number(name, i, '');
|
|
295
|
+
continue;
|
|
296
|
+
}
|
|
297
|
+
const count = number(name, i, ' list length');
|
|
298
|
+
if (!Number.isSafeInteger(count))
|
|
299
|
+
throw new LoaderError(F, 'syntax', `expected an integer in ${name} ${i} list length, got ${count}`);
|
|
300
|
+
checkCorners(count);
|
|
301
|
+
for (let k = 0; k < count; k++)
|
|
302
|
+
polygon[k] = number(name, i, ' list');
|
|
303
|
+
if (isFace && p === faceList)
|
|
304
|
+
triangulate(indices, count, at, vertexCount, F, limit.maxTriangles);
|
|
305
|
+
if (isFace && p !== faceList)
|
|
306
|
+
faceProperties[properties[p].name] = Array.from(polygon.subarray(0, count));
|
|
307
|
+
}
|
|
308
|
+
if (isVertex)
|
|
309
|
+
storeVertex(i);
|
|
310
|
+
if (isFace)
|
|
311
|
+
storeFace();
|
|
312
|
+
if (isEdge)
|
|
313
|
+
storeEdge(i);
|
|
314
|
+
}
|
|
315
|
+
}
|
|
316
|
+
}
|
|
317
|
+
else {
|
|
318
|
+
const little = header.format === 'binary_little_endian';
|
|
319
|
+
const view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
|
|
320
|
+
let offset = header.bodyOffset;
|
|
321
|
+
const end = bytes.byteLength;
|
|
322
|
+
const truncated = (what) => {
|
|
323
|
+
throw new LoaderError(F, 'truncated', `binary data ends inside ${what}`);
|
|
324
|
+
};
|
|
325
|
+
for (const element of header.elements) {
|
|
326
|
+
const isVertex = element === vertexElement, isFace = element === faceElement, isEdge = element === edgeElement, properties = element.properties, width = properties.length;
|
|
327
|
+
const minimum = properties.reduce((sum, p) => sum + SIZES[p.list ? p.list.count : p.type], 0);
|
|
328
|
+
if (element.count * minimum > end - offset)
|
|
329
|
+
truncated(`the "${element.name}" element`);
|
|
330
|
+
const fixed = properties.every((p) => !p.list);
|
|
331
|
+
// Fixed-size rows of other elements are never looked at, and the check above already proved they fit.
|
|
332
|
+
if (fixed && !isVertex && !isFace && !isEdge) {
|
|
333
|
+
offset += element.count * minimum;
|
|
334
|
+
continue;
|
|
335
|
+
}
|
|
336
|
+
// Value type and list count type (-1 for a scalar) of each property.
|
|
337
|
+
const codes = properties.map((p) => CODES[p.type]), counts = properties.map((p) => (p.list ? CODES[p.list.count] : -1));
|
|
338
|
+
for (let i = 0; i < element.count; i++) {
|
|
339
|
+
for (let p = 0; p < width; p++) {
|
|
340
|
+
const code = codes[p], itemSize = CODE_SIZES[code], countCode = counts[p];
|
|
341
|
+
if (countCode < 0) {
|
|
342
|
+
if (!fixed && offset + itemSize > end)
|
|
343
|
+
truncated(`${element.name} ${i}`);
|
|
344
|
+
row[p] = read(view, offset, code, little);
|
|
345
|
+
offset += itemSize;
|
|
346
|
+
continue;
|
|
347
|
+
}
|
|
348
|
+
const countSize = CODE_SIZES[countCode];
|
|
349
|
+
if (offset + countSize > end)
|
|
350
|
+
truncated(`${element.name} ${i}`);
|
|
351
|
+
const count = read(view, offset, countCode, little);
|
|
352
|
+
offset += countSize;
|
|
353
|
+
checkCorners(count);
|
|
354
|
+
if (offset + count * itemSize > end)
|
|
355
|
+
truncated(`${element.name} ${i}`);
|
|
356
|
+
if (isFace) {
|
|
357
|
+
for (let k = 0; k < count; k++) {
|
|
358
|
+
polygon[k] = read(view, offset, code, little);
|
|
359
|
+
offset += itemSize;
|
|
360
|
+
}
|
|
361
|
+
if (p === faceList)
|
|
362
|
+
triangulate(indices, count, at, vertexCount, F, limit.maxTriangles);
|
|
363
|
+
else
|
|
364
|
+
faceProperties[properties[p].name] = Array.from(polygon.subarray(0, count));
|
|
365
|
+
}
|
|
366
|
+
else
|
|
367
|
+
offset += count * itemSize;
|
|
368
|
+
}
|
|
369
|
+
if (isVertex)
|
|
370
|
+
storeVertex(i);
|
|
371
|
+
if (isFace)
|
|
372
|
+
storeFace();
|
|
373
|
+
if (isEdge)
|
|
374
|
+
storeEdge(i);
|
|
375
|
+
}
|
|
376
|
+
}
|
|
377
|
+
}
|
|
378
|
+
const result = { kind: indices.length ? 'mesh' : 'points', positions };
|
|
379
|
+
if (normals)
|
|
380
|
+
result.normals = normals;
|
|
381
|
+
if (colors)
|
|
382
|
+
result.colors = colors;
|
|
383
|
+
if (uvs)
|
|
384
|
+
result.uvs = uvs;
|
|
385
|
+
if (indices.length)
|
|
386
|
+
result.indices = indices.result();
|
|
387
|
+
if (lines.length)
|
|
388
|
+
result.lineIndices = lines.result();
|
|
389
|
+
if (groups.length)
|
|
390
|
+
result.groups = groups;
|
|
391
|
+
return result;
|
|
392
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Corner triples (0 based, into `corners`) of a polygon that is not convex, ear-clipped on the plane of its Newell
|
|
3
|
+
* normal so each triple turns the way the polygon does; `null` when the polygon is convex or degenerate, where a fan
|
|
4
|
+
* from corner 0 is right. Ear clipping: D. Eberly, Triangulation by Ear Clipping, 2002 (geometry/triangulate).
|
|
5
|
+
*
|
|
6
|
+
* @param points Coordinates, three per point.
|
|
7
|
+
* @param corners Point indices of the polygon in order.
|
|
8
|
+
*/
|
|
9
|
+
export declare function concaveTriangles(points: ArrayLike<number>, corners: ArrayLike<number>): number[] | null;
|
package/dist/polygon.js
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
// Triangles of one planar polygon given by point indices, for formats whose polygons may be concave.
|
|
2
|
+
import { triangulate } from '@margoengine/geometry/triangulate';
|
|
3
|
+
/**
|
|
4
|
+
* Corner triples (0 based, into `corners`) of a polygon that is not convex, ear-clipped on the plane of its Newell
|
|
5
|
+
* normal so each triple turns the way the polygon does; `null` when the polygon is convex or degenerate, where a fan
|
|
6
|
+
* from corner 0 is right. Ear clipping: D. Eberly, Triangulation by Ear Clipping, 2002 (geometry/triangulate).
|
|
7
|
+
*
|
|
8
|
+
* @param points Coordinates, three per point.
|
|
9
|
+
* @param corners Point indices of the polygon in order.
|
|
10
|
+
*/
|
|
11
|
+
export function concaveTriangles(points, corners) {
|
|
12
|
+
const n = corners.length;
|
|
13
|
+
if (n < 4)
|
|
14
|
+
return null;
|
|
15
|
+
let nx = 0, ny = 0, nz = 0;
|
|
16
|
+
for (let c = 0, d = n - 1; c < n; d = c++) {
|
|
17
|
+
const a = corners[d] * 3, b = corners[c] * 3;
|
|
18
|
+
nx += (points[a + 1] - points[b + 1]) * (points[a + 2] + points[b + 2]);
|
|
19
|
+
ny += (points[a + 2] - points[b + 2]) * (points[a] + points[b]);
|
|
20
|
+
nz += (points[a] - points[b]) * (points[a + 1] + points[b + 1]);
|
|
21
|
+
}
|
|
22
|
+
const ax = Math.abs(nx), ay = Math.abs(ny), az = Math.abs(nz);
|
|
23
|
+
if (!(ax + ay + az > 0))
|
|
24
|
+
return null;
|
|
25
|
+
// Drop the normal's largest axis; swap the other two when the normal points down it, so the turn is kept.
|
|
26
|
+
const [u, w] = az >= ax && az >= ay
|
|
27
|
+
? nz >= 0
|
|
28
|
+
? [0, 1]
|
|
29
|
+
: [1, 0]
|
|
30
|
+
: ax >= ay
|
|
31
|
+
? nx >= 0
|
|
32
|
+
? [1, 2]
|
|
33
|
+
: [2, 1]
|
|
34
|
+
: ny >= 0
|
|
35
|
+
? [2, 0]
|
|
36
|
+
: [0, 2];
|
|
37
|
+
const flat = new Array(n * 2);
|
|
38
|
+
for (let c = 0; c < n; c++) {
|
|
39
|
+
flat[c * 2] = points[corners[c] * 3 + u];
|
|
40
|
+
flat[c * 2 + 1] = points[corners[c] * 3 + w];
|
|
41
|
+
}
|
|
42
|
+
let convex = true;
|
|
43
|
+
for (let c = 0; c < n && convex; c++) {
|
|
44
|
+
const p = (c + n - 1) % n, q = (c + 1) % n, ex = flat[c * 2] - flat[p * 2], ey = flat[c * 2 + 1] - flat[p * 2 + 1], fx = flat[q * 2] - flat[c * 2], fy = flat[q * 2 + 1] - flat[c * 2 + 1];
|
|
45
|
+
if (ex * fy - ey * fx < 0)
|
|
46
|
+
convex = false;
|
|
47
|
+
}
|
|
48
|
+
return convex ? null : triangulate(flat, [], 2);
|
|
49
|
+
}
|
package/dist/prwm.d.ts
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { type LoadedGeometry, type ParseLimits } from './types.js';
|
|
2
|
+
import { type BinaryInput } from './util.js';
|
|
3
|
+
/** Options of parsePRWM: the shared size limits (ParseLimits). */
|
|
4
|
+
export interface PRWMOptions extends ParseLimits {
|
|
5
|
+
}
|
|
6
|
+
/** The result of parsePRWM: a LoadedGeometry plus what the file said about each attribute. */
|
|
7
|
+
export interface PRWMGeometry extends LoadedGeometry {
|
|
8
|
+
/** Every attribute in file order: its name, components per vertex (1 to 4), and whether the file marks it normalized or integer. */
|
|
9
|
+
layout: {
|
|
10
|
+
name: string;
|
|
11
|
+
components: number;
|
|
12
|
+
normalized: boolean;
|
|
13
|
+
integer: boolean;
|
|
14
|
+
}[];
|
|
15
|
+
/** True when the file had no index block (the triangle list is then 0, 1, 2, ...). */
|
|
16
|
+
nonIndexed: boolean;
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* Parses a Packed Raw WebGL Model (PRWM version 1; specifications/prwm.md of github.com/kchapelier/PRWM).
|
|
20
|
+
* Attributes named `position`, `normal`, `uv` (also `uv0`, `texcoord`) and `color` fill the matching fields (normalized
|
|
21
|
+
* integers become floats in [0,1] or [-1,1]; `color` keeps RGB); every other attribute goes to `attributes` flattened.
|
|
22
|
+
* A non-indexed file gets the sequential triangle list.
|
|
23
|
+
*/
|
|
24
|
+
export declare function parsePRWM(input: BinaryInput, options?: PRWMOptions): PRWMGeometry;
|
package/dist/prwm.js
ADDED
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
import { LoaderError } from './types.js';
|
|
2
|
+
import { checkCount, limits, toBytes } from './util.js';
|
|
3
|
+
const F = 'PRWM';
|
|
4
|
+
const ENCODINGS = {
|
|
5
|
+
1: [4, (v, a, l) => v.getFloat32(a, l), 1, true],
|
|
6
|
+
3: [1, (v, a) => v.getInt8(a), 127, true],
|
|
7
|
+
4: [2, (v, a, l) => v.getInt16(a, l), 32767, true],
|
|
8
|
+
6: [4, (v, a, l) => v.getInt32(a, l), 2147483647, true],
|
|
9
|
+
7: [1, (v, a) => v.getUint8(a), 255, false],
|
|
10
|
+
8: [2, (v, a, l) => v.getUint16(a, l), 65535, false],
|
|
11
|
+
10: [4, (v, a, l) => v.getUint32(a, l), 4294967295, false],
|
|
12
|
+
};
|
|
13
|
+
/**
|
|
14
|
+
* Parses a Packed Raw WebGL Model (PRWM version 1; specifications/prwm.md of github.com/kchapelier/PRWM).
|
|
15
|
+
* Attributes named `position`, `normal`, `uv` (also `uv0`, `texcoord`) and `color` fill the matching fields (normalized
|
|
16
|
+
* integers become floats in [0,1] or [-1,1]; `color` keeps RGB); every other attribute goes to `attributes` flattened.
|
|
17
|
+
* A non-indexed file gets the sequential triangle list.
|
|
18
|
+
*/
|
|
19
|
+
export function parsePRWM(input, options = {}) {
|
|
20
|
+
const bytes = toBytes(input, F), limit = limits(options, F);
|
|
21
|
+
if (bytes.length < 8)
|
|
22
|
+
throw new LoaderError(F, 'truncated', 'file is shorter than the 8-byte header');
|
|
23
|
+
const view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
|
|
24
|
+
const version = bytes[0], flags = bytes[1];
|
|
25
|
+
if (version === 0)
|
|
26
|
+
throw new LoaderError(F, 'header', 'version 0 is invalid');
|
|
27
|
+
if (version !== 1)
|
|
28
|
+
throw new LoaderError(F, 'unsupported', `PRWM version ${version} is not supported`);
|
|
29
|
+
const indexed = !!(flags & 0x80), index32 = !!(flags & 0x40), little = !(flags & 0x20), attributeCount = flags & 0x1f;
|
|
30
|
+
const u24 = (at) => little
|
|
31
|
+
? bytes[at] | (bytes[at + 1] << 8) | (bytes[at + 2] << 16)
|
|
32
|
+
: (bytes[at] << 16) | (bytes[at + 1] << 8) | bytes[at + 2];
|
|
33
|
+
const valueCount = checkCount(u24(2), limit.maxVertices, F, 'vertex'), indexCount = u24(5);
|
|
34
|
+
if (attributeCount === 0)
|
|
35
|
+
throw new LoaderError(F, 'header', 'a model needs at least one attribute');
|
|
36
|
+
if (!indexed && (indexCount !== 0 || index32))
|
|
37
|
+
throw new LoaderError(F, 'header', 'a non-indexed model must have no indices and the 16-bit index type');
|
|
38
|
+
checkCount(indexCount, limit.maxTriangles * 3, F, 'index');
|
|
39
|
+
if (indexed && indexCount % 3)
|
|
40
|
+
throw new LoaderError(F, 'header', 'the index count is not a multiple of 3');
|
|
41
|
+
let pos = 8;
|
|
42
|
+
const result = { kind: 'mesh', positions: new Float32Array(0), layout: [], nonIndexed: !indexed };
|
|
43
|
+
let hasPosition = false;
|
|
44
|
+
for (let a = 0; a < attributeCount; a++) {
|
|
45
|
+
let end = pos;
|
|
46
|
+
while (end < bytes.length && bytes[end] !== 0)
|
|
47
|
+
end++;
|
|
48
|
+
if (end >= bytes.length - 1)
|
|
49
|
+
throw new LoaderError(F, 'truncated', `attribute ${a} header is cut off`);
|
|
50
|
+
let name = '';
|
|
51
|
+
for (let i = pos; i < end; i++)
|
|
52
|
+
name += String.fromCharCode(bytes[i]);
|
|
53
|
+
const flag = bytes[end + 1];
|
|
54
|
+
pos = (end + 2 + 3) & ~3; // the values start at a multiple of 4
|
|
55
|
+
const integer = !!(flag & 0x80), normalized = !!(flag & 0x40), components = ((flag >> 4) & 3) + 1, encoding = ENCODINGS[flag & 15];
|
|
56
|
+
if (!encoding)
|
|
57
|
+
throw new LoaderError(F, 'unsupported', `attribute "${name}" uses reserved encoding ${flag & 15}`);
|
|
58
|
+
const [size, read, max, signed] = encoding, total = valueCount * components;
|
|
59
|
+
if (pos + total * size > bytes.length)
|
|
60
|
+
throw new LoaderError(F, 'truncated', `attribute "${name}" ends past the end of the file`);
|
|
61
|
+
const out = new Float32Array(total);
|
|
62
|
+
const scale = normalized && (flag & 15) !== 1;
|
|
63
|
+
for (let i = 0; i < total; i++) {
|
|
64
|
+
const raw = read(view, pos + i * size, little);
|
|
65
|
+
out[i] = scale ? (signed ? Math.max(raw / max, -1) : raw / max) : raw;
|
|
66
|
+
}
|
|
67
|
+
pos += total * size;
|
|
68
|
+
result.layout.push({ name, components, normalized, integer });
|
|
69
|
+
const key = name.toLowerCase();
|
|
70
|
+
if (key === 'position' && components === 3) {
|
|
71
|
+
result.positions = out;
|
|
72
|
+
hasPosition = true;
|
|
73
|
+
}
|
|
74
|
+
else if (key === 'normal' && components === 3)
|
|
75
|
+
result.normals = out;
|
|
76
|
+
else if (['uv', 'uv0', 'texcoord'].includes(key) && components === 2)
|
|
77
|
+
result.uvs ??= out;
|
|
78
|
+
else if (key === 'color' && components >= 3) {
|
|
79
|
+
const rgb = new Float32Array(valueCount * 3);
|
|
80
|
+
for (let i = 0; i < valueCount; i++)
|
|
81
|
+
for (let c = 0; c < 3; c++)
|
|
82
|
+
rgb[i * 3 + c] = out[i * components + c];
|
|
83
|
+
result.colors = rgb;
|
|
84
|
+
}
|
|
85
|
+
else
|
|
86
|
+
(result.attributes ??= {})[name] = out;
|
|
87
|
+
}
|
|
88
|
+
if (!hasPosition)
|
|
89
|
+
throw new LoaderError(F, 'header', 'no 3-component "position" attribute');
|
|
90
|
+
let indices;
|
|
91
|
+
if (indexed) {
|
|
92
|
+
pos = (pos + 3) & ~3;
|
|
93
|
+
const size = index32 ? 4 : 2;
|
|
94
|
+
if (pos + indexCount * size > bytes.length)
|
|
95
|
+
throw new LoaderError(F, 'truncated', 'index block is cut off');
|
|
96
|
+
indices = new Uint32Array(indexCount);
|
|
97
|
+
for (let i = 0; i < indexCount; i++) {
|
|
98
|
+
const v = index32 ? view.getUint32(pos + i * 4, little) : view.getUint16(pos + i * 2, little);
|
|
99
|
+
if (v >= valueCount)
|
|
100
|
+
throw new LoaderError(F, 'range', `index ${i} (${v}) is past the ${valueCount} vertices`);
|
|
101
|
+
indices[i] = v;
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
else {
|
|
105
|
+
if (valueCount % 3)
|
|
106
|
+
throw new LoaderError(F, 'header', 'a non-indexed model needs a multiple of 3 vertices');
|
|
107
|
+
checkCount(valueCount, limit.maxTriangles * 3, F, 'index');
|
|
108
|
+
indices = new Uint32Array(valueCount);
|
|
109
|
+
for (let i = 0; i < valueCount; i++)
|
|
110
|
+
indices[i] = i;
|
|
111
|
+
}
|
|
112
|
+
result.indices = indices;
|
|
113
|
+
return result;
|
|
114
|
+
}
|
package/dist/render.d.ts
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import type { EngineKernel } from '@margoengine/core';
|
|
2
|
+
// @ts-ignore optional peer: its types apply when it is installed
|
|
3
|
+
import { type PBRModel } from '@margoengine/gltf/pbr-model';
|
|
4
|
+
import type { CreateMeshesOptions } from './engine.js';
|
|
5
|
+
import { type LoadedGeometry } from './types.js';
|
|
6
|
+
/**
|
|
7
|
+
* Options for {@link createPBRMeshes}: `parent` (of the model root), `normals`, `creaseAngle` (default 40),
|
|
8
|
+
* `roughness` (default 0.6), `metalness` (default 0) and `doubleSided` (default true) as for `createMeshes`. Colors
|
|
9
|
+
* multiply in linear light.
|
|
10
|
+
*/
|
|
11
|
+
export interface CreatePBRMeshesOptions extends Pick<CreateMeshesOptions, 'parent' | 'normals' | 'roughness' | 'metalness' | 'doubleSided'> {
|
|
12
|
+
/** As for `createMeshes`: faces within this many degrees are smoothed together. Default 40. */
|
|
13
|
+
creaseAngle?: number;
|
|
14
|
+
/** Linear RGBA material multiplier in [0,1]. Default white, preserving source colors and opacity. */
|
|
15
|
+
baseColorFactor?: readonly [number, number, number, number];
|
|
16
|
+
/** Use the renderer's existing unlit material. Default false. */
|
|
17
|
+
unlit?: boolean;
|
|
18
|
+
/** Maximum packed vertex + index bytes across all parts, checked before GPU allocation. Default 256 MiB. */
|
|
19
|
+
maxBytes?: number;
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* Renders triangle loader output with exact per-vertex RGB and opacity through optional `gltf/pbr`.
|
|
23
|
+
* Source RGB is sRGB; opacity is linear. Group RGBA supplies only missing vertex channels, independently.
|
|
24
|
+
* Uncovered index ranges render with white/opaque fallback. The explicit material factor multiplies RGBA.
|
|
25
|
+
* Returns the renderer's owned model: dispose it once finished; its existing lifecycle restores GPU resources.
|
|
26
|
+
* The input is never modified. A part past 262,144 vertices (gltf/pbr's default `maxPartVertices`) becomes several primitives of one
|
|
27
|
+
* glTF mesh, split as toGeometries splits (normals seam-free). With vertex colors, neighboring groups that differ in
|
|
28
|
+
* color alone are one part, as their corners already carry it (a scan has a group for almost every color). Point
|
|
29
|
+
* clouds, overlapping groups, more than 4096 parts, or an exceeded packed-data budget are refused before any GPU
|
|
30
|
+
* allocation.
|
|
31
|
+
*/
|
|
32
|
+
export declare function createPBRMeshes(engine: EngineKernel, loaded: LoadedGeometry, options?: CreatePBRMeshesOptions): PBRModel;
|