@needle-tools/materialx 1.0.0-alpha → 1.0.0-next.1935162

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/src/materialx.ts CHANGED
@@ -1,220 +1,221 @@
1
- import { Context, delay, isDevEnvironment, ObjectUtils } from "@needle-tools/engine";
2
- import MaterialX from "../bin/JsMaterialXGenShader.js";
3
- import { debug } from "./utils.js";
4
- import { renderPMREMToEquirect } from "./textureHelper.js";
5
- import { Light, MeshBasicMaterial, Object3D, PMREMGenerator } from "three";
6
- import { registerLights } from "./helper.js";
7
-
8
- // Configure MaterialX with the correct path for its data files
9
- const materialXConfig = {
10
- locateFile: (path: string, scriptDirectory: string) => {
11
- if (debug) console.debug("MaterialX locateFile called:", { path, scriptDirectory });
12
-
13
- // Return the correct path for MaterialX data files
14
- if (path.endsWith('.data') || path.endsWith('.wasm')) {
15
- // For Vite dev server, we need to use the correct module path
16
- const correctPath = new URL(`../bin/${path}`, import.meta.url).href;
17
- if (debug) console.log("Resolved path:", correctPath);
18
- return correctPath;
19
- }
20
- return scriptDirectory + path;
21
- },
22
- // Add buffer allocation to handle the data file properly
23
- wasmBinary: null,
24
- wasmMemory: null
25
- };
26
-
27
- // Global MaterialX module instance - initialized lazily
28
- export const state = new class {
29
- materialXModule: any = null;
30
- materialXGenerator: any = null;
31
- materialXGenContext: any = null;
32
- materialXStdLib: any = null;
33
- materialXInitPromise: Promise<void> | null = null;
34
-
35
- // Global MaterialX environment instance
36
- private _materialXEnvironment: MaterialXEnvironment | null = null;
37
- get materialXEnvironment() {
38
- this._materialXEnvironment ??= new MaterialXEnvironment();
39
- return this._materialXEnvironment;
40
- }
41
- }
42
-
43
-
44
- // Initialize MaterialX WASM module lazily
45
- export async function initializeMaterialX(): Promise<void> {
46
- if (state.materialXInitPromise) {
47
- return state.materialXInitPromise;
48
- }
49
- return state.materialXInitPromise = (async () => {
50
- if (state.materialXModule) return; // Already initialized
51
- if (debug) console.log("Initializing MaterialX WASM module...");
52
- try {
53
- const module = await MaterialX(materialXConfig);
54
- if (debug) console.log("MaterialXLoader module loaded", module);
55
- state.materialXModule = module;
56
-
57
- // Initialize shader generator and context
58
- state.materialXGenerator = module.EsslShaderGenerator.create();
59
- state.materialXGenContext = new module.GenContext(state.materialXGenerator);
60
-
61
- // Load standard libraries
62
- const tempDoc = module.createDocument();
63
- state.materialXStdLib = module.loadStandardLibraries(state.materialXGenContext);
64
- tempDoc.setDataLibrary(state.materialXStdLib);
65
-
66
- // Initialize basic lighting with default light rig
67
- const defaultLightRigXml = `<?xml version="1.0"?>
68
- <materialx version="1.39">
69
- <!-- Default directional light -->
70
- <directional_light name="default_light" type="lightshader">
71
- <input name="direction" type="vector3" value="0.0, -1.0, -0.5" />
72
- <input name="color" type="color3" value="1.0, 1.0, 1.0" />
73
- <input name="intensity" type="float" value="1.0" />
74
- </directional_light>
75
- </materialx>`;
76
-
77
- // This prewarms the shader generation context to have all light types
78
- await registerLights(state.materialXModule, [], state.materialXGenContext);
79
-
80
- if (debug) console.log("MaterialX generator initialized successfully");
81
- } catch (error) {
82
- console.error("Failed to load MaterialX module:", error);
83
- throw error;
84
- }
85
- })();
86
- }
87
-
88
- // MaterialX Environment Manager - handles lighting and environment setup
89
- export class MaterialXEnvironment {
90
- private lights: any[] = [];
91
- private lightData: any = null;
92
- private radianceTexture: any = null;
93
- private irradianceTexture: any = null;
94
- private context: Context | null = null;
95
- private initialized: boolean = false;
96
-
97
- constructor() {
98
- if (debug) console.log("MaterialX Environment created");
99
- }
100
-
101
- setContext(context: Context) {
102
- this.context = context;
103
- }
104
-
105
- /*
106
- // Initialize MaterialX lighting system based on the reference implementation
107
- async initializeLighting(lightRigXml: string, renderer?: any, radianceTexture?: any, irradianceTexture?: any): Promise<void> {
108
- if (!materialXModule || !materialXGenContext) {
109
- console.warn("MaterialX module not initialized, skipping lighting setup");
110
- return;
111
- }
112
-
113
- registerLights(materialXModule, this.lights, materialXGenContext);
114
- }
115
- */
116
-
117
- // Initialize with Needle Engine context
118
- async initializeFromContext(): Promise<void> {
119
- if (!this.context) {
120
- console.warn("No Needle context available for MaterialX environment initialization");
121
- return;
122
- }
123
-
124
- // Prevent multiple initializations
125
- if (this.initialized) {
126
- if (debug) console.log("MaterialX environment already initialized, skipping");
127
- return;
128
- }
129
-
130
- // Clean up previous textures if they exist
131
- if (this.radianceTexture) {
132
- if (debug) console.log("Disposing previous radiance texture");
133
- this.radianceTexture.dispose();
134
- this.radianceTexture = null;
135
- }
136
- if (this.irradianceTexture) {
137
- if (debug) console.log("Disposing previous irradiance texture");
138
- this.irradianceTexture.dispose();
139
- this.irradianceTexture = null;
140
- }
141
-
142
- // Get renderer from context
143
- const renderer = this.context.renderer;
144
-
145
- // TODO remove this delay; we should wait for the scene lighting to be ready
146
- // and then update the uniforms
147
- let envMap = this.context.scene.environment;
148
- while (!envMap) {
149
- await delay(200);
150
- envMap = this.context.scene.environment;
151
- }
152
- var pmrem = new PMREMGenerator(renderer);
153
- const target = pmrem.fromEquirectangular(envMap);
154
-
155
- const radianceRenderTarget = renderPMREMToEquirect(renderer, target.texture, 0.0, 1024, 512, target.height);
156
- const irradianceRenderTarget = renderPMREMToEquirect(renderer, target.texture, 1.0, 32, 16, target.height);
157
-
158
- this.radianceTexture = radianceRenderTarget.texture;
159
- this.irradianceTexture = irradianceRenderTarget.texture;
160
-
161
- // Clean up PMREM generator and its render target
162
- target.dispose();
163
- pmrem.dispose();
164
-
165
- if (debug) {
166
- console.log({ radiance: this.radianceTexture, irradiance: this.irradianceTexture });
167
- // Show both of them on cubes in the scene
168
- const unlitMat = new MeshBasicMaterial();
169
- const radianceMat = unlitMat.clone();
170
- radianceMat.map = this.radianceTexture;
171
- const radianceCube = ObjectUtils.createPrimitive("Cube", { material: radianceMat });
172
- const irradianceMat = unlitMat.clone();
173
- irradianceMat.map = this.irradianceTexture;
174
- const irradianceCube = ObjectUtils.createPrimitive("Cube", { material: irradianceMat });
175
- this.context.scene.add(radianceCube);
176
- this.context.scene.add(irradianceCube);
177
- radianceCube.position.set(2, 0, 0);
178
- radianceCube.scale.y = 0.00001;
179
- irradianceCube.position.set(-2, 0, 0);
180
- irradianceCube.scale.y = 0.00001;
181
- // await this.initializeLighting(defaultLightRigXml, renderer);
182
- console.log("MaterialX environment initialized from Needle context", this, this.context.scene);
183
- }
184
-
185
- // Find lights in scene
186
- let lights = new Array<Light>();
187
- this.context.scene.traverse((object: Object3D) => {
188
- if ((object as Light).isLight) lights.push(object as Light);
189
- });
190
-
191
- this.lightData = await registerLights(state.materialXModule, lights, state.materialXGenContext);
192
-
193
- // Mark as initialized
194
- this.initialized = true;
195
- }
196
-
197
- // getLights() { return this.lights; }
198
- getLightData() { return this.lightData; }
199
- getRadianceTexture() { return this.radianceTexture; }
200
- getIrradianceTexture() { return this.irradianceTexture; }
201
-
202
- setRadianceTexture(texture: any) { this.radianceTexture = texture; }
203
- setIrradianceTexture(texture: any) { this.irradianceTexture = texture; }
204
-
205
- // Reset the environment to allow re-initialization
206
- reset() {
207
- if (debug) console.log("Resetting MaterialX environment");
208
- if (this.radianceTexture) {
209
- this.radianceTexture.dispose();
210
- this.radianceTexture = null;
211
- }
212
- if (this.irradianceTexture) {
213
- this.irradianceTexture.dispose();
214
- this.irradianceTexture = null;
215
- }
216
- this.initialized = false;
217
- // this.lights = [];
218
- this.lightData = null;
219
- }
220
- }
1
+ import { Context, delay, isDevEnvironment, ObjectUtils } from "@needle-tools/engine";
2
+ import MaterialX from "../bin/JsMaterialXGenShader.js";
3
+ import { debug } from "./utils.js";
4
+ import { renderPMREMToEquirect } from "./textureHelper.js";
5
+ import { Light, MeshBasicMaterial, Object3D, PMREMGenerator } from "three";
6
+ import { registerLights } from "./helper.js";
7
+
8
+ // Configure MaterialX with the correct path for its data files
9
+ const materialXConfig = {
10
+ locateFile: (path: string, scriptDirectory: string) => {
11
+ if (debug) console.debug("MaterialX locateFile called:", { path, scriptDirectory });
12
+
13
+ // Return the correct path for MaterialX data files
14
+ if (path.endsWith('.data') || path.endsWith('.wasm')) {
15
+ // For Vite dev server, we need to use the correct module path
16
+ const url = new URL(`../bin/${path}?url`, import.meta.url);
17
+ const correctPath = url.href;
18
+ if (debug) console.debug("MaterialX locateFile resolved:", correctPath, { url, meta_url: import.meta.url });
19
+ return correctPath;
20
+ }
21
+ return scriptDirectory + path;
22
+ },
23
+ // Add buffer allocation to handle the data file properly
24
+ wasmBinary: null,
25
+ wasmMemory: null
26
+ };
27
+
28
+ // Global MaterialX module instance - initialized lazily
29
+ export const state = new class {
30
+ materialXModule: any = null;
31
+ materialXGenerator: any = null;
32
+ materialXGenContext: any = null;
33
+ materialXStdLib: any = null;
34
+ materialXInitPromise: Promise<void> | null = null;
35
+
36
+ // Global MaterialX environment instance
37
+ private _materialXEnvironment: MaterialXEnvironment | null = null;
38
+ get materialXEnvironment() {
39
+ this._materialXEnvironment ??= new MaterialXEnvironment();
40
+ return this._materialXEnvironment;
41
+ }
42
+ }
43
+
44
+
45
+ // Initialize MaterialX WASM module lazily
46
+ export async function initializeMaterialX(): Promise<void> {
47
+ if (state.materialXInitPromise) {
48
+ return state.materialXInitPromise;
49
+ }
50
+ return state.materialXInitPromise = (async () => {
51
+ if (state.materialXModule) return; // Already initialized
52
+ if (debug) console.log("Initializing MaterialX WASM module...");
53
+ try {
54
+ const module = await MaterialX(materialXConfig);
55
+ if (debug) console.log("MaterialXLoader module loaded", module);
56
+ state.materialXModule = module;
57
+
58
+ // Initialize shader generator and context
59
+ state.materialXGenerator = module.EsslShaderGenerator.create();
60
+ state.materialXGenContext = new module.GenContext(state.materialXGenerator);
61
+
62
+ // Load standard libraries
63
+ const tempDoc = module.createDocument();
64
+ state.materialXStdLib = module.loadStandardLibraries(state.materialXGenContext);
65
+ tempDoc.setDataLibrary(state.materialXStdLib);
66
+
67
+ // Initialize basic lighting with default light rig
68
+ const defaultLightRigXml = `<?xml version="1.0"?>
69
+ <materialx version="1.39">
70
+ <!-- Default directional light -->
71
+ <directional_light name="default_light" type="lightshader">
72
+ <input name="direction" type="vector3" value="0.0, -1.0, -0.5" />
73
+ <input name="color" type="color3" value="1.0, 1.0, 1.0" />
74
+ <input name="intensity" type="float" value="1.0" />
75
+ </directional_light>
76
+ </materialx>`;
77
+
78
+ // This prewarms the shader generation context to have all light types
79
+ await registerLights(state.materialXModule, [], state.materialXGenContext);
80
+
81
+ if (debug) console.log("MaterialX generator initialized successfully");
82
+ } catch (error) {
83
+ console.error("Failed to load MaterialX module:", error);
84
+ throw error;
85
+ }
86
+ })();
87
+ }
88
+
89
+ // MaterialX Environment Manager - handles lighting and environment setup
90
+ export class MaterialXEnvironment {
91
+ private lights: any[] = [];
92
+ private lightData: any = null;
93
+ private radianceTexture: any = null;
94
+ private irradianceTexture: any = null;
95
+ private context: Context | null = null;
96
+ private initialized: boolean = false;
97
+
98
+ constructor() {
99
+ if (debug) console.log("MaterialX Environment created");
100
+ }
101
+
102
+ setContext(context: Context) {
103
+ this.context = context;
104
+ }
105
+
106
+ /*
107
+ // Initialize MaterialX lighting system based on the reference implementation
108
+ async initializeLighting(lightRigXml: string, renderer?: any, radianceTexture?: any, irradianceTexture?: any): Promise<void> {
109
+ if (!materialXModule || !materialXGenContext) {
110
+ console.warn("MaterialX module not initialized, skipping lighting setup");
111
+ return;
112
+ }
113
+
114
+ registerLights(materialXModule, this.lights, materialXGenContext);
115
+ }
116
+ */
117
+
118
+ // Initialize with Needle Engine context
119
+ async initializeFromContext(): Promise<void> {
120
+ if (!this.context) {
121
+ console.warn("No Needle context available for MaterialX environment initialization");
122
+ return;
123
+ }
124
+
125
+ // Prevent multiple initializations
126
+ if (this.initialized) {
127
+ if (debug) console.log("MaterialX environment already initialized, skipping");
128
+ return;
129
+ }
130
+
131
+ // Clean up previous textures if they exist
132
+ if (this.radianceTexture) {
133
+ if (debug) console.log("Disposing previous radiance texture");
134
+ this.radianceTexture.dispose();
135
+ this.radianceTexture = null;
136
+ }
137
+ if (this.irradianceTexture) {
138
+ if (debug) console.log("Disposing previous irradiance texture");
139
+ this.irradianceTexture.dispose();
140
+ this.irradianceTexture = null;
141
+ }
142
+
143
+ // Get renderer from context
144
+ const renderer = this.context.renderer;
145
+
146
+ // TODO remove this delay; we should wait for the scene lighting to be ready
147
+ // and then update the uniforms
148
+ let envMap = this.context.scene.environment;
149
+ while (!envMap) {
150
+ await delay(200);
151
+ envMap = this.context.scene.environment;
152
+ }
153
+ var pmrem = new PMREMGenerator(renderer);
154
+ const target = pmrem.fromEquirectangular(envMap);
155
+
156
+ const radianceRenderTarget = renderPMREMToEquirect(renderer, target.texture, 0.0, 1024, 512, target.height);
157
+ const irradianceRenderTarget = renderPMREMToEquirect(renderer, target.texture, 1.0, 32, 16, target.height);
158
+
159
+ this.radianceTexture = radianceRenderTarget.texture;
160
+ this.irradianceTexture = irradianceRenderTarget.texture;
161
+
162
+ // Clean up PMREM generator and its render target
163
+ target.dispose();
164
+ pmrem.dispose();
165
+
166
+ if (debug) {
167
+ console.log({ radiance: this.radianceTexture, irradiance: this.irradianceTexture });
168
+ // Show both of them on cubes in the scene
169
+ const unlitMat = new MeshBasicMaterial();
170
+ const radianceMat = unlitMat.clone();
171
+ radianceMat.map = this.radianceTexture;
172
+ const radianceCube = ObjectUtils.createPrimitive("Cube", { material: radianceMat });
173
+ const irradianceMat = unlitMat.clone();
174
+ irradianceMat.map = this.irradianceTexture;
175
+ const irradianceCube = ObjectUtils.createPrimitive("Cube", { material: irradianceMat });
176
+ this.context.scene.add(radianceCube);
177
+ this.context.scene.add(irradianceCube);
178
+ radianceCube.position.set(2, 0, 0);
179
+ radianceCube.scale.y = 0.00001;
180
+ irradianceCube.position.set(-2, 0, 0);
181
+ irradianceCube.scale.y = 0.00001;
182
+ // await this.initializeLighting(defaultLightRigXml, renderer);
183
+ console.log("MaterialX environment initialized from Needle context", this, this.context.scene);
184
+ }
185
+
186
+ // Find lights in scene
187
+ let lights = new Array<Light>();
188
+ this.context.scene.traverse((object: Object3D) => {
189
+ if ((object as Light).isLight) lights.push(object as Light);
190
+ });
191
+
192
+ this.lightData = await registerLights(state.materialXModule, lights, state.materialXGenContext);
193
+
194
+ // Mark as initialized
195
+ this.initialized = true;
196
+ }
197
+
198
+ // getLights() { return this.lights; }
199
+ getLightData() { return this.lightData; }
200
+ getRadianceTexture() { return this.radianceTexture; }
201
+ getIrradianceTexture() { return this.irradianceTexture; }
202
+
203
+ setRadianceTexture(texture: any) { this.radianceTexture = texture; }
204
+ setIrradianceTexture(texture: any) { this.irradianceTexture = texture; }
205
+
206
+ // Reset the environment to allow re-initialization
207
+ reset() {
208
+ if (debug) console.log("Resetting MaterialX environment");
209
+ if (this.radianceTexture) {
210
+ this.radianceTexture.dispose();
211
+ this.radianceTexture = null;
212
+ }
213
+ if (this.irradianceTexture) {
214
+ this.irradianceTexture.dispose();
215
+ this.irradianceTexture = null;
216
+ }
217
+ this.initialized = false;
218
+ // this.lights = [];
219
+ this.lightData = null;
220
+ }
221
+ }