Browse Source

Add BoolNode and UintNode (#23636)

LeviPesin 3 years ago
parent
commit
8323901ec2

+ 4 - 0
examples/jsm/nodes/ShaderNode.js

@@ -4,9 +4,11 @@ import VarNode from './core/VarNode.js';
 import AttributeNode from './core/AttributeNode.js';
 import AttributeNode from './core/AttributeNode.js';
 
 
 // input nodes
 // input nodes
+import BoolNode from './inputs/BoolNode.js';
 import ColorNode from './inputs/ColorNode.js';
 import ColorNode from './inputs/ColorNode.js';
 import FloatNode from './inputs/FloatNode.js';
 import FloatNode from './inputs/FloatNode.js';
 import IntNode from './inputs/IntNode.js';
 import IntNode from './inputs/IntNode.js';
+import UintNode from './inputs/UintNode.js';
 import Vector2Node from './inputs/Vector2Node.js';
 import Vector2Node from './inputs/Vector2Node.js';
 import Vector3Node from './inputs/Vector3Node.js';
 import Vector3Node from './inputs/Vector3Node.js';
 import Vector4Node from './inputs/Vector4Node.js';
 import Vector4Node from './inputs/Vector4Node.js';
@@ -259,6 +261,8 @@ const ConvertType = function ( nodeClass, type, valueClass = null, valueComponen
 
 
 export const float = new ConvertType( FloatNode, 'float' );
 export const float = new ConvertType( FloatNode, 'float' );
 export const int = new ConvertType( IntNode, 'int' );
 export const int = new ConvertType( IntNode, 'int' );
+export const uint = new ConvertType( UintNode, 'uint' );
+export const bool = new ConvertType( BoolNode, 'bool' );
 export const color = new ConvertType( ColorNode, 'color', Color );
 export const color = new ConvertType( ColorNode, 'color', Color );
 
 
 export const vec2 = new ConvertType( Vector2Node, 'vec2', Vector2, 2 );
 export const vec2 = new ConvertType( Vector2Node, 'vec2', Vector2, 2 );

+ 35 - 4
examples/jsm/nodes/core/NodeBuilder.js

@@ -155,7 +155,10 @@ class NodeBuilder {
 	// rename to generate
 	// rename to generate
 	getConst( type, value ) {
 	getConst( type, value ) {
 
 
-		if ( type === 'float' ) return value + ( value % 1 ? '' : '.0' );
+		if ( type === 'float' ) return toFloat( value );
+		if ( type === 'int' ) return `${ Math.round( value ) }`;
+		if ( type === 'uint' ) return ( value >= 0 ) ? `${ Math.round( value ) }` : '0';
+		if ( type === 'bool' ) return value ? 'true' : 'false';
 		if ( type === 'vec2' ) return `${ this.getType( 'vec2' ) }( ${ toFloat( value.x ) }, ${ toFloat( value.y ) } )`;
 		if ( type === 'vec2' ) return `${ this.getType( 'vec2' ) }( ${ toFloat( value.x ) }, ${ toFloat( value.y ) } )`;
 		if ( type === 'vec3' ) return `${ this.getType( 'vec3' ) }( ${ toFloat( value.x ) }, ${ toFloat( value.y ) }, ${ toFloat( value.z ) } )`;
 		if ( type === 'vec3' ) return `${ this.getType( 'vec3' ) }( ${ toFloat( value.x ) }, ${ toFloat( value.y ) }, ${ toFloat( value.z ) } )`;
 		if ( type === 'vec4' ) return `${ this.getType( 'vec4' ) }( ${ toFloat( value.x ) }, ${ toFloat( value.y ) }, ${ toFloat( value.z ) }, ${ toFloat( value.w ) } )`;
 		if ( type === 'vec4' ) return `${ this.getType( 'vec4' ) }( ${ toFloat( value.x ) }, ${ toFloat( value.y ) }, ${ toFloat( value.z ) }, ${ toFloat( value.w ) } )`;
@@ -275,7 +278,7 @@ class NodeBuilder {
 		const vecNum = /vec([2-4])/.exec( vecType );
 		const vecNum = /vec([2-4])/.exec( vecType );
 
 
 		if ( vecNum !== null ) return Number( vecNum[ 1 ] );
 		if ( vecNum !== null ) return Number( vecNum[ 1 ] );
-		if ( vecType === 'float' || vecType === 'bool' ) return 1;
+		if ( vecType === 'float' || vecType === 'bool' || vecType === 'int' || vecType === 'uint' ) return 1;
 
 
 		return 0;
 		return 0;
 
 
@@ -600,37 +603,65 @@ class NodeBuilder {
 		switch ( typeToType ) {
 		switch ( typeToType ) {
 
 
 			case 'int to float' : return `${ this.getType( 'float' ) }( ${ snippet } )`;
 			case 'int to float' : return `${ this.getType( 'float' ) }( ${ snippet } )`;
+			case 'int to uint' : return `${ this.getType( 'uint' ) }( ${ snippet } )`;
+			case 'int to bool' : return `${ this.getType( 'bool' ) }( ${ snippet } )`;
 			case 'int to vec2' : return `${ this.getType( 'vec2' ) }( ${ this.getType( 'float' ) }( ${ snippet } ) )`;
 			case 'int to vec2' : return `${ this.getType( 'vec2' ) }( ${ this.getType( 'float' ) }( ${ snippet } ) )`;
 			case 'int to vec3' : return `${ this.getType( 'vec3' ) }( ${ this.getType( 'float' ) }( ${ snippet } ) )`;
 			case 'int to vec3' : return `${ this.getType( 'vec3' ) }( ${ this.getType( 'float' ) }( ${ snippet } ) )`;
 			case 'int to vec4' : return `${ this.getType( 'vec4' ) }( ${ this.getType( 'vec3' ) }( ${ this.getType( 'float' ) }( ${ snippet } ) ), 1.0 )`;
 			case 'int to vec4' : return `${ this.getType( 'vec4' ) }( ${ this.getType( 'vec3' ) }( ${ this.getType( 'float' ) }( ${ snippet } ) ), 1.0 )`;
 
 
+			case 'uint to float' : return `${ this.getType( 'float' ) }( ${ snippet } )`;
+			case 'uint to int' : return `${ this.getType( 'int' ) }( ${ snippet } )`;
+			case 'uint to bool' : return `${ this.getType( 'bool' ) }( ${ snippet } )`;
+			case 'uint to vec2' : return `${ this.getType( 'vec2' ) }( ${ this.getType( 'float' ) }( ${ snippet } ) )`;
+			case 'uint to vec3' : return `${ this.getType( 'vec3' ) }( ${ this.getType( 'float' ) }( ${ snippet } ) )`;
+			case 'uint to vec4' : return `${ this.getType( 'vec4' ) }( ${ this.getType( 'vec3' ) }( ${ this.getType( 'float' ) }( ${ snippet } ) ), 1.0 )`;
+
+			case 'bool to float' : return `${ this.getType( 'float' ) }( ${ snippet } )`;
+			case 'bool to int' : return `${ this.getType( 'int' ) }( ${ snippet } )`;
+			case 'bool to uint' : return `${ this.getType( 'uint' ) }( ${ snippet } )`;
+			case 'bool to vec2' : return `${ this.getType( 'vec2' ) }( ${ this.getType( 'float' ) }( ${ snippet } ) )`;
+			case 'bool to vec3' : return `${ this.getType( 'vec3' ) }( ${ this.getType( 'float' ) }( ${ snippet } ) )`;
+			case 'bool to vec4' : return `${ this.getType( 'vec4' ) }( ${ this.getType( 'vec3' ) }( ${ this.getType( 'float' ) }( ${ snippet } ) ), 1.0 )`;
+
 			case 'float to int' : return `${ this.getType( 'int' ) }( ${ snippet } )`;
 			case 'float to int' : return `${ this.getType( 'int' ) }( ${ snippet } )`;
+			case 'float to uint' : return `${ this.getType( 'uint' ) }( ${ snippet } )`;
+			case 'float to bool' : return `${ this.getType( 'bool' ) }( ${ snippet } )`;
 			case 'float to vec2' : return `${ this.getType( 'vec2' ) }( ${ snippet } )`;
 			case 'float to vec2' : return `${ this.getType( 'vec2' ) }( ${ snippet } )`;
 			case 'float to vec3' : return `${ this.getType( 'vec3' ) }( ${ snippet } )`;
 			case 'float to vec3' : return `${ this.getType( 'vec3' ) }( ${ snippet } )`;
 			case 'float to vec4' : return `${ this.getType( 'vec4' ) }( ${ this.getType( 'vec3' ) }( ${ snippet } ), 1.0 )`;
 			case 'float to vec4' : return `${ this.getType( 'vec4' ) }( ${ this.getType( 'vec3' ) }( ${ snippet } ), 1.0 )`;
 
 
 			case 'vec2 to int' : return `${ this.getType( 'int' ) }( ${ snippet }.x )`;
 			case 'vec2 to int' : return `${ this.getType( 'int' ) }( ${ snippet }.x )`;
+			case 'vec2 to uint' : return `${ this.getType( 'uint' ) }( ${ snippet }.x )`;
+			case 'vec2 to bool' : return `${ this.getType( 'bool' ) }( ${ snippet }.x )`;
 			case 'vec2 to float' : return `${ snippet }.x`;
 			case 'vec2 to float' : return `${ snippet }.x`;
 			case 'vec2 to vec3' : return `${ this.getType( 'vec3' ) }( ${ snippet }, 0.0 )`;
 			case 'vec2 to vec3' : return `${ this.getType( 'vec3' ) }( ${ snippet }, 0.0 )`;
 			case 'vec2 to vec4' : return `${ this.getType( 'vec4' ) }( ${ snippet }.xy, 0.0, 1.0 )`;
 			case 'vec2 to vec4' : return `${ this.getType( 'vec4' ) }( ${ snippet }.xy, 0.0, 1.0 )`;
 
 
 			case 'vec3 to int' : return `${ this.getType( 'int' ) }( ${ snippet }.x )`;
 			case 'vec3 to int' : return `${ this.getType( 'int' ) }( ${ snippet }.x )`;
+			case 'vec3 to uint' : return `${ this.getType( 'uint' ) }( ${ snippet }.x )`;
+			case 'vec3 to bool' : return `${ this.getType( 'bool' ) }( ${ snippet }.x )`;
 			case 'vec3 to float' : return `${ snippet }.x`;
 			case 'vec3 to float' : return `${ snippet }.x`;
 			case 'vec3 to vec2' : return `${ snippet }.xy`;
 			case 'vec3 to vec2' : return `${ snippet }.xy`;
 			case 'vec3 to vec4' : return `${ this.getType( 'vec4' ) }( ${ snippet }, 1.0 )`;
 			case 'vec3 to vec4' : return `${ this.getType( 'vec4' ) }( ${ snippet }, 1.0 )`;
 
 
 			case 'vec4 to int' : return `${ this.getType( 'int' ) }( ${ snippet }.x )`;
 			case 'vec4 to int' : return `${ this.getType( 'int' ) }( ${ snippet }.x )`;
+			case 'vec4 to uint' : return `${ this.getType( 'uint' ) }( ${ snippet }.x )`;
+			case 'vec4 to bool' : return `${ this.getType( 'bool' ) }( ${ snippet }.x )`;
 			case 'vec4 to float' : return `${ snippet }.x`;
 			case 'vec4 to float' : return `${ snippet }.x`;
 			case 'vec4 to vec2' : return `${ snippet }.xy`;
 			case 'vec4 to vec2' : return `${ snippet }.xy`;
 			case 'vec4 to vec3' : return `${ snippet }.xyz`;
 			case 'vec4 to vec3' : return `${ snippet }.xyz`;
 
 
-			case 'mat3 to int' : return `${ this.getType( 'int' ) }( ${ snippet } * ${ this.getType( 'vec3' ) }( 1.0 ) ).x`;
+			case 'mat3 to int' : return `${ this.getType( 'int' ) }( ( ${ snippet } * ${ this.getType( 'vec3' ) }( 1.0 ) ).x )`;
+			case 'mat3 to uint' : return `${ this.getType( 'uint' ) }( ( ${ snippet } * ${ this.getType( 'vec3' ) }( 1.0 ) ).x )`;
+			case 'mat3 to bool' : return `${ this.getType( 'bool' ) }( ( ${ snippet } * ${ this.getType( 'vec3' ) }( 1.0 ) ).x )`;
 			case 'mat3 to float' : return `( ${ snippet } * ${ this.getType( 'vec3' ) }( 1.0 ) ).x`;
 			case 'mat3 to float' : return `( ${ snippet } * ${ this.getType( 'vec3' ) }( 1.0 ) ).x`;
 			case 'mat3 to vec2' : return `( ${ snippet } * ${ this.getType( 'vec3' ) }( 1.0 ) ).xy`;
 			case 'mat3 to vec2' : return `( ${ snippet } * ${ this.getType( 'vec3' ) }( 1.0 ) ).xy`;
 			case 'mat3 to vec3' : return `( ${ snippet } * ${ this.getType( 'vec3' ) }( 1.0 ) ).xyz`;
 			case 'mat3 to vec3' : return `( ${ snippet } * ${ this.getType( 'vec3' ) }( 1.0 ) ).xyz`;
 			case 'mat3 to vec4' : return `${ this.getType( 'vec4' ) }( ${ snippet } * ${ this.getType( 'vec3' ) }( 1.0 ), 1.0 )`;
 			case 'mat3 to vec4' : return `${ this.getType( 'vec4' ) }( ${ snippet } * ${ this.getType( 'vec3' ) }( 1.0 ), 1.0 )`;
 
 
-			case 'mat4 to int' : return `${ this.getType( 'int' ) }( ${ snippet } * ${ this.getType( 'vec4' ) }( 1.0 ) ).x`;
+			case 'mat4 to int' : return `${ this.getType( 'int' ) }( ( ${ snippet } * ${ this.getType( 'vec4' ) }( 1.0 ) ).x )`;
+			case 'mat4 to uint' : return `${ this.getType( 'uint' ) }( ( ${ snippet } * ${ this.getType( 'vec4' ) }( 1.0 ) ).x )`;
+			case 'mat4 to bool' : return `${ this.getType( 'bool' ) }( ( ${ snippet } * ${ this.getType( 'vec4' ) }( 1.0 ) ).x )`;
 			case 'mat4 to float' : return `( ${ snippet } * ${ this.getType( 'vec4' ) }( 1.0 ) ).x`;
 			case 'mat4 to float' : return `( ${ snippet } * ${ this.getType( 'vec4' ) }( 1.0 ) ).x`;
 			case 'mat4 to vec2' : return `( ${ snippet } * ${ this.getType( 'vec4' ) }( 1.0 ) ).xy`;
 			case 'mat4 to vec2' : return `( ${ snippet } * ${ this.getType( 'vec4' ) }( 1.0 ) ).xy`;
 			case 'mat4 to vec3' : return `( ${ snippet } * ${ this.getType( 'vec4' ) }( 1.0 ) ).xyz`;
 			case 'mat4 to vec3' : return `( ${ snippet } * ${ this.getType( 'vec4' ) }( 1.0 ) ).xyz`;

+ 33 - 0
examples/jsm/nodes/inputs/BoolNode.js

@@ -0,0 +1,33 @@
+import InputNode from '../core/InputNode.js';
+
+class BoolNode extends InputNode {
+
+	constructor( value = false ) {
+
+		super( 'bool' );
+
+		this.value = value;
+
+	}
+
+	serialize( data ) {
+
+		super.serialize( data );
+
+		data.value = this.value;
+
+	}
+
+	deserialize( data ) {
+
+		super.serialize( data );
+
+		this.value = data.value;
+
+	}
+
+}
+
+BoolNode.prototype.isBoolNode = true;
+
+export default BoolNode;

+ 33 - 0
examples/jsm/nodes/inputs/UintNode.js

@@ -0,0 +1,33 @@
+import InputNode from '../core/InputNode.js';
+
+class UintNode extends InputNode {
+
+	constructor( value = 0 ) {
+
+		super( 'uint' );
+
+		this.value = value;
+
+	}
+
+	serialize( data ) {
+
+		super.serialize( data );
+
+		data.value = this.value;
+
+	}
+
+	deserialize( data ) {
+
+		super.serialize( data );
+
+		this.value = data.value;
+
+	}
+
+}
+
+UintNode.prototype.isUintNode = true;
+
+export default UintNode;

+ 2 - 0
examples/jsm/renderers/webgpu/nodes/WebGPUNodeBuilder.js

@@ -31,6 +31,8 @@ import { getRoughness } from 'three-nodes/functions/PhysicalMaterialFunctions.js
 const wgslTypeLib = {
 const wgslTypeLib = {
 	float: 'f32',
 	float: 'f32',
 	int: 'i32',
 	int: 'i32',
+	uint: 'u32',
+	bool: 'bool',
 	vec2: 'vec2<f32>',
 	vec2: 'vec2<f32>',
 	vec3: 'vec3<f32>',
 	vec3: 'vec3<f32>',
 	vec4: 'vec4<f32>',
 	vec4: 'vec4<f32>',