Преглед изворни кода

Merge pull request #7830 from sunag/dev

renamed LibNode to NodeLib
Mr.doob пре 9 година
родитељ
комит
9b7b3b9e28

+ 43 - 10
examples/js/nodes/BuilderNode.js

@@ -6,19 +6,60 @@ THREE.BuilderNode = function( material ) {
 
 	this.material = material;
 
-	this.require = {};
+	this.caches = [];
 	this.isVerify = false;
-	this.cache = '';
+
+	this.addCache();
 
 };
 
 THREE.BuilderNode.prototype = {
 	constructor: THREE.BuilderNode,
 
+	addCache : function( name, requires ) {
+
+		this.caches.push( {
+			name : name || '',
+			requires : requires || {}
+		} );
+
+		return this.updateCache();
+
+	},
+
+	removeCache : function() {
+
+		this.caches.pop();
+
+		return this.updateCache();
+
+	},
+
+	updateCache : function() {
+
+		var cache = this.caches[ this.caches.length - 1 ];
+
+		this.cache = cache.name;
+		this.requires = cache.requires;
+
+		return this;
+
+	},
+
+	require : function( name, node ) {
+
+		this.requires[ name ] = node;
+
+		return this;
+
+	},
+
 	include : function( func ) {
 
 		this.material.include( this.shader, func );
 
+		return this;
+
 	},
 
 	getFormatConstructor : function( len ) {
@@ -89,14 +130,6 @@ THREE.BuilderNode.prototype = {
 
 	},
 
-	setCache : function( name ) {
-
-		this.cache = name || '';
-
-		return this;
-
-	},
-
 	getElementByIndex : function( index ) {
 
 		return THREE.BuilderNode.elements[ index ];

+ 2 - 2
examples/js/nodes/FunctionNode.js

@@ -138,9 +138,9 @@ THREE.FunctionNode.prototype.parse = function( src, includes, extensions ) {
 
 			}
 
-			if ( this.getInclude( reference ) === undefined && THREE.LibNode.contains( reference ) ) {
+			if ( this.getInclude( reference ) === undefined && THREE.NodeLib.contains( reference ) ) {
 
-				this.includes.push( THREE.LibNode.get( reference ) );
+				this.includes.push( THREE.NodeLib.get( reference ) );
 
 			}
 

+ 10 - 9
examples/js/nodes/GLNode.js

@@ -13,40 +13,41 @@ THREE.GLNode = function( type ) {
 
 };
 
-THREE.GLNode.prototype.verify = function( builder ) {
+THREE.GLNode.prototype.verify = function( builder, cache, requires ) {
 
 	builder.isVerify = true;
 
 	var material = builder.material;
 
-	this.build( builder, 'v4' );
+	this.build( builder.addCache( cache, requires ), 'v4' );
 
 	material.clearVertexNode();
 	material.clearFragmentNode();
 
-	builder.setCache(); // reset cache
+	builder.removeCache();
 
 	builder.isVerify = false;
 
 };
 
-THREE.GLNode.prototype.verifyAndBuildCode = function( builder, output, cache ) {
+THREE.GLNode.prototype.verifyAndBuildCode = function( builder, output, cache, requires ) {
 
-	this.verify( builder.setCache( cache ) );
+	this.verify( builder, cache, requires );
 
-	return this.buildCode( builder.setCache( cache ), output );
+	return this.buildCode( builder, output, cache, requires );
 
 };
 
-THREE.GLNode.prototype.buildCode = function( builder, output, uuid ) {
+THREE.GLNode.prototype.buildCode = function( builder, output, cache, requires ) {
 
 	var material = builder.material;
-	var data = { result : this.build( builder, output, uuid ) };
+
+	var data = { result : this.build( builder.addCache( cache, requires ), output ) };
 
 	if ( builder.isShader( 'vertex' ) ) data.code = material.clearVertexNode();
 	else data.code = material.clearFragmentNode();
 
-	builder.setCache(); // reset cache
+	builder.removeCache();
 
 	return data;
 

+ 6 - 6
examples/js/nodes/LibNode.js → examples/js/nodes/NodeLib.js

@@ -2,7 +2,7 @@
  * @author sunag / http://www.sunag.com.br/
  */
 
-THREE.LibNode = {
+THREE.NodeLib = {
 	nodes: {},
 	add: function( node ) {
 
@@ -30,13 +30,13 @@ THREE.LibNode = {
 //	Luma
 //
 
-THREE.LibNode.add( new THREE.ConstNode( "vec3 LUMA = vec3(0.2125, 0.7154, 0.0721);" ) );
+THREE.NodeLib.add( new THREE.ConstNode( "vec3 LUMA = vec3(0.2125, 0.7154, 0.0721);" ) );
 
 //
 //	DepthColor
 //
 
-THREE.LibNode.add( new THREE.FunctionNode( [
+THREE.NodeLib.add( new THREE.FunctionNode( [
 "float depthcolor( float mNear, float mFar ) {",
 
 	"#ifdef USE_LOGDEPTHBUF_EXT",
@@ -53,7 +53,7 @@ THREE.LibNode.add( new THREE.FunctionNode( [
 //	NormalMap
 //
 
-THREE.LibNode.add( new THREE.FunctionNode( [
+THREE.NodeLib.add( new THREE.FunctionNode( [
 // Per-Pixel Tangent Space Normal Mapping
 // http://hacksoflife.blogspot.ch/2009/11/per-pixel-tangent-space-normal-mapping.html
 "vec3 perturbNormal2Arb( vec3 eye_pos, vec3 surf_norm, vec3 map, vec2 mUv, float scale ) {",
@@ -75,7 +75,7 @@ THREE.LibNode.add( new THREE.FunctionNode( [
 //	Saturation
 //
 
-THREE.LibNode.add( new THREE.FunctionNode( [
+THREE.NodeLib.add( new THREE.FunctionNode( [
 // Algorithm from Chapter 16 of OpenGL Shading Language
 "vec3 saturation_rgb(vec3 rgb, float adjustment) {",
 	"vec3 intensity = vec3(dot(rgb, LUMA));",
@@ -87,7 +87,7 @@ THREE.LibNode.add( new THREE.FunctionNode( [
 //	Luminance
 //
 
-THREE.LibNode.add( new THREE.FunctionNode( [
+THREE.NodeLib.add( new THREE.FunctionNode( [
 // Algorithm from Chapter 10 of Graphics Shaders.
 "float luminance_rgb(vec3 rgb) {",
 	"return dot(rgb, LUMA);",

+ 1 - 1
examples/js/nodes/NodeMaterial.js

@@ -436,7 +436,7 @@ THREE.NodeMaterial.prototype.include = function( shader, node ) {
 
 	var includes;
 
-	node = typeof node === 'string' ? THREE.LibNode.get( node ) : node;
+	node = typeof node === 'string' ? THREE.NodeLib.get( node ) : node;
 
 	if ( node instanceof THREE.FunctionNode ) {
 

+ 1 - 1
examples/js/nodes/accessors/ReflectNode.js

@@ -30,7 +30,7 @@ THREE.ReflectNode.prototype.generate = function( builder, output ) {
 	}
 	else {
 
-		console.warn( "THREE.ReflectNode is not compatible with " + builder.shader + " shader" );
+		console.warn( "THREE.ReflectNode is not compatible with " + builder.shader + " shader." );
 
 		return builder.format( 'vec3( 0.0 )', this.type, output );
 

+ 3 - 3
examples/js/nodes/inputs/CubeTextureNode.js

@@ -25,11 +25,11 @@ THREE.CubeTextureNode.prototype.generate = function( builder, output ) {
 
 	var cubetex = this.getTexture( builder, output );
 	var coord = this.coord.build( builder, 'v3' );
-	var bias = this.bias ? this.bias.build( builder, 'fv1' ) : undefined;;
+	var bias = this.bias ? this.bias.build( builder, 'fv1' ) : undefined;
 
-	if ( bias == undefined && builder.require.cubeTextureBias ) {
+	if ( bias == undefined && builder.requires.bias ) {
 
-		bias = builder.require.cubeTextureBias.build( builder, 'fv1' );
+		bias = builder.requires.bias.build( builder, 'fv1' );
 
 	}
 

+ 6 - 0
examples/js/nodes/inputs/TextureNode.js

@@ -27,6 +27,12 @@ THREE.TextureNode.prototype.generate = function( builder, output ) {
 	var coord = this.coord.build( builder, 'v2' );
 	var bias = this.bias ? this.bias.build( builder, 'fv1' ) : undefined;
 
+	if ( bias == undefined && builder.requires.bias ) {
+
+		bias = builder.requires.bias.build( builder, 'fv1' );
+
+	}
+
 	var code;
 
 	if ( bias ) code = 'texture2D(' + tex + ',' + coord + ',' + bias + ')';

+ 1 - 1
examples/js/nodes/materials/PhongNode.js

@@ -132,7 +132,7 @@ THREE.PhongNode.prototype.build = function( builder ) {
 		var normal = this.normal ? this.normal.buildCode( builder, 'v3' ) : undefined;
 		var normalScale = this.normalScale && this.normal ? this.normalScale.buildCode( builder, 'fv1' ) : undefined;
 
-		var environment = this.environment ? this.environment.buildCode( builder.setCache( 'env' ), 'c' ) : undefined;
+		var environment = this.environment ? this.environment.buildCode( builder, 'c' ) : undefined;
 		var reflectivity = this.reflectivity && this.environment ? this.reflectivity.buildCode( builder, 'fv1' ) : undefined;
 
 		material.requestAttrib.transparent = alpha != undefined;

+ 6 - 4
examples/js/nodes/materials/StandardNode.js

@@ -97,9 +97,11 @@ THREE.StandardNode.prototype.build = function( builder ) {
 	}
 	else {
 
-		// CubeMap blur effect (PBR)
+		// autoblur textures for PBR Material effect
 
-		builder.require.cubeTextureBias = builder.require.cubeTextureBias || new THREE.RoughnessToBlinnExponentNode();
+		var requires = {
+			bias : new THREE.RoughnessToBlinnExponentNode()
+		};
 
 		// verify all nodes to reuse generate codes
 
@@ -117,7 +119,7 @@ THREE.StandardNode.prototype.build = function( builder ) {
 		if ( this.normal ) this.normal.verify( builder );
 		if ( this.normalScale && this.normal ) this.normalScale.verify( builder );
 
-		if ( this.environment ) this.environment.verify( builder.setCache( 'env' ) ); // isolate environment from others inputs ( see TextureNode, CubeTextureNode )
+		if ( this.environment ) this.environment.verify( builder, 'env', requires ); // isolate environment from others inputs ( see TextureNode, CubeTextureNode )
 		if ( this.reflectivity && this.environment ) this.reflectivity.verify( builder );
 
 		// build code
@@ -136,7 +138,7 @@ THREE.StandardNode.prototype.build = function( builder ) {
 		var normal = this.normal ? this.normal.buildCode( builder, 'v3' ) : undefined;
 		var normalScale = this.normalScale && this.normal ? this.normalScale.buildCode( builder, 'fv1' ) : undefined;
 
-		var environment = this.environment ? this.environment.buildCode( builder.setCache( 'env' ), 'c' ) : undefined;
+		var environment = this.environment ? this.environment.buildCode( builder, 'c', 'env', requires ) : undefined;
 		var reflectivity = this.reflectivity && this.environment ? this.reflectivity.buildCode( builder, 'fv1' ) : undefined;
 
 		material.requestAttrib.transparent = alpha != undefined;

+ 1 - 1
examples/js/nodes/utils/NormalMapNode.js

@@ -34,7 +34,7 @@ THREE.NormalMapNode.prototype.generate = function( builder, output ) {
 	}
 	else {
 
-		console.warn( "THREE.NormalMap is not compatible with " + builder.shader + " shader" );
+		console.warn( "THREE.NormalMap is not compatible with " + builder.shader + " shader." );
 
 		return builder.format( 'vec3( 0.0 )', this.type, output );
 

+ 2 - 2
examples/js/nodes/utils/RoughnessToBlinnExponentNode.js

@@ -24,7 +24,7 @@ THREE.RoughnessToBlinnExponentNode.prototype.generate = function( builder, outpu
 		}
 		else {
 
-			console.warn( "THREE.RoughnessToBlinnExponentNode is compatible with StandardMaterial only" );
+			console.warn( "THREE.RoughnessToBlinnExponentNode is compatible with StandardMaterial only." );
 
 			material.addFragmentNode( 'float specularMIPLevel = 0.0;' );
 
@@ -35,7 +35,7 @@ THREE.RoughnessToBlinnExponentNode.prototype.generate = function( builder, outpu
 	}
 	else {
 
-		console.warn( "THREE.RoughnessToBlinnExponentNode is not compatible with " + builder.shader + " shader" );
+		console.warn( "THREE.RoughnessToBlinnExponentNode is not compatible with " + builder.shader + " shader." );
 
 		return builder.format( '0.0', this.type, output );
 

+ 1 - 1
examples/webgl_materials_nodes.html

@@ -48,7 +48,7 @@
 		<script src="js/nodes/FunctionNode.js"></script>
 		<script src="js/nodes/FunctionCallNode.js"></script>
 		<script src="js/nodes/BuilderNode.js"></script>
-		<script src="js/nodes/LibNode.js"></script>
+		<script src="js/nodes/NodeLib.js"></script>
 		<script src="js/nodes/NodeMaterial.js"></script>
 
 		<!-- Accessors -->

+ 1 - 1
examples/webgl_postprocessing_nodes.html

@@ -49,7 +49,7 @@
 		<script src="js/nodes/FunctionNode.js"></script>
 		<script src="js/nodes/FunctionCallNode.js"></script>
 		<script src="js/nodes/BuilderNode.js"></script>
-		<script src="js/nodes/LibNode.js"></script>
+		<script src="js/nodes/NodeLib.js"></script>
 		<script src="js/nodes/NodeMaterial.js"></script>
 
 		<!-- Accessors -->