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. constructor( method = OscNode.SINE, timeNode = new TimerNode() ) {
  6. super();
  7. this.method = method;
  8. this.timeNode = timeNode;
  9. }
  10. getNodeType( builder ) {
  11. return this.timeNode.getNodeType( builder );
  12. }
  13. construct() {
  14. const method = this.method;
  15. const timeNode = this.timeNode;
  16. let outputNode = null;
  17. if ( method === OscNode.SINE ) {
  18. outputNode = add( mul( sin( mul( add( timeNode, .75 ), Math.PI * 2 ) ), .5 ), .5 );
  19. } else if ( method === OscNode.SQUARE ) {
  20. outputNode = round( fract( timeNode ) );
  21. } else if ( method === OscNode.TRIANGLE ) {
  22. outputNode = abs( sub( 1, mul( fract( add( timeNode, .5 ) ), 2 ) ) );
  23. } else if ( method === OscNode.SAWTOOTH ) {
  24. outputNode = fract( timeNode );
  25. }
  26. return outputNode;
  27. }
  28. serialize( data ) {
  29. super.serialize( data );
  30. data.method = this.method;
  31. }
  32. deserialize( data ) {
  33. super.deserialize( data );
  34. this.method = data.method;
  35. }
  36. }
  37. OscNode.SINE = 'sine';
  38. OscNode.SQUARE = 'square';
  39. OscNode.TRIANGLE = 'triangle';
  40. OscNode.SAWTOOTH = 'sawtooth';
  41. export default OscNode;