Browse Source

add support for binding to materials.

Ben Houston 10 years ago
parent
commit
1e219a674a
1 changed files with 19 additions and 9 deletions
  1. 19 9
      src/animation/PropertyBinding.js

+ 19 - 9
src/animation/PropertyBinding.js

@@ -15,6 +15,7 @@ THREE.PropertyBinding = function ( rootNode, trackName ) {
 
 
 	this.directoryName = parseResults.directoryName || null;
 	this.directoryName = parseResults.directoryName || null;
 	this.nodeName = parseResults.nodeName;
 	this.nodeName = parseResults.nodeName;
+	this.material = parseResults.material;
 	this.propertyName = parseResults.propertyName || null;
 	this.propertyName = parseResults.propertyName || null;
 	this.propertyArrayIndex = parseResults.propertyArrayIndex || -1;
 	this.propertyArrayIndex = parseResults.propertyArrayIndex || -1;
 
 
@@ -30,14 +31,20 @@ THREE.PropertyBinding.prototype = {
 
 
 		 console.log( "PropertyBinding.set( " + value + ")" );
 		 console.log( "PropertyBinding.set( " + value + ")" );
 
 
+		 var targetObject = this.node;
+
  		// ensure there is a value node
  		// ensure there is a value node
-		if( ! this.node ) {
+		if( ! targetObject ) {
 			console.log( "  trying to update node for track: " + this.trackName + " but it wasn't found." );
 			console.log( "  trying to update node for track: " + this.trackName + " but it wasn't found." );
 			return;
 			return;
 		}
 		}
 
 
+		if( this.material ) {
+			targetObject = targetObject.material;
+		}
+
  		// ensure there is a value property on the node
  		// ensure there is a value property on the node
-		var nodeProperty = this.node[ this.propertyName ];
+		var nodeProperty = targetObject[ this.propertyName ];
 		if( ! nodeProperty ) {
 		if( ! nodeProperty ) {
 			console.log( "  trying to update property for track: " + this.nodeName + '.' + this.propertyName + " but it wasn't found.", this );				
 			console.log( "  trying to update property for track: " + this.nodeName + '.' + this.propertyName + " but it wasn't found.", this );				
 			return;
 			return;
@@ -56,17 +63,17 @@ THREE.PropertyBinding.prototype = {
 		// otherwise just set the property directly on the node (do not use nodeProperty as it may not be a reference object)
 		// otherwise just set the property directly on the node (do not use nodeProperty as it may not be a reference object)
 		else {
 		else {
 			console.log( '  update property ' + this.name + '.' + this.propertyName + ' via assignment.' );				
 			console.log( '  update property ' + this.name + '.' + this.propertyName + ' via assignment.' );				
-			this.node[ this.propertyName ] = value;	
+			targetObject[ this.propertyName ] = value;	
 		}
 		}
 
 
 		// trigger node dirty			
 		// trigger node dirty			
-		if( this.node.needsUpdate !== undefined ) { // material
+		if( targetObject.needsUpdate !== undefined ) { // material
 			console.log( '  triggering material as dirty' );
 			console.log( '  triggering material as dirty' );
 			this.node.needsUpdate = true;
 			this.node.needsUpdate = true;
 		}			
 		}			
-		if( this.node.matrixWorldNeedsUpdate !== undefined ) { // node transform
+		if( targetObject.matrixWorldNeedsUpdate !== undefined ) { // node transform
 			console.log( '  triggering node as dirty' );
 			console.log( '  triggering node as dirty' );
-			this.node.matrixWorldNeedsUpdate = true;
+			targetObject.matrixWorldNeedsUpdate = true;
 		}
 		}
 
 
 	},
 	},
@@ -85,12 +92,14 @@ THREE.PropertyBinding.parseTrackName = function( trackName ) {
 	// matches strings in the form of:
 	// matches strings in the form of:
 	//    nodeName.property
 	//    nodeName.property
 	//    nodeName.property[accessor]
 	//    nodeName.property[accessor]
+	//    nodeName.material.property[accessor]
 	//    uuid.property[accessor]
 	//    uuid.property[accessor]
+	//    uuid.material.property[accessor]
 	//    parentName/nodeName.property
 	//    parentName/nodeName.property
 	//    parentName/parentName/nodeName.property[index]
 	//    parentName/parentName/nodeName.property[index]
 	// created and tested via https://regex101.com/#javascript
 	// created and tested via https://regex101.com/#javascript
 
 
-	var re = /^(([\w]+\/)*)([\w-]+)?(\.([\w]+)(\[([\w]+)\])?)?$/; 
+	var re = /^(([\w]+\/)*)([\w-]+)?(\.material)?(\.([\w]+)(\[([\w]+)\])?)?$/; 
 	var matches = re.exec(trackName);
 	var matches = re.exec(trackName);
 
 
 	if( ! matches ) {
 	if( ! matches ) {
@@ -104,8 +113,9 @@ THREE.PropertyBinding.parseTrackName = function( trackName ) {
 	var results = {
 	var results = {
 		directoryName: matches[0],
 		directoryName: matches[0],
 		nodeName: matches[3], 	// allowed to be null, specified root node.
 		nodeName: matches[3], 	// allowed to be null, specified root node.
-		propertyName: matches[5],
-		propertySubElement: matches[7]	// allowed to be null, specifies that the whole property is set.
+		material: ( matches[4] && matches[4].length > 0 ),
+		propertyName: matches[6],
+		propertySubElement: matches[8]	// allowed to be null, specifies that the whole property is set.
 	};
 	};
 
 
 	console.log( "PropertyBinding.parseTrackName", trackName, results, matches );
 	console.log( "PropertyBinding.parseTrackName", trackName, results, matches );