فهرست منبع

MaterialX: Fix `mx_hsvtorgb` and `brick_procedural` example (#29150)

* revision mx_hsvtorgb

* add optional return for function with layout

* added brick procedural

* cleanup
sunag 1 سال پیش
والد
کامیت
17d60f6de0

BIN
examples/screenshots/webgpu_loader_materialx.jpg


+ 2 - 2
examples/webgpu_loader_materialx.html

@@ -42,7 +42,7 @@
 
 			const samples = [
 				'standard_surface_brass_tiled.mtlx',
-				//'standard_surface_brick_procedural.mtlx',
+				'standard_surface_brick_procedural.mtlx',
 				'standard_surface_carpaint.mtlx',
 				//'standard_surface_chess_set.mtlx',
 				'standard_surface_chrome.mtlx',
@@ -51,7 +51,7 @@
 				//'standard_surface_glass.mtlx',
 				//'standard_surface_glass_tinted.mtlx',
 				'standard_surface_gold.mtlx',
-				'standard_surface_greysphere.mtlx',
+				//'standard_surface_greysphere.mtlx',
 				//'standard_surface_greysphere_calibration.mtlx',
 				'standard_surface_jade.mtlx',
 				//'standard_surface_look_brass_tiled.mtlx',

+ 25 - 20
src/nodes/materialx/lib/mx_hsv.js

@@ -2,55 +2,60 @@
 // https://github.com/AcademySoftwareFoundation/MaterialX/blob/main/libraries/stdlib/genglsl/lib/mx_hsv.glsl
 
 import { int, float, vec3, If, Fn } from '../../shadernode/ShaderNode.js';
-import { add, sub, mul } from '../../math/OperatorNode.js';
+import { add } from '../../math/OperatorNode.js';
 import { floor, trunc, max, min } from '../../math/MathNode.js';
 
-export const mx_hsvtorgb = /*#__PURE__*/ Fn( ( [ hsv_immutable ] ) => {
+export const mx_hsvtorgb = /*#__PURE__*/ Fn( ( [ hsv ] ) => {
 
-	const hsv = vec3( hsv_immutable ).toVar();
-	const h = float( hsv.x ).toVar();
-	const s = float( hsv.y ).toVar();
-	const v = float( hsv.z ).toVar();
+	const s = hsv.y;
+	const v = hsv.z;
+
+	const result = vec3().toVar();
 
 	If( s.lessThan( 0.0001 ), () => {
 
-		return vec3( v, v, v );
+		result.assign( vec3( v, v, v ) );
 
 	} ).Else( () => {
 
-		h.assign( mul( 6.0, h.sub( floor( h ) ) ) );
-		const hi = int( trunc( h ) ).toVar();
-		const f = float( h.sub( float( hi ) ) ).toVar();
-		const p = float( v.mul( sub( 1.0, s ) ) ).toVar();
-		const q = float( v.mul( sub( 1.0, s.mul( f ) ) ) ).toVar();
-		const t = float( v.mul( sub( 1.0, s.mul( sub( 1.0, f ) ) ) ) ).toVar();
+		let h = hsv.x;
+		h = h.sub( floor( h ) ).mul( 6.0 ).toVar(); // TODO: check what .toVar() is needed in node system cache
+		const hi = int( trunc( h ) );
+		const f = h.sub( float( hi ) );
+		const p = v.mul( s.oneMinus() );
+		const q = v.mul( s.mul( f ).oneMinus() );
+		const t = v.mul( s.mul( f.oneMinus() ).oneMinus() );
 
 		If( hi.equal( int( 0 ) ), () => {
 
-			return vec3( v, t, p );
+			result.assign( vec3( v, t, p ) );
 
 		} ).ElseIf( hi.equal( int( 1 ) ), () => {
 
-			return vec3( q, v, p );
+			result.assign( vec3( q, v, p ) );
 
 		} ).ElseIf( hi.equal( int( 2 ) ), () => {
 
-			return vec3( p, v, t );
+			result.assign( vec3( p, v, t ) );
 
 		} ).ElseIf( hi.equal( int( 3 ) ), () => {
 
-			return vec3( p, q, v );
+			result.assign( vec3( p, q, v ) );
 
 		} ).ElseIf( hi.equal( int( 4 ) ), () => {
 
-			return vec3( t, p, v );
+			result.assign( vec3( t, p, v ) );
 
-		} );
+		} ).Else( () => {
 
-		return vec3( v, p, q );
+			result.assign( vec3( v, p, q ) );
+
+		} );
 
 	} );
 
+	return result;
+
 } ).setLayout( {
 	name: 'mx_hsvtorgb',
 	type: 'vec3',

+ 9 - 3
src/renderers/webgpu/nodes/WGSLNodeBuilder.js

@@ -598,12 +598,18 @@ class WGSLNodeBuilder extends NodeBuilder {
 
 		//
 
-		const code = `fn ${ layout.name }( ${ parameters.join( ', ' ) } ) -> ${ this.getType( layout.type ) } {
+		let code = `fn ${ layout.name }( ${ parameters.join( ', ' ) } ) -> ${ this.getType( layout.type ) } {
 ${ flowData.vars }
 ${ flowData.code }
-	return ${ flowData.result };
+`;
+
+		if ( flowData.result ) {
+
+			code += `\treturn ${ flowData.result };\n`;
+
+		}
 
-}`;
+		code += '\n}\n';
 
 		//
 

粤ICP备19079148号