Skip to content

Commit

Permalink
NodeMaterial: back-compat fixes.
Browse files Browse the repository at this point in the history
  • Loading branch information
donmccurdy committed Jul 28, 2018
1 parent 6a498fd commit 8f95052
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 16 deletions.
12 changes: 8 additions & 4 deletions examples/js/nodes/materials/MeshStandardNodeMaterial.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,15 @@ import { MeshStandardNode } from './nodes/MeshStandardNode.js';
import { NodeMaterial } from './NodeMaterial.js';
import { NodeUtils } from '../core/NodeUtils.js';

function MeshStandardNodeMaterial() {
function MeshStandardNodeMaterial( parameters ) {

var node = new MeshStandardNode();

NodeMaterial.call( this, node, node );

this.type = "MeshStandardNodeMaterial";

var options = arguments[ 0 ];

if ( typeof options === 'object' ) Object.assign( this, options );
this.setValues( parameters );

}

Expand All @@ -25,6 +23,12 @@ MeshStandardNodeMaterial.prototype.constructor = MeshStandardNodeMaterial;

NodeUtils.addShortcuts( MeshStandardNodeMaterial.prototype, 'properties', [
"color",
"emissive",
"emissiveMap",
"emissiveIntensity",
"ao",
"aoMap",
"aoMapIntensity",
"roughness",
"metalness",
"map",
Expand Down
24 changes: 24 additions & 0 deletions examples/js/nodes/materials/NodeMaterial.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,30 @@ Object.defineProperties( NodeMaterial.prototype, {

} );

NodeMaterial.prototype.setValues = function( values ) {

if ( values === undefined ) return;

for ( var key in values ) {

var newValue = values[ key ];

if ( newValue === undefined ) {

console.warn( 'THREE.' + this.type + ': "' + key + '" parameter is undefined.' );
continue;

}

// TODO: The base Material class would warn about setting properties that do not have
// a current value, "...not a property of this material".

this[ key ] = newValue;

}

};

NodeMaterial.prototype.updateFrame = function ( frame ) {

for ( var i = 0; i < this.updaters.length; ++ i ) {
Expand Down
31 changes: 30 additions & 1 deletion examples/js/nodes/materials/nodes/MeshStandardNode.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,17 @@ function MeshStandardNode() {

this.properties = {
color: new THREE.Color( 0xffffff ),
emissive: new THREE.Color( 0x000000 ),
ao: 1.0,
roughness: 0.5,
metalness: 0.5,
normalScale: new THREE.Vector2( 1, 1 )
};

this.inputs = {
color: new PropertyNode( this.properties, 'color', 'c' ),
emissive: new PropertyNode( this.properties, 'emissive', 'c' ),
ao: new PropertyNode( this.properties, 'ao', 'f' ),
roughness: new PropertyNode( this.properties, 'roughness', 'f' ),
metalness: new PropertyNode( this.properties, 'metalness', 'f' ),
normalScale: new PropertyNode( this.properties, 'normalScale', 'v2' )
Expand Down Expand Up @@ -48,15 +52,40 @@ MeshStandardNode.prototype.build = function ( builder ) {

this.color = map ? new OperatorNode( color, map, OperatorNode.MUL ) : color;

// slots
// * emissive
// * emissiveMap
// * emissiveIntensity

var emissive = builder.findNode( props.emissive, inputs.emissive ),
emissiveMap = builder.resolve( props.emissiveMap ),
emissiveIntensity = builder.resolve( props.emissiveIntensity );

this.emissive = emissiveMap ? new OperatorNode( emissive, emissiveMap, OperatorNode.MUL ) : emissive;

if ( emissiveIntensity !== undefined ) {

this.emissive = new OperatorNode( this.emissive, emissiveIntensity, OperatorNode.MUL );

}

// slots
// * ao
// * aoMap
// * aoMapIntensity

var ao = builder.findNode( props.ao, inputs.ao ),
aoMap = builder.resolve( props.aoMap );
aoMap = builder.resolve( props.aoMap ),
aoMapIntensity = builder.resolve( props.aoMapIntensity );

this.ao = aoMap ? new OperatorNode( ao, new SwitchNode( aoMap, "r" ), OperatorNode.MUL ) : ao;

if ( aoMapIntensity !== undefined ) {

this.ao = new OperatorNode( this.ao, aoMapIntensity, OperatorNode.MUL );

}

// slots
// * roughness
// * roughnessMap
Expand Down
16 changes: 5 additions & 11 deletions examples/js/nodes/utils/ColorSpaceNode.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,7 @@ function ColorSpaceNode( input, method ) {

this.input = input;

this.method = method;

if ( this.method === undefined ) {

this.method = this.getEncodingMethod( this.input.value.encoding );

}
this.method = method || ColorSpaceNode.LINEAR;

This comment has been minimized.

Copy link
@sunag

sunag Jul 28, 2018

Collaborator

Niice! Only here if you could replace to ColorSpaceNode.LINEAR_TO_LINEAR


}

Expand Down Expand Up @@ -209,15 +203,15 @@ ColorSpaceNode.LOG_LUV_TO_LINEAR = 'LogLuvToLinear';

ColorSpaceNode.prototype = Object.create( TempNode.prototype );
ColorSpaceNode.prototype.constructor = ColorSpaceNode;
ColorSpaceNode.prototype.nodeType = "ColorSpaceNode";
ColorSpaceNode.prototype.nodeType = "ColorSpace";

ColorSpaceNode.prototype.generate = function ( builder, output ) {

var input = builder.context.input || this.input.build( builder, 'v4' ),
encodingMethod = builder.context.encoding !== undefined ? this.getEncodingMethod( builder.context.encoding ) : [ this.method ],
factor = this.factor ? this.factor.build( builder, 'f' ) : encodingMethod[ 1 ];
decodingMethod = builder.context.encoding !== undefined ? this.getDecodingMethod( builder.context.encoding ) : [ this.method ],
factor = this.factor ? this.factor.build( builder, 'f' ) : decodingMethod[ 1 ];

var method = builder.include( ColorSpaceNode.Nodes[ encodingMethod[ 0 ] ] );
var method = builder.include( ColorSpaceNode.Nodes[ decodingMethod[ 0 ] ] );

if ( factor ) {

Expand Down

0 comments on commit 8f95052

Please sign in to comment.