@needle-tools/materialx 1.0.0-next.834eeec → 1.0.0-next.a660bbe

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 CHANGED
@@ -1,20 +1 @@
1
- # Needle MaterialX
2
-
3
- ## How to use
4
-
5
- To use with Needle Engine simply import the module
6
-
7
- ```ts
8
- import "@needle-tools/materialx"
9
- ```
10
-
11
- <br />
12
-
13
- # Contact ✒️
14
- <b>[🌵 Needle](https://needle.tools)</b> •
15
- [Github](https://github.com/needle-tools) •
16
- [Twitter](https://twitter.com/NeedleTools) •
17
- [Discord](https://discord.needle.tools) •
18
- [Forum](https://forum.needle.tools) •
19
- [Youtube](https://www.youtube.com/@needle-tools)
20
1
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@needle-tools/materialx",
3
- "version": "1.0.0-next.834eeec",
3
+ "version": "1.0.0-next.a660bbe",
4
4
  "type": "module",
5
5
  "main": "index.ts",
6
6
  "exports": {
package/src/index.ts CHANGED
@@ -2,5 +2,4 @@
2
2
 
3
3
  const getMaterialXEnvironment = () => state.materialXEnvironment;
4
4
 
5
- export { initializeMaterialX, getMaterialXEnvironment };
6
- export { needleMaterialX } from "./vite/index.js";
5
+ export { initializeMaterialX, getMaterialXEnvironment };
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: Array<string> = await Promise.all([
36
- /** @ts-ignore */
37
- import(`../bin/JsMaterialXCore.wasm?url`).then(m => m.default || m),
38
- /** @ts-ignore */
39
- import(`../bin/JsMaterialXGenShader.wasm?url`).then(m => m.default || m),
40
- /** @ts-ignore */
41
- import(`../bin/JsMaterialXGenShader.data.txt?url`).then(m => m.default || m),
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
 
@@ -72,15 +84,15 @@ export async function initializeMaterialX(): Promise<void> {
72
84
  tempDoc.setDataLibrary(state.materialXStdLib);
73
85
 
74
86
  // Initialize basic lighting with default light rig
75
- // const defaultLightRigXml = `<?xml version="1.0"?>
76
- // <materialx version="1.39">
77
- // <!-- Default directional light -->
78
- // <directional_light name="default_light" type="lightshader">
79
- // <input name="direction" type="vector3" value="0.0, -1.0, -0.5" />
80
- // <input name="color" type="color3" value="1.0, 1.0, 1.0" />
81
- // <input name="intensity" type="float" value="1.0" />
82
- // </directional_light>
83
- // </materialx>`;
87
+ const defaultLightRigXml = `<?xml version="1.0"?>
88
+ <materialx version="1.39">
89
+ <!-- Default directional light -->
90
+ <directional_light name="default_light" type="lightshader">
91
+ <input name="direction" type="vector3" value="0.0, -1.0, -0.5" />
92
+ <input name="color" type="color3" value="1.0, 1.0, 1.0" />
93
+ <input name="intensity" type="float" value="1.0" />
94
+ </directional_light>
95
+ </materialx>`;
84
96
 
85
97
  // This prewarms the shader generation context to have all light types
86
98
  await registerLights(state.materialXModule, [], state.materialXGenContext);