Math3Node.js 2.4 KB

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