Skip to content

Commit 0371364

Browse files
committed
WebGPURenderer: MaterialX Support
1 parent 0d64c42 commit 0371364

File tree

8 files changed

+1657
-752
lines changed

8 files changed

+1657
-752
lines changed

examples/jsm/loaders/MaterialXLoader.js

+53-53
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ const colorSpaceLib = {
2525
mx_srgb_texture_to_lin_rec709
2626
};
2727

28-
class MtlXElement {
28+
class MXElement {
2929

3030
constructor( name, nodeFunc, params = null ) {
3131

@@ -39,41 +39,41 @@ class MtlXElement {
3939

4040
// Ref: https://github.com/mrdoob/three.js/issues/24674
4141

42-
const MtlXElements = [
42+
const MXElements = [
4343

4444
// << Math >>
45-
new MtlXElement( 'add', add, [ 'in1', 'in2' ] ),
46-
new MtlXElement( 'subtract', sub, [ 'in1', 'in2' ] ),
47-
new MtlXElement( 'multiply', mul, [ 'in1', 'in2' ] ),
48-
new MtlXElement( 'divide', div, [ 'in1', 'in2' ] ),
49-
new MtlXElement( 'modulo', mod, [ 'in1', 'in2' ] ),
50-
new MtlXElement( 'absval', abs, [ 'in1', 'in2' ] ),
51-
new MtlXElement( 'sign', sign, [ 'in1', 'in2' ] ),
52-
new MtlXElement( 'floor', floor, [ 'in1', 'in2' ] ),
53-
new MtlXElement( 'ceil', ceil, [ 'in1', 'in2' ] ),
54-
new MtlXElement( 'round', round, [ 'in1', 'in2' ] ),
55-
new MtlXElement( 'power', pow, [ 'in1', 'in2' ] ),
56-
new MtlXElement( 'sin', sin, [ 'in' ] ),
57-
new MtlXElement( 'cos', cos, [ 'in' ] ),
58-
new MtlXElement( 'tan', tan, [ 'in' ] ),
59-
new MtlXElement( 'asin', asin, [ 'in' ] ),
60-
new MtlXElement( 'acos', acos, [ 'in' ] ),
61-
new MtlXElement( 'atan2', atan2, [ 'in1', 'in2' ] ),
62-
new MtlXElement( 'sqrt', sqrt, [ 'in' ] ),
45+
new MXElement( 'add', add, [ 'in1', 'in2' ] ),
46+
new MXElement( 'subtract', sub, [ 'in1', 'in2' ] ),
47+
new MXElement( 'multiply', mul, [ 'in1', 'in2' ] ),
48+
new MXElement( 'divide', div, [ 'in1', 'in2' ] ),
49+
new MXElement( 'modulo', mod, [ 'in1', 'in2' ] ),
50+
new MXElement( 'absval', abs, [ 'in1', 'in2' ] ),
51+
new MXElement( 'sign', sign, [ 'in1', 'in2' ] ),
52+
new MXElement( 'floor', floor, [ 'in1', 'in2' ] ),
53+
new MXElement( 'ceil', ceil, [ 'in1', 'in2' ] ),
54+
new MXElement( 'round', round, [ 'in1', 'in2' ] ),
55+
new MXElement( 'power', pow, [ 'in1', 'in2' ] ),
56+
new MXElement( 'sin', sin, [ 'in' ] ),
57+
new MXElement( 'cos', cos, [ 'in' ] ),
58+
new MXElement( 'tan', tan, [ 'in' ] ),
59+
new MXElement( 'asin', asin, [ 'in' ] ),
60+
new MXElement( 'acos', acos, [ 'in' ] ),
61+
new MXElement( 'atan2', atan2, [ 'in1', 'in2' ] ),
62+
new MXElement( 'sqrt', sqrt, [ 'in' ] ),
6363
//new MtlXElement( 'ln', ... ),
64-
new MtlXElement( 'exp', exp, [ 'in' ] ),
65-
new MtlXElement( 'clamp', clamp, [ 'in', 'low', 'high' ] ),
66-
new MtlXElement( 'min', min, [ 'in1', 'in2' ] ),
67-
new MtlXElement( 'max', max, [ 'in1', 'in2' ] ),
68-
new MtlXElement( 'normalize', normalize, [ 'in' ] ),
69-
new MtlXElement( 'magnitude', length, [ 'in1', 'in2' ] ),
70-
new MtlXElement( 'dotproduct', dot, [ 'in1', 'in2' ] ),
71-
new MtlXElement( 'crossproduct', cross, [ 'in' ] ),
64+
new MXElement( 'exp', exp, [ 'in' ] ),
65+
new MXElement( 'clamp', clamp, [ 'in', 'low', 'high' ] ),
66+
new MXElement( 'min', min, [ 'in1', 'in2' ] ),
67+
new MXElement( 'max', max, [ 'in1', 'in2' ] ),
68+
new MXElement( 'normalize', normalize, [ 'in' ] ),
69+
new MXElement( 'magnitude', length, [ 'in1', 'in2' ] ),
70+
new MXElement( 'dotproduct', dot, [ 'in1', 'in2' ] ),
71+
new MXElement( 'crossproduct', cross, [ 'in' ] ),
7272
//new MtlXElement( 'transformpoint', ... ),
7373
//new MtlXElement( 'transformvector', ... ),
7474
//new MtlXElement( 'transformnormal', ... ),
7575
//new MtlXElement( 'transformmatrix', ... ),
76-
new MtlXElement( 'normalmap', normalMap, [ 'in', 'scale' ] ),
76+
new MXElement( 'normalmap', normalMap, [ 'in', 'scale' ] ),
7777
//new MtlXElement( 'transpose', ... ),
7878
//new MtlXElement( 'determinant', ... ),
7979
//new MtlXElement( 'invertmatrix', ... ),
@@ -83,44 +83,44 @@ const MtlXElements = [
8383
//new MtlXElement( 'dot', ... ),
8484

8585
// << Adjustment >>
86-
new MtlXElement( 'remap', remap, [ 'in', 'inlow', 'inhigh', 'outlow', 'outhigh' ] ),
87-
new MtlXElement( 'smoothstep', smoothstep, [ 'in', 'low', 'high' ] ),
86+
new MXElement( 'remap', remap, [ 'in', 'inlow', 'inhigh', 'outlow', 'outhigh' ] ),
87+
new MXElement( 'smoothstep', smoothstep, [ 'in', 'low', 'high' ] ),
8888
//new MtlXElement( 'curveadjust', ... ),
8989
//new MtlXElement( 'curvelookup', ... ),
90-
new MtlXElement( 'luminance', luminance, [ 'in', 'lumacoeffs' ] ),
91-
new MtlXElement( 'rgbtohsv', mx_rgbtohsv, [ 'in' ] ),
92-
new MtlXElement( 'hsvtorgb', mx_hsvtorgb, [ 'in' ] ),
90+
new MXElement( 'luminance', luminance, [ 'in', 'lumacoeffs' ] ),
91+
new MXElement( 'rgbtohsv', mx_rgbtohsv, [ 'in' ] ),
92+
new MXElement( 'hsvtorgb', mx_hsvtorgb, [ 'in' ] ),
9393

9494
// << Mix >>
95-
new MtlXElement( 'mix', mix, [ 'bg', 'fg', 'mix' ] ),
95+
new MXElement( 'mix', mix, [ 'bg', 'fg', 'mix' ] ),
9696

9797
// << Channel >>
98-
new MtlXElement( 'combine2', vec2, [ 'in1', 'in2' ] ),
99-
new MtlXElement( 'combine3', vec3, [ 'in1', 'in2', 'in3' ] ),
100-
new MtlXElement( 'combine4', vec4, [ 'in1', 'in2', 'in3', 'in4' ] ),
98+
new MXElement( 'combine2', vec2, [ 'in1', 'in2' ] ),
99+
new MXElement( 'combine3', vec3, [ 'in1', 'in2', 'in3' ] ),
100+
new MXElement( 'combine4', vec4, [ 'in1', 'in2', 'in3', 'in4' ] ),
101101

102102
// << Procedural >>
103-
new MtlXElement( 'ramplr', mx_ramplr, [ 'valuel', 'valuer', 'texcoord' ] ),
104-
new MtlXElement( 'ramptb', mx_ramptb, [ 'valuet', 'valueb', 'texcoord' ] ),
105-
new MtlXElement( 'splitlr', mx_splitlr, [ 'valuel', 'valuer', 'texcoord' ] ),
106-
new MtlXElement( 'splittb', mx_splittb, [ 'valuet', 'valueb', 'texcoord' ] ),
107-
new MtlXElement( 'noise2d', mx_noise_float, [ 'texcoord', 'amplitude', 'pivot' ] ),
108-
new MtlXElement( 'noise3d', mx_noise_float, [ 'texcoord', 'amplitude', 'pivot' ] ),
109-
new MtlXElement( 'fractal3d', mx_fractal_noise_float, [ 'position', 'octaves', 'lacunarity', 'diminish', 'amplitude' ] ),
110-
new MtlXElement( 'cellnoise2d', mx_cell_noise_float, [ 'texcoord' ] ),
111-
new MtlXElement( 'cellnoise3d', mx_cell_noise_float, [ 'texcoord' ] ),
112-
new MtlXElement( 'worleynoise2d', mx_worley_noise_float, [ 'texcoord', 'jitter' ] ),
113-
new MtlXElement( 'worleynoise3d', mx_worley_noise_float, [ 'texcoord', 'jitter' ] ),
103+
new MXElement( 'ramplr', mx_ramplr, [ 'valuel', 'valuer', 'texcoord' ] ),
104+
new MXElement( 'ramptb', mx_ramptb, [ 'valuet', 'valueb', 'texcoord' ] ),
105+
new MXElement( 'splitlr', mx_splitlr, [ 'valuel', 'valuer', 'texcoord' ] ),
106+
new MXElement( 'splittb', mx_splittb, [ 'valuet', 'valueb', 'texcoord' ] ),
107+
new MXElement( 'noise2d', mx_noise_float, [ 'texcoord', 'amplitude', 'pivot' ] ),
108+
new MXElement( 'noise3d', mx_noise_float, [ 'texcoord', 'amplitude', 'pivot' ] ),
109+
new MXElement( 'fractal3d', mx_fractal_noise_float, [ 'position', 'octaves', 'lacunarity', 'diminish', 'amplitude' ] ),
110+
new MXElement( 'cellnoise2d', mx_cell_noise_float, [ 'texcoord' ] ),
111+
new MXElement( 'cellnoise3d', mx_cell_noise_float, [ 'texcoord' ] ),
112+
new MXElement( 'worleynoise2d', mx_worley_noise_float, [ 'texcoord', 'jitter' ] ),
113+
new MXElement( 'worleynoise3d', mx_worley_noise_float, [ 'texcoord', 'jitter' ] ),
114114

115115
// << Supplemental >>
116116
//new MtlXElement( 'tiledimage', ... ),
117117
//new MtlXElement( 'triplanarprojection', triplanarTextures, [ 'filex', 'filey', 'filez' ] ),
118118
//new MtlXElement( 'ramp4', ... ),
119119
//new MtlXElement( 'place2d', mx_place2d, [ 'texcoord', 'pivot', 'scale', 'rotate', 'offset' ] ),
120-
new MtlXElement( 'safepower', mx_safepower, [ 'in1', 'in2' ] ),
121-
new MtlXElement( 'contrast', mx_contrast, [ 'in', 'amount', 'pivot' ] ),
120+
new MXElement( 'safepower', mx_safepower, [ 'in1', 'in2' ] ),
121+
new MXElement( 'contrast', mx_contrast, [ 'in', 'amount', 'pivot' ] ),
122122
//new MtlXElement( 'hsvadjust', ... ),
123-
new MtlXElement( 'saturate', saturation, [ 'in', 'amount' ] ),
123+
new MXElement( 'saturate', saturation, [ 'in', 'amount' ] ),
124124
//new MtlXElement( 'extract', ... ),
125125
//new MtlXElement( 'separate2', ... ),
126126
//new MtlXElement( 'separate3', ... ),
@@ -129,7 +129,7 @@ const MtlXElements = [
129129
];
130130

131131
const MtlXLibrary = {};
132-
MtlXElements.forEach( element => MtlXLibrary[ element.name ] = element );
132+
MXElements.forEach( element => MtlXLibrary[ element.name ] = element );
133133

134134
class MaterialXLoader extends Loader {
135135

examples/jsm/nodes/code/CodeNode.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -12,21 +12,21 @@ class CodeNode extends Node {
1212
this.code = code;
1313
this.language = language;
1414

15-
this._includes = includes;
15+
this.includes = includes;
1616

1717
}
1818

1919
setIncludes( includes ) {
2020

21-
this._includes = includes;
21+
this.includes = includes;
2222

2323
return this;
2424

2525
}
2626

2727
getIncludes( /*builder*/ ) {
2828

29-
return this._includes;
29+
return this.includes;
3030

3131
}
3232

examples/jsm/nodes/core/NodeBuilder.js

+7
Original file line numberDiff line numberDiff line change
@@ -855,6 +855,13 @@ class NodeBuilder {
855855

856856
const previous = this.currentFunctionNode;
857857

858+
this.currentFunctionNode = fn;
859+
860+
fn.code = this.buildFunctionCode( shaderNode );
861+
862+
this.currentFunctionNode = previous;
863+
864+
return fn;
858865

859866
}
860867

examples/jsm/nodes/core/VarNode.js

+2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ class VarNode extends Node {
1010
this.node = node;
1111
this.name = name;
1212

13+
this.isVarNode = true;
14+
1315
}
1416

1517
isGlobal() {

examples/jsm/nodes/materialx/MaterialXNodes.js

+10-10
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import {
2-
mx_perlin_noise_float, mx_perlin_noise_vec2, mx_perlin_noise_vec3,
2+
mx_perlin_noise_float, mx_perlin_noise_vec3,
33
mx_worley_noise_float as worley_noise_float, mx_worley_noise_vec2 as worley_noise_vec2, mx_worley_noise_vec3 as worley_noise_vec3,
44
mx_cell_noise_float as cell_noise_float,
55
mx_fractal_noise_float as fractal_noise_float, mx_fractal_noise_vec2 as fractal_noise_vec2, mx_fractal_noise_vec3 as fractal_noise_vec3, mx_fractal_noise_vec4 as fractal_noise_vec4
@@ -8,7 +8,7 @@ import { mx_hsvtorgb, mx_rgbtohsv } from './lib/mx_hsv.js';
88
import { mx_srgb_texture_to_lin_rec709 } from './lib/mx_transform_color.js';
99
import { mix, smoothstep } from '../math/MathNode.js';
1010
import { uv } from '../accessors/UVNode.js';
11-
import { float, vec2, vec4 } from '../shadernode/ShaderNode.js';
11+
import { float, vec2, vec4, int } from '../shadernode/ShaderNode.js';
1212

1313
export const mx_aastep = ( threshold, value ) => {
1414

@@ -42,7 +42,7 @@ export const mx_safepower = ( in1, in2 = 1 ) => {
4242
export const mx_contrast = ( input, amount = 1, pivot = .5 ) => float( input ).sub( pivot ).mul( amount ).add( pivot );
4343

4444
export const mx_noise_float = ( texcoord = uv(), amplitude = 1, pivot = 0 ) => mx_perlin_noise_float( texcoord.convert( 'vec2|vec3' ) ).mul( amplitude ).add( pivot );
45-
export const mx_noise_vec2 = ( texcoord = uv(), amplitude = 1, pivot = 0 ) => mx_perlin_noise_vec2( texcoord.convert( 'vec2|vec3' ) ).mul( amplitude ).add( pivot );
45+
//export const mx_noise_vec2 = ( texcoord = uv(), amplitude = 1, pivot = 0 ) => mx_perlin_noise_vec3( texcoord.convert( 'vec2|vec3' ) ).mul( amplitude ).add( pivot );
4646
export const mx_noise_vec3 = ( texcoord = uv(), amplitude = 1, pivot = 0 ) => mx_perlin_noise_vec3( texcoord.convert( 'vec2|vec3' ) ).mul( amplitude ).add( pivot );
4747
export const mx_noise_vec4 = ( texcoord = uv(), amplitude = 1, pivot = 0 ) => {
4848

@@ -54,15 +54,15 @@ export const mx_noise_vec4 = ( texcoord = uv(), amplitude = 1, pivot = 0 ) => {
5454

5555
};
5656

57-
export const mx_worley_noise_float = ( texcoord = uv(), jitter = 1 ) => worley_noise_float( texcoord.convert( 'vec2|vec3' ), jitter, 1 );
58-
export const mx_worley_noise_vec2 = ( texcoord = uv(), jitter = 1 ) => worley_noise_vec2( texcoord.convert( 'vec2|vec3' ), jitter, 1 );
59-
export const mx_worley_noise_vec3 = ( texcoord = uv(), jitter = 1 ) => worley_noise_vec3( texcoord.convert( 'vec2|vec3' ), jitter, 1 );
57+
export const mx_worley_noise_float = ( texcoord = uv(), jitter = 1 ) => worley_noise_float( texcoord.convert( 'vec2|vec3' ), jitter, int( 1 ) );
58+
export const mx_worley_noise_vec2 = ( texcoord = uv(), jitter = 1 ) => worley_noise_vec2( texcoord.convert( 'vec2|vec3' ), jitter, int( 1 ) );
59+
export const mx_worley_noise_vec3 = ( texcoord = uv(), jitter = 1 ) => worley_noise_vec3( texcoord.convert( 'vec2|vec3' ), jitter, int( 1 ) );
6060

6161
export const mx_cell_noise_float = ( texcoord = uv() ) => cell_noise_float( texcoord.convert( 'vec2|vec3' ) );
6262

63-
export const mx_fractal_noise_float = ( position = uv(), octaves = 3, lacunarity = 2, diminish = .5, amplitude = 1 ) => fractal_noise_float( position, octaves, lacunarity, diminish ).mul( amplitude );
64-
export const mx_fractal_noise_vec2 = ( position = uv(), octaves = 3, lacunarity = 2, diminish = .5, amplitude = 1 ) => fractal_noise_vec2( position, octaves, lacunarity, diminish ).mul( amplitude );
65-
export const mx_fractal_noise_vec3 = ( position = uv(), octaves = 3, lacunarity = 2, diminish = .5, amplitude = 1 ) => fractal_noise_vec3( position, octaves, lacunarity, diminish ).mul( amplitude );
66-
export const mx_fractal_noise_vec4 = ( position = uv(), octaves = 3, lacunarity = 2, diminish = .5, amplitude = 1 ) => fractal_noise_vec4( position, octaves, lacunarity, diminish ).mul( amplitude );
63+
export const mx_fractal_noise_float = ( position = uv(), octaves = 3, lacunarity = 2, diminish = .5, amplitude = 1 ) => fractal_noise_float( position, int( octaves ), lacunarity, diminish ).mul( amplitude );
64+
export const mx_fractal_noise_vec2 = ( position = uv(), octaves = 3, lacunarity = 2, diminish = .5, amplitude = 1 ) => fractal_noise_vec2( position, int( octaves ), lacunarity, diminish ).mul( amplitude );
65+
export const mx_fractal_noise_vec3 = ( position = uv(), octaves = 3, lacunarity = 2, diminish = .5, amplitude = 1 ) => fractal_noise_vec3( position, int( octaves ), lacunarity, diminish ).mul( amplitude );
66+
export const mx_fractal_noise_vec4 = ( position = uv(), octaves = 3, lacunarity = 2, diminish = .5, amplitude = 1 ) => fractal_noise_vec4( position, int( octaves ), lacunarity, diminish ).mul( amplitude );
6767

6868
export { mx_hsvtorgb, mx_rgbtohsv, mx_srgb_texture_to_lin_rec709 };

0 commit comments

Comments
 (0)