SwitchNode.js 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. /**
  2. * @author sunag / http://www.sunag.com.br/
  3. */
  4. THREE.SwitchNode = function( node, components ) {
  5. THREE.GLNode.call( this, 'fv1' );
  6. this.node = node;
  7. this.components = components || 'x';
  8. };
  9. THREE.SwitchNode.prototype = Object.create( THREE.GLNode.prototype );
  10. THREE.SwitchNode.prototype.constructor = THREE.SwitchNode;
  11. THREE.SwitchNode.prototype.getType = function( builder ) {
  12. return builder.getFormatByLength( this.components.length );
  13. };
  14. THREE.SwitchNode.prototype.generate = function( builder, output ) {
  15. var type = this.node.getType( builder );
  16. var inputLength = builder.getFormatLength( type ) - 1;
  17. var components = builder.colorToVector( this.components );
  18. var node = this.node.build( builder, type );
  19. var outputLength = 0;
  20. var i, len = components.length;
  21. // get max length
  22. for ( i = 0; i < len; i ++ ) {
  23. outputLength = Math.max( outputLength, builder.getIndexByElement( components.charAt( i ) ) );
  24. }
  25. if ( outputLength > inputLength ) outputLength = inputLength;
  26. // build switch
  27. node += '.';
  28. for ( i = 0; i < len; i ++ ) {
  29. var elm = components.charAt( i );
  30. var idx = builder.getIndexByElement( components.charAt( i ) );
  31. if ( idx > outputLength ) idx = outputLength;
  32. if ( builder.getElementByIndex( idx ) == undefined ) {
  33. console.log( builder.getElementByIndex( idx ) );
  34. }
  35. node += builder.getElementByIndex( idx );
  36. }
  37. return builder.format( node, this.getType( builder ), output );
  38. };