@needle-tools/gltf-build-pipeline 2.4.0-alpha.4 → 2.4.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/CHANGELOG.md CHANGED
@@ -4,6 +4,13 @@ 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
+ ## [2.4.0] - 2023-09-11
8
+ - release
9
+
10
+ ## [2.4.0-beta.5] - 2023-09-06
11
+ - fix: issue where mesh LODs would in some cases be compressed with "draco" while the root file would be compressed with "mesh-opt" if not explicitly specified in the root file. This would cause invalid rendering and randomly scaled LOD meshes. Now mesh LODs will always use the same mesh compression. [NE 5297](https://linear.app/needle/issue/NE-5297)
12
+ - change: update gltf-transform dependency to 4.0.8
13
+
7
14
  ## [2.4.0-beta] - 2023-08-13
8
15
  - fix: issue where a mesh lod is removed during second processing of a directory that contains existing mesh lods
9
16
 
@@ -1,6 +1,7 @@
1
1
  import { Extension, ExtensionProperty, PropertyType, ReaderContext, WriterContext } from "@gltf-transform/core";
2
2
  export declare class NEEDLE_mesh_compression_model_root {
3
3
  compression: "none" | "draco" | "meshopt";
4
+ constructor(compression: "none" | "draco" | "meshopt");
4
5
  }
5
6
  export declare class NEEDLE_mesh_compression_model {
6
7
  useSimplifier: boolean;
@@ -3,6 +3,9 @@ const EXTENSION_NAME = "NEEDLE_mesh_compression";
3
3
  // TODO: this extension can now be on the root and on primitives containing different contents
4
4
  export class NEEDLE_mesh_compression_model_root {
5
5
  compression;
6
+ constructor(compression) {
7
+ this.compression = compression;
8
+ }
6
9
  }
7
10
  export class NEEDLE_mesh_compression_model {
8
11
  useSimplifier;
@@ -1,10 +1,15 @@
1
1
  import { Transform, Document, Primitive, Mesh } from "@gltf-transform/core";
2
2
  import { SimplifyOptions, WeldOptions } from "@gltf-transform/functions";
3
+ import { NEEDLE_mesh_compression_model_root } from "../extensions/index.js";
3
4
  import { Config } from "../config/index.js";
4
5
  declare type MeshTransformContext = {
5
6
  file: string;
6
7
  config: Config;
7
8
  };
9
+ /**
10
+ * Which compression type for a model should be used. This should be respected when creating LOD meshes and the appropriate extension should be added to the LOD mesh so it receives the same mesh compression as the parent mesh
11
+ */
12
+ export declare function determineMeshCompression(document: Document, filePath: string): NEEDLE_mesh_compression_model_root;
8
13
  export declare const needle_mesh_transform: (context: MeshTransformContext, simplify: Omit<SimplifyOptions, "simplifier">) => Transform;
9
14
  export declare function compressPrimitive(document: Document, mesh: Mesh, prim: Primitive, index: number, weldOptions: Required<WeldOptions>, meshOptions: Required<SimplifyOptions>): Promise<Primitive>;
10
15
  export {};
@@ -1,43 +1,27 @@
1
1
  import { Primitive } from "@gltf-transform/core";
2
2
  import { meshopt, simplifyPrimitive, SIMPLIFY_DEFAULTS, WELD_DEFAULTS, draco } from "@gltf-transform/functions";
3
3
  import { MeshoptEncoder, MeshoptSimplifier } from "meshoptimizer";
4
- import { NEEDLE_mesh_compression } from "../extensions/index.js";
4
+ import { NEEDLE_mesh_compression, NEEDLE_mesh_compression_model_root } from "../extensions/index.js";
5
5
  import { weldPrimitive } from "../functions/weld.3.js";
6
6
  const NAME = 'needle_mesh_transform';
7
- export const needle_mesh_transform = function (context, simplify) {
8
- return async (document, _context) => {
9
- // TODO: needs to be moved into NEEDLE_progressive
10
- await simplifierTransform(document, _context, simplify);
11
- await applyMeshCompression(document, _context, context);
12
- };
13
- };
14
- async function applyMeshCompression(document, transform_context, context) {
7
+ /**
8
+ * Which compression type for a model should be used. This should be respected when creating LOD meshes and the appropriate extension should be added to the LOD mesh so it receives the same mesh compression as the parent mesh
9
+ */
10
+ export function determineMeshCompression(document, filePath) {
15
11
  const rootExtension = document.getRoot().getExtension(NEEDLE_mesh_compression.EXTENSION_NAME);
16
12
  if (rootExtension) {
17
13
  const rootModel = rootExtension.model;
18
- if (rootModel.compression) {
19
- if (rootModel.compression === "none") {
20
- document.getLogger().info(`[${NAME}]: NONE → will not apply mesh compression`);
21
- return;
22
- }
23
- document.getLogger().info(`[${NAME}]: ${rootModel.compression.toUpperCase()} → will apply ${rootModel.compression} mesh compression`);
24
- if (rootModel.compression === "draco") {
25
- const transform = draco();
26
- return transform(document, transform_context);
27
- }
28
- else if (rootModel.compression === "meshopt") {
29
- const transform = meshopt({ encoder: MeshoptEncoder });
30
- return transform(document, transform_context);
14
+ if (rootModel) {
15
+ if (!rootModel.compression) {
16
+ throw new Error(`[${NAME}]: Invalid mesh compression model extension on root node of ${filePath}`);
31
17
  }
18
+ return rootModel;
32
19
  }
33
20
  }
34
- // Meshop is better at re-using vertex attributes
35
- if (context.file.endsWith(".vrm")) {
21
+ if (filePath.endsWith(".vrm")) {
36
22
  const hasBlendshapes = document.getRoot().listMeshes().some((mesh) => mesh.listPrimitives().some((prim) => prim.listTargets().length > 0));
37
23
  if (hasBlendshapes) {
38
- document.getLogger().info(`[${NAME}]: VRM → will apply meshopt mesh compression`);
39
- const transform = meshopt({ encoder: MeshoptEncoder });
40
- return transform(document, transform_context);
24
+ return new NEEDLE_mesh_compression_model_root("meshopt");
41
25
  }
42
26
  }
43
27
  // heuristic to detect if we should choose meshopt over draco
@@ -56,8 +40,7 @@ async function applyMeshCompression(document, transform_context, context) {
56
40
  // TODO: make this more robus with progressive loading: when progressive loading is enabled we need to run this check on the root mesh and enforce the same mesh compression for all children. Until we can do that we can not use the code for choosing compression based on blendshape size
57
41
  if (sizeOfBlendshapes > 0) {
58
42
  document.getLogger().info(`[${NAME}]: will apply meshopt mesh compression because the mesh contains blendshapes`);
59
- const transform = meshopt({ encoder: MeshoptEncoder });
60
- return transform(document, transform_context);
43
+ return new NEEDLE_mesh_compression_model_root("meshopt");
61
44
  }
62
45
  // consider meshopt if blendshape size is > 256 KB
63
46
  // const sizeThreshold = 256 * 1024;
@@ -72,6 +55,35 @@ async function applyMeshCompression(document, transform_context, context) {
72
55
  // return transform(document, transform_context);
73
56
  // }
74
57
  // }
58
+ return new NEEDLE_mesh_compression_model_root("draco");
59
+ }
60
+ export const needle_mesh_transform = function (context, simplify) {
61
+ return async (document, _context) => {
62
+ // TODO: needs to be moved into NEEDLE_progressive
63
+ await simplifierTransform(document, _context, simplify);
64
+ await applyMeshCompression(document, _context, context);
65
+ };
66
+ };
67
+ async function applyMeshCompression(document, transform_context, context) {
68
+ const compressionModel = determineMeshCompression(document, context.file); // document.getRoot().getExtension(NEEDLE_mesh_compression.EXTENSION_NAME) as NEEDLE_mesh_compression_property;
69
+ if (compressionModel) {
70
+ // const rootModel = rootExtension.model as NEEDLE_mesh_compression_model_root;
71
+ if (compressionModel.compression) {
72
+ if (compressionModel.compression === "none") {
73
+ document.getLogger().info(`[${NAME}]: NONE → will not apply mesh compression`);
74
+ return;
75
+ }
76
+ document.getLogger().info(`[${NAME}]: ${compressionModel.compression.toUpperCase()} → will apply ${compressionModel.compression} mesh compression`);
77
+ if (compressionModel.compression === "draco") {
78
+ const transform = draco();
79
+ return transform(document, transform_context);
80
+ }
81
+ else if (compressionModel.compression === "meshopt") {
82
+ const transform = meshopt({ encoder: MeshoptEncoder });
83
+ return transform(document, transform_context);
84
+ }
85
+ }
86
+ }
75
87
  // by default we still want to run draco if the root doesnt have a compression model configured
76
88
  document.getLogger().info(`[${NAME}]: → will apply draco mesh compression (default)`);
77
89
  return draco()(document, transform_context);
@@ -10,7 +10,7 @@ import sharp from "sharp";
10
10
  import { detectTextureCompressionMode, ensureIsDirectory, foreachGLTF, generateGuid, getOutputPath, isLightmap, setLightmapName, validateImageMimeType, writeNodeIO } from '../utils/index.js';
11
11
  import { needle_asset, writeNeedleAsset } from './needle_asset.js';
12
12
  import { MeshoptDecoder, MeshoptSimplifier } from 'meshoptimizer';
13
- import { compressPrimitive } from './needle_mesh_transform.js';
13
+ import { compressPrimitive, determineMeshCompression } from './needle_mesh_transform.js';
14
14
  import { calculateMeshDensity, getMeshInformation } from "../utils/mesh.js";
15
15
  export async function make_progressive(opts) {
16
16
  const file = opts.path;
@@ -907,12 +907,11 @@ async function createMeshLod(doc, basePath, level, mesh, meshInfo, guid, lodLeve
907
907
  density: density,
908
908
  }));
909
909
  // we need to compress the mesh with the same compression method as the original file
910
- const root = doc.getRoot();
911
- const compressionExt = root.getExtension(NEEDLE_mesh_compression.EXTENSION_NAME);
912
- if (compressionExt) {
910
+ const model = determineMeshCompression(doc, fileName);
911
+ if (model) {
913
912
  const newExt = newDoc.createExtension(NEEDLE_mesh_compression);
914
- const newProp = newExt.create(compressionExt.model);
915
- logger.info(`[${NAME}] Pass compression format to mesh LOD ${level} → ${JSON.stringify(compressionExt.model)}`);
913
+ const newProp = newExt.create(model);
914
+ logger.info(`[${NAME}] Pass compression format to mesh LOD ${level} → ${JSON.stringify(model)}`);
916
915
  newDoc.getRoot().setExtension(NEEDLE_mesh_compression.EXTENSION_NAME, newProp);
917
916
  }
918
917
  await newDoc.transform(sparse());
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@needle-tools/gltf-build-pipeline",
3
- "version": "2.4.0-alpha.4",
3
+ "version": "2.4.0",
4
4
  "description": "Pipeline and tools for optimizing gltf files using gltf-transform and compression settings within glTF extensions",
5
5
  "main": "dist/index.js",
6
6
  "type": "module",
@@ -47,9 +47,9 @@
47
47
  },
48
48
  "dependencies": {
49
49
  "@donmccurdy/caporal": "~0.0.10",
50
- "@gltf-transform/core": "^4.0.2",
51
- "@gltf-transform/extensions": "^4.0.2",
52
- "@gltf-transform/functions": "^4.0.2",
50
+ "@gltf-transform/core": "^4.0.8",
51
+ "@gltf-transform/extensions": "^4.0.8",
52
+ "@gltf-transform/functions": "^4.0.8",
53
53
  "command-exists": "^1.2.9",
54
54
  "command-line-args": "^5.2.1",
55
55
  "draco3dgltf": "^1.5.3",