SpriteSheetUVNode.js 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. import Node from '../core/Node.js';
  2. import FloatNode from '../inputs/FloatNode.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( count, uv = new UVNode() ) {
  10. super( 'vec2' );
  11. this.count = count;
  12. this.uv = uv;
  13. this.frame = new FloatNode( 0 ).setConst( true );
  14. }
  15. generate( builder, output ) {
  16. const nodeData = builder.getDataFromNode( this );
  17. let uvFrame = nodeData.uvFrame;
  18. if ( nodeData.uvFrame === undefined ) {
  19. const uv = this.uv;
  20. const count = this.count;
  21. const frame = this.frame;
  22. const one = new FloatNode( 1 ).setConst( true );
  23. const width = new SplitNode( count, 'x' );
  24. const height = new SplitNode( count, 'y' );
  25. const total = new OperatorNode( '*', width, height );
  26. const roundFrame = new MathNode( MathNode.FLOOR, new MathNode( MathNode.MOD, frame, total ) );
  27. const frameNum = new OperatorNode( '+', roundFrame, one );
  28. const cell = new MathNode( MathNode.MOD, roundFrame, width );
  29. const row = new MathNode( MathNode.CEIL, new OperatorNode( '/', frameNum, width ) );
  30. const rowInv = new OperatorNode( '-', height, row );
  31. const scale = new OperatorNode( '/', one, count );
  32. const uvFrameOffset = new JoinNode( [
  33. new OperatorNode( '*', cell, new SplitNode( scale, 'x' ) ),
  34. new OperatorNode( '*', rowInv, new SplitNode( scale, 'y' ) )
  35. ] );
  36. const uvScale = new OperatorNode( '*', uv, scale );
  37. uvFrame = new OperatorNode( '+', uvScale, uvFrameOffset );
  38. nodeData.uvFrame = uvFrame;
  39. }
  40. return uvFrame.build( builder, output );
  41. }
  42. }
  43. export default SpriteSheetUVNode;