Math3Node.js 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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.toJSON = function ( meta ) {
  54. var data = this.getJSONNode( meta );
  55. if ( ! data ) {
  56. data = this.createJSONNode( meta );
  57. data.a = this.a.toJSON( meta ).uuid;
  58. data.b = this.b.toJSON( meta ).uuid;
  59. data.c = this.c.toJSON( meta ).uuid;
  60. data.method = this.method;
  61. }
  62. return data;
  63. };