OscNode.js 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. import Node from '../core/Node.js';
  2. import TimerNode from './TimerNode.js';
  3. import { abs, fract, round, sin, add, sub, mul } from '../shadernode/ShaderNodeBaseElements.js';
  4. class OscNode extends Node {
  5. static SINE = 'sine';
  6. static SQUARE = 'square';
  7. static TRIANGLE = 'triangle';
  8. static SAWTOOTH = 'sawtooth';
  9. constructor( method = OscNode.SINE, timeNode = new TimerNode() ) {
  10. super();
  11. this.method = method;
  12. this.timeNode = timeNode;
  13. }
  14. getNodeType( builder ) {
  15. return this.timeNode.getNodeType( builder );
  16. }
  17. generate( builder ) {
  18. const method = this.method;
  19. const timeNode = this.timeNode;
  20. let outputNode = null;
  21. if ( method === OscNode.SINE ) {
  22. outputNode = add( mul( sin( mul( add( timeNode, .75 ), Math.PI * 2 ) ), .5 ), .5 );
  23. } else if ( method === OscNode.SQUARE ) {
  24. outputNode = round( fract( timeNode ) );
  25. } else if ( method === OscNode.TRIANGLE ) {
  26. outputNode = abs( sub( 1, mul( fract( add( timeNode, .5 ) ), 2 ) ) );
  27. } else if ( method === OscNode.SAWTOOTH ) {
  28. outputNode = fract( timeNode );
  29. }
  30. return outputNode.build( builder );
  31. }
  32. serialize( data ) {
  33. super.serialize( data );
  34. data.method = this.method;
  35. }
  36. deserialize( data ) {
  37. super.deserialize( data );
  38. this.method = data.method;
  39. }
  40. }
  41. export default OscNode;