@needle-tools/gltf-build-pipeline 2.16.0-next.19247c3 → 2.16.0-next.2b9edfd
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/README.md +29 -2
- package/dist/config/index.d.ts +11 -0
- package/dist/config/index.js +9 -4
- package/dist/extensions/NEEDLE_opaque/NEEDLE_opaque.js +30 -0
- package/dist/scripts/pack-gltf.js +10 -1
- package/dist/transforms/index.d.ts +3 -0
- package/dist/transforms/index.js +3 -0
- package/dist/transforms/needle_audio.d.ts +12 -0
- package/dist/transforms/needle_audio.js +104 -0
- package/dist/transforms/needle_audio_ffmpeg.d.ts +14 -0
- package/dist/transforms/needle_audio_ffmpeg.js +51 -0
- package/dist/transforms/needle_audio_registry.d.ts +36 -0
- package/dist/transforms/needle_audio_registry.js +45 -0
- package/dist/transforms/needle_texture_transform.js +4 -0
- package/dist/transforms/toktx.js +23 -36
- package/dist/utils/index.d.ts +0 -1
- package/dist/utils/index.js +0 -1
- package/dist/utils/version.gen.d.ts +1 -1
- package/dist/utils/version.gen.js +1 -1
- package/package.json +1 -1
- package/dist/utils/merge.d.ts +0 -12
- package/dist/utils/merge.js +0 -31
package/README.md
CHANGED
|
@@ -14,10 +14,10 @@ Includes support for generating LOD quality level for textures and meshes. Use w
|
|
|
14
14
|
In general all files that have been processed by this package are valid glTF/GLB files.
|
|
15
15
|
The glTF Build Pipeline package is meant to be used as a last step before delivery.
|
|
16
16
|
That means:
|
|
17
|
-
- files are meant to be loaded
|
|
17
|
+
- files are meant to be loaded by [Needle Engine](https://needle.tools) (and other three.js based runtimes)
|
|
18
18
|
- files are not to be opened in Blender again to be further edited
|
|
19
19
|
|
|
20
|
-
To load progressively enhanced glTF files in
|
|
20
|
+
To load progressively enhanced glTF files in Needle Engine or another three.js based runtime, see examples at [@needle-tools/gltf-progressive](https://www.npmjs.com/package/@needle-tools/gltf-progressive)
|
|
21
21
|
|
|
22
22
|
### Installation
|
|
23
23
|
|
|
@@ -26,6 +26,33 @@ To load progressively enhanced glTF files in any three.js based engine see examp
|
|
|
26
26
|
### Commands
|
|
27
27
|
- `help` - use help to view options
|
|
28
28
|
|
|
29
|
+
### Configuration
|
|
30
|
+
|
|
31
|
+
The pipeline reads an optional `needle.config.json` from the working directory (or any parent directory). Configure pipeline behaviour under the `gltf` key:
|
|
32
|
+
|
|
33
|
+
```json
|
|
34
|
+
{
|
|
35
|
+
"gltf": {
|
|
36
|
+
"usecase": "product",
|
|
37
|
+
"textures": { "lods": true },
|
|
38
|
+
"meshes": { "lods": true },
|
|
39
|
+
"audio": { "enabled": true, "bitrate": 64 },
|
|
40
|
+
"exr": { "enabled": true }
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
The `gltf` value may also be a string shorthand for `usecase` — `"gltf": "product"` is equivalent to `"gltf": { "usecase": "product" }`.
|
|
46
|
+
|
|
47
|
+
| Key | Type | Default | Description |
|
|
48
|
+
|-----|------|---------|-------------|
|
|
49
|
+
| `usecase` | `"default" \| "product" \| "world"` | `"default"` | High-level optimisation profile. |
|
|
50
|
+
| `textures.lods` | `boolean` | `true` | Generate progressive texture LODs. |
|
|
51
|
+
| `meshes.lods` | `boolean` | `true` | Generate progressive mesh LODs. |
|
|
52
|
+
| `audio.enabled` | `boolean` | `true` | Compress externally referenced audio files (`.wav` / `.mp3` / `.ogg` / `.flac` / `.m4a` / `.aac`) to Opus in an `.ogg` container, rewriting the URIs in the glTF. Requires `ffmpeg` with `libopus` on `PATH`; when missing the build logs a warning and leaves the audio files untouched. |
|
|
53
|
+
| `audio.bitrate` | `number` (kbps) | `128` | Opus encoding bitrate. 96 is near-transparent for most music; 128 matches YouTube's music tier and is the safe "won't sound bad" default. Drop to 48–64 for known mono/UI-only assets. |
|
|
54
|
+
| `exr.enabled` | `boolean` | `true` | Convert EXR textures to KTX2 HDR via PMREM. When `false`, EXR textures (bundled and externally referenced) are left as-is so a downstream step can handle them. |
|
|
55
|
+
|
|
29
56
|
# Contact
|
|
30
57
|
|
|
31
58
|
For licensing questions please contact us.
|
package/dist/config/index.d.ts
CHANGED
|
@@ -13,6 +13,17 @@ export type Config = {
|
|
|
13
13
|
meshes?: {
|
|
14
14
|
lods: boolean;
|
|
15
15
|
};
|
|
16
|
+
audio?: {
|
|
17
|
+
/** Enable Opus/Ogg compression of externally referenced audio files. Default: true. */
|
|
18
|
+
enabled?: boolean;
|
|
19
|
+
/** Opus encoding bitrate in kbps. Default: 64. */
|
|
20
|
+
bitrate?: number;
|
|
21
|
+
};
|
|
22
|
+
exr?: {
|
|
23
|
+
/** Enable EXR -> KTX2 HDR conversion via PMREM. When false, EXR textures (bundled or
|
|
24
|
+
* externally referenced) are left untouched. Default: true. */
|
|
25
|
+
enabled?: boolean;
|
|
26
|
+
};
|
|
16
27
|
};
|
|
17
28
|
/**
|
|
18
29
|
* Create a config object with the given usecase.
|
package/dist/config/index.js
CHANGED
|
@@ -70,8 +70,11 @@ export function getConfig(configPath, fallbackSearchDirectory, opts) {
|
|
|
70
70
|
throw new Error(`Failed parsing config at \"${configPath}\"`);
|
|
71
71
|
}
|
|
72
72
|
}
|
|
73
|
+
// Always return a fresh shallow copy. Callers (notably the CLI) mutate the
|
|
74
|
+
// returned object (`config.usecase = ...`); returning the cache by reference
|
|
75
|
+
// would let those mutations poison subsequent getConfig() calls.
|
|
73
76
|
if (_cachedConfig !== undefined) {
|
|
74
|
-
return _cachedConfig;
|
|
77
|
+
return { ..._cachedConfig };
|
|
75
78
|
}
|
|
76
79
|
// Resolve the config once and cache it
|
|
77
80
|
// Check if a needle.config.json exists in the directory from which the script is run (or any parent directory)
|
|
@@ -82,12 +85,14 @@ export function getConfig(configPath, fallbackSearchDirectory, opts) {
|
|
|
82
85
|
logger.info(`Using config with usecase: ${_cachedConfig.usecase}`);
|
|
83
86
|
}
|
|
84
87
|
}
|
|
85
|
-
// If no config was found, use the default config
|
|
88
|
+
// If no config was found, use the default config. Store a copy in the cache
|
|
89
|
+
// so we never alias defaultConfig directly — that way even if a downstream
|
|
90
|
+
// bug ever returned the cache by reference, defaultConfig itself stays clean.
|
|
86
91
|
if (!_cachedConfig) {
|
|
87
92
|
logger.debug("No config found. Using default config.");
|
|
88
|
-
_cachedConfig = defaultConfig;
|
|
93
|
+
_cachedConfig = { ...defaultConfig };
|
|
89
94
|
}
|
|
90
|
-
return _cachedConfig;
|
|
95
|
+
return { ..._cachedConfig };
|
|
91
96
|
}
|
|
92
97
|
let _cachedConfig = undefined;
|
|
93
98
|
export function searchConfig(directory, logger) {
|
|
@@ -5,6 +5,7 @@ import { NEEDLE_progressive_texture_settings } from '../NEEDLE_progressive_textu
|
|
|
5
5
|
import { testAssert } from '../../utils/test-assert.js';
|
|
6
6
|
import { existsSync, readFileSync } from 'fs';
|
|
7
7
|
import { dirname, join } from 'path';
|
|
8
|
+
import { isAudioUri, isRelativeAudioUri, listAudioRefs, registerAudioRef } from '../../transforms/needle_audio_registry.js';
|
|
8
9
|
const ALL_PROPERTY_TYPES = [];
|
|
9
10
|
for (const key in PropertyType) {
|
|
10
11
|
ALL_PROPERTY_TYPES.push(PropertyType[key]);
|
|
@@ -627,6 +628,25 @@ class JsonPointerHandler {
|
|
|
627
628
|
else
|
|
628
629
|
console.warn("WARN: failed registering pointer", fullPath, value);
|
|
629
630
|
}
|
|
631
|
+
// External audio reference (e.g. "click.wav" or "music.mp3" on a NEEDLE_components AudioSource).
|
|
632
|
+
// Audio is never embedded in the GLB — it stays as a sidecar file. We register the
|
|
633
|
+
// (obj, key) location so `needle_audio_transform` can compress the file and so the
|
|
634
|
+
// write-time pass can rewrite the JSON string to the new URI.
|
|
635
|
+
if (!isTexturePointer && isAudioUri(value) && isRelativeAudioUri(value)) {
|
|
636
|
+
const inputDir = getExtensionInputFile() ? dirname(getExtensionInputFile()) : '';
|
|
637
|
+
const audioPath = inputDir ? join(inputDir, value) : '';
|
|
638
|
+
if (audioPath && existsSync(audioPath)) {
|
|
639
|
+
registerAudioRef({ obj, key, sourcePath: audioPath, sourceUri: value });
|
|
640
|
+
if (this.debug)
|
|
641
|
+
console.log(`Registered external audio "${value}" at ${fullPath}`);
|
|
642
|
+
}
|
|
643
|
+
else if (audioPath) {
|
|
644
|
+
// Non-fatal: leave the URI untouched and warn. Mirrors EXR error path
|
|
645
|
+
// but uses warn instead of testAssert since missing audio shouldn't
|
|
646
|
+
// hard-fail the build.
|
|
647
|
+
this.document.getLogger().warn(`NEEDLE_opaque: external audio not found: ${audioPath}`);
|
|
648
|
+
}
|
|
649
|
+
}
|
|
630
650
|
// External EXR file reference (e.g. "StudioHDRI_ferndale_studio_04_1k.exr" in a ReflectionProbe)
|
|
631
651
|
// Inject the file into the document as a Texture so PMREM can process it
|
|
632
652
|
if (!isTexturePointer && value.match(/\.exr$/i)) {
|
|
@@ -816,6 +836,16 @@ class JsonPointerHandler {
|
|
|
816
836
|
if (ptr.resolve(context, step))
|
|
817
837
|
ptr.write();
|
|
818
838
|
});
|
|
839
|
+
// Resolve external audio pointers: rewrite the JSON string from the original URI
|
|
840
|
+
// (e.g. "click.wav") to the compressed URI (e.g. "click.opus.ogg"). Refs whose
|
|
841
|
+
// `newUri` was never set (ffmpeg missing, compression skipped) are left untouched.
|
|
842
|
+
for (const ref of listAudioRefs()) {
|
|
843
|
+
if (ref.newUri && ref.obj[ref.key] === ref.sourceUri) {
|
|
844
|
+
if (this.debug)
|
|
845
|
+
console.log(`< Resolved external audio: ${ref.sourceUri} → ${ref.newUri}`);
|
|
846
|
+
ref.obj[ref.key] = ref.newUri;
|
|
847
|
+
}
|
|
848
|
+
}
|
|
819
849
|
// Resolve external EXR pointers: update component data with the texture's current URI
|
|
820
850
|
for (const { obj, key, texture } of this.externalExrPointers) {
|
|
821
851
|
const uri = texture.getURI() || texture.getName() || '';
|
|
@@ -6,7 +6,7 @@ import { existsSync, readFileSync, statSync, writeFileSync } from 'fs';
|
|
|
6
6
|
import { MeshoptDecoder, MeshoptEncoder } from 'meshoptimizer';
|
|
7
7
|
import { dedup, metalRough, prune, resample } from '@gltf-transform/functions';
|
|
8
8
|
import { ALL_EXTENSIONS as NEEDLE_EXTENSIONS, NEEDLE_compression_texture, NEEDLE_mesh_compression, NEEDLE_pmrem, NEEDLE_lightmaps_ext, setExtensionInputFile } from '../extensions/index.js';
|
|
9
|
-
import { isLOD, isMeshLOD, needle_animation_transform, needle_asset, needle_mesh_transform, needle_texture_transform, } from '../transforms/index.js';
|
|
9
|
+
import { clearAudioRegistry, isLOD, isMeshLOD, listAudioRefs, needle_animation_transform, needle_asset, needle_audio_transform, needle_mesh_transform, needle_texture_transform, } from '../transforms/index.js';
|
|
10
10
|
import { copyExternalResources, getOutputPath, getVersion, isMaterialOnlyGLB, ioTryReadWithMissingResources, writeNodeIO } from "../utils/index.js";
|
|
11
11
|
import { addToCache, getHash, tryGetFromCache, } from "../cache/index.js";
|
|
12
12
|
import { TestAssertionError } from '../utils/test-assert.js';
|
|
@@ -70,6 +70,7 @@ export async function packGLTF(inputFile, outputFile, options) {
|
|
|
70
70
|
// Use sourceFile when available (e.g. when inputFile is a progressive output copy)
|
|
71
71
|
const assetSourceFile = options.sourceFile ?? inputFile;
|
|
72
72
|
setExtensionInputFile(assetSourceFile);
|
|
73
|
+
clearAudioRegistry();
|
|
73
74
|
const document = await ioTryReadWithMissingResources(io, inputFile);
|
|
74
75
|
if (options.logger) {
|
|
75
76
|
document.setLogger(options.logger);
|
|
@@ -96,6 +97,7 @@ export async function packGLTF(inputFile, outputFile, options) {
|
|
|
96
97
|
config,
|
|
97
98
|
}),
|
|
98
99
|
needle_mesh_transform({ file: inputFile, config }),
|
|
100
|
+
needle_audio_transform({ outfile: outputFile, useCache, config }),
|
|
99
101
|
needle_animation_transform({}),
|
|
100
102
|
resample({
|
|
101
103
|
tolerance: 1e-6, // NOTE: lowered from 1e-4 to 1e-6 to fix https://linear.app/needle/issue/NE-6872
|
|
@@ -148,6 +150,13 @@ export async function packGLTF(inputFile, outputFile, options) {
|
|
|
148
150
|
}
|
|
149
151
|
}
|
|
150
152
|
}
|
|
153
|
+
for (const ref of listAudioRefs()) {
|
|
154
|
+
const uri = ref.newUri ?? ref.sourceUri;
|
|
155
|
+
const absPath = path.resolve(outDir, uri);
|
|
156
|
+
if (existsSync(absPath)) {
|
|
157
|
+
options.externalFiles.push(absPath);
|
|
158
|
+
}
|
|
159
|
+
}
|
|
151
160
|
}
|
|
152
161
|
logger.debug(`← Writing to ${outputFile}`);
|
|
153
162
|
await writeNodeIO(io, outputFile, document);
|
|
@@ -7,3 +7,6 @@ export * from "./needle_mesh_transform.js";
|
|
|
7
7
|
export * from "./needle_common.js";
|
|
8
8
|
export * from "./needle_texture_transform.js";
|
|
9
9
|
export * from "./needle_animation_transform.js";
|
|
10
|
+
export * from "./needle_audio.js";
|
|
11
|
+
export * from "./needle_audio_registry.js";
|
|
12
|
+
export * from "./needle_audio_ffmpeg.js";
|
package/dist/transforms/index.js
CHANGED
|
@@ -7,3 +7,6 @@ export * from "./needle_mesh_transform.js";
|
|
|
7
7
|
export * from "./needle_common.js";
|
|
8
8
|
export * from "./needle_texture_transform.js";
|
|
9
9
|
export * from "./needle_animation_transform.js";
|
|
10
|
+
export * from "./needle_audio.js";
|
|
11
|
+
export * from "./needle_audio_registry.js";
|
|
12
|
+
export * from "./needle_audio_ffmpeg.js";
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { Transform } from '@gltf-transform/core';
|
|
2
|
+
import { Config } from '../config/index.js';
|
|
3
|
+
export type AudioTransformOptions = {
|
|
4
|
+
/** The path the GLB will be written to. Audio sidecars are placed alongside. */
|
|
5
|
+
outfile: string;
|
|
6
|
+
/** Disk cache toggle */
|
|
7
|
+
useCache: boolean;
|
|
8
|
+
config: Config;
|
|
9
|
+
};
|
|
10
|
+
export declare function needle_audio_transform(options: AudioTransformOptions): Transform;
|
|
11
|
+
/** "click.wav" → "click.opus.ogg". Preserves the basename casing. */
|
|
12
|
+
export declare function computeOpusOggUri(sourceUri: string): string;
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
import { createTransform } from '@gltf-transform/functions';
|
|
2
|
+
import { createHash } from 'crypto';
|
|
3
|
+
import { existsSync, mkdirSync, readFileSync, rmSync, writeFileSync } from 'fs';
|
|
4
|
+
import { basename, dirname, extname, join } from 'path';
|
|
5
|
+
import { v4 as uuid } from 'uuid';
|
|
6
|
+
import tmp from 'tmp';
|
|
7
|
+
import { addToCache, tryGetFromCache } from '../cache/index.js';
|
|
8
|
+
import { safeRmDir } from '../utils/fileutils.js';
|
|
9
|
+
import { encodeOpusOgg, findFfmpeg } from './needle_audio_ffmpeg.js';
|
|
10
|
+
import { listAudioRefs } from './needle_audio_registry.js';
|
|
11
|
+
const DEFAULT_BITRATE_KBPS = 128;
|
|
12
|
+
export function needle_audio_transform(options) {
|
|
13
|
+
return createTransform('needle_audio_transform', async (document) => {
|
|
14
|
+
const refs = listAudioRefs();
|
|
15
|
+
if (refs.length === 0)
|
|
16
|
+
return;
|
|
17
|
+
const logger = document.getLogger();
|
|
18
|
+
const audioCfg = options.config.audio ?? {};
|
|
19
|
+
if (audioCfg.enabled === false) {
|
|
20
|
+
logger.debug(`audio: disabled in config — ${refs.length} external audio reference(s) left untouched`);
|
|
21
|
+
return;
|
|
22
|
+
}
|
|
23
|
+
const bitrate = audioCfg.bitrate ?? DEFAULT_BITRATE_KBPS;
|
|
24
|
+
const ffmpeg = await findFfmpeg();
|
|
25
|
+
if (!ffmpeg) {
|
|
26
|
+
logger.warn(`audio: ffmpeg not found on PATH — ${refs.length} audio file(s) will pass through uncompressed`);
|
|
27
|
+
return;
|
|
28
|
+
}
|
|
29
|
+
if (!ffmpeg.hasLibopus) {
|
|
30
|
+
logger.warn(`audio: ffmpeg is installed but libopus codec is missing — ${refs.length} audio file(s) will pass through uncompressed`);
|
|
31
|
+
return;
|
|
32
|
+
}
|
|
33
|
+
const outDir = dirname(options.outfile);
|
|
34
|
+
const bySource = new Map();
|
|
35
|
+
for (const ref of refs) {
|
|
36
|
+
const list = bySource.get(ref.sourcePath) ?? [];
|
|
37
|
+
list.push(ref);
|
|
38
|
+
bySource.set(ref.sourcePath, list);
|
|
39
|
+
}
|
|
40
|
+
const tmpDir = join(tmp.tmpdir, 'needle-audio', uuid());
|
|
41
|
+
mkdirSync(tmpDir, { recursive: true });
|
|
42
|
+
try {
|
|
43
|
+
for (const [sourcePath, refsForSource] of bySource) {
|
|
44
|
+
if (!existsSync(sourcePath)) {
|
|
45
|
+
logger.warn(`audio: source not found at write time, skipping: ${sourcePath}`);
|
|
46
|
+
continue;
|
|
47
|
+
}
|
|
48
|
+
const sourceUri = refsForSource[0].sourceUri;
|
|
49
|
+
const newUri = computeOpusOggUri(sourceUri);
|
|
50
|
+
const sourceBytes = readFileSync(sourcePath);
|
|
51
|
+
const contentHash = createHash('md5').update(sourceBytes).digest('hex');
|
|
52
|
+
const cacheKey = `audio-opus-${contentHash}-${bitrate}k`;
|
|
53
|
+
let oggBytes = null;
|
|
54
|
+
if (options.useCache) {
|
|
55
|
+
const cached = tryGetFromCache(cacheKey);
|
|
56
|
+
if (cached && cached.length > 0) {
|
|
57
|
+
logger.debug(`audio: cache hit for ${sourceUri} (${cacheKey})`);
|
|
58
|
+
oggBytes = cached;
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
if (!oggBytes) {
|
|
62
|
+
const tmpOgg = join(tmpDir, `${uuid()}.ogg`);
|
|
63
|
+
const t0 = Date.now();
|
|
64
|
+
await encodeOpusOgg(ffmpeg.path, sourcePath, tmpOgg, bitrate);
|
|
65
|
+
if (!existsSync(tmpOgg)) {
|
|
66
|
+
logger.error(`audio: ffmpeg did not produce output for ${sourceUri}`);
|
|
67
|
+
continue;
|
|
68
|
+
}
|
|
69
|
+
oggBytes = new Uint8Array(readFileSync(tmpOgg));
|
|
70
|
+
const elapsed = ((Date.now() - t0) / 1000).toFixed(1);
|
|
71
|
+
const srcKb = sourceBytes.length / 1024;
|
|
72
|
+
const dstKb = oggBytes.length / 1024;
|
|
73
|
+
logger.info(`audio: ${sourceUri} (${srcKb.toFixed(0)} KB) → ${newUri} (${dstKb.toFixed(0)} KB) in ${elapsed}s`);
|
|
74
|
+
if (options.useCache)
|
|
75
|
+
addToCache(cacheKey, oggBytes);
|
|
76
|
+
}
|
|
77
|
+
const outPath = join(outDir, newUri);
|
|
78
|
+
mkdirSync(dirname(outPath), { recursive: true });
|
|
79
|
+
writeFileSync(outPath, oggBytes);
|
|
80
|
+
// Remove the source audio file from the output dir (mirrors PMREM at needle_pmrem.ts:161-164).
|
|
81
|
+
// The source bytes have already been read into memory above, so this is safe even in
|
|
82
|
+
// in-place mode (input dir == output dir).
|
|
83
|
+
const sourceInOutDir = join(outDir, sourceUri);
|
|
84
|
+
if (existsSync(sourceInOutDir)) {
|
|
85
|
+
rmSync(sourceInOutDir);
|
|
86
|
+
}
|
|
87
|
+
for (const ref of refsForSource) {
|
|
88
|
+
ref.newUri = newUri;
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
finally {
|
|
93
|
+
await safeRmDir(tmpDir);
|
|
94
|
+
}
|
|
95
|
+
});
|
|
96
|
+
}
|
|
97
|
+
/** "click.wav" → "click.opus.ogg". Preserves the basename casing. */
|
|
98
|
+
export function computeOpusOggUri(sourceUri) {
|
|
99
|
+
const dir = dirname(sourceUri);
|
|
100
|
+
const ext = extname(sourceUri);
|
|
101
|
+
const stem = basename(sourceUri, ext);
|
|
102
|
+
const newName = `${stem}.opus.ogg`;
|
|
103
|
+
return dir === '.' || dir === '' ? newName : join(dir, newName);
|
|
104
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Soft-dependency probe for system `ffmpeg` with `libopus` encoder.
|
|
3
|
+
*
|
|
4
|
+
* Returns `{ path, hasLibopus }` if ffmpeg is on PATH, else `null`. Cached for the
|
|
5
|
+
* process lifetime — `findFfmpeg()` only spawns the probe once. Test code can
|
|
6
|
+
* reset the cache via `__resetFfmpegProbeForTests()`.
|
|
7
|
+
*/
|
|
8
|
+
export type FfmpegInfo = {
|
|
9
|
+
path: string;
|
|
10
|
+
hasLibopus: boolean;
|
|
11
|
+
};
|
|
12
|
+
export declare function findFfmpeg(): Promise<FfmpegInfo | null>;
|
|
13
|
+
export declare function __resetFfmpegProbeForTests(): void;
|
|
14
|
+
export declare function encodeOpusOgg(ffmpegPath: string, inputPath: string, outputPath: string, bitrateKbps: number): Promise<void>;
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Soft-dependency probe for system `ffmpeg` with `libopus` encoder.
|
|
3
|
+
*
|
|
4
|
+
* Returns `{ path, hasLibopus }` if ffmpeg is on PATH, else `null`. Cached for the
|
|
5
|
+
* process lifetime — `findFfmpeg()` only spawns the probe once. Test code can
|
|
6
|
+
* reset the cache via `__resetFfmpegProbeForTests()`.
|
|
7
|
+
*/
|
|
8
|
+
import { execFile } from 'child_process';
|
|
9
|
+
let _probe;
|
|
10
|
+
export function findFfmpeg() {
|
|
11
|
+
if (_probe === undefined) {
|
|
12
|
+
_probe = probe();
|
|
13
|
+
}
|
|
14
|
+
return _probe;
|
|
15
|
+
}
|
|
16
|
+
export function __resetFfmpegProbeForTests() {
|
|
17
|
+
_probe = undefined;
|
|
18
|
+
}
|
|
19
|
+
function probe() {
|
|
20
|
+
return new Promise(resolve => {
|
|
21
|
+
execFile('ffmpeg', ['-hide_banner', '-codecs'], { timeout: 5000, windowsHide: true }, (err, stdout) => {
|
|
22
|
+
if (err) {
|
|
23
|
+
resolve(null);
|
|
24
|
+
return;
|
|
25
|
+
}
|
|
26
|
+
const hasLibopus = /libopus/i.test(stdout) && /opus/i.test(stdout);
|
|
27
|
+
resolve({ path: 'ffmpeg', hasLibopus });
|
|
28
|
+
});
|
|
29
|
+
});
|
|
30
|
+
}
|
|
31
|
+
export function encodeOpusOgg(ffmpegPath, inputPath, outputPath, bitrateKbps) {
|
|
32
|
+
return new Promise((resolve, reject) => {
|
|
33
|
+
const args = [
|
|
34
|
+
'-hide_banner',
|
|
35
|
+
'-loglevel', 'error',
|
|
36
|
+
'-y',
|
|
37
|
+
'-i', inputPath,
|
|
38
|
+
'-vn',
|
|
39
|
+
'-c:a', 'libopus',
|
|
40
|
+
'-b:a', `${bitrateKbps}k`,
|
|
41
|
+
outputPath,
|
|
42
|
+
];
|
|
43
|
+
execFile(ffmpegPath, args, { windowsHide: true }, (err, _stdout, stderr) => {
|
|
44
|
+
if (err) {
|
|
45
|
+
reject(new Error(`ffmpeg failed (exit ${err.code}):\n${stderr || err.message}`));
|
|
46
|
+
return;
|
|
47
|
+
}
|
|
48
|
+
resolve();
|
|
49
|
+
});
|
|
50
|
+
});
|
|
51
|
+
}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Module-global registry for external audio file references discovered during glTF read.
|
|
3
|
+
*
|
|
4
|
+
* Mirrors the `_extensionInputFile` module-global pattern used elsewhere in the codebase.
|
|
5
|
+
* The CLI processes files sequentially, so the registry is reset at the start of every
|
|
6
|
+
* `packGLTF` call via `clearAudioRegistry()`.
|
|
7
|
+
*
|
|
8
|
+
* Population happens in `JsonPointerHandler.read` when it encounters a string ending
|
|
9
|
+
* in a known audio extension. Consumption happens in two places:
|
|
10
|
+
* 1. `needle_audio_transform` — reads `listAudioRefs()`, compresses each unique
|
|
11
|
+
* source file, fills in `newUri` on every ref pointing at that source.
|
|
12
|
+
* 2. `JsonPointerHandler.resolveAndWrite` — overwrites `ref.obj[ref.key]` with
|
|
13
|
+
* `ref.newUri` so the JSON output points at the compressed file.
|
|
14
|
+
*/
|
|
15
|
+
export type AudioRef = {
|
|
16
|
+
/** The component-data object whose `[key]` field holds the audio path */
|
|
17
|
+
obj: object;
|
|
18
|
+
/** Key on `obj` that holds the original audio URI string */
|
|
19
|
+
key: string;
|
|
20
|
+
/** Absolute path to the source audio file on disk */
|
|
21
|
+
sourcePath: string;
|
|
22
|
+
/** Original URI as it appeared in the JSON (relative path) */
|
|
23
|
+
sourceUri: string;
|
|
24
|
+
/** Set by `needle_audio_transform` after compression. If unset, the JSON ref
|
|
25
|
+
* is left untouched (e.g. ffmpeg missing, compression skipped). */
|
|
26
|
+
newUri?: string;
|
|
27
|
+
};
|
|
28
|
+
export declare function registerAudioRef(ref: AudioRef): void;
|
|
29
|
+
export declare function listAudioRefs(): AudioRef[];
|
|
30
|
+
export declare function clearAudioRegistry(): void;
|
|
31
|
+
/** Audio extensions we recognise. Comparison is case-insensitive. */
|
|
32
|
+
export declare const AUDIO_EXTENSIONS: readonly [".wav", ".mp3", ".ogg", ".flac", ".m4a", ".aac"];
|
|
33
|
+
export declare function isAudioUri(value: string): boolean;
|
|
34
|
+
/** Returns true if the URI is a relative path we should attempt to resolve+compress.
|
|
35
|
+
* Absolute URLs (http(s), data, file, blob, leading slash, drive letter) are passed through unchanged. */
|
|
36
|
+
export declare function isRelativeAudioUri(value: string): boolean;
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Module-global registry for external audio file references discovered during glTF read.
|
|
3
|
+
*
|
|
4
|
+
* Mirrors the `_extensionInputFile` module-global pattern used elsewhere in the codebase.
|
|
5
|
+
* The CLI processes files sequentially, so the registry is reset at the start of every
|
|
6
|
+
* `packGLTF` call via `clearAudioRegistry()`.
|
|
7
|
+
*
|
|
8
|
+
* Population happens in `JsonPointerHandler.read` when it encounters a string ending
|
|
9
|
+
* in a known audio extension. Consumption happens in two places:
|
|
10
|
+
* 1. `needle_audio_transform` — reads `listAudioRefs()`, compresses each unique
|
|
11
|
+
* source file, fills in `newUri` on every ref pointing at that source.
|
|
12
|
+
* 2. `JsonPointerHandler.resolveAndWrite` — overwrites `ref.obj[ref.key]` with
|
|
13
|
+
* `ref.newUri` so the JSON output points at the compressed file.
|
|
14
|
+
*/
|
|
15
|
+
let _refs = [];
|
|
16
|
+
export function registerAudioRef(ref) {
|
|
17
|
+
_refs.push(ref);
|
|
18
|
+
}
|
|
19
|
+
export function listAudioRefs() {
|
|
20
|
+
return _refs;
|
|
21
|
+
}
|
|
22
|
+
export function clearAudioRegistry() {
|
|
23
|
+
_refs = [];
|
|
24
|
+
}
|
|
25
|
+
/** Audio extensions we recognise. Comparison is case-insensitive. */
|
|
26
|
+
export const AUDIO_EXTENSIONS = ['.wav', '.mp3', '.ogg', '.flac', '.m4a', '.aac'];
|
|
27
|
+
const _audioRegex = new RegExp(`(${AUDIO_EXTENSIONS.map(e => '\\' + e).join('|')})$`, 'i');
|
|
28
|
+
export function isAudioUri(value) {
|
|
29
|
+
return _audioRegex.test(value);
|
|
30
|
+
}
|
|
31
|
+
/** Returns true if the URI is a relative path we should attempt to resolve+compress.
|
|
32
|
+
* Absolute URLs (http(s), data, file, blob, leading slash, drive letter) are passed through unchanged. */
|
|
33
|
+
export function isRelativeAudioUri(value) {
|
|
34
|
+
if (!value)
|
|
35
|
+
return false;
|
|
36
|
+
if (/^[a-z]+:/i.test(value))
|
|
37
|
+
return false; // http:, https:, data:, file:, blob:, etc.
|
|
38
|
+
if (value.startsWith('//'))
|
|
39
|
+
return false; // protocol-relative
|
|
40
|
+
if (value.startsWith('/'))
|
|
41
|
+
return false; // POSIX absolute
|
|
42
|
+
if (/^[a-z]:[\\/]/i.test(value))
|
|
43
|
+
return false; // Windows drive (C:\ or C:/)
|
|
44
|
+
return true;
|
|
45
|
+
}
|
|
@@ -96,6 +96,10 @@ async function processTexture(texture, index, document, logger, context) {
|
|
|
96
96
|
const originalMimeType = texture.getMimeType();
|
|
97
97
|
// Handle EXR textures via PMREM → KTX2 HDR
|
|
98
98
|
if (originalMimeType === "image/exr") {
|
|
99
|
+
if (context.config.exr?.enabled === false) {
|
|
100
|
+
logger.debug(`→ Skipping EXR -> KTX2 HDR for ${texture.getName()} [${index}] (config.exr.enabled = false)`);
|
|
101
|
+
return;
|
|
102
|
+
}
|
|
99
103
|
await context.pmremContext.process(index, texture, null, context);
|
|
100
104
|
return;
|
|
101
105
|
}
|
package/dist/transforms/toktx.js
CHANGED
|
@@ -4,8 +4,7 @@ import os from 'os';
|
|
|
4
4
|
import semver from 'semver';
|
|
5
5
|
import tmp from 'tmp';
|
|
6
6
|
import { TextureChannel } from '@gltf-transform/core';
|
|
7
|
-
import {
|
|
8
|
-
import { spawn, commandExists, waitExit, MICROMATCH_OPTIONS } from './util.js';
|
|
7
|
+
import { spawn, waitExit, MICROMATCH_OPTIONS } from './util.js';
|
|
9
8
|
import { TextureResizeFilter, getTextureColorSpace } from '@gltf-transform/functions';
|
|
10
9
|
import { ETC1S_DEFAULTS, UASTC_DEFAULTS } from './toktx_types.js';
|
|
11
10
|
tmp.setGracefulCleanup();
|
|
@@ -143,53 +142,41 @@ export function createParams(texture, slots, channels, size, logger, numTextures
|
|
|
143
142
|
}
|
|
144
143
|
return params;
|
|
145
144
|
}
|
|
146
|
-
let
|
|
145
|
+
let didLogKtxVersion = false;
|
|
147
146
|
export async function checkKTXSoftware(logger, env) {
|
|
148
|
-
|
|
149
|
-
|
|
147
|
+
// Probe the `ktx` binary because that's what the compression step actually runs
|
|
148
|
+
// (see needle_toktx.ts: spawn('ktx', params, ...)). Probing `toktx` would let a
|
|
149
|
+
// partial install pass the prerequisite check and then fail mid-compression
|
|
150
|
+
// when `ktx create` is invoked. `ktx` ships with KTX-Software 4.3+ alongside
|
|
151
|
+
// `toktx`, so this matches our KTX_SOFTWARE_VERSION_MIN.
|
|
152
|
+
let failedFindingKtx = false;
|
|
153
|
+
const proc = spawn('ktx', ['--version'], { env: env });
|
|
150
154
|
proc.on('error', (err) => {
|
|
151
155
|
if (err.message.endsWith("ENOENT")) {
|
|
152
|
-
|
|
156
|
+
failedFindingKtx = true;
|
|
153
157
|
}
|
|
154
158
|
});
|
|
155
159
|
const [status, stdout, stderr] = await waitExit(proc);
|
|
156
|
-
if (
|
|
157
|
-
console.log(`WARN: Unable to find "
|
|
160
|
+
if (failedFindingKtx) {
|
|
161
|
+
console.log(`WARN: Unable to find "ktx". Confirm KTX-Software is installed from: https://github.com/KhronosGroup/KTX-Software.`);
|
|
158
162
|
return null;
|
|
159
163
|
}
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
throw new Error('Unable to find "toktx" version. Confirm KTX-Software is installed from:\n\nhttps://github.com/KhronosGroup/KTX-Software.');
|
|
164
|
+
// Extract the X.Y.Z from outputs like "ktx version: v4.3.2~6" or "toktx v4.3.0~28".
|
|
165
|
+
const versionMatch = (stdout || stderr).match(/v?(\d+\.\d+\.\d+)/);
|
|
166
|
+
const version = versionMatch ? versionMatch[1] : null;
|
|
167
|
+
if (status !== 0 || !version || !semver.valid(version)) {
|
|
168
|
+
throw new Error('Unable to find "ktx" version. Confirm KTX-Software is installed from:\n\nhttps://github.com/KhronosGroup/KTX-Software.');
|
|
166
169
|
}
|
|
167
|
-
else if (semver.lt(
|
|
168
|
-
logger.warn(`
|
|
170
|
+
else if (semver.lt(version, KTX_SOFTWARE_VERSION_MIN)) {
|
|
171
|
+
logger.warn(`ktx: Expected KTX-Software >= v${KTX_SOFTWARE_VERSION_MIN}, found ${version}.`);
|
|
169
172
|
return null;
|
|
170
173
|
}
|
|
171
174
|
else {
|
|
172
|
-
if (!
|
|
173
|
-
logger.debug(`
|
|
174
|
-
|
|
175
|
+
if (!didLogKtxVersion)
|
|
176
|
+
logger.debug(`ktx: Found KTX-Software ${version}.`);
|
|
177
|
+
didLogKtxVersion = true;
|
|
175
178
|
}
|
|
176
|
-
return
|
|
177
|
-
}
|
|
178
|
-
async function toktxCommandExistsSync(env) {
|
|
179
|
-
if (env) {
|
|
180
|
-
// if env is defined use that to check if toktx is installed
|
|
181
|
-
try {
|
|
182
|
-
execSync('toktx --version', { encoding: 'utf-8', env: env });
|
|
183
|
-
return true;
|
|
184
|
-
}
|
|
185
|
-
catch (error) {
|
|
186
|
-
return false;
|
|
187
|
-
}
|
|
188
|
-
}
|
|
189
|
-
else if (!await commandExists('toktx')) {
|
|
190
|
-
return false;
|
|
191
|
-
}
|
|
192
|
-
return true;
|
|
179
|
+
return version;
|
|
193
180
|
}
|
|
194
181
|
function isPowerOfTwo(value) {
|
|
195
182
|
if (value <= 2)
|
package/dist/utils/index.d.ts
CHANGED
package/dist/utils/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export declare const version = "2.16.0-next.
|
|
1
|
+
export declare const version = "2.16.0-next.2b9edfd";
|
|
@@ -1 +1 @@
|
|
|
1
|
-
export const version = "2.16.0-next.
|
|
1
|
+
export const version = "2.16.0-next.2b9edfd";
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@needle-tools/gltf-build-pipeline",
|
|
3
|
-
"version": "2.16.0-next.
|
|
3
|
+
"version": "2.16.0-next.2b9edfd",
|
|
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",
|
package/dist/utils/merge.d.ts
DELETED
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Simple object check.
|
|
3
|
-
* @param item
|
|
4
|
-
* @returns {boolean}
|
|
5
|
-
*/
|
|
6
|
-
export declare function isObject(item: object): boolean;
|
|
7
|
-
/**
|
|
8
|
-
* Deep merge two objects.
|
|
9
|
-
* @param target
|
|
10
|
-
* @param ...sources
|
|
11
|
-
*/
|
|
12
|
-
export declare function mergeDeep(target: any, ...sources: any[]): any;
|
package/dist/utils/merge.js
DELETED
|
@@ -1,31 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Simple object check.
|
|
3
|
-
* @param item
|
|
4
|
-
* @returns {boolean}
|
|
5
|
-
*/
|
|
6
|
-
export function isObject(item) {
|
|
7
|
-
return (item && typeof item === 'object' && !Array.isArray(item));
|
|
8
|
-
}
|
|
9
|
-
/**
|
|
10
|
-
* Deep merge two objects.
|
|
11
|
-
* @param target
|
|
12
|
-
* @param ...sources
|
|
13
|
-
*/
|
|
14
|
-
export function mergeDeep(target, ...sources) {
|
|
15
|
-
if (!sources.length)
|
|
16
|
-
return target;
|
|
17
|
-
const source = sources.shift();
|
|
18
|
-
if (isObject(target) && isObject(source)) {
|
|
19
|
-
for (const key in source) {
|
|
20
|
-
if (isObject(source[key])) {
|
|
21
|
-
if (!target[key])
|
|
22
|
-
Object.assign(target, { [key]: {} });
|
|
23
|
-
mergeDeep(target[key], source[key]);
|
|
24
|
-
}
|
|
25
|
-
else {
|
|
26
|
-
Object.assign(target, { [key]: source[key] });
|
|
27
|
-
}
|
|
28
|
-
}
|
|
29
|
-
}
|
|
30
|
-
return mergeDeep(target, ...sources);
|
|
31
|
-
}
|