NodeMath3.js 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. /**
  2. * @author sunag / http://www.sunag.com.br/
  3. */
  4. THREE.NodeMath3 = function( a, b, c, method ) {
  5. THREE.NodeTemp.call( this );
  6. this.a = a;
  7. this.b = b;
  8. this.c = c;
  9. this.method = method || THREE.NodeMath3.MIX;
  10. };
  11. THREE.NodeMath3.prototype = Object.create( THREE.NodeTemp.prototype );
  12. THREE.NodeMath3.prototype.constructor = THREE.NodeMath3;
  13. THREE.NodeMath3.MIX = 'mix';
  14. THREE.NodeMath3.REFRACT = 'refract';
  15. THREE.NodeMath3.SMOOTHSTEP = 'smoothstep';
  16. THREE.NodeMath3.FACEFORWARD = 'faceforward';
  17. THREE.NodeMath3.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.NodeMath3.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.NodeMath3.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.NodeMath3.MIX:
  45. case THREE.NodeMath3.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. };