@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/README.md +1 -1
- package/codegen/register_types.ts +6 -6
- package/index.ts +4 -4
- package/package.json +20 -20
- package/package.needle.json +2 -2
- package/src/helper.js +457 -457
- package/src/index.ts +4 -4
- package/src/loader/loader.needle.ts +106 -106
- package/src/loader/loader.three.ts +555 -555
- package/src/materialx.ts +221 -220
- package/src/textureHelper.ts +169 -169
- package/src/utils.ts +57 -57
- package/tsconfig.json +20 -20
- package/.github/workflows/release.yml +0 -39
- package/bin/README.md +0 -5
- package/workspace.code-workspace +0 -18
package/src/index.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { initializeMaterialX, state } from "./materialx.js";
|
|
2
|
-
|
|
3
|
-
const getMaterialXEnvironment = () => state.materialXEnvironment;
|
|
4
|
-
|
|
1
|
+
import { initializeMaterialX, state } from "./materialx.js";
|
|
2
|
+
|
|
3
|
+
const getMaterialXEnvironment = () => state.materialXEnvironment;
|
|
4
|
+
|
|
5
5
|
export { initializeMaterialX, getMaterialXEnvironment };
|
|
@@ -1,106 +1,106 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
import { Group, Camera, Material, Mesh, Object3D } from "three";
|
|
4
|
-
import { Context, GLTF, addCustomExtensionPlugin, Component, INeedleGLTFExtensionPlugin } from "@needle-tools/engine";
|
|
5
|
-
import type { GLTFLoader } from "three/examples/jsm/loaders/GLTFLoader.js";
|
|
6
|
-
import type { GLTFExporter } from "three/examples/jsm/exporters/GLTFExporter.js";
|
|
7
|
-
import { MaterialXLoader } from "./loader.three.js";
|
|
8
|
-
import { debug } from "../utils.js";
|
|
9
|
-
import { state } from "../materialx.js";
|
|
10
|
-
|
|
11
|
-
//@dont-generate-component
|
|
12
|
-
export class MaterialXUniformUpdate extends Component {
|
|
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
|
-
|
|
26
|
-
onEnable(): void {
|
|
27
|
-
this.context.addBeforeRenderListener(this.gameObject, this._onBeforeRender);
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
onDisable(): void {
|
|
31
|
-
this.context.removeBeforeRenderListener(this.gameObject, this._onBeforeRender);
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
_onBeforeRender = () => {
|
|
35
|
-
// Update uniforms or perform any pre-render logic here
|
|
36
|
-
const gameObject = this.gameObject as any as Mesh;
|
|
37
|
-
const material = gameObject?.material;
|
|
38
|
-
|
|
39
|
-
const camera = this.context.mainCamera;
|
|
40
|
-
if (!camera) return;
|
|
41
|
-
|
|
42
|
-
MaterialXUniformUpdate.updateMaterial(material, gameObject, camera);
|
|
43
|
-
|
|
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
|
-
});
|
|
50
|
-
}
|
|
51
|
-
}
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
export class MaterialXLoaderPlugin implements INeedleGLTFExtensionPlugin {
|
|
55
|
-
name = "MaterialXLoaderPlugin";
|
|
56
|
-
|
|
57
|
-
mtlxLoader: MaterialXLoader | null = null;
|
|
58
|
-
|
|
59
|
-
onImport = (loader: GLTFLoader, url: string, context: Context) => {
|
|
60
|
-
if (debug) console.log("MaterialXLoaderPlugin: Registering MaterialX extension for", url);
|
|
61
|
-
|
|
62
|
-
// Register the MaterialX loader extension
|
|
63
|
-
// Environment initialization is now handled in the MaterialXLoader constructor
|
|
64
|
-
loader.register(p => {
|
|
65
|
-
this.mtlxLoader = new MaterialXLoader(p, context);
|
|
66
|
-
return this.mtlxLoader;
|
|
67
|
-
});
|
|
68
|
-
};
|
|
69
|
-
|
|
70
|
-
onLoaded = (url: string, gltf: GLTF, _context: Context) => {
|
|
71
|
-
if (debug) console.log("MaterialXLoaderPlugin: glTF loaded", url, gltf.scene);
|
|
72
|
-
|
|
73
|
-
// Set up onBeforeRender callbacks for objects with MaterialX materials
|
|
74
|
-
// This ensures uniforms are updated properly during rendering
|
|
75
|
-
gltf.scene.traverse((child) => {
|
|
76
|
-
if ((child as any).isMesh) {
|
|
77
|
-
const mesh = child as Mesh;
|
|
78
|
-
const material = mesh.material as Material;
|
|
79
|
-
|
|
80
|
-
if (material?.userData?.updateUniforms) {
|
|
81
|
-
if (debug) console.log("Adding MaterialX uniform update component to:", child.name);
|
|
82
|
-
child.addComponent(MaterialXUniformUpdate);
|
|
83
|
-
}
|
|
84
|
-
}
|
|
85
|
-
});
|
|
86
|
-
|
|
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
|
-
});
|
|
96
|
-
};
|
|
97
|
-
|
|
98
|
-
onExport = (_exporter: GLTFExporter, _context: Context) => {
|
|
99
|
-
console.log("TODO: MaterialXLoaderPlugin: Setting up export extensions");
|
|
100
|
-
// TODO: Add MaterialX export functionality if needed
|
|
101
|
-
};
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
export function registerNeedleLoader() {
|
|
105
|
-
addCustomExtensionPlugin(new MaterialXLoaderPlugin());
|
|
106
|
-
}
|
|
1
|
+
|
|
2
|
+
|
|
3
|
+
import { Group, Camera, Material, Mesh, Object3D } from "three";
|
|
4
|
+
import { Context, GLTF, addCustomExtensionPlugin, Component, INeedleGLTFExtensionPlugin } from "@needle-tools/engine";
|
|
5
|
+
import type { GLTFLoader } from "three/examples/jsm/loaders/GLTFLoader.js";
|
|
6
|
+
import type { GLTFExporter } from "three/examples/jsm/exporters/GLTFExporter.js";
|
|
7
|
+
import { MaterialXLoader } from "./loader.three.js";
|
|
8
|
+
import { debug } from "../utils.js";
|
|
9
|
+
import { state } from "../materialx.js";
|
|
10
|
+
|
|
11
|
+
//@dont-generate-component
|
|
12
|
+
export class MaterialXUniformUpdate extends Component {
|
|
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
|
+
|
|
26
|
+
onEnable(): void {
|
|
27
|
+
this.context.addBeforeRenderListener(this.gameObject, this._onBeforeRender);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
onDisable(): void {
|
|
31
|
+
this.context.removeBeforeRenderListener(this.gameObject, this._onBeforeRender);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
_onBeforeRender = () => {
|
|
35
|
+
// Update uniforms or perform any pre-render logic here
|
|
36
|
+
const gameObject = this.gameObject as any as Mesh;
|
|
37
|
+
const material = gameObject?.material;
|
|
38
|
+
|
|
39
|
+
const camera = this.context.mainCamera;
|
|
40
|
+
if (!camera) return;
|
|
41
|
+
|
|
42
|
+
MaterialXUniformUpdate.updateMaterial(material, gameObject, camera);
|
|
43
|
+
|
|
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
|
+
});
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
export class MaterialXLoaderPlugin implements INeedleGLTFExtensionPlugin {
|
|
55
|
+
name = "MaterialXLoaderPlugin";
|
|
56
|
+
|
|
57
|
+
mtlxLoader: MaterialXLoader | null = null;
|
|
58
|
+
|
|
59
|
+
onImport = (loader: GLTFLoader, url: string, context: Context) => {
|
|
60
|
+
if (debug) console.log("MaterialXLoaderPlugin: Registering MaterialX extension for", url);
|
|
61
|
+
|
|
62
|
+
// Register the MaterialX loader extension
|
|
63
|
+
// Environment initialization is now handled in the MaterialXLoader constructor
|
|
64
|
+
loader.register(p => {
|
|
65
|
+
this.mtlxLoader = new MaterialXLoader(p, context);
|
|
66
|
+
return this.mtlxLoader;
|
|
67
|
+
});
|
|
68
|
+
};
|
|
69
|
+
|
|
70
|
+
onLoaded = (url: string, gltf: GLTF, _context: Context) => {
|
|
71
|
+
if (debug) console.log("MaterialXLoaderPlugin: glTF loaded", url, gltf.scene);
|
|
72
|
+
|
|
73
|
+
// Set up onBeforeRender callbacks for objects with MaterialX materials
|
|
74
|
+
// This ensures uniforms are updated properly during rendering
|
|
75
|
+
gltf.scene.traverse((child) => {
|
|
76
|
+
if ((child as any).isMesh) {
|
|
77
|
+
const mesh = child as Mesh;
|
|
78
|
+
const material = mesh.material as Material;
|
|
79
|
+
|
|
80
|
+
if (material?.userData?.updateUniforms) {
|
|
81
|
+
if (debug) console.log("Adding MaterialX uniform update component to:", child.name);
|
|
82
|
+
child.addComponent(MaterialXUniformUpdate);
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
});
|
|
86
|
+
|
|
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
|
+
});
|
|
96
|
+
};
|
|
97
|
+
|
|
98
|
+
onExport = (_exporter: GLTFExporter, _context: Context) => {
|
|
99
|
+
console.log("TODO: MaterialXLoaderPlugin: Setting up export extensions");
|
|
100
|
+
// TODO: Add MaterialX export functionality if needed
|
|
101
|
+
};
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
export function registerNeedleLoader() {
|
|
105
|
+
addCustomExtensionPlugin(new MaterialXLoaderPlugin());
|
|
106
|
+
}
|