OperatorNode.js 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. /**
  2. * @author sunag / http://www.sunag.com.br/
  3. */
  4. THREE.OperatorNode = function ( a, b, op ) {
  5. THREE.TempNode.call( this );
  6. this.a = a;
  7. this.b = b;
  8. this.op = op || THREE.OperatorNode.ADD;
  9. };
  10. THREE.OperatorNode.ADD = '+';
  11. THREE.OperatorNode.SUB = '-';
  12. THREE.OperatorNode.MUL = '*';
  13. THREE.OperatorNode.DIV = '/';
  14. THREE.OperatorNode.prototype = Object.create( THREE.TempNode.prototype );
  15. THREE.OperatorNode.prototype.constructor = THREE.OperatorNode;
  16. THREE.OperatorNode.prototype.nodeType = "Operator";
  17. THREE.OperatorNode.prototype.getType = function ( builder ) {
  18. var a = this.a.getType( builder );
  19. var b = this.b.getType( builder );
  20. if ( builder.isFormatMatrix( a ) ) {
  21. return 'v4';
  22. } else if ( builder.getFormatLength( b ) > builder.getFormatLength( a ) ) {
  23. // use the greater length vector
  24. return b;
  25. }
  26. return a;
  27. };
  28. THREE.OperatorNode.prototype.generate = function ( builder, output ) {
  29. var material = builder.material,
  30. data = material.getDataNode( this.uuid );
  31. var type = this.getType( builder );
  32. var a = this.a.build( builder, type );
  33. var b = this.b.build( builder, type );
  34. return builder.format( '(' + a + this.op + b + ')', type, output );
  35. };
  36. THREE.OperatorNode.prototype.toJSON = function ( meta ) {
  37. var data = this.getJSONNode( meta );
  38. if ( ! data ) {
  39. data = this.createJSONNode( meta );
  40. data.a = this.a.toJSON( meta ).uuid;
  41. data.b = this.b.toJSON( meta ).uuid;
  42. data.op = this.op;
  43. }
  44. return data;
  45. };