Browse Source

new mask slot and dissolve example

sunag 6 years ago
parent
commit
590b8bbe42

+ 1 - 0
examples/js/nodes/materials/PhongNodeMaterial.js

@@ -32,6 +32,7 @@ NodeUtils.addShortcuts( PhongNodeMaterial.prototype, 'fragment', [
 	'ao',
 	'ao',
 	'environment',
 	'environment',
 	'environmentAlpha',
 	'environmentAlpha',
+	'mask',
 	'position'
 	'position'
 ] );
 ] );
 
 

+ 1 - 0
examples/js/nodes/materials/SpriteNodeMaterial.js

@@ -22,6 +22,7 @@ SpriteNodeMaterial.prototype.constructor = SpriteNodeMaterial;
 NodeUtils.addShortcuts( SpriteNodeMaterial.prototype, 'fragment', [
 NodeUtils.addShortcuts( SpriteNodeMaterial.prototype, 'fragment', [
 	'color',
 	'color',
 	'alpha',
 	'alpha',
+	'mask',
 	'position',
 	'position',
 	'spherical'
 	'spherical'
 ] );
 ] );

+ 1 - 0
examples/js/nodes/materials/StandardNodeMaterial.js

@@ -34,6 +34,7 @@ NodeUtils.addShortcuts( StandardNodeMaterial.prototype, 'fragment', [
 	'shadow',
 	'shadow',
 	'ao',
 	'ao',
 	'environment',
 	'environment',
+	'mask',
 	'position'
 	'position'
 ] );
 ] );
 
 

+ 21 - 2
examples/js/nodes/materials/nodes/PhongNode.js

@@ -103,6 +103,8 @@ PhongNode.prototype.build = function ( builder ) {
 
 
 		// parse all nodes to reuse generate codes
 		// parse all nodes to reuse generate codes
 
 
+		if ( this.mask ) this.mask.parse( builder );
+
 		this.color.parse( builder, { slot: 'color' } );
 		this.color.parse( builder, { slot: 'color' } );
 		this.specular.parse( builder );
 		this.specular.parse( builder );
 		this.shininess.parse( builder );
 		this.shininess.parse( builder );
@@ -123,6 +125,8 @@ PhongNode.prototype.build = function ( builder ) {
 
 
 		// build code
 		// build code
 
 
+		var mask = this.mask ? this.mask.buildCode( builder, 'b' ) : undefined;
+
 		var color = this.color.buildCode( builder, 'c', { slot: 'color' } );
 		var color = this.color.buildCode( builder, 'c', { slot: 'color' } );
 		var specular = this.specular.buildCode( builder, 'c' );
 		var specular = this.specular.buildCode( builder, 'c' );
 		var shininess = this.shininess.buildCode( builder, 'f' );
 		var shininess = this.shininess.buildCode( builder, 'f' );
@@ -157,8 +161,19 @@ PhongNode.prototype.build = function ( builder ) {
 			"#include <normal_fragment_begin>",
 			"#include <normal_fragment_begin>",
 
 
 			// prevent undeclared material
 			// prevent undeclared material
-			"	BlinnPhongMaterial material;",
+			"	BlinnPhongMaterial material;"
+		];
+
+		if ( mask ) {
+
+			output.push(
+				mask.code,
+				'if ( ' + mask.result + ' ) discard;'
+			);
 
 
+		}
+
+		output.push(
 			color.code,
 			color.code,
 			"	vec3 diffuseColor = " + color.result + ";",
 			"	vec3 diffuseColor = " + color.result + ";",
 			"	ReflectedLight reflectedLight = ReflectedLight( vec3( 0.0 ), vec3( 0.0 ), vec3( 0.0 ), vec3( 0.0 ) );",
 			"	ReflectedLight reflectedLight = ReflectedLight( vec3( 0.0 ), vec3( 0.0 ), vec3( 0.0 ), vec3( 0.0 ) );",
@@ -172,7 +187,7 @@ PhongNode.prototype.build = function ( builder ) {
 			"	float shininess = max( 0.0001, " + shininess.result + " );",
 			"	float shininess = max( 0.0001, " + shininess.result + " );",
 
 
 			"	float specularStrength = 1.0;" // Ignored in MaterialNode ( replace to specular )
 			"	float specularStrength = 1.0;" // Ignored in MaterialNode ( replace to specular )
-		];
+		);
 
 
 		if ( alpha ) {
 		if ( alpha ) {
 
 
@@ -335,6 +350,8 @@ PhongNode.prototype.copy = function ( source ) {
 	this.specular = source.specular;
 	this.specular = source.specular;
 	this.shininess = source.shininess;
 	this.shininess = source.shininess;
 
 
+	if ( source.mask ) this.mask = source.mask;
+
 	if ( source.alpha ) this.alpha = source.alpha;
 	if ( source.alpha ) this.alpha = source.alpha;
 
 
 	if ( source.normal ) this.normal = source.normal;
 	if ( source.normal ) this.normal = source.normal;
@@ -370,6 +387,8 @@ PhongNode.prototype.toJSON = function ( meta ) {
 		data.specular = this.specular.toJSON( meta ).uuid;
 		data.specular = this.specular.toJSON( meta ).uuid;
 		data.shininess = this.shininess.toJSON( meta ).uuid;
 		data.shininess = this.shininess.toJSON( meta ).uuid;
 
 
+		if ( this.mask ) data.mask = this.mask.toJSON( meta ).uuid;
+
 		if ( this.alpha ) data.alpha = this.alpha.toJSON( meta ).uuid;
 		if ( this.alpha ) data.alpha = this.alpha.toJSON( meta ).uuid;
 
 
 		if ( this.normal ) data.normal = this.normal.toJSON( meta ).uuid;
 		if ( this.normal ) data.normal = this.normal.toJSON( meta ).uuid;

+ 23 - 6
examples/js/nodes/materials/nodes/SpriteNode.js

@@ -123,18 +123,31 @@ SpriteNode.prototype.build = function ( builder ) {
 
 
 		// parse all nodes to reuse generate codes
 		// parse all nodes to reuse generate codes
 
 
+		if ( this.mask ) this.mask.parse( builder );
+
 		if ( this.alpha ) this.alpha.parse( builder );
 		if ( this.alpha ) this.alpha.parse( builder );
 
 
 		this.color.parse( builder, { slot: 'color' } );
 		this.color.parse( builder, { slot: 'color' } );
 
 
 		// build code
 		// build code
 
 
-		var alpha = this.alpha ? this.alpha.buildCode( builder, 'f' ) : undefined,
-			color = this.color.buildCode( builder, 'c', { slot: 'color' } );
+		var mask = this.mask ? this.mask.buildCode( builder, 'b' ) : undefined,
+			alpha = this.alpha ? this.alpha.buildCode( builder, 'f' ) : undefined,
+			color = this.color.buildCode( builder, 'c', { slot: 'color' } ),
+			output = [];
+
+		if ( mask ) {
+
+			output.push(
+				mask.code,
+				'if ( ' + mask.result + ' ) discard;'
+			);
+
+		}
 
 
 		if ( alpha ) {
 		if ( alpha ) {
 
 
-			output = [
+			output.push(
 				alpha.code,
 				alpha.code,
 				'#ifdef ALPHATEST',
 				'#ifdef ALPHATEST',
 
 
@@ -143,14 +156,14 @@ SpriteNode.prototype.build = function ( builder ) {
 				'#endif',
 				'#endif',
 				color.code,
 				color.code,
 				"gl_FragColor = vec4( " + color.result + ", " + alpha.result + " );"
 				"gl_FragColor = vec4( " + color.result + ", " + alpha.result + " );"
-			];
+			);
 
 
 		} else {
 		} else {
 
 
-			output = [
+			output.push(
 				color.code,
 				color.code,
 				"gl_FragColor = vec4( " + color.result + ", 1.0 );"
 				"gl_FragColor = vec4( " + color.result + ", 1.0 );"
-			];
+			);
 
 
 		}
 		}
 
 
@@ -180,6 +193,8 @@ SpriteNode.prototype.copy = function ( source ) {
 
 
 	if ( source.spherical !== undefined ) this.spherical = source.spherical;
 	if ( source.spherical !== undefined ) this.spherical = source.spherical;
 
 
+	if ( source.mask ) this.mask = source.mask;
+
 	if ( source.alpha ) this.alpha = source.alpha;
 	if ( source.alpha ) this.alpha = source.alpha;
 
 
 };
 };
@@ -202,6 +217,8 @@ SpriteNode.prototype.toJSON = function ( meta ) {
 
 
 		if ( this.spherical === false ) data.spherical = false;
 		if ( this.spherical === false ) data.spherical = false;
 
 
+		if ( this.mask ) data.mask = this.mask.toJSON( meta ).uuid;
+
 		if ( this.alpha ) data.alpha = this.alpha.toJSON( meta ).uuid;
 		if ( this.alpha ) data.alpha = this.alpha.toJSON( meta ).uuid;
 
 
 	}
 	}

+ 22 - 3
examples/js/nodes/materials/nodes/StandardNode.js

@@ -117,6 +117,8 @@ StandardNode.prototype.build = function ( builder ) {
 
 
 		// parse all nodes to reuse generate codes
 		// parse all nodes to reuse generate codes
 
 
+		if ( this.mask ) this.mask.parse( builder );
+
 		this.color.parse( builder, { slot: 'color', context: contextGammaOnly } );
 		this.color.parse( builder, { slot: 'color', context: contextGammaOnly } );
 		this.roughness.parse( builder );
 		this.roughness.parse( builder );
 		this.metalness.parse( builder );
 		this.metalness.parse( builder );
@@ -141,6 +143,8 @@ StandardNode.prototype.build = function ( builder ) {
 
 
 		// build code
 		// build code
 
 
+		var mask = this.mask ? this.mask.buildCode( builder, 'f' ) : undefined;
+
 		var color = this.color.buildCode( builder, 'c', { slot: 'color', context: contextGammaOnly } );
 		var color = this.color.buildCode( builder, 'c', { slot: 'color', context: contextGammaOnly } );
 		var roughness = this.roughness.buildCode( builder, 'f' );
 		var roughness = this.roughness.buildCode( builder, 'f' );
 		var metalness = this.metalness.buildCode( builder, 'f' );
 		var metalness = this.metalness.buildCode( builder, 'f' );
@@ -194,8 +198,19 @@ StandardNode.prototype.build = function ( builder ) {
 
 
 			// add before: prevent undeclared material
 			// add before: prevent undeclared material
 			"	PhysicalMaterial material;",
 			"	PhysicalMaterial material;",
-			"	material.diffuseColor = vec3( 1.0 );",
+			"	material.diffuseColor = vec3( 1.0 );"
+		];
+
+		if ( mask ) {
+
+			output.push(
+				mask.code,
+				'if ( ' + mask.result + ' ) discard;'
+			);
 
 
+		}
+
+		output.push(
 			color.code,
 			color.code,
 			"	vec3 diffuseColor = " + color.result + ";",
 			"	vec3 diffuseColor = " + color.result + ";",
 			"	ReflectedLight reflectedLight = ReflectedLight( vec3( 0.0 ), vec3( 0.0 ), vec3( 0.0 ), vec3( 0.0 ) );",
 			"	ReflectedLight reflectedLight = ReflectedLight( vec3( 0.0 ), vec3( 0.0 ), vec3( 0.0 ), vec3( 0.0 ) );",
@@ -207,7 +222,7 @@ StandardNode.prototype.build = function ( builder ) {
 
 
 			metalness.code,
 			metalness.code,
 			"	float metalnessFactor = " + metalness.result + ";"
 			"	float metalnessFactor = " + metalness.result + ";"
-		];
+		);
 
 
 		if ( alpha ) {
 		if ( alpha ) {
 
 
@@ -215,7 +230,7 @@ StandardNode.prototype.build = function ( builder ) {
 				alpha.code,
 				alpha.code,
 				'#ifdef ALPHATEST',
 				'#ifdef ALPHATEST',
 
 
-				'if ( ' + alpha.result + ' <= ALPHATEST ) discard;',
+				'	if ( ' + alpha.result + ' <= ALPHATEST ) discard;',
 
 
 				'#endif'
 				'#endif'
 			);
 			);
@@ -403,6 +418,8 @@ StandardNode.prototype.copy = function ( source ) {
 	this.roughness = source.roughness;
 	this.roughness = source.roughness;
 	this.metalness = source.metalness;
 	this.metalness = source.metalness;
 
 
+	if ( source.mask ) this.mask = source.mask;
+
 	if ( source.alpha ) this.alpha = source.alpha;
 	if ( source.alpha ) this.alpha = source.alpha;
 
 
 	if ( source.normal ) this.normal = source.normal;
 	if ( source.normal ) this.normal = source.normal;
@@ -442,6 +459,8 @@ StandardNode.prototype.toJSON = function ( meta ) {
 		data.roughness = this.roughness.toJSON( meta ).uuid;
 		data.roughness = this.roughness.toJSON( meta ).uuid;
 		data.metalness = this.metalness.toJSON( meta ).uuid;
 		data.metalness = this.metalness.toJSON( meta ).uuid;
 
 
+		if ( this.mask ) data.mask = this.mask.toJSON( meta ).uuid;
+
 		if ( this.alpha ) data.alpha = this.alpha.toJSON( meta ).uuid;
 		if ( this.alpha ) data.alpha = this.alpha.toJSON( meta ).uuid;
 
 
 		if ( this.normal ) data.normal = this.normal.toJSON( meta ).uuid;
 		if ( this.normal ) data.normal = this.normal.toJSON( meta ).uuid;

+ 35 - 16
examples/js/nodes/math/CondNode.js

@@ -4,18 +4,18 @@
 
 
 import { TempNode } from '../core/TempNode.js';
 import { TempNode } from '../core/TempNode.js';
 
 
-function CondNode( a, b, ifNode, elseNode, op ) {
+function CondNode( a, b, op, ifNode, elseNode ) {
 
 
 	TempNode.call( this );
 	TempNode.call( this );
 
 
 	this.a = a;
 	this.a = a;
 	this.b = b;
 	this.b = b;
 
 
+	this.op = op;
+	
 	this.ifNode = ifNode;
 	this.ifNode = ifNode;
 	this.elseNode = elseNode;
 	this.elseNode = elseNode;
 
 
-	this.op = op;
-
 }
 }
 
 
 CondNode.EQUAL = '==';
 CondNode.EQUAL = '==';
@@ -31,13 +31,22 @@ CondNode.prototype.nodeType = "Cond";
 
 
 CondNode.prototype.getType = function ( builder ) {
 CondNode.prototype.getType = function ( builder ) {
 
 
-	if ( builder.getTypeLength( this.elseNode.getType( builder ) ) > builder.getTypeLength( this.ifNode.getType( builder ) ) ) {
+	if (this.ifNode) {
+		
+		var ifType = this.ifNode.getType( builder );
+		var elseType = this.elseNode.getType( builder );
+		
+		if ( builder.getTypeLength( elseType ) > builder.getTypeLength( ifType ) ) {
 
 
-		return this.elseNode.getType( builder );
+			return elseType;
 
 
-	}
+		}
 
 
-	return this.ifNode.getType( builder );
+		return ifType;
+		
+	}
+	
+	return 'b';
 
 
 };
 };
 
 
@@ -59,10 +68,20 @@ CondNode.prototype.generate = function ( builder, output ) {
 		condType = this.getCondType( builder ),
 		condType = this.getCondType( builder ),
 		a = this.a.build( builder, condType ),
 		a = this.a.build( builder, condType ),
 		b = this.b.build( builder, condType ),
 		b = this.b.build( builder, condType ),
-		ifNode = this.ifNode.build( builder, type ),
-		elseNode = this.elseNode.build( builder, type );
-
-	var code = '( ' + [ a, this.op, b, '?', ifNode, ':', elseNode ].join( ' ' ) + ' )';
+		code;
+		
+	if (this.ifNode) {
+		
+		var ifCode = this.ifNode.build( builder, type ),
+			elseCode = this.elseNode.build( builder, type );
+		
+		code = '( ' + [ a, this.op, b, '?', ifCode, ':', elseCode ].join( ' ' ) + ' )';
+		
+	} else {
+
+		code = '( ' + a + ' ' + this.op + ' ' +  b  + ' )';
+		
+	}
 
 
 	return builder.format( code, this.getType( builder ), output );
 	return builder.format( code, this.getType( builder ), output );
 
 
@@ -75,11 +94,11 @@ CondNode.prototype.copy = function ( source ) {
 	this.a = source.a;
 	this.a = source.a;
 	this.b = source.b;
 	this.b = source.b;
 
 
+	this.op = source.op;
+
 	this.ifNode = source.ifNode;
 	this.ifNode = source.ifNode;
 	this.elseNode = source.elseNode;
 	this.elseNode = source.elseNode;
 
 
-	this.op = source.op;
-
 };
 };
 
 
 CondNode.prototype.toJSON = function ( meta ) {
 CondNode.prototype.toJSON = function ( meta ) {
@@ -93,11 +112,11 @@ CondNode.prototype.toJSON = function ( meta ) {
 		data.a = this.a.toJSON( meta ).uuid;
 		data.a = this.a.toJSON( meta ).uuid;
 		data.b = this.b.toJSON( meta ).uuid;
 		data.b = this.b.toJSON( meta ).uuid;
 
 
-		data.ifNode = this.ifNode.toJSON( meta ).uuid;
-		data.elseNode = this.elseNode.toJSON( meta ).uuid;
-
 		data.op = this.op;
 		data.op = this.op;
 
 
+		if ( data.ifNode ) data.ifNode = this.ifNode.toJSON( meta ).uuid;
+		if ( data.elseNode ) data.elseNode = this.elseNode.toJSON( meta ).uuid;
+
 	}
 	}
 
 
 	return data;
 	return data;

+ 70 - 1
examples/webgl_materials_nodes.html

@@ -180,6 +180,7 @@
 					'adv / skin-phong': 'skin-phong',
 					'adv / skin-phong': 'skin-phong',
 					'adv / caustic': 'caustic',
 					'adv / caustic': 'caustic',
 					'adv / displace': 'displace',
 					'adv / displace': 'displace',
+					'adv / dissolve': 'dissolve',
 					'adv / plush': 'plush',
 					'adv / plush': 'plush',
 					'adv / toon': 'toon',
 					'adv / toon': 'toon',
 					'adv / camera-depth': 'camera-depth',
 					'adv / camera-depth': 'camera-depth',
@@ -1321,6 +1322,74 @@
 
 
 						break;
 						break;
 
 
+						
+					case 'dissolve':
+
+						// MATERIAL
+
+						mtl = new THREE.StandardNodeMaterial();
+
+						var color = new THREE.ColorNode( 0xEEEEEE );
+						var borderColor = new THREE.ColorNode( 0x0054df );
+						var threshold = new THREE.FloatNode( .1 );
+						var borderSize = new THREE.FloatNode( .2 );
+
+						// animate uv
+
+						var tex = new THREE.TextureNode( getTexture( "cloud" ) );
+						var texArea = new THREE.SwitchNode( tex, 'w' );
+
+						var thresholdBorder = new THREE.Math3Node(
+							new THREE.OperatorNode( threshold, borderSize, THREE.OperatorNode.ADD ),
+							threshold,
+							texArea,
+							THREE.Math3Node.SMOOTHSTEP
+						);
+
+						var thresholdEmissive = new THREE.OperatorNode(
+							borderColor,
+							thresholdBorder,
+							THREE.OperatorNode.MUL
+						);
+
+						// APPLY
+
+						mtl.color = color;
+						mtl.emissive = thresholdEmissive;
+						mtl.mask = new THREE.CondNode(
+							texArea, // a: value
+							threshold, // b: value
+							THREE.CondNode.LESS // condition
+						);
+
+						// GUI
+
+						addGui( 'threshold', threshold.value, function ( val ) {
+
+							threshold.value = val;
+
+						}, false, -.3, 1.3 );
+
+						addGui( 'borderSize', borderSize.value, function ( val ) {
+
+							borderSize.value = val;
+
+						}, false, 0, .5 );
+
+						addGui( 'color', color.value.getHex(), function ( val ) {
+
+							color.value.setHex( val );
+
+						}, true );
+
+						addGui( 'borderColor', borderColor.value.getHex(), function ( val ) {
+
+							borderColor.value.setHex( val );
+
+						}, true );
+
+						break;
+						
 					case 'smoke':
 					case 'smoke':
 
 
 						// MATERIAL
 						// MATERIAL
@@ -2172,7 +2241,7 @@
 							ifNode = new THREE.ColorNode( 0x0000FF ),
 							ifNode = new THREE.ColorNode( 0x0000FF ),
 							elseNode = new THREE.ColorNode( 0xFF0000 );
 							elseNode = new THREE.ColorNode( 0xFF0000 );
 
 
-						var cond = new THREE.CondNode( a, b, ifNode, elseNode, THREE.CondNode.EQUAL );
+						var cond = new THREE.CondNode( a, b, THREE.CondNode.EQUAL, ifNode, elseNode );
 
 
 						mtl.color = cond;
 						mtl.color = cond;