@needle-tools/gltf-build-pipeline 1.4.1-beta.13 → 1.4.1-beta.14

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/CHANGELOG.md CHANGED
@@ -4,6 +4,9 @@ All notable changes to this package will be documented in this file.
4
4
  The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
5
5
  and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).
6
6
 
7
+ ## [1.4.1-beta.14] - 2023-04-02
8
+ - fix: issue where discarding all LODs might result in broken mesh due to simplify having modified the original mesh
9
+
7
10
  ## [1.4.1-beta.13] - 2023-03-26
8
11
  - change: passthrough `NEEDLE_editor` & `NEEDLE_gltf_dependencies` extensions
9
12
 
@@ -331,8 +331,13 @@ export function make_progressive_meshes(filePath, _options, opts) {
331
331
  }
332
332
  /** Map of visited GUID -> extension */
333
333
  const visited = new Map();
334
- const meshes = doc.getRoot().listMeshes();
334
+ const root = doc.getRoot();
335
+ const meshes = root.listMeshes();
335
336
  for (const mesh of meshes) {
337
+ await processMesh(mesh);
338
+ }
339
+ console.log(`[${NAME}]: Finished generating progressive meshes for \"${filename}\"`);
340
+ async function processMesh(mesh) {
336
341
  /** The size and vertex count of the original mesh */
337
342
  const originalMeshInformation = getMeshInformation(mesh);
338
343
  const { totalVertexCount: originalTotalVertexCount, maxVertexCount: originalMaxVertexCount } = originalMeshInformation;
@@ -340,7 +345,7 @@ export function make_progressive_meshes(filePath, _options, opts) {
340
345
  if (!settings) {
341
346
  if (originalMaxVertexCount < 32) {
342
347
  logger.debug(`[${NAME}] Skipping, no settings found and no primitive with more than 32 vertices. mesh[${index}] has ${originalTotalVertexCount} vertices (max: ${originalMaxVertexCount})`);
343
- continue;
348
+ return;
344
349
  }
345
350
  else {
346
351
  settings = {
@@ -352,12 +357,12 @@ export function make_progressive_meshes(filePath, _options, opts) {
352
357
  }
353
358
  else if (settings.generateLods === false) {
354
359
  logger.debug(`[${NAME}] progressive mesh disabled for mesh at index ${index++}`);
355
- continue;
360
+ return;
356
361
  }
357
362
  const progressiveExt = doc.createExtension(NEEDLE_progressive);
358
363
  if (!progressiveExt || !progressiveExt.createMesh) {
359
364
  logger.debug(`[${NAME}] Skipping because progressive extension is not available. ${index++}`);
360
- continue;
365
+ return;
361
366
  }
362
367
  // check if we have already produced LODs for this mesh
363
368
  let needsToGenerateLODs = true;
@@ -449,7 +454,11 @@ export function make_progressive_meshes(filePath, _options, opts) {
449
454
  let maxDensity = 0;
450
455
  let primitiveIndex = 0;
451
456
  const lodLevelInFile = generatedLODInfos.length;
457
+ const clonedData = new Array();
452
458
  for (const prim of mesh.listPrimitives()) {
459
+ const data = clonePrimitiveData(prim);
460
+ clonedData.push(data);
461
+ console.log(`[${NAME}] Simplify mesh[${index}] primitive[${primitiveIndex}] with ${data.indices.length / 3} triangles`);
453
462
  const lastLod = LODS[lodLevelInFile];
454
463
  // TODO: this should be run after simplification but then we need to change the runtime to use the LOD 0 density
455
464
  const density = calculateMeshDensity(prim);
@@ -476,7 +485,14 @@ export function make_progressive_meshes(filePath, _options, opts) {
476
485
  catch (err) {
477
486
  logger.error(`ERR: [${NAME}] Failed to remove LOD files for \"${mesh.getName()}\"`);
478
487
  }
479
- continue;
488
+ // reset data (simplify might have changed the mesh data that we cloned before)
489
+ const primitives = mesh.listPrimitives();
490
+ if (clonedData.length == primitives.length) {
491
+ for (let i = 0; i < primitives.length; i++) {
492
+ setPrimitiveData(primitives[i], clonedData[i]);
493
+ }
494
+ }
495
+ return;
480
496
  }
481
497
  if (generatedLODInfos.length > 0) {
482
498
  const model = progressiveExt.createMesh({
@@ -501,9 +517,31 @@ export function make_progressive_meshes(filePath, _options, opts) {
501
517
  }
502
518
  }
503
519
  }
504
- console.log(`[${NAME}]: Finished generating progressive meshes for \"${filename}\"`);
505
520
  });
506
521
  }
522
+ function clonePrimitiveData(prim) {
523
+ const indices = new Uint32Array(prim.getIndices().getArray());
524
+ const attributes = {};
525
+ for (const attr of prim.listSemantics()) {
526
+ const accessor = prim.getAttribute(attr);
527
+ if (accessor) {
528
+ attributes[attr] = new Float32Array(accessor.getArray());
529
+ }
530
+ }
531
+ return { indices, attributes };
532
+ }
533
+ function setPrimitiveData(prim, data) {
534
+ const indices = prim.getIndices();
535
+ if (indices) {
536
+ indices.setArray(data.indices);
537
+ }
538
+ for (const attr of prim.listSemantics()) {
539
+ const accessor = prim.getAttribute(attr);
540
+ if (accessor) {
541
+ accessor.setArray(data.attributes[attr]);
542
+ }
543
+ }
544
+ }
507
545
  /**
508
546
  * @param basePath the base path to save the lod files
509
547
  * @param level: 0 = original, 1 = first lod, 2 = second lod, ...
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@needle-tools/gltf-build-pipeline",
3
- "version": "1.4.1-beta.13",
3
+ "version": "1.4.1-beta.14",
4
4
  "description": "Pipeline and tools for optimizing gltf files using gltf-transform and compression settings within glTF extensions",
5
5
  "main": "index.mjs",
6
6
  "type": "module",