Skip to content

Commit 17d60f6

Browse files
authored
MaterialX: Fix mx_hsvtorgb and brick_procedural example (#29150)
* revision mx_hsvtorgb * add optional return for function with layout * added brick procedural * cleanup
1 parent df91505 commit 17d60f6

File tree

4 files changed

+36
-25
lines changed

4 files changed

+36
-25
lines changed
527 Bytes
Loading

examples/webgpu_loader_materialx.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242

4343
const samples = [
4444
'standard_surface_brass_tiled.mtlx',
45-
//'standard_surface_brick_procedural.mtlx',
45+
'standard_surface_brick_procedural.mtlx',
4646
'standard_surface_carpaint.mtlx',
4747
//'standard_surface_chess_set.mtlx',
4848
'standard_surface_chrome.mtlx',
@@ -51,7 +51,7 @@
5151
//'standard_surface_glass.mtlx',
5252
//'standard_surface_glass_tinted.mtlx',
5353
'standard_surface_gold.mtlx',
54-
'standard_surface_greysphere.mtlx',
54+
//'standard_surface_greysphere.mtlx',
5555
//'standard_surface_greysphere_calibration.mtlx',
5656
'standard_surface_jade.mtlx',
5757
//'standard_surface_look_brass_tiled.mtlx',

src/nodes/materialx/lib/mx_hsv.js

Lines changed: 25 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,55 +2,60 @@
22
// https://github.com/AcademySoftwareFoundation/MaterialX/blob/main/libraries/stdlib/genglsl/lib/mx_hsv.glsl
33

44
import { int, float, vec3, If, Fn } from '../../shadernode/ShaderNode.js';
5-
import { add, sub, mul } from '../../math/OperatorNode.js';
5+
import { add } from '../../math/OperatorNode.js';
66
import { floor, trunc, max, min } from '../../math/MathNode.js';
77

8-
export const mx_hsvtorgb = /*#__PURE__*/ Fn( ( [ hsv_immutable ] ) => {
8+
export const mx_hsvtorgb = /*#__PURE__*/ Fn( ( [ hsv ] ) => {
99

10-
const hsv = vec3( hsv_immutable ).toVar();
11-
const h = float( hsv.x ).toVar();
12-
const s = float( hsv.y ).toVar();
13-
const v = float( hsv.z ).toVar();
10+
const s = hsv.y;
11+
const v = hsv.z;
12+
13+
const result = vec3().toVar();
1414

1515
If( s.lessThan( 0.0001 ), () => {
1616

17-
return vec3( v, v, v );
17+
result.assign( vec3( v, v, v ) );
1818

1919
} ).Else( () => {
2020

21-
h.assign( mul( 6.0, h.sub( floor( h ) ) ) );
22-
const hi = int( trunc( h ) ).toVar();
23-
const f = float( h.sub( float( hi ) ) ).toVar();
24-
const p = float( v.mul( sub( 1.0, s ) ) ).toVar();
25-
const q = float( v.mul( sub( 1.0, s.mul( f ) ) ) ).toVar();
26-
const t = float( v.mul( sub( 1.0, s.mul( sub( 1.0, f ) ) ) ) ).toVar();
21+
let h = hsv.x;
22+
h = h.sub( floor( h ) ).mul( 6.0 ).toVar(); // TODO: check what .toVar() is needed in node system cache
23+
const hi = int( trunc( h ) );
24+
const f = h.sub( float( hi ) );
25+
const p = v.mul( s.oneMinus() );
26+
const q = v.mul( s.mul( f ).oneMinus() );
27+
const t = v.mul( s.mul( f.oneMinus() ).oneMinus() );
2728

2829
If( hi.equal( int( 0 ) ), () => {
2930

30-
return vec3( v, t, p );
31+
result.assign( vec3( v, t, p ) );
3132

3233
} ).ElseIf( hi.equal( int( 1 ) ), () => {
3334

34-
return vec3( q, v, p );
35+
result.assign( vec3( q, v, p ) );
3536

3637
} ).ElseIf( hi.equal( int( 2 ) ), () => {
3738

38-
return vec3( p, v, t );
39+
result.assign( vec3( p, v, t ) );
3940

4041
} ).ElseIf( hi.equal( int( 3 ) ), () => {
4142

42-
return vec3( p, q, v );
43+
result.assign( vec3( p, q, v ) );
4344

4445
} ).ElseIf( hi.equal( int( 4 ) ), () => {
4546

46-
return vec3( t, p, v );
47+
result.assign( vec3( t, p, v ) );
4748

48-
} );
49+
} ).Else( () => {
4950

50-
return vec3( v, p, q );
51+
result.assign( vec3( v, p, q ) );
52+
53+
} );
5154

5255
} );
5356

57+
return result;
58+
5459
} ).setLayout( {
5560
name: 'mx_hsvtorgb',
5661
type: 'vec3',

src/renderers/webgpu/nodes/WGSLNodeBuilder.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -598,12 +598,18 @@ class WGSLNodeBuilder extends NodeBuilder {
598598

599599
//
600600

601-
const code = `fn ${ layout.name }( ${ parameters.join( ', ' ) } ) -> ${ this.getType( layout.type ) } {
601+
let code = `fn ${ layout.name }( ${ parameters.join( ', ' ) } ) -> ${ this.getType( layout.type ) } {
602602
${ flowData.vars }
603603
${ flowData.code }
604-
return ${ flowData.result };
604+
`;
605+
606+
if ( flowData.result ) {
607+
608+
code += `\treturn ${ flowData.result };\n`;
609+
610+
}
605611

606-
}`;
612+
code += '\n}\n';
607613

608614
//
609615

0 commit comments

Comments
 (0)