Math3Node.js 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /**
  2. * @author sunag / http://www.sunag.com.br/
  3. */
  4. import { TempNode } from '../core/TempNode.js';
  5. function Math3Node( a, b, c, method ) {
  6. TempNode.call( this );
  7. this.a = a;
  8. this.b = b;
  9. this.c = c;
  10. this.method = method || Math3Node.MIX;
  11. };
  12. Math3Node.MIX = 'mix';
  13. Math3Node.REFRACT = 'refract';
  14. Math3Node.SMOOTHSTEP = 'smoothstep';
  15. Math3Node.FACEFORWARD = 'faceforward';
  16. Math3Node.prototype = Object.create( TempNode.prototype );
  17. Math3Node.prototype.constructor = Math3Node;
  18. Math3Node.prototype.nodeType = "Math3";
  19. Math3Node.prototype.getType = function ( builder ) {
  20. var a = builder.getTypeLength( this.a.getType( builder ) );
  21. var b = builder.getTypeLength( this.b.getType( builder ) );
  22. var c = builder.getTypeLength( this.c.getType( builder ) );
  23. if ( a > b && a > c ) {
  24. return this.a.getType( builder );
  25. } else if ( b > c ) {
  26. return this.b.getType( builder );
  27. }
  28. return this.c.getType( builder );
  29. };
  30. Math3Node.prototype.generate = function ( builder, output ) {
  31. var a, b, c,
  32. al = builder.getTypeLength( this.a.getType( builder ) ),
  33. bl = builder.getTypeLength( this.b.getType( builder ) ),
  34. cl = builder.getTypeLength( this.c.getType( builder ) ),
  35. type = this.getType( builder );
  36. // optimzer
  37. switch ( this.method ) {
  38. case Math3Node.REFRACT:
  39. a = this.a.build( builder, type );
  40. b = this.b.build( builder, type );
  41. c = this.c.build( builder, 'f' );
  42. break;
  43. case Math3Node.MIX:
  44. a = this.a.build( builder, type );
  45. b = this.b.build( builder, type );
  46. c = this.c.build( builder, cl === 1 ? 'f' : type );
  47. break;
  48. default:
  49. a = this.a.build( builder, type );
  50. b = this.b.build( builder, type );
  51. c = this.c.build( builder, type );
  52. break;
  53. }
  54. return builder.format( this.method + '( ' + a + ', ' + b + ', ' + c + ' )', type, output );
  55. };
  56. Math3Node.prototype.copy = function ( source ) {
  57. TempNode.prototype.copy.call( this, source );
  58. this.a = source.a;
  59. this.b = source.b;
  60. this.c = source.c;
  61. this.method = source.method;
  62. };
  63. Math3Node.prototype.toJSON = function ( meta ) {
  64. var data = this.getJSONNode( meta );
  65. if ( ! data ) {
  66. data = this.createJSONNode( meta );
  67. data.a = this.a.toJSON( meta ).uuid;
  68. data.b = this.b.toJSON( meta ).uuid;
  69. data.c = this.c.toJSON( meta ).uuid;
  70. data.method = this.method;
  71. }
  72. return data;
  73. };
  74. export { Math3Node };