@needle-tools/materialx 1.1.1-next.b7e5e45 → 1.1.1-next.fb25319

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.
@@ -0,0 +1,37 @@
1
+ import { BufferGeometry, Camera, Group, IUniform, MaterialParameters, Object3D, Scene, ShaderMaterial, Texture, WebGLRenderer } from "three";
2
+ import { MaterialXContext, MaterialXEnvironment } from "./materialx.js";
3
+ import { Callbacks } from "./materialx.helper.js";
4
+
5
+ declare type MaterialXMaterialInitParameters = {
6
+ name: string;
7
+ shaderName?: string | null;
8
+ shader: any;
9
+ loaders: Callbacks;
10
+ context: MaterialXContext;
11
+ parameters?: MaterialParameters;
12
+ debug?: boolean;
13
+ }
14
+
15
+ type Uniforms = Record<string, IUniform & { needsUpdate?: boolean }>;
16
+ type Precision = "highp" | "mediump" | "lowp";
17
+
18
+ export declare class MaterialXMaterial extends ShaderMaterial {
19
+ readonly shaderName: string | null;
20
+
21
+ copy(source: MaterialXMaterial): this;
22
+
23
+ private _context: MaterialXContext | null;
24
+ private _shader: any;
25
+ private _needsTangents: boolean;
26
+
27
+ constructor(init?: MaterialXMaterialInitParameters);
28
+
29
+ private _missingTangentsWarned: boolean;
30
+ onBeforeRender(renderer: WebGLRenderer, _scene: Scene, camera: Camera, geometry: BufferGeometry, object: Object3D, _group: Group): void;
31
+
32
+ envMapIntensity: number;
33
+ envMap: Texture | null;
34
+ updateUniforms(environment: MaterialXEnvironment, _renderer: WebGLRenderer, object: Object3D, camera: Camera, time?: number, frame?: number): void;
35
+
36
+ private updateEnvironmentUniforms(environment: MaterialXEnvironment): void;
37
+ }
@@ -1,38 +1,43 @@
1
- import { BufferGeometry, Camera, FrontSide, GLSL3, Group, IUniform, MaterialParameters, Matrix3, Matrix4, Object3D, Scene, ShaderMaterial, Texture, Vector3, WebGLRenderer } from "three";
1
+ import { BufferGeometry, Camera, FrontSide, GLSL3, Group, Matrix3, Matrix4, Object3D, Scene, ShaderMaterial, Texture, Vector3, WebGLRenderer } from "three";
2
2
  import { debug, getFrame, getTime } from "./utils.js";
3
- import { MaterialXContext, MaterialXEnvironment } from "./materialx.js";
4
- import { generateMaterialPropertiesForUniforms, getUniformValues, Callbacks } from "./materialx.helper.js";
3
+ import { MaterialXEnvironment } from "./materialx.js";
4
+ import { generateMaterialPropertiesForUniforms, getUniformValues } from "./materialx.helper.js";
5
5
  import { cloneUniforms, cloneUniformsGroups } from "three/src/renderers/shaders/UniformsUtils.js";
6
6
 
7
-
8
7
  // Add helper matrices for uniform updates (similar to MaterialX example)
9
8
  const normalMat = new Matrix3();
10
9
  const worldViewPos = new Vector3();
11
10
 
12
- declare type MaterialXMaterialInitParameters = {
13
- name: string,
14
- shaderName?: string | null, // Optional name of the shader
15
- shader: any,
16
- loaders: Callbacks,
17
- context: MaterialXContext,
18
- // Optional parameters
19
- parameters?: MaterialParameters,
20
- debug?: boolean,
21
- }
22
-
23
- type Uniforms = Record<string, IUniform & { needsUpdate?: boolean }>;
24
- type Precision = "highp" | "mediump" | "lowp";
11
+ /**
12
+ * @typedef {Object} MaterialXMaterialInitParameters
13
+ * @property {string} name
14
+ * @property {string | null} [shaderName] - Optional name of the shader
15
+ * @property {any} shader
16
+ * @property {import('./materialx.helper.js').Callbacks} loaders
17
+ * @property {import('./materialx.js').MaterialXContext} context
18
+ * @property {import('three').MaterialParameters} [parameters] - Optional parameters
19
+ * @property {boolean} [debug] - Debug flag
20
+ */
21
+
22
+ /**
23
+ * @typedef {"highp" | "mediump" | "lowp"} Precision
24
+ */
25
25
 
26
26
  export class MaterialXMaterial extends ShaderMaterial {
27
27
 
28
- /** The original name of the shader */
29
- readonly shaderName: string | null = null;
28
+ /** The original name of the shader
29
+ * @type {string | null} */
30
+ shaderName = null;
30
31
 
31
- copy(source: MaterialXMaterial): this {
32
+ /**
33
+ * @param {MaterialXMaterial} source
34
+ * @returns {this}
35
+ */
36
+ copy(source) {
32
37
  super.copy(source);
33
38
  this._context = source._context;
34
39
  this._shader = source._shader;
35
- this.uniforms = cloneUniforms(source.uniforms) as Uniforms;
40
+ this.uniforms = cloneUniforms(source.uniforms);
36
41
  this.uniformsGroups = cloneUniformsGroups(source.uniformsGroups);
37
42
  this.envMapIntensity = source.envMapIntensity;
38
43
  this.envMap = source.envMap;
@@ -42,11 +47,17 @@ export class MaterialXMaterial extends ShaderMaterial {
42
47
  return this;
43
48
  }
44
49
 
45
- private _context: MaterialXContext | null = null;
46
- private _shader: any;
47
- private _needsTangents: boolean = false;
50
+ /** @type {import('./materialx.js').MaterialXContext | null} */
51
+ _context = null;
52
+ /** @type {any} */
53
+ _shader = null;
54
+ /** @type {boolean} */
55
+ _needsTangents = false;
48
56
 
49
- constructor(init?: MaterialXMaterialInitParameters) {
57
+ /**
58
+ * @param {MaterialXMaterialInitParameters} [init]
59
+ */
60
+ constructor(init) {
50
61
 
51
62
  // TODO: we need to properly copy the uniforms and other properties from the source material
52
63
  if (!init) {
@@ -77,7 +88,7 @@ export class MaterialXMaterial extends ShaderMaterial {
77
88
  vertexShader = vertexShader.replace(/\bi_color_0\b/g, 'color');
78
89
 
79
90
  // Patch fragmentShader
80
- const precision: Precision = init.parameters?.precision || "highp" as Precision;
91
+ const precision = init.parameters?.precision || "highp";
81
92
  vertexShader = vertexShader.replace(/precision mediump float;/g, `precision ${precision} float;`);
82
93
  fragmentShader = fragmentShader.replace(/precision mediump float;/g, `precision ${precision} float;`);
83
94
  fragmentShader = fragmentShader.replace(/#define M_FLOAT_EPS 1e-8/g, precision === "highp" ? `#define M_FLOAT_EPS 1e-8` : `#define M_FLOAT_EPS 1e-3`);
@@ -138,7 +149,7 @@ export class MaterialXMaterial extends ShaderMaterial {
138
149
  #include <tonemapping_fragment>
139
150
  #include <colorspace_fragment>`);
140
151
 
141
- const defines: Record<string, string> = {};
152
+ const defines = {};
142
153
  if (hasUv1) defines['USE_UV1'] = '';
143
154
  if (hasUv2) defines['USE_UV2'] = '';
144
155
  if (hasUv3) defines['USE_UV3'] = '';
@@ -199,8 +210,18 @@ export class MaterialXMaterial extends ShaderMaterial {
199
210
  }
200
211
  }
201
212
 
202
- private _missingTangentsWarned: boolean = false;
203
- onBeforeRender(renderer: WebGLRenderer, _scene: Scene, camera: Camera, geometry: BufferGeometry, object: Object3D, _group: Group): void {
213
+ /** @type {boolean} */
214
+ _missingTangentsWarned = false;
215
+
216
+ /**
217
+ * @param {WebGLRenderer} renderer
218
+ * @param {Scene} _scene
219
+ * @param {Camera} camera
220
+ * @param {BufferGeometry} geometry
221
+ * @param {Object3D} object
222
+ * @param {Group} _group
223
+ */
224
+ onBeforeRender(renderer, _scene, camera, geometry, object, _group) {
204
225
  if (this._needsTangents && !geometry.attributes.tangent) {
205
226
  if (!this._missingTangentsWarned) {
206
227
  this._missingTangentsWarned = true;
@@ -217,12 +238,22 @@ export class MaterialXMaterial extends ShaderMaterial {
217
238
  }
218
239
  }
219
240
 
241
+ /** @type {number} */
242
+ envMapIntensity = 1.0; // Default intensity for environment map
243
+ /** @type {Texture | null} */
244
+ envMap = null; // Environment map texture, can be set externally
220
245
 
221
- envMapIntensity: number = 1.0; // Default intensity for environment map
222
- envMap: Texture | null = null; // Environment map texture, can be set externally
223
- updateUniforms = (environment: MaterialXEnvironment, _renderer: WebGLRenderer, object: Object3D, camera: Camera, time?: number, frame?: number) => {
246
+ /**
247
+ * @param {MaterialXEnvironment} environment
248
+ * @param {WebGLRenderer} _renderer
249
+ * @param {Object3D} object
250
+ * @param {Camera} camera
251
+ * @param {number} [time]
252
+ * @param {number} [frame]
253
+ */
254
+ updateUniforms = (environment, _renderer, object, camera, time, frame) => {
224
255
 
225
- const uniforms = this.uniforms as Uniforms;
256
+ const uniforms = this.uniforms;
226
257
 
227
258
  // Update standard transformation matrices
228
259
  if (uniforms.u_worldMatrix) {
@@ -269,9 +300,13 @@ export class MaterialXMaterial extends ShaderMaterial {
269
300
  this.uniformsNeedUpdate = true;
270
301
  }
271
302
 
272
- private updateEnvironmentUniforms = (environment: MaterialXEnvironment) => {
303
+ /**
304
+ * @private
305
+ * @param {MaterialXEnvironment} environment
306
+ */
307
+ updateEnvironmentUniforms = (environment) => {
273
308
 
274
- const uniforms = this.uniforms as Uniforms;
309
+ const uniforms = this.uniforms;
275
310
 
276
311
  // Get lighting data from environment
277
312
  const lightData = environment.lightData || null;
package/src/utils.d.ts ADDED
@@ -0,0 +1,17 @@
1
+ /**
2
+ * Get URL parameter value
3
+ */
4
+ export function getParam(name: string): boolean | string;
5
+
6
+ export const debug: boolean | string;
7
+ export const debugUpdate: boolean;
8
+
9
+ /**
10
+ * Get current time in seconds
11
+ */
12
+ export function getTime(): number;
13
+
14
+ /**
15
+ * Get current frame number
16
+ */
17
+ export function getFrame(): number;
@@ -1,8 +1,12 @@
1
1
  // import { BufferGeometry } from 'three';
2
2
  // import * as BufferGeometryUtils from 'three/examples/jsm/utils/BufferGeometryUtils.js';
3
3
 
4
-
5
- export function getParam(name: string) {
4
+ /**
5
+ * Get URL parameter value
6
+ * @param {string} name - Parameter name
7
+ * @returns {boolean|string} Parameter value or false if not found
8
+ */
9
+ export function getParam(name) {
6
10
  const urlParams = new URLSearchParams(window.location.search);
7
11
  const param = urlParams.get(name);
8
12
  if (param == null || param === "0" || param === "false") return false;
@@ -13,21 +17,30 @@ export function getParam(name: string) {
13
17
  export const debug = getParam("debugmaterialx");
14
18
  export const debugUpdate = debug === "update";
15
19
 
16
-
17
20
  let time = 0;
21
+ /**
22
+ * Get current time in seconds
23
+ * @returns {number} Current time
24
+ */
18
25
  export function getTime() {
19
26
  return time;
20
27
  }
28
+
21
29
  let frame = 0;
30
+ /**
31
+ * Get current frame number
32
+ * @returns {number} Current frame
33
+ */
22
34
  export function getFrame() {
23
35
  return frame;
24
36
  }
25
37
 
26
- const performance = window.performance || (window as any).webkitPerformance || (window as any).mozPerformance;
38
+ const performance = window.performance || /** @type {any} */ (window).webkitPerformance || /** @type {any} */ (window).mozPerformance;
39
+
27
40
  function updateTime() {
28
41
  time = performance.now() / 1000; // Convert to seconds
29
42
  frame++;
30
43
  window.requestAnimationFrame(updateTime);
31
44
  }
32
- window.requestAnimationFrame(updateTime);
33
45
 
46
+ window.requestAnimationFrame(updateTime);
@@ -0,0 +1,13 @@
1
+ import { WebGLRenderer, WebGLRenderTarget, Texture } from 'three';
2
+
3
+ /**
4
+ * Renders a PMREM environment map to an equirectangular texture with specified roughness
5
+ */
6
+ export function renderPMREMToEquirect(
7
+ renderer: WebGLRenderer,
8
+ pmremTexture: Texture,
9
+ roughness?: number,
10
+ width?: number,
11
+ height?: number,
12
+ renderTargetHeight?: number
13
+ ): WebGLRenderTarget;
@@ -1,4 +1,4 @@
1
- import { WebGLRenderer, Scene, WebGLRenderTarget, PlaneGeometry, OrthographicCamera, ShaderMaterial, RGBAFormat, FloatType, LinearFilter, Mesh, EquirectangularReflectionMapping, RepeatWrapping, LinearMipMapLinearFilter, Texture, WebGLUtils } from 'three';
1
+ import { WebGLRenderer, Scene, WebGLRenderTarget, PlaneGeometry, OrthographicCamera, ShaderMaterial, RGBAFormat, FloatType, LinearFilter, Mesh, EquirectangularReflectionMapping, RepeatWrapping, LinearMipMapLinearFilter, Texture } from 'three';
2
2
  import { getParam } from './utils.js';
3
3
 
4
4
  const debug = getParam("debugmaterialx");
@@ -7,25 +7,26 @@ const debug = getParam("debugmaterialx");
7
7
  * Renders a PMREM environment map to an equirectangular texture with specified roughness
8
8
  * @param {WebGLRenderer} renderer - Three.js WebGL renderer
9
9
  * @param {Texture} pmremTexture - PMREM texture (2D CubeUV layout) to convert
10
- * @param {number} roughness - Roughness value (0.0 to 1.0)
11
- * @param {number} width - Output texture width (default: 1024)
12
- * @param {number} height - Output texture height (default: 512)
13
- * @param {number} renderTargetHeight - Original render target height (optional, for proper PMREM parameter calculation)
10
+ * @param {number} [roughness=0.0] - Roughness value (0.0 to 1.0)
11
+ * @param {number} [width=1024] - Output texture width
12
+ * @param {number} [height=512] - Output texture height
13
+ * @param {number} [renderTargetHeight] - Original render target height (optional, for proper PMREM parameter calculation)
14
14
  * @returns {WebGLRenderTarget} Render target containing the equirectangular texture
15
- * @example // Creating an equirectangular texture from a PMREM environment map at a certain roughness level:
15
+ * @example
16
+ * // Creating an equirectangular texture from a PMREM environment map at a certain roughness level:
16
17
  * const pmremRenderTarget = pmremGenerator.fromEquirectangular(envMap);
17
18
  * const equirectRenderTarget = await renderPMREMToEquirect(renderer, pmremRenderTarget.texture, 0.5, 2048, 1024, pmremRenderTarget.height);
18
-
19
- // Use the rendered equirectangular texture
20
- const equirectTexture = equirectRenderTarget.texture;
21
-
22
- // Apply to your material or save/export
23
- someMaterial.map = equirectTexture;
24
-
25
- // Don't forget to dispose when done
26
- // equirectRenderTarget.dispose();
19
+ *
20
+ * // Use the rendered equirectangular texture
21
+ * const equirectTexture = equirectRenderTarget.texture;
22
+ *
23
+ * // Apply to your material or save/export
24
+ * someMaterial.map = equirectTexture;
25
+ *
26
+ * // Don't forget to dispose when done
27
+ * // equirectRenderTarget.dispose();
27
28
  */
28
- export function renderPMREMToEquirect(renderer: WebGLRenderer, pmremTexture: Texture, roughness = 0.0, width = 1024, height = 512, renderTargetHeight?: number) {
29
+ export function renderPMREMToEquirect(renderer, pmremTexture, roughness = 0.0, width = 1024, height = 512, renderTargetHeight) {
29
30
  // TODO Validate inputs
30
31
  // console.log(renderer, pmremTexture);
31
32
 
@@ -167,4 +168,4 @@ export function renderPMREMToEquirect(renderer: WebGLRenderer, pmremTexture: Tex
167
168
  });
168
169
 
169
170
  return renderTarget;
170
- }
171
+ }
@@ -1,43 +0,0 @@
1
- import { addCustomExtensionPlugin } from "@needle-tools/engine";
2
- import { Context, GLTF, INeedleGLTFExtensionPlugin } from "@needle-tools/engine";
3
- import type { GLTFLoader } from "three/examples/jsm/loaders/GLTFLoader.js";
4
- import type { GLTFExporter } from "three/examples/jsm/exporters/GLTFExporter.js";
5
- import { useNeedleMaterialX as _useNeedleMaterialX, MaterialXLoader } from "./loader.three.js";
6
- import { debug } from "../utils.js";
7
- import { MaterialParameters } from "three";
8
-
9
-
10
- export class MaterialXLoaderPlugin implements INeedleGLTFExtensionPlugin {
11
- readonly name = "MaterialXLoaderPlugin";
12
-
13
- private loader: MaterialXLoader | null = null;
14
-
15
- onImport = (loader: GLTFLoader, url: string, context: Context) => {
16
- if (debug) console.log("MaterialXLoaderPlugin: Registering MaterialX extension for", url);
17
- _useNeedleMaterialX(loader, {
18
- cacheKey: url,
19
- parameters: {
20
- precision: context.renderer.capabilities.getMaxPrecision("highp") as MaterialParameters["precision"],
21
- }
22
- }, {
23
- getTime: () => context.time.time,
24
- getFrame: () => context.time.frame,
25
- });
26
- };
27
-
28
- onLoaded = (url: string, gltf: GLTF, _context: Context) => {
29
- if (debug) console.log("[MaterialX] MaterialXLoaderPlugin: glTF loaded", { url, scene: gltf.scene, materialX_root_data: this.loader?.materialX_root_data });
30
- };
31
-
32
- onExport = (_exporter: GLTFExporter, _context: Context) => {
33
- console.warn("[MaterialX] Export is not supported");
34
- };
35
- }
36
-
37
-
38
- /**
39
- * Add the MaterialXLoaderPlugin to the Needle Engine.
40
- */
41
- export async function useNeedleMaterialX() {
42
- addCustomExtensionPlugin(new MaterialXLoaderPlugin());
43
- }
package/tsconfig.json DELETED
@@ -1,20 +0,0 @@
1
- {
2
- "compilerOptions": {
3
- "target": "ESNext",
4
- "useDefineForClassFields": true,
5
- "module": "ESNext",
6
- "lib": ["ESNext", "DOM"],
7
- "moduleResolution": "Node",
8
- "strict": true,
9
- "sourceMap": true,
10
- "resolveJsonModule": true,
11
- "esModuleInterop": true,
12
- "noEmit": true,
13
- "noUnusedLocals": false,
14
- "noUnusedParameters": true,
15
- "noImplicitReturns": true,
16
- "noImplicitAny": false,
17
- "experimentalDecorators": true
18
- },
19
- "include": ["."]
20
- }