@needle-tools/materialx 1.0.0-next.4880afa → 1.0.0-next.8019dc2

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 +1,27 @@
1
+ # Needle MaterialX
2
+
3
+ ## Usage
4
+
5
+ ### Vite
6
+
7
+ Make sure to disable `optimizeDeps` in your vite.config.js
8
+ ```js
9
+ // config:
10
+ {
11
+ optimizeDeps: {
12
+ exclude: ['@needle-tools/materialx']
13
+ },
14
+ }
15
+ ```
16
+
17
+
18
+ <br />
19
+
20
+ # Contact ✒️
21
+ <b>[🌵 Needle](https://needle.tools)</b> •
22
+ [Github](https://github.com/needle-tools) •
23
+ [Twitter](https://twitter.com/NeedleTools) •
24
+ [Discord](https://discord.needle.tools) •
25
+ [Forum](https://forum.needle.tools) •
26
+ [Youtube](https://www.youtube.com/@needle-tools)
1
27
 
package/package.json CHANGED
@@ -1,14 +1,18 @@
1
1
  {
2
2
  "name": "@needle-tools/materialx",
3
- "version": "1.0.0-next.4880afa",
3
+ "version": "1.0.0-next.8019dc2",
4
4
  "type": "module",
5
5
  "main": "index.ts",
6
6
  "exports": {
7
7
  ".": {
8
- "import": "./src/index.ts",
9
- "require": "./src/index.js"
8
+ "import": "./index.ts",
9
+ "require": "./index.js"
10
10
  },
11
- "./package.json": "./package.json"
11
+ "./package.json": "./package.json",
12
+ "./vite": {
13
+ "import": "./src/vite/index.js",
14
+ "require": "./src/vite/index.js"
15
+ }
12
16
  },
13
17
  "peerDependencies": {
14
18
  "@needle-tools/engine": "4.x",
package/src/index.ts CHANGED
@@ -2,4 +2,5 @@
2
2
 
3
3
  const getMaterialXEnvironment = () => state.materialXEnvironment;
4
4
 
5
- export { initializeMaterialX, getMaterialXEnvironment };
5
+ export { initializeMaterialX, getMaterialXEnvironment };
6
+ export { needleMaterialX } from "./vite/index.js";
package/src/materialx.ts CHANGED
@@ -5,44 +5,6 @@ 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
- };
46
8
 
47
9
  // Global MaterialX module instance - initialized lazily
48
10
  export const state = new class {
@@ -60,7 +22,6 @@ export const state = new class {
60
22
  }
61
23
  }
62
24
 
63
-
64
25
  // Initialize MaterialX WASM module lazily
65
26
  export async function initializeMaterialX(): Promise<void> {
66
27
  if (state.materialXInitPromise) {
@@ -70,7 +31,34 @@ export async function initializeMaterialX(): Promise<void> {
70
31
  if (state.materialXModule) return; // Already initialized
71
32
  if (debug) console.log("Initializing MaterialX WASM module...");
72
33
  try {
73
- const module = await MaterialX(materialXConfig);
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?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
+ });
74
62
  if (debug) console.log("MaterialXLoader module loaded", module);
75
63
  state.materialXModule = module;
76
64
 
@@ -84,15 +72,15 @@ export async function initializeMaterialX(): Promise<void> {
84
72
  tempDoc.setDataLibrary(state.materialXStdLib);
85
73
 
86
74
  // Initialize basic lighting with default light rig
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>`;
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>`;
96
84
 
97
85
  // This prewarms the shader generation context to have all light types
98
86
  await registerLights(state.materialXModule, [], state.materialXGenContext);
@@ -0,0 +1,18 @@
1
+ /**
2
+ * Vite plugin to exclude the @needle-tools/materialx package from dependency optimization.
3
+ * @returns {import('vite').Plugin} The Vite plugin configuration.
4
+ */
5
+ export function needleMaterialX() {
6
+ return {
7
+ name: "needle-materialx",
8
+ config: (config, _env) => {
9
+ config.optimizeDeps ??= {};
10
+ config.optimizeDeps.exclude ??= [];
11
+ // to make .data files work
12
+ if (!config.optimizeDeps.exclude.includes("@needle-tools/materialx")) {
13
+ config.optimizeDeps.exclude.push("@needle-tools/materialx");
14
+ }
15
+ return config;
16
+ }
17
+ }
18
+ }