Math3Node.js 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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.getType = function( builder ) {
  18. var a = builder.getFormatLength( this.a.getType( builder ) );
  19. var b = builder.getFormatLength( this.b.getType( builder ) );
  20. var c = builder.getFormatLength( this.c.getType( builder ) );
  21. if ( a > b && a > c ) return this.a.getType( builder );
  22. else if ( b > c ) return this.b.getType( builder );
  23. return this.c.getType( builder );
  24. };
  25. THREE.Math3Node.prototype.generate = function( builder, output ) {
  26. var material = builder.material;
  27. var type = this.getType( builder );
  28. var a, b, c,
  29. al = builder.getFormatLength( this.a.getType( builder ) ),
  30. bl = builder.getFormatLength( this.b.getType( builder ) ),
  31. cl = builder.getFormatLength( this.c.getType( builder ) );
  32. // optimzer
  33. switch ( this.method ) {
  34. case THREE.Math3Node.REFRACT:
  35. a = this.a.build( builder, type );
  36. b = this.b.build( builder, type );
  37. c = this.c.build( builder, 'fv1' );
  38. break;
  39. case THREE.Math3Node.MIX:
  40. a = this.a.build( builder, type );
  41. b = this.b.build( builder, type );
  42. c = this.c.build( builder, cl == 1 ? 'fv1' : type );
  43. break;
  44. default:
  45. a = this.a.build( builder, type );
  46. b = this.b.build( builder, type );
  47. c = this.c.build( builder, type );
  48. break;
  49. }
  50. return builder.format( this.method + '(' + a + ',' + b + ',' + c + ')', type, output );
  51. };