@needle-tools/gltf-build-pipeline 2.15.0-next.d34f97f → 2.15.0-next.fd1fff4

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.
@@ -191,8 +191,6 @@ export function getHash(property, level = 0) {
191
191
  if (property instanceof ExtensibleProperty) {
192
192
  const extensions = property.listExtensions();
193
193
  const extras = property.getExtras();
194
- // const slots = listTextureSlots(doc, texture);
195
- // const channels = getTextureChannelMask(doc, texture);
196
194
  hash += hashObject({
197
195
  extensions,
198
196
  extras,
@@ -269,38 +267,44 @@ function hashObject(obj, depth = 0) {
269
267
  return hashString(String(obj));
270
268
  }
271
269
  try {
272
- const str = JSON.stringify(obj);
270
+ const seen = new WeakSet();
271
+ const str = JSON.stringify(obj, (_key, value) => {
272
+ if (typeof value === 'object' && value !== null) {
273
+ if (seen.has(value))
274
+ return undefined;
275
+ seen.add(value);
276
+ }
277
+ if (typeof value === 'function')
278
+ return undefined;
279
+ return value;
280
+ });
273
281
  return hashString(str);
274
282
  }
275
283
  catch (err) {
276
284
  console.warn("WARN: [Cache] error during hashing of object:\n" + err);
277
285
  if (debugLogs)
278
286
  debugger;
279
- // Fallback to a simple hash of the keys and values
280
- // TODO: maybe we want to exclude this object now from the cache if it fails to serialize?
281
- let fallbackHashj = 0;
282
- if (typeof obj === 'object') {
283
- if (obj === null)
284
- return 0;
287
+ let fallbackHash = 0;
288
+ if (typeof obj === 'object' && obj !== null) {
285
289
  for (const key in obj) {
286
290
  if (Object.prototype.hasOwnProperty.call(obj, key)) {
287
291
  const value = obj[key];
288
- fallbackHashj += hashString(key);
289
- if (typeof value === 'object') {
292
+ fallbackHash += hashString(key);
293
+ if (typeof value === 'object' && value !== null) {
290
294
  if (depth > 10) {
291
- fallbackHashj += hashString("[object]");
295
+ fallbackHash += hashString("[object]");
292
296
  }
293
297
  else {
294
- fallbackHashj += hashObject(value, depth + 1);
298
+ fallbackHash += hashObject(value, depth + 1);
295
299
  }
296
300
  }
297
301
  else {
298
- fallbackHashj += hashString(String(value));
302
+ fallbackHash += hashString(String(value));
299
303
  }
300
304
  }
301
305
  }
302
306
  }
303
- return fallbackHashj;
307
+ return fallbackHash;
304
308
  }
305
309
  }
306
310
  function hashBuffer(buffer, offset, length) {
@@ -1,4 +1,4 @@
1
- import { Extension, Texture, WriterContext } from '@gltf-transform/core';
1
+ import { Extension, PropertyType, ReaderContext, Texture, WriterContext } from '@gltf-transform/core';
2
2
  /**
3
3
  * NEEDLE_pmrem extension for gltf-transform.
4
4
  * Marks textures that have been pre-processed with PMREM (EXR → KTX2 HDR).
@@ -13,7 +13,9 @@ export declare class NEEDLE_pmrem extends Extension {
13
13
  /** Mark a texture as PMREM-converted */
14
14
  addTexture(texture: Texture): void;
15
15
  /** @hidden */
16
- read(): this;
16
+ preread(context: ReaderContext, _propertyType: PropertyType): this;
17
+ /** @hidden */
18
+ read(context: ReaderContext): this;
17
19
  /** @hidden */
18
20
  write(context: WriterContext): this;
19
21
  }
@@ -16,7 +16,44 @@ export class NEEDLE_pmrem extends Extension {
16
16
  this.pmremTextures.add(texture);
17
17
  }
18
18
  /** @hidden */
19
- read() {
19
+ preread(context, _propertyType) {
20
+ const json = context.jsonDoc.json;
21
+ // For each texture with NEEDLE_pmrem, ensure `source` is set so the core reader
22
+ // links the texture to its image (we removed KHR_texture_basisu.source at write time).
23
+ for (const textureDef of json.textures ?? []) {
24
+ const pmrem = textureDef.extensions?.[EXTENSION_NAME];
25
+ if (pmrem?.source !== undefined && textureDef.source === undefined) {
26
+ textureDef.source = pmrem.source;
27
+ }
28
+ }
29
+ // Also ensure the image has the correct mime type
30
+ for (const textureDef of json.textures ?? []) {
31
+ const pmrem = textureDef.extensions?.[EXTENSION_NAME];
32
+ if (pmrem?.source !== undefined) {
33
+ const imageDef = json.images?.[pmrem.source];
34
+ if (imageDef && !imageDef.mimeType) {
35
+ imageDef.mimeType = 'image/ktx2';
36
+ }
37
+ }
38
+ }
39
+ return this;
40
+ }
41
+ /** @hidden */
42
+ read(context) {
43
+ const json = context.jsonDoc.json;
44
+ const textureDefs = json.textures ?? [];
45
+ const textures = this.document.getRoot().listTextures();
46
+ for (let i = 0; i < textureDefs.length; i++) {
47
+ const textureDef = textureDefs[i];
48
+ const pmrem = textureDef.extensions?.[EXTENSION_NAME];
49
+ if (pmrem?.source === undefined)
50
+ continue;
51
+ const texture = textures[i];
52
+ if (!texture)
53
+ continue;
54
+ texture.setMimeType('image/ktx2');
55
+ this.pmremTextures.add(texture);
56
+ }
20
57
  return this;
21
58
  }
22
59
  /** @hidden */
@@ -39,6 +76,20 @@ export class NEEDLE_pmrem extends Extension {
39
76
  }
40
77
  }
41
78
  }
79
+ // If no textures still use KHR_texture_basisu, remove it from extensionsUsed/Required
80
+ // to prevent KHRTextureBasisu.preread() from crashing when re-reading the GLB
81
+ const anyBasisu = (jsonDoc.json.textures ?? []).some((t) => t.extensions?.['KHR_texture_basisu']);
82
+ if (!anyBasisu) {
83
+ const remove = (arr) => {
84
+ if (!arr)
85
+ return;
86
+ const idx = arr.indexOf('KHR_texture_basisu');
87
+ if (idx >= 0)
88
+ arr.splice(idx, 1);
89
+ };
90
+ remove(jsonDoc.json.extensionsUsed);
91
+ remove(jsonDoc.json.extensionsRequired);
92
+ }
42
93
  return this;
43
94
  }
44
95
  }
@@ -10,10 +10,14 @@ export * from "./NEEDLE_lightmaps/index.js";
10
10
  import { ALL_EXTENSIONS } from '@gltf-transform/extensions';
11
11
  import { ALL_EXTENSIONS as NEEDLE_EXTENSIONS } from "./NEEDLE_opaque/index.js";
12
12
  import { NEEDLE_progressive } from './NEEDLE_progressive/index.js';
13
+ import { NEEDLE_pmrem } from './NEEDLE_pmrem/index.js';
14
+ import { NEEDLE_lightmaps_ext } from './NEEDLE_lightmaps/index.js';
13
15
  export function registerExtensions(node) {
14
16
  node.registerExtensions(ALL_EXTENSIONS);
15
17
  node.registerExtensions(NEEDLE_EXTENSIONS);
16
18
  node.registerExtensions([
17
- NEEDLE_progressive
19
+ NEEDLE_progressive,
20
+ NEEDLE_pmrem,
21
+ NEEDLE_lightmaps_ext,
18
22
  ]);
19
23
  }
@@ -76,10 +76,9 @@ export class NeedleToktxContext {
76
76
  return false;
77
77
  }
78
78
  this.basisuExtension = document.createExtension(KHRTextureBasisu).setRequired(true);
79
- const tmpDir = `${tmp.tmpdir}/gltf-transform`;
79
+ const tmpDir = `${tmp.tmpdir}/gltf-transform/${uuid()}`;
80
80
  this.cacheDirectory = tmpDir;
81
- if (!(existsSync(tmpDir)))
82
- mkdirSync(tmpDir);
81
+ mkdirSync(tmpDir, { recursive: true });
83
82
  this.tmpPathBase = tmpDir + "/" + uuid();
84
83
  this.numTextures = document.getRoot().listTextures().length;
85
84
  }
@@ -1 +1 @@
1
- export declare const version = "2.15.0-next.d34f97f";
1
+ export declare const version = "2.15.0-next.fd1fff4";
@@ -1 +1 @@
1
- export const version = "2.15.0-next.d34f97f";
1
+ export const version = "2.15.0-next.fd1fff4";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@needle-tools/gltf-build-pipeline",
3
- "version": "2.15.0-next.d34f97f",
3
+ "version": "2.15.0-next.fd1fff4",
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",