Math2Node.js 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /**
  2. * @author sunag / http://www.sunag.com.br/
  3. */
  4. THREE.Math2Node = function( a, b, method ) {
  5. THREE.TempNode.call( this );
  6. this.a = a;
  7. this.b = b;
  8. this.method = method || THREE.Math2Node.DISTANCE;
  9. };
  10. THREE.Math2Node.MIN = 'min';
  11. THREE.Math2Node.MAX = 'max';
  12. THREE.Math2Node.MOD = 'mod';
  13. THREE.Math2Node.STEP = 'step';
  14. THREE.Math2Node.REFLECT = 'reflect';
  15. THREE.Math2Node.DISTANCE = 'distance';
  16. THREE.Math2Node.DOT = 'dot';
  17. THREE.Math2Node.CROSS = 'cross';
  18. THREE.Math2Node.POW = 'pow';
  19. THREE.Math2Node.prototype = Object.create( THREE.TempNode.prototype );
  20. THREE.Math2Node.prototype.constructor = THREE.Math2Node;
  21. THREE.Math2Node.prototype.getInputType = function( builder ) {
  22. // use the greater length vector
  23. if ( builder.getFormatLength( this.b.getType( builder ) ) > builder.getFormatLength( this.a.getType( builder ) ) ) {
  24. return this.b.getType( builder );
  25. }
  26. return this.a.getType( builder );
  27. };
  28. THREE.Math2Node.prototype.getType = function( builder ) {
  29. switch ( this.method ) {
  30. case THREE.Math2Node.DISTANCE:
  31. case THREE.Math2Node.DOT:
  32. return 'fv1';
  33. case THREE.Math2Node.CROSS:
  34. return 'v3';
  35. }
  36. return this.getInputType( builder );
  37. };
  38. THREE.Math2Node.prototype.generate = function( builder, output ) {
  39. var material = builder.material;
  40. var type = this.getInputType( builder );
  41. var a, b,
  42. al = builder.getFormatLength( this.a.getType( builder ) ),
  43. bl = builder.getFormatLength( this.b.getType( builder ) );
  44. // optimzer
  45. switch ( this.method ) {
  46. case THREE.Math2Node.CROSS:
  47. a = this.a.build( builder, 'v3' );
  48. b = this.b.build( builder, 'v3' );
  49. break;
  50. case THREE.Math2Node.STEP:
  51. a = this.a.build( builder, al == 1 ? 'fv1' : type );
  52. b = this.b.build( builder, type );
  53. break;
  54. case THREE.Math2Node.MIN:
  55. case THREE.Math2Node.MAX:
  56. case THREE.Math2Node.MOD:
  57. a = this.a.build( builder, type );
  58. b = this.b.build( builder, bl == 1 ? 'fv1' : type );
  59. break;
  60. default:
  61. a = this.a.build( builder, type );
  62. b = this.b.build( builder, type );
  63. break;
  64. }
  65. return builder.format( this.method + '(' + a + ',' + b + ')', this.getType( builder ), output );
  66. };