From c1cd65e6a24f66b4113ce8bb80b7bc053e60f653 Mon Sep 17 00:00:00 2001 From: SUNAG Date: Mon, 21 Dec 2015 06:10:08 -0200 Subject: [PATCH] cache improvement (multilayer) and TextureNode support for PBR`M environment --- examples/js/nodes/BuilderNode.js | 53 +++++++++++++++++---- examples/js/nodes/GLNode.js | 19 ++++---- examples/js/nodes/inputs/CubeTextureNode.js | 6 +-- examples/js/nodes/inputs/TextureNode.js | 6 +++ examples/js/nodes/materials/StandardNode.js | 10 ++-- 5 files changed, 68 insertions(+), 26 deletions(-) diff --git a/examples/js/nodes/BuilderNode.js b/examples/js/nodes/BuilderNode.js index 88f8d6a6b5..4c447ffcdb 100644 --- a/examples/js/nodes/BuilderNode.js +++ b/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 ]; diff --git a/examples/js/nodes/GLNode.js b/examples/js/nodes/GLNode.js index 7591ae2b96..dc53231b7a 100644 --- a/examples/js/nodes/GLNode.js +++ b/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; diff --git a/examples/js/nodes/inputs/CubeTextureNode.js b/examples/js/nodes/inputs/CubeTextureNode.js index 0413b621bf..13408ca047 100644 --- a/examples/js/nodes/inputs/CubeTextureNode.js +++ b/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' ); } diff --git a/examples/js/nodes/inputs/TextureNode.js b/examples/js/nodes/inputs/TextureNode.js index 5074bf3397..cb2a963fb3 100644 --- a/examples/js/nodes/inputs/TextureNode.js +++ b/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 + ')'; diff --git a/examples/js/nodes/materials/StandardNode.js b/examples/js/nodes/materials/StandardNode.js index aeea5d5456..06b4f66136 100644 --- a/examples/js/nodes/materials/StandardNode.js +++ b/examples/js/nodes/materials/StandardNode.js @@ -96,9 +96,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 @@ -116,7 +118,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 @@ -135,7 +137,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; -- GitLab