Math3Node.js 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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.prototype = Object.create( THREE.TempNode.prototype );
  12. THREE.Math3Node.prototype.constructor = THREE.Math3Node;
  13. THREE.Math3Node.MIX = 'mix';
  14. THREE.Math3Node.REFRACT = 'refract';
  15. THREE.Math3Node.SMOOTHSTEP = 'smoothstep';
  16. THREE.Math3Node.FACEFORWARD = 'faceforward';
  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 ) {
  22. if ( a > c ) return this.a.getType( builder );
  23. return this.c.getType( builder );
  24. }
  25. else {
  26. if ( b > c ) return this.b.getType( builder );
  27. return this.c.getType( builder );
  28. }
  29. };
  30. THREE.Math3Node.prototype.generate = function( builder, output ) {
  31. var material = builder.material;
  32. var type = this.getType( builder );
  33. var a, b, c,
  34. al = builder.getFormatLength( this.a.getType( builder ) ),
  35. bl = builder.getFormatLength( this.b.getType( builder ) ),
  36. cl = builder.getFormatLength( this.c.getType( builder ) )
  37. // optimzer
  38. switch ( this.method ) {
  39. case THREE.Math3Node.REFRACT:
  40. a = this.a.build( builder, type );
  41. b = this.b.build( builder, type );
  42. c = this.c.build( builder, 'fv1' );
  43. break;
  44. case THREE.Math3Node.MIX:
  45. case THREE.Math3Node.SMOOTHSTEP:
  46. a = this.a.build( builder, type );
  47. b = this.b.build( builder, type );
  48. c = this.c.build( builder, cl == 1 ? 'fv1' : type );
  49. break;
  50. default:
  51. a = this.a.build( builder, type );
  52. b = this.b.build( builder, type );
  53. c = this.c.build( builder, type );
  54. break;
  55. }
  56. return builder.format( this.method + '(' + a + ',' + b + ',' + c + ')', type, output );
  57. };