ソースを参照

InputNode readonly option

sunag 7 年 前
コミット
dec1707a4e

+ 7 - 0
examples/js/loaders/NodeMaterialLoader.js

@@ -125,8 +125,15 @@ Object.assign( THREE.NodeMaterialLoader.prototype, {
 
 				this.names[ object.name ] = object;
 
+			} else {
+
+				// ignore "uniform" shader input ( for optimization )
+				object.readonly = true;
+
 			}
 
+			if ( node.readonly !== undefined ) object.readonly = node.readonly;
+
 			this.nodes[ uuid ] = object;
 
 		}

+ 22 - 11
examples/js/nodes/InputNode.js

@@ -9,6 +9,8 @@ THREE.InputNode = function ( type, params ) {
 
 	THREE.TempNode.call( this, type, params );
 
+	this.readonly = false;
+
 };
 
 THREE.InputNode.prototype = Object.create( THREE.TempNode.prototype );
@@ -21,27 +23,36 @@ THREE.InputNode.prototype.generate = function ( builder, output, uuid, type, ns,
 	uuid = builder.getUuid( uuid || this.getUuid() );
 	type = type || this.getType( builder );
 
-	var data = material.getDataNode( uuid );
+	var data = material.getDataNode( uuid ),
+		readonly = this.readonly && this.generateReadonly !== undefined;
 
-	if ( builder.isShader( 'vertex' ) ) {
+	if ( readonly ) {
 
-		if ( ! data.vertex ) {
+		return this.generateReadonly( builder, output, uuid, type, ns, needsUpdate );
 
-			data.vertex = material.createVertexUniform( type, this.value, ns, needsUpdate );
+	} else {
 
-		}
+		if ( builder.isShader( 'vertex' ) ) {
 
-		return builder.format( data.vertex.name, type, output );
+			if ( ! data.vertex ) {
 
-	} else {
+				data.vertex = material.createVertexUniform( type, this.value, ns, needsUpdate );
 
-		if ( ! data.fragment ) {
+			}
 
-			data.fragment = material.createFragmentUniform( type, this.value, ns, needsUpdate );
+			return builder.format( data.vertex.name, type, output );
 
-		}
+		} else {
 
-		return builder.format( data.fragment.name, type, output );
+			if ( ! data.fragment ) {
+
+				data.fragment = material.createFragmentUniform( type, this.value, ns, needsUpdate );
+
+			}
+
+			return builder.format( data.fragment.name, type, output );
+
+		}
 
 	}
 

+ 8 - 0
examples/js/nodes/inputs/ColorNode.js

@@ -16,6 +16,12 @@ THREE.ColorNode.prototype.nodeType = "Color";
 
 THREE.NodeMaterial.addShortcuts( THREE.ColorNode.prototype, 'value', [ 'r', 'g', 'b' ] );
 
+THREE.ColorNode.prototype.generateReadonly = function ( builder, output, uuid, type, ns, needsUpdate ) {
+
+	return builder.format( "vec3( " + this.r + ", " + this.g + ", " + this.b + " )", type, output );
+
+};
+
 THREE.ColorNode.prototype.toJSON = function ( meta ) {
 
 	var data = this.getJSONNode( meta );
@@ -28,6 +34,8 @@ THREE.ColorNode.prototype.toJSON = function ( meta ) {
 		data.g = this.g;
 		data.b = this.b;
 
+		if ( this.readyonly === true ) data.readyonly = true;
+
 	}
 
 	return data;

+ 10 - 0
examples/js/nodes/inputs/FloatNode.js

@@ -29,6 +29,14 @@ Object.defineProperties( THREE.FloatNode.prototype, {
 	}
 } );
 
+THREE.FloatNode.prototype.generateReadonly = function ( builder, output, uuid, type, ns, needsUpdate ) {
+
+	var value = this.number;
+
+	return builder.format( ~~value !== value ? value : value + ".0", type, output );
+
+};
+
 THREE.FloatNode.prototype.toJSON = function ( meta ) {
 
 	var data = this.getJSONNode( meta );
@@ -39,6 +47,8 @@ THREE.FloatNode.prototype.toJSON = function ( meta ) {
 
 		data.number = this.number;
 
+		if ( this.readyonly === true ) data.readyonly = true;
+
 	}
 
 	return data;

+ 8 - 0
examples/js/nodes/inputs/IntNode.js

@@ -29,6 +29,12 @@ Object.defineProperties( THREE.IntNode.prototype, {
 	}
 } );
 
+THREE.IntNode.prototype.generateReadonly = function ( builder, output, uuid, type, ns, needsUpdate ) {
+
+	return builder.format( this.number, type, output );
+
+};
+
 THREE.IntNode.prototype.toJSON = function ( meta ) {
 
 	var data = this.getJSONNode( meta );
@@ -39,6 +45,8 @@ THREE.IntNode.prototype.toJSON = function ( meta ) {
 
 		data.number = this.number;
 
+		if ( this.readyonly === true ) data.readyonly = true;
+
 	}
 
 	return data;

+ 8 - 0
examples/js/nodes/inputs/Matrix3Node.js

@@ -14,6 +14,12 @@ THREE.Matrix3Node.prototype = Object.create( THREE.InputNode.prototype );
 THREE.Matrix3Node.prototype.constructor = THREE.Matrix3Node;
 THREE.Matrix3Node.prototype.nodeType = "Matrix3";
 
+THREE.Matrix3Node.prototype.generateReadonly = function ( builder, output, uuid, type, ns, needsUpdate ) {
+
+	return builder.format( "mat3( " + this.value.elements.joint( ", " ) + " )", type, output );
+
+};
+
 THREE.Matrix3Node.prototype.toJSON = function ( meta ) {
 
 	var data = this.getJSONNode( meta );
@@ -24,6 +30,8 @@ THREE.Matrix3Node.prototype.toJSON = function ( meta ) {
 
 		data.elements = this.value.elements.concat();
 
+		if ( this.readyonly === true ) data.readyonly = true;
+
 	}
 
 	return data;

+ 8 - 0
examples/js/nodes/inputs/Matrix4Node.js

@@ -14,6 +14,12 @@ THREE.Matrix4Node.prototype = Object.create( THREE.InputNode.prototype );
 THREE.Matrix4Node.prototype.constructor = THREE.Matrix4Node;
 THREE.Matrix4Node.prototype.nodeType = "Matrix4";
 
+THREE.Matrix4Node.prototype.generateReadonly = function ( builder, output, uuid, type, ns, needsUpdate ) {
+
+	return builder.format( "mat4( " + this.value.elements.joint( ", " ) + " )", type, output );
+
+};
+
 THREE.Matrix4Node.prototype.toJSON = function ( meta ) {
 
 	var data = this.getJSONNode( meta );
@@ -24,6 +30,8 @@ THREE.Matrix4Node.prototype.toJSON = function ( meta ) {
 
 		data.elements = this.value.elements.concat();
 
+		if ( this.readyonly === true ) data.readyonly = true;
+
 	}
 
 	return data;

+ 8 - 0
examples/js/nodes/inputs/Vector2Node.js

@@ -16,6 +16,12 @@ THREE.Vector2Node.prototype.nodeType = "Vector2";
 
 THREE.NodeMaterial.addShortcuts( THREE.Vector2Node.prototype, 'value', [ 'x', 'y' ] );
 
+THREE.Vector2Node.prototype.generateReadonly = function ( builder, output, uuid, type, ns, needsUpdate ) {
+
+	return builder.format( "vec2( " + this.x + ", " + this.y + " )", type, output );
+
+};
+
 THREE.Vector2Node.prototype.toJSON = function ( meta ) {
 
 	var data = this.getJSONNode( meta );
@@ -27,6 +33,8 @@ THREE.Vector2Node.prototype.toJSON = function ( meta ) {
 		data.x = this.x;
 		data.y = this.y;
 
+		if ( this.readyonly === true ) data.readyonly = true;
+
 	}
 
 	return data;

+ 8 - 0
examples/js/nodes/inputs/Vector3Node.js

@@ -17,6 +17,12 @@ THREE.Vector3Node.prototype.nodeType = "Vector3";
 
 THREE.NodeMaterial.addShortcuts( THREE.Vector3Node.prototype, 'value', [ 'x', 'y', 'z' ] );
 
+THREE.Vector3Node.prototype.generateReadonly = function ( builder, output, uuid, type, ns, needsUpdate ) {
+
+	return builder.format( "vec3( " + this.x + ", " + this.y + ", " + this.z + " )", type, output );
+
+};
+
 THREE.Vector3Node.prototype.toJSON = function ( meta ) {
 
 	var data = this.getJSONNode( meta );
@@ -29,6 +35,8 @@ THREE.Vector3Node.prototype.toJSON = function ( meta ) {
 		data.y = this.y;
 		data.z = this.z;
 
+		if ( this.readyonly === true ) data.readyonly = true;
+
 	}
 
 	return data;

+ 8 - 0
examples/js/nodes/inputs/Vector4Node.js

@@ -16,6 +16,12 @@ THREE.Vector4Node.prototype.nodeType = "Vector4";
 
 THREE.NodeMaterial.addShortcuts( THREE.Vector4Node.prototype, 'value', [ 'x', 'y', 'z', 'w' ] );
 
+THREE.Vector4Node.prototype.generateReadonly = function ( builder, output, uuid, type, ns, needsUpdate ) {
+
+	return builder.format( "vec4( " + this.x + ", " + this.y + ", " + this.z + ", " + this.w + " )", type, output );
+
+};
+
 THREE.Vector4Node.prototype.toJSON = function ( meta ) {
 
 	var data = this.getJSONNode( meta );
@@ -29,6 +35,8 @@ THREE.Vector4Node.prototype.toJSON = function ( meta ) {
 		data.z = this.z;
 		data.w = this.w;
 
+		if ( this.readyonly === true ) data.readyonly = true;
+
 	}
 
 	return data;

+ 17 - 0
examples/webgl_materials_nodes.html

@@ -256,6 +256,7 @@
 				'misc / firefly': 'firefly',
 				'misc / reserved-keywords': 'reserved-keywords',
 				'misc / varying': 'varying',
+				'misc / readonly': 'readonly',
 				'misc / custom-attribute': 'custom-attribute'
 			} ).onFinishChange( function () {
 
@@ -2019,6 +2020,22 @@
 
 					break;
 
+				case 'readonly':
+
+					mtl = new THREE.PhongNodeMaterial();
+
+					mtl.color = new THREE.ColorNode( 0xFFFFFF );
+					mtl.specular = new THREE.FloatNode( .5 );
+					mtl.shininess = new THREE.FloatNode( 15 );
+
+					// not use "uniform" input ( for optimization ) 
+					// instead use explicit declaration example: 
+					// vec3( 1.0, 1.0, 1.0 ) instead "uniform vec3"
+					// but not allow change the value after build the shader material
+					mtl.color.readonly = mtl.specular.readonly = mtl.shininess.readonly = true;
+
+					break;
+
 				case 'triangle-blur':
 
 					// MATERIAL