提交 805d92d7 编写于 作者: M Mr.doob

Merge pull request #7849 from sunag/dev

NodeMaterial auto cache JoinNode and cleanup
......@@ -10,9 +10,6 @@ THREE.ConstNode = function( name, useDefine ) {
};
THREE.ConstNode.prototype = Object.create( THREE.TempNode.prototype );
THREE.ConstNode.prototype.constructor = THREE.ConstNode;
THREE.ConstNode.PI = 'PI';
THREE.ConstNode.PI2 = 'PI2';
THREE.ConstNode.RECIPROCAL_PI = 'RECIPROCAL_PI';
......@@ -20,6 +17,9 @@ THREE.ConstNode.RECIPROCAL_PI2 = 'RECIPROCAL_PI2';
THREE.ConstNode.LOG2 = 'LOG2';
THREE.ConstNode.EPSILON = 'EPSILON';
THREE.ConstNode.prototype = Object.create( THREE.TempNode.prototype );
THREE.ConstNode.prototype.constructor = THREE.ConstNode;
THREE.ConstNode.prototype.parse = function( src, useDefine ) {
var name, type;
......
......@@ -11,10 +11,7 @@ THREE.NodeMaterial = function( vertex, fragment ) {
};
THREE.NodeMaterial.prototype = Object.create( THREE.ShaderMaterial.prototype );
THREE.NodeMaterial.prototype.constructor = THREE.NodeMaterial;
THREE.NodeMaterial.Type = {
THREE.NodeMaterial.types = {
t : 'sampler2D',
tc : 'samplerCube',
bv1 : 'bool',
......@@ -26,39 +23,46 @@ THREE.NodeMaterial.Type = {
v4 : 'vec4'
};
THREE.NodeMaterial.GetShortcuts = function( prop, name ) {
THREE.NodeMaterial.addShortcuts = function( proto, prop, list ) {
return {
get: function() {
function applyShortcut( prop, name ) {
return this[ prop ][ name ];
return {
get: function() {
},
set: function( val ) {
return this[ prop ][ name ];
this[ prop ][ name ] = val;
},
set: function( val ) {
this[ prop ][ name ] = val;
}
};
}
};
};
return (function() {
THREE.NodeMaterial.Shortcuts = function( proto, prop, list ) {
var shortcuts = {};
var shortcuts = {};
for ( var i = 0; i < list.length; ++ i ) {
for ( var i = 0; i < list.length; ++ i ) {
var name = list[ i ];
var name = list[ i ];
shortcuts[ name ] = applyShortcut( prop, name );
shortcuts[ name ] = this.GetShortcuts( prop, name );
}
}
Object.defineProperties( proto, shortcuts );
Object.defineProperties( proto, shortcuts );
})();
};
THREE.NodeMaterial.prototype = Object.create( THREE.ShaderMaterial.prototype );
THREE.NodeMaterial.prototype.constructor = THREE.NodeMaterial;
THREE.NodeMaterial.prototype.updateAnimation = function( delta ) {
for ( var i = 0; i < this.requestUpdate.length; ++ i ) {
......@@ -388,7 +392,7 @@ THREE.NodeMaterial.prototype.getCodePars = function( pars, prefix ) {
if ( parsType == 't' && parsValue instanceof THREE.CubeTexture ) parsType = 'tc';
var type = THREE.NodeMaterial.Type[ parsType ];
var type = THREE.NodeMaterial.types[ parsType ];
if ( type == undefined ) throw new Error( "Node pars " + parsType + " not found." );
......
......@@ -11,12 +11,12 @@ THREE.CameraNode = function( scope, camera ) {
};
THREE.CameraNode.prototype = Object.create( THREE.TempNode.prototype );
THREE.CameraNode.prototype.constructor = THREE.CameraNode;
THREE.CameraNode.POSITION = 'position';
THREE.CameraNode.DEPTH = 'depth';
THREE.CameraNode.prototype = Object.create( THREE.TempNode.prototype );
THREE.CameraNode.prototype.constructor = THREE.CameraNode;
THREE.CameraNode.prototype.setCamera = function( camera ) {
this.camera = camera;
......
......@@ -10,12 +10,12 @@ THREE.ColorsNode = function( index ) {
};
THREE.ColorsNode.prototype = Object.create( THREE.TempNode.prototype );
THREE.ColorsNode.prototype.constructor = THREE.ColorsNode;
THREE.ColorsNode.vertexDict = [ 'color', 'color2' ];
THREE.ColorsNode.fragmentDict = [ 'vColor', 'vColor2' ];
THREE.ColorsNode.prototype = Object.create( THREE.TempNode.prototype );
THREE.ColorsNode.prototype.constructor = THREE.ColorsNode;
THREE.ColorsNode.prototype.generate = function( builder, output ) {
var material = builder.material;
......
......@@ -10,13 +10,13 @@ THREE.NormalNode = function( scope ) {
};
THREE.NormalNode.prototype = Object.create( THREE.TempNode.prototype );
THREE.NormalNode.prototype.constructor = THREE.NormalNode;
THREE.NormalNode.LOCAL = 'local';
THREE.NormalNode.WORLD = 'world';
THREE.NormalNode.VIEW = 'view';
THREE.NormalNode.prototype = Object.create( THREE.TempNode.prototype );
THREE.NormalNode.prototype.constructor = THREE.NormalNode;
THREE.NormalNode.prototype.isShared = function( builder ) {
switch ( this.scope ) {
......
......@@ -10,14 +10,14 @@ THREE.PositionNode = function( scope ) {
};
THREE.PositionNode.prototype = Object.create( THREE.TempNode.prototype );
THREE.PositionNode.prototype.constructor = THREE.PositionNode;
THREE.PositionNode.LOCAL = 'local';
THREE.PositionNode.WORLD = 'world';
THREE.PositionNode.VIEW = 'view';
THREE.PositionNode.PROJECTION = 'projection';
THREE.PositionNode.prototype = Object.create( THREE.TempNode.prototype );
THREE.PositionNode.prototype.constructor = THREE.PositionNode;
THREE.PositionNode.prototype.getType = function( builder ) {
switch ( this.scope ) {
......
......@@ -10,12 +10,12 @@ THREE.UVNode = function( index ) {
};
THREE.UVNode.prototype = Object.create( THREE.TempNode.prototype );
THREE.UVNode.prototype.constructor = THREE.UVNode;
THREE.UVNode.vertexDict = [ 'uv', 'uv2' ];
THREE.UVNode.fragmentDict = [ 'vUv', 'vUv2' ];
THREE.UVNode.prototype = Object.create( THREE.TempNode.prototype );
THREE.UVNode.prototype.constructor = THREE.UVNode;
THREE.UVNode.prototype.generate = function( builder, output ) {
var material = builder.material;
......
......@@ -13,4 +13,4 @@ THREE.ColorNode = function( color ) {
THREE.ColorNode.prototype = Object.create( THREE.InputNode.prototype );
THREE.ColorNode.prototype.constructor = THREE.ColorNode;
THREE.NodeMaterial.Shortcuts( THREE.ColorNode.prototype, 'value', [ 'r', 'g', 'b' ] );
THREE.NodeMaterial.addShortcuts( THREE.ColorNode.prototype, 'value', [ 'r', 'g', 'b' ] );
......@@ -13,4 +13,4 @@ THREE.Vector2Node = function( x, y ) {
THREE.Vector2Node.prototype = Object.create( THREE.InputNode.prototype );
THREE.Vector2Node.prototype.constructor = THREE.Vector2Node;
THREE.NodeMaterial.Shortcuts( THREE.Vector2Node.prototype, 'value', [ 'x', 'y' ] );
THREE.NodeMaterial.addShortcuts( THREE.Vector2Node.prototype, 'value', [ 'x', 'y' ] );
......@@ -14,4 +14,4 @@ THREE.Vector3Node = function( x, y, z ) {
THREE.Vector3Node.prototype = Object.create( THREE.InputNode.prototype );
THREE.Vector3Node.prototype.constructor = THREE.Vector3Node;
THREE.NodeMaterial.Shortcuts( THREE.Vector3Node.prototype, 'value', [ 'x', 'y', 'z' ] );
THREE.NodeMaterial.addShortcuts( THREE.Vector3Node.prototype, 'value', [ 'x', 'y', 'z' ] );
......@@ -13,4 +13,4 @@ THREE.Vector4Node = function( x, y, z, w ) {
THREE.Vector4Node.prototype = Object.create( THREE.InputNode.prototype );
THREE.Vector4Node.prototype.constructor = THREE.Vector4Node;
THREE.NodeMaterial.Shortcuts( THREE.Vector4Node.prototype, 'value', [ 'x', 'y', 'z', 'w' ] );
THREE.NodeMaterial.addShortcuts( THREE.Vector4Node.prototype, 'value', [ 'x', 'y', 'z', 'w' ] );
......@@ -27,7 +27,7 @@ THREE.PhongNode.prototype.build = function( builder ) {
if ( builder.isShader( 'vertex' ) ) {
var transform = this.transform ? this.transform.verifyAndBuildCode( builder, 'v3' ) : undefined;
var transform = this.transform ? this.transform.verifyAndBuildCode( builder, 'v3', 'transform' ) : undefined;
material.mergeUniform( THREE.UniformsUtils.merge( [
......@@ -114,7 +114,7 @@ THREE.PhongNode.prototype.build = function( builder ) {
if ( this.normalScale && this.normal ) this.normalScale.verify( builder );
if ( this.environment ) this.environment.verify( builder );
if ( this.environmentIntensity && this.environment ) this.environmentIntensity.verify( builder );
if ( this.environmentAlpha && this.environment ) this.environmentAlpha.verify( builder );
// build code
......@@ -133,7 +133,7 @@ THREE.PhongNode.prototype.build = function( builder ) {
var normalScale = this.normalScale && this.normal ? this.normalScale.buildCode( builder, 'v2' ) : undefined;
var environment = this.environment ? this.environment.buildCode( builder, 'c' ) : undefined;
var environmentIntensity = this.environmentIntensity && this.environment ? this.environmentIntensity.buildCode( builder, 'fv1' ) : undefined;
var environmentAlpha = this.environmentAlpha && this.environment ? this.environmentAlpha.buildCode( builder, 'fv1' ) : undefined;
material.requestAttrib.transparent = alpha != undefined;
......@@ -236,11 +236,11 @@ THREE.PhongNode.prototype.build = function( builder ) {
output.push( environment.code );
if ( environmentIntensity ) {
if ( environmentAlpha ) {
output.push( environmentIntensity.code );
output.push( environmentAlpha.code );
output.push( "outgoingLight = mix(" + 'outgoingLight' + "," + environment.result + "," + environmentIntensity.result + ");" );
output.push( "outgoingLight = mix(" + 'outgoingLight' + "," + environment.result + "," + environmentAlpha.result + ");" );
}
else {
......
......@@ -13,5 +13,5 @@ THREE.PhongNodeMaterial = function() {
THREE.PhongNodeMaterial.prototype = Object.create( THREE.NodeMaterial.prototype );
THREE.PhongNodeMaterial.prototype.constructor = THREE.PhongNodeMaterial;
THREE.NodeMaterial.Shortcuts( THREE.PhongNodeMaterial.prototype, 'node',
[ 'color', 'alpha', 'specular', 'shininess', 'normal', 'normalScale', 'emissive', 'ambient', 'shadow', 'ao', 'environment', 'environmentIntensity', 'transform' ] );
THREE.NodeMaterial.addShortcuts( THREE.PhongNodeMaterial.prototype, 'node',
[ 'color', 'alpha', 'specular', 'shininess', 'normal', 'normalScale', 'emissive', 'ambient', 'shadow', 'ao', 'environment', 'environmentAlpha', 'transform' ] );
......@@ -27,7 +27,7 @@ THREE.StandardNode.prototype.build = function( builder ) {
if ( builder.isShader( 'vertex' ) ) {
var transform = this.transform ? this.transform.verifyAndBuildCode( builder, 'v3' ) : undefined;
var transform = this.transform ? this.transform.verifyAndBuildCode( builder, 'v3', 'transform' ) : undefined;
material.mergeUniform( THREE.UniformsUtils.merge( [
......@@ -120,7 +120,6 @@ THREE.StandardNode.prototype.build = function( builder ) {
if ( this.normalScale && this.normal ) this.normalScale.verify( builder );
if ( this.environment ) this.environment.verify( builder, 'env', requires ); // isolate environment from others inputs ( see TextureNode, CubeTextureNode )
if ( this.environmentIntensity && this.environment ) this.environmentIntensity.verify( builder );
// build code
......@@ -139,7 +138,6 @@ THREE.StandardNode.prototype.build = function( builder ) {
var normalScale = this.normalScale && this.normal ? this.normalScale.buildCode( builder, 'v2' ) : undefined;
var environment = this.environment ? this.environment.buildCode( builder, 'c', 'env', requires ) : undefined;
var environmentIntensity = this.environmentIntensity && this.environment ? this.environmentIntensity.buildCode( builder, 'fv1' ) : undefined;
material.requestAttrib.transparent = alpha != undefined;
......@@ -254,13 +252,6 @@ THREE.StandardNode.prototype.build = function( builder ) {
output.push( environment.code );
output.push( "RE_IndirectSpecular(" + environment.result + ", geometry, material, reflectedLight );" );
if ( environmentIntensity ) {
output.push( environmentIntensity.code );
output.push( "reflectedLight.indirectSpecular *= " + environmentIntensity.result + ";" );
}
}
output.push( "vec3 outgoingLight = reflectedLight.directDiffuse + reflectedLight.indirectDiffuse + reflectedLight.directSpecular + reflectedLight.indirectSpecular;" );
......
......@@ -13,5 +13,5 @@ THREE.StandardNodeMaterial = function() {
THREE.StandardNodeMaterial.prototype = Object.create( THREE.NodeMaterial.prototype );
THREE.StandardNodeMaterial.prototype.constructor = THREE.StandardNodeMaterial;
THREE.NodeMaterial.Shortcuts( THREE.StandardNodeMaterial.prototype, 'node',
[ 'color', 'alpha', 'roughness', 'metalness', 'normal', 'normalScale', 'emissive', 'ambient', 'shadow', 'ao', 'environment', 'environmentIntensity', 'transform' ] );
THREE.NodeMaterial.addShortcuts( THREE.StandardNodeMaterial.prototype, 'node',
[ 'color', 'alpha', 'roughness', 'metalness', 'normal', 'normalScale', 'emissive', 'ambient', 'shadow', 'ao', 'environment', 'transform' ] );
......@@ -12,9 +12,6 @@ THREE.Math1Node = function( a, method ) {
};
THREE.Math1Node.prototype = Object.create( THREE.TempNode.prototype );
THREE.Math1Node.prototype.constructor = THREE.Math1Node;
THREE.Math1Node.RAD = 'radians';
THREE.Math1Node.DEG = 'degrees';
THREE.Math1Node.EXP = 'exp';
......@@ -39,6 +36,9 @@ THREE.Math1Node.LENGTH = 'length';
THREE.Math1Node.NEGATE = 'negate';
THREE.Math1Node.INVERT = 'invert';
THREE.Math1Node.prototype = Object.create( THREE.TempNode.prototype );
THREE.Math1Node.prototype.constructor = THREE.Math1Node;
THREE.Math1Node.prototype.getType = function( builder ) {
switch ( this.method ) {
......
......@@ -13,9 +13,6 @@ THREE.Math2Node = function( a, b, method ) {
};
THREE.Math2Node.prototype = Object.create( THREE.TempNode.prototype );
THREE.Math2Node.prototype.constructor = THREE.Math2Node;
THREE.Math2Node.MIN = 'min';
THREE.Math2Node.MAX = 'max';
THREE.Math2Node.MOD = 'mod';
......@@ -26,6 +23,9 @@ THREE.Math2Node.DOT = 'dot';
THREE.Math2Node.CROSS = 'cross';
THREE.Math2Node.POW = 'pow';
THREE.Math2Node.prototype = Object.create( THREE.TempNode.prototype );
THREE.Math2Node.prototype.constructor = THREE.Math2Node;
THREE.Math2Node.prototype.getInputType = function( builder ) {
// use the greater length vector
......
......@@ -14,14 +14,14 @@ THREE.Math3Node = function( a, b, c, method ) {
};
THREE.Math3Node.prototype = Object.create( THREE.TempNode.prototype );
THREE.Math3Node.prototype.constructor = THREE.Math3Node;
THREE.Math3Node.MIX = 'mix';
THREE.Math3Node.REFRACT = 'refract';
THREE.Math3Node.SMOOTHSTEP = 'smoothstep';
THREE.Math3Node.FACEFORWARD = 'faceforward';
THREE.Math3Node.prototype = Object.create( THREE.TempNode.prototype );
THREE.Math3Node.prototype.constructor = THREE.Math3Node;
THREE.Math3Node.prototype.getType = function( builder ) {
var a = builder.getFormatLength( this.a.getType( builder ) );
......
......@@ -6,21 +6,20 @@ THREE.OperatorNode = function( a, b, op ) {
THREE.TempNode.call( this );
this.op = op || THREE.OperatorNode.ADD;
this.a = a;
this.b = b;
this.op = op || THREE.OperatorNode.ADD;
};
THREE.OperatorNode.prototype = Object.create( THREE.TempNode.prototype );
THREE.OperatorNode.prototype.constructor = THREE.OperatorNode;
THREE.OperatorNode.ADD = '+';
THREE.OperatorNode.SUB = '-';
THREE.OperatorNode.MUL = '*';
THREE.OperatorNode.DIV = '/';
THREE.OperatorNode.prototype = Object.create( THREE.TempNode.prototype );
THREE.OperatorNode.prototype.constructor = THREE.OperatorNode;
THREE.OperatorNode.prototype.getType = function( builder ) {
// use the greater length vector
......
......@@ -20,6 +20,8 @@ THREE.NodePass = function() {
THREE.NodePass.prototype = Object.create( THREE.ShaderPass.prototype );
THREE.NodePass.prototype.constructor = THREE.NodePass;
THREE.NodeMaterial.addShortcuts( THREE.NodePass.prototype, 'fragment', [ 'value' ] );
THREE.NodePass.prototype.build = function() {
this.node.build();
......@@ -28,6 +30,3 @@ THREE.NodePass.prototype.build = function() {
this.material = this.node;
};
THREE.NodeMaterial.Shortcuts( THREE.NodePass.prototype, 'fragment',
[ 'value' ] );
......@@ -4,7 +4,7 @@
THREE.JoinNode = function( x, y, z, w ) {
THREE.GLNode.call( this, 'fv1' );
THREE.TempNode.call( this, 'fv1' );
this.x = x;
this.y = y;
......@@ -13,11 +13,11 @@ THREE.JoinNode = function( x, y, z, w ) {
};
THREE.JoinNode.prototype = Object.create( THREE.GLNode.prototype );
THREE.JoinNode.prototype.constructor = THREE.JoinNode;
THREE.JoinNode.inputs = [ 'x', 'y', 'z', 'w' ];
THREE.JoinNode.prototype = Object.create( THREE.TempNode.prototype );
THREE.JoinNode.prototype.constructor = THREE.JoinNode;
THREE.JoinNode.prototype.getNumElements = function() {
var inputs = THREE.JoinNode.inputs;
......
......@@ -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.NormalMapNode is not compatible with " + builder.shader + " shader." );
return builder.format( 'vec3( 0.0 )', this.type, output );
......
......@@ -2,13 +2,12 @@
* @author sunag / http://www.sunag.com.br/
*/
THREE.SwitchNode = function( a, component ) {
THREE.SwitchNode = function( node, components ) {
THREE.GLNode.call( this, 'fv1' );
this.component = component || 'x';
this.a = a;
this.node = node;
this.components = components || 'x';
};
......@@ -17,26 +16,26 @@ THREE.SwitchNode.prototype.constructor = THREE.SwitchNode;
THREE.SwitchNode.prototype.getType = function( builder ) {
return builder.getFormatByLength( this.component.length );
return builder.getFormatByLength( this.components.length );
};
THREE.SwitchNode.prototype.generate = function( builder, output ) {
var type = this.a.getType( builder );
var type = this.node.getType( builder );
var inputLength = builder.getFormatLength( type ) - 1;
var a = this.a.build( builder, type );
var node = this.node.build( builder, type );
var outputLength = 0;
var i, len = this.component.length;
var i, len = this.components.length;
// get max length
for ( i = 0; i < len; i ++ ) {
outputLength = Math.max( outputLength, builder.getIndexByElement( this.component.charAt( i ) ) );
outputLength = Math.max( outputLength, builder.getIndexByElement( this.components.charAt( i ) ) );
}
......@@ -44,12 +43,12 @@ THREE.SwitchNode.prototype.generate = function( builder, output ) {
// build switch
a += '.';
node += '.';
for ( i = 0; i < len; i ++ ) {
var elm = this.component.charAt( i );
var idx = builder.getIndexByElement( this.component.charAt( i ) );
var elm = this.components.charAt( i );
var idx = builder.getIndexByElement( this.components.charAt( i ) );
if ( idx > outputLength ) idx = outputLength;
......@@ -59,10 +58,10 @@ THREE.SwitchNode.prototype.generate = function( builder, output ) {
}
a += builder.getElementByIndex( idx );
node += builder.getElementByIndex( idx );
}
return builder.format( a, this.getType( builder ), output );
return builder.format( node, this.getType( builder ), output );
};
......@@ -273,7 +273,7 @@
//mtl.shadow = // shadowmap
//mtl.ao = // ambient occlusion
//mtl.environment = // reflection map (CubeMap recommended)
//mtl.environmentIntensity = // environment intensity
//mtl.environmentAlpha = // environment alpha
//mtl.transform = // vertex transformation
var mask = new THREE.SwitchNode( new THREE.TextureNode( decalDiffuse ), 'w' );
......@@ -282,7 +282,7 @@
mtl.specular = new THREE.FloatNode( .5 );
mtl.shininess = new THREE.FloatNode( 15 );
mtl.environment = new THREE.CubeTextureNode( cubemap );
mtl.environmentIntensity = mask;
mtl.environmentAlpha = mask;
mtl.normal = new THREE.TextureNode( grassNormal );
mtl.normalScale = new THREE.Math1Node( mask, THREE.Math1Node.INVERT );
......@@ -305,7 +305,7 @@
//mtl.shadow = // shadowmap
//mtl.ao = // ambient occlusion
//mtl.environment = // reflection map (CubeMap recommended)
//mtl.environmentIntensity = // environment intensity
//mtl.environmentAlpha = // environment alpha
//mtl.transform = // vertex transformation
var mask = new THREE.SwitchNode( new THREE.TextureNode( decalDiffuse ), 'w' );
......@@ -619,7 +619,7 @@
mtl.color = new THREE.ColorNode( 0x3399FF );
mtl.environment = color;
mtl.environmentIntensity = new THREE.Math1Node( fresnel, THREE.Math1Node.SAT );
mtl.environmentAlpha = new THREE.Math1Node( fresnel, THREE.Math1Node.SAT );
// GUI
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册