2
0

Math3Node.js 2.3 KB

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