@needle-tools/materialx 1.0.0-next.2fb4497 → 1.0.0-next.4880afa

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.
Files changed (2) hide show
  1. package/package.json +3 -3
  2. package/src/materialx.ts +40 -28
package/package.json CHANGED
@@ -1,12 +1,12 @@
1
1
  {
2
2
  "name": "@needle-tools/materialx",
3
- "version": "1.0.0-next.2fb4497",
3
+ "version": "1.0.0-next.4880afa",
4
4
  "type": "module",
5
5
  "main": "index.ts",
6
6
  "exports": {
7
7
  ".": {
8
- "import": "./index.ts",
9
- "require": "./index.js"
8
+ "import": "./src/index.ts",
9
+ "require": "./src/index.js"
10
10
  },
11
11
  "./package.json": "./package.json"
12
12
  },
package/src/materialx.ts CHANGED
@@ -5,6 +5,44 @@ import { renderPMREMToEquirect } from "./textureHelper.js";
5
5
  import { Light, MeshBasicMaterial, Object3D, PMREMGenerator } from "three";
6
6
  import { registerLights } from "./helper.js";
7
7
 
8
+ const coreWasmUrl = new URL('../bin/JsMaterialXCore.wasm', import.meta.url).href;
9
+ const shaderDataUrl = new URL('../bin/JsMaterialXGenShader.data', import.meta.url).href;
10
+ const shaderWasmUrl = new URL('../bin/JsMaterialXGenShader.wasm', import.meta.url).href;
11
+
12
+ // const moduleUrls = import.meta.glob('../bin/JsMaterialXCore.wasm', {
13
+ // query: '?url',
14
+ // import: 'default',
15
+ // })
16
+ // import jsMaterialCoreUrl from "../bin/JsMaterialXCore.wasm?url";
17
+
18
+ // Configure MaterialX with the correct path for its data files
19
+ const materialXConfig = {
20
+ locateFile: (path: string, scriptDirectory: string) => {
21
+ if (debug) console.debug("MaterialX locateFile called:", { path, scriptDirectory });
22
+
23
+ // Return the correct path for MaterialX data files
24
+ if (path.endsWith('.data') || path.endsWith('.wasm')) {
25
+ if(path.includes("JsMaterialXCore.wasm")) {
26
+ return coreWasmUrl; // Use the URL for the core WASM file
27
+ }
28
+ else if (path.includes("JsMaterialXGenShader.data")) {
29
+ return shaderDataUrl; // Use the URL for the shader data file
30
+ }
31
+ else if (path.includes("JsMaterialXGenShader.wasm")) {
32
+ return shaderWasmUrl; // Use the URL for the shader WASM file
33
+ }
34
+ // For Vite dev server, we need to use the correct module path
35
+ const url = new URL(`../bin/${path}?url`, import.meta.url);
36
+ const correctPath = url.href;
37
+ if (debug) console.debug("MaterialX locateFile resolved:", correctPath, { url, meta_url: import.meta.url });
38
+ return correctPath;
39
+ }
40
+ return scriptDirectory + path;
41
+ },
42
+ // Add buffer allocation to handle the data file properly
43
+ wasmBinary: null,
44
+ wasmMemory: null
45
+ };
8
46
 
9
47
  // Global MaterialX module instance - initialized lazily
10
48
  export const state = new class {
@@ -22,6 +60,7 @@ export const state = new class {
22
60
  }
23
61
  }
24
62
 
63
+
25
64
  // Initialize MaterialX WASM module lazily
26
65
  export async function initializeMaterialX(): Promise<void> {
27
66
  if (state.materialXInitPromise) {
@@ -31,34 +70,7 @@ export async function initializeMaterialX(): Promise<void> {
31
70
  if (state.materialXModule) return; // Already initialized
32
71
  if (debug) console.log("Initializing MaterialX WASM module...");
33
72
  try {
34
-
35
- const urls = await Promise.all([
36
- /** @ts-ignore */
37
- import(`../bin/JsMaterialXCore.wasm?url`),
38
- /** @ts-ignore */
39
- import(`../bin/JsMaterialXGenShader.wasm?url`),
40
- /** @ts-ignore */
41
- import(`../bin/JsMaterialXGenShader.data?url`),
42
- ]);
43
- const [JsMaterialXCore, JsMaterialXGenShader, JsMaterialXGenShader_data] = urls;
44
-
45
- const module = await MaterialX({
46
- locateFile: (path: string, scriptDirectory: string) => {
47
- if (debug) console.debug("MaterialX locateFile called:", { path, scriptDirectory });
48
-
49
- if (path.includes("JsMaterialXCore.wasm")) {
50
- return JsMaterialXCore; // Use the URL for the core WASM file
51
- }
52
- else if (path.includes("JsMaterialXGenShader.wasm")) {
53
- return JsMaterialXGenShader; // Use the URL for the shader WASM file
54
- }
55
- else if (path.includes("JsMaterialXGenShader.data")) {
56
- return JsMaterialXGenShader_data; // Use the URL for the shader data file
57
- }
58
-
59
- return scriptDirectory + path;
60
- },
61
- });
73
+ const module = await MaterialX(materialXConfig);
62
74
  if (debug) console.log("MaterialXLoader module loaded", module);
63
75
  state.materialXModule = module;
64
76