@needle-tools/materialx 1.0.1-next.b9467c8 → 1.0.1-next.b9638d9

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.
@@ -6,21 +6,32 @@ import type { GLTFLoader } from "three/examples/jsm/loaders/GLTFLoader.js";
6
6
  import type { GLTFExporter } from "three/examples/jsm/exporters/GLTFExporter.js";
7
7
  import { MaterialXLoader } from "./loader.three.js";
8
8
  import { debug } from "../utils.js";
9
- import { MaterialXEnvironment, state } from "../materialx.js";
10
- import { MaterialXMaterial } from "../materialx.material.js";
9
+ import { state } from "../materialx.js";
11
10
 
12
11
  //@dont-generate-component
13
12
  export class MaterialXUniformUpdate extends Component {
14
13
 
14
+ static updateMaterial(mat: Material | Material[], object: Object3D, camera: Camera) {
15
+ if (Array.isArray(mat)) {
16
+ mat.forEach(m => {
17
+ if (m.userData?.updateUniforms) {
18
+ m.userData.updateUniforms(object, camera);
19
+ }
20
+ });
21
+ } else if (mat.userData?.updateUniforms) {
22
+ mat.userData.updateUniforms(object, camera);
23
+ }
24
+ }
25
+
15
26
  onEnable(): void {
16
- this.context.addBeforeRenderListener(this.gameObject, this.onBeforeRenderThree);
27
+ this.context.addBeforeRenderListener(this.gameObject, this._onBeforeRender);
17
28
  }
18
29
 
19
30
  onDisable(): void {
20
- this.context.removeBeforeRenderListener(this.gameObject, this.onBeforeRenderThree);
31
+ this.context.removeBeforeRenderListener(this.gameObject, this._onBeforeRender);
21
32
  }
22
33
 
23
- onBeforeRenderThree = () => {
34
+ _onBeforeRender = () => {
24
35
  // Update uniforms or perform any pre-render logic here
25
36
  const gameObject = this.gameObject as any as Mesh;
26
37
  const material = gameObject?.material;
@@ -28,24 +39,22 @@ export class MaterialXUniformUpdate extends Component {
28
39
  const camera = this.context.mainCamera;
29
40
  if (!camera) return;
30
41
 
31
- const env = state.materialXEnvironment;
42
+ MaterialXUniformUpdate.updateMaterial(material, gameObject, camera);
32
43
 
33
- if (Array.isArray(material)) {
34
- for (const entry of material) {
35
- if (entry && entry instanceof MaterialXMaterial) {
36
- entry.updateUniforms(this.context, env, this.gameObject, camera);
37
- }
38
- }
39
- } else if (material instanceof MaterialXMaterial) {
40
- material.updateUniforms(this.context, env, this.gameObject, camera);
44
+ // If this is a Group, we need to update all direct children
45
+ if ((gameObject as any as Group).isGroup) {
46
+ gameObject.children.forEach((child: Object3D) => {
47
+ if (child instanceof Mesh && child.material)
48
+ MaterialXUniformUpdate.updateMaterial(child.material, child, camera);
49
+ });
41
50
  }
42
51
  }
43
52
  }
44
53
 
45
54
  export class MaterialXLoaderPlugin implements INeedleGLTFExtensionPlugin {
46
- readonly name = "MaterialXLoaderPlugin";
55
+ name = "MaterialXLoaderPlugin";
47
56
 
48
- private loader: MaterialXLoader | null = null;
57
+ mtlxLoader: MaterialXLoader | null = null;
49
58
 
50
59
  onImport = (loader: GLTFLoader, url: string, context: Context) => {
51
60
  if (debug) console.log("MaterialXLoaderPlugin: Registering MaterialX extension for", url);
@@ -53,18 +62,14 @@ export class MaterialXLoaderPlugin implements INeedleGLTFExtensionPlugin {
53
62
  // Register the MaterialX loader extension
54
63
  // Environment initialization is now handled in the MaterialXLoader constructor
55
64
  loader.register(p => {
56
- this.loader = new MaterialXLoader(p, context);
57
- return this.loader;
65
+ this.mtlxLoader = new MaterialXLoader(p, context);
66
+ return this.mtlxLoader;
58
67
  });
59
68
  };
60
69
 
61
70
  onLoaded = (url: string, gltf: GLTF, _context: Context) => {
62
- if (debug) console.log("[MaterialX] MaterialXLoaderPlugin: glTF loaded", { url, scene: gltf.scene, materialX_root_data: this.loader?.materialX_root_data });
71
+ if (debug) console.log("MaterialXLoaderPlugin: glTF loaded", url, gltf.scene);
63
72
 
64
- // If we don't have MaterialX data in the loaded glTF we don't need to do anything else here
65
- if (!this.loader?.materialX_root_data) {
66
- return;
67
- }
68
73
  // Set up onBeforeRender callbacks for objects with MaterialX materials
69
74
  // This ensures uniforms are updated properly during rendering
70
75
  gltf.scene.traverse((child) => {
@@ -72,18 +77,26 @@ export class MaterialXLoaderPlugin implements INeedleGLTFExtensionPlugin {
72
77
  const mesh = child as Mesh;
73
78
  const material = mesh.material as Material;
74
79
 
75
- if (material instanceof MaterialXMaterial) {
76
- if (debug) console.log("[MaterialX] Adding MaterialX uniform update component to:", child.name);
80
+ if (material?.userData?.updateUniforms) {
81
+ if (debug) console.log("Adding MaterialX uniform update component to:", child.name);
77
82
  child.addComponent(MaterialXUniformUpdate);
78
83
  }
79
84
  }
80
85
  });
81
86
 
82
- if (debug) console.log("[MaterialX] Loaded: ", this.loader);
87
+ if (debug) console.log("Loaded: ", this.mtlxLoader);
88
+
89
+ // Initialize MaterialX lighting system with scene data
90
+ const environment = state.materialXEnvironment;
91
+ environment.initializeFromContext().then(() => {
92
+ if (this.mtlxLoader) {
93
+ this.mtlxLoader.updateLightingFromEnvironment(environment);
94
+ }
95
+ });
83
96
  };
84
97
 
85
98
  onExport = (_exporter: GLTFExporter, _context: Context) => {
86
- console.log("[MaterialX] TODO: MaterialXLoaderPlugin: Setting up export extensions");
99
+ console.log("TODO: MaterialXLoaderPlugin: Setting up export extensions");
87
100
  // TODO: Add MaterialX export functionality if needed
88
101
  };
89
102
  }