@needle-tools/gltf-build-pipeline 2.4.0-alpha.3 → 2.4.0-alpha.5
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 +7 -0
- package/dist/cli/index.js +20 -1
- package/dist/constants.d.ts +1 -0
- package/dist/constants.js +1 -0
- package/dist/extensions/NEEDLE_mesh_compression/NEEDLE_mesh_compression.d.ts +1 -0
- package/dist/extensions/NEEDLE_mesh_compression/NEEDLE_mesh_compression.js +3 -0
- package/dist/scripts/pack-gltf.js +1 -1
- package/dist/transforms/needle_mesh_transform.d.ts +5 -0
- package/dist/transforms/needle_mesh_transform.js +41 -29
- package/dist/transforms/needle_progressive.js +7 -7
- package/dist/utils/fileutils.js +5 -5
- package/package.json +4 -4
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-beta.5] - 2023-09-06
|
|
8
|
+
- 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)
|
|
9
|
+
- change: update gltf-transform dependency to 4.0.8
|
|
10
|
+
|
|
11
|
+
## [2.4.0-beta] - 2023-08-13
|
|
12
|
+
- fix: issue where a mesh lod is removed during second processing of a directory that contains existing mesh lods
|
|
13
|
+
|
|
7
14
|
## [2.4.0-alpha.3] - 2023-08-11
|
|
8
15
|
- fix: issue where files would not correctly be overriden in subdirectories when no output directory was specified
|
|
9
16
|
|
package/dist/cli/index.js
CHANGED
|
@@ -4,8 +4,9 @@ import { calculateStats, packGLTF } from "../scripts/index.js";
|
|
|
4
4
|
import { printStats, writeFileStatsToFile } from '../utils/stats.js';
|
|
5
5
|
import { getVersion } from '../utils/version.js';
|
|
6
6
|
import { UsecaseOptions, createConfig, getConfig } from '../config/index.js';
|
|
7
|
-
import { make_progressive } from '../transforms/needle_progressive.js';
|
|
7
|
+
import { isLOD, make_progressive } from '../transforms/needle_progressive.js';
|
|
8
8
|
import { cacheSizeLimit, clearCache, limitCacheSize } from '../cache/cache.js';
|
|
9
|
+
import { existsSync } from 'fs';
|
|
9
10
|
import { ensureIsDirectory, foreachGLTF, isDirectory } from '../utils/fileutils.js';
|
|
10
11
|
import path, { resolve } from 'path';
|
|
11
12
|
import { ERROR_CODES } from '../constants.js';
|
|
@@ -112,6 +113,20 @@ Each version will be compressed and written to the output directory.
|
|
|
112
113
|
const debug = options.debug === true;
|
|
113
114
|
const verbose = options.verbose === true;
|
|
114
115
|
await foreachGLTF(input, async (file) => {
|
|
116
|
+
if (!existsSync(file)) {
|
|
117
|
+
// Safeguard if we run compression in a souce directory that already has progressive assets
|
|
118
|
+
// Here it can happen that a LOD file is generated with the same GUID at the moment
|
|
119
|
+
// And then later removed because simplification is too small.
|
|
120
|
+
// See https://linear.app/needle/issue/NE-5518
|
|
121
|
+
if (isLOD(file)) {
|
|
122
|
+
logger.warn(`WARN: Ignore previously found file that does not exist anymore: \"${file}\"`);
|
|
123
|
+
return;
|
|
124
|
+
}
|
|
125
|
+
else {
|
|
126
|
+
logger.error(`ERR: Previously found file is now missing: \"${file}\"`);
|
|
127
|
+
process.exit(ERROR_CODES.LOST_FILE);
|
|
128
|
+
}
|
|
129
|
+
}
|
|
115
130
|
logger.info("→ Transform " + file);
|
|
116
131
|
const progressive_results = new Array();
|
|
117
132
|
if (options.progressive === true) {
|
|
@@ -121,6 +136,7 @@ Each version will be compressed and written to the output directory.
|
|
|
121
136
|
results: progressive_results,
|
|
122
137
|
config,
|
|
123
138
|
debug,
|
|
139
|
+
verbose,
|
|
124
140
|
useCache,
|
|
125
141
|
}).catch(err => {
|
|
126
142
|
logger.error(err);
|
|
@@ -128,6 +144,9 @@ Each version will be compressed and written to the output directory.
|
|
|
128
144
|
}) === false) {
|
|
129
145
|
process.exit(ERROR_CODES.PROGRESSIVE_FAILED);
|
|
130
146
|
}
|
|
147
|
+
if (verbose) {
|
|
148
|
+
console.log("Progressive results:\n", progressive_results);
|
|
149
|
+
}
|
|
131
150
|
}
|
|
132
151
|
if (options.compress === true) {
|
|
133
152
|
const opts = {
|
package/dist/constants.d.ts
CHANGED
package/dist/constants.js
CHANGED
|
@@ -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;
|
|
@@ -17,7 +17,7 @@ export async function packGLTF(inputFile, outputFile, options) {
|
|
|
17
17
|
}
|
|
18
18
|
const { debug, stats, packedFiles, useCache, logger } = options;
|
|
19
19
|
if (!existsSync(inputFile)) {
|
|
20
|
-
logger.error(
|
|
20
|
+
logger.error(`WARN: can not pack, file does not exist: \"${inputFile}\"`);
|
|
21
21
|
return false;
|
|
22
22
|
}
|
|
23
23
|
// check if it's in the cache.
|
|
@@ -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
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
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
|
|
19
|
-
if (rootModel.compression
|
|
20
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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;
|
|
@@ -65,7 +65,7 @@ async function onProcess(file, opts) {
|
|
|
65
65
|
if (document) {
|
|
66
66
|
const logger = document.getLogger();
|
|
67
67
|
//@ts-ignore
|
|
68
|
-
logger.verbosity = opts.debug || opts.verbose ? Verbosity.DEBUG : Verbosity.WARN;
|
|
68
|
+
logger.verbosity = (opts.debug || opts.verbose) ? Verbosity.DEBUG : Verbosity.WARN;
|
|
69
69
|
let smallesTextureSize = 128;
|
|
70
70
|
if (opts.config.usecase === "product") {
|
|
71
71
|
smallesTextureSize = 512;
|
|
@@ -652,6 +652,7 @@ export function make_progressive_meshes(filePath, _options, opts) {
|
|
|
652
652
|
}
|
|
653
653
|
}
|
|
654
654
|
if (shouldOmitLevel) {
|
|
655
|
+
logger.debug(`[${NAME}] Omitting LOD ${level} for \"${mesh.getName()}\" at \"${lodInfo.path}\"`);
|
|
655
656
|
generatedLODInfos.splice(i, 1);
|
|
656
657
|
rmSync(fullPath);
|
|
657
658
|
i--;
|
|
@@ -906,12 +907,11 @@ async function createMeshLod(doc, basePath, level, mesh, meshInfo, guid, lodLeve
|
|
|
906
907
|
density: density,
|
|
907
908
|
}));
|
|
908
909
|
// we need to compress the mesh with the same compression method as the original file
|
|
909
|
-
const
|
|
910
|
-
|
|
911
|
-
if (compressionExt) {
|
|
910
|
+
const model = determineMeshCompression(doc, fileName);
|
|
911
|
+
if (model) {
|
|
912
912
|
const newExt = newDoc.createExtension(NEEDLE_mesh_compression);
|
|
913
|
-
const newProp = newExt.create(
|
|
914
|
-
logger.info(`[${NAME}] Pass compression format to mesh LOD ${level} → ${JSON.stringify(
|
|
913
|
+
const newProp = newExt.create(model);
|
|
914
|
+
logger.info(`[${NAME}] Pass compression format to mesh LOD ${level} → ${JSON.stringify(model)}`);
|
|
915
915
|
newDoc.getRoot().setExtension(NEEDLE_mesh_compression.EXTENSION_NAME, newProp);
|
|
916
916
|
}
|
|
917
917
|
await newDoc.transform(sparse());
|
package/dist/utils/fileutils.js
CHANGED
|
@@ -77,7 +77,7 @@ export async function foreachGLTF(args, callback) {
|
|
|
77
77
|
}
|
|
78
78
|
}
|
|
79
79
|
if (context.files.length <= 0) {
|
|
80
|
-
console.log(
|
|
80
|
+
console.log(`WARN: No glTF files found. Make sure to invoke with a valid path to a glTF file or directory.\nBase Directory: ${context.baseDirectory}\nPaths can be relative to \"${initDir}\"`);
|
|
81
81
|
}
|
|
82
82
|
else {
|
|
83
83
|
for (const file of context.files) {
|
|
@@ -99,13 +99,13 @@ async function testEntry(entry, context) {
|
|
|
99
99
|
// If no base directory is defined we cannot try to resolve the path
|
|
100
100
|
if (context.baseDirectory === undefined) {
|
|
101
101
|
console.log("WARN: Path does not exist: " + entry);
|
|
102
|
-
return;
|
|
102
|
+
return false;
|
|
103
103
|
}
|
|
104
104
|
// Try resolve potential relative path
|
|
105
105
|
const newPath = context.baseDirectory + "/" + entry;
|
|
106
106
|
if (existsSync(newPath) === false) {
|
|
107
107
|
console.log(`INFO: Resolved path failed: \"${entry}\" -> ${newPath} (doesnt exist or is locked by another process)`);
|
|
108
|
-
return;
|
|
108
|
+
return false;
|
|
109
109
|
}
|
|
110
110
|
else {
|
|
111
111
|
console.log(`INFO: Resolved path: ${entry} -> ${newPath}`);
|
|
@@ -122,7 +122,7 @@ async function testEntry(entry, context) {
|
|
|
122
122
|
console.log(`INFO: Resolved path: ${entry} -> ${newPath}`);
|
|
123
123
|
entry = newPath;
|
|
124
124
|
if (!existsSync(entry)) {
|
|
125
|
-
return;
|
|
125
|
+
return false;
|
|
126
126
|
}
|
|
127
127
|
}
|
|
128
128
|
}
|
|
@@ -135,7 +135,7 @@ async function testEntry(entry, context) {
|
|
|
135
135
|
if (stat.isDirectory()) {
|
|
136
136
|
// Don't traverse node_modules
|
|
137
137
|
if (entry.endsWith("node_modules")) {
|
|
138
|
-
return;
|
|
138
|
+
return false;
|
|
139
139
|
}
|
|
140
140
|
const files = readdirSync(entry);
|
|
141
141
|
for (const file of files) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@needle-tools/gltf-build-pipeline",
|
|
3
|
-
"version": "2.4.0-alpha.
|
|
3
|
+
"version": "2.4.0-alpha.5",
|
|
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.
|
|
51
|
-
"@gltf-transform/extensions": "^4.0.
|
|
52
|
-
"@gltf-transform/functions": "^4.0.
|
|
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",
|