SpriteSheetUVNode.js 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. import Node from '../core/Node.js';
  2. import ConstNode from '../core/ConstNode.js';
  3. import UVNode from '../accessors/UVNode.js';
  4. import MathNode from '../math/MathNode.js';
  5. import OperatorNode from '../math/OperatorNode.js';
  6. import SplitNode from '../utils/SplitNode.js';
  7. import JoinNode from '../utils/JoinNode.js';
  8. class SpriteSheetUVNode extends Node {
  9. constructor( countNode, uvNode = new UVNode(), frameNode = new ConstNode( 0 ) ) {
  10. super( 'vec2' );
  11. this.countNode = countNode;
  12. this.uvNode = uvNode;
  13. this.frameNode = frameNode;
  14. }
  15. construct() {
  16. const { frameNode, uvNode, countNode } = this;
  17. const one = new ConstNode( 1 );
  18. const width = new SplitNode( countNode, 'x' );
  19. const height = new SplitNode( countNode, 'y' );
  20. const total = new OperatorNode( '*', width, height );
  21. const roundFrame = new MathNode( MathNode.FLOOR, new MathNode( MathNode.MOD, frameNode, total ) );
  22. const frameNum = new OperatorNode( '+', roundFrame, one );
  23. const cell = new MathNode( MathNode.MOD, roundFrame, width );
  24. const row = new MathNode( MathNode.CEIL, new OperatorNode( '/', frameNum, width ) );
  25. const rowInv = new OperatorNode( '-', height, row );
  26. const scale = new OperatorNode( '/', one, countNode );
  27. const uvFrameOffset = new JoinNode( [
  28. new OperatorNode( '*', cell, new SplitNode( scale, 'x' ) ),
  29. new OperatorNode( '*', rowInv, new SplitNode( scale, 'y' ) )
  30. ] );
  31. const uvScale = new OperatorNode( '*', uvNode, scale );
  32. const uvFrame = new OperatorNode( '+', uvScale, uvFrameOffset );
  33. return uvFrame;
  34. }
  35. }
  36. export default SpriteSheetUVNode;