Просмотр исходного кода

SpriteSheetUVNode: Transform to TSL function and fix `countNode` access. (#32693)

Match 1 месяц назад
Родитель
Сommit
8485acc415
4 измененных файлов с 36 добавлено и 92 удалено
  1. 0 1
      src/nodes/Nodes.js
  2. 1 1
      src/nodes/TSL.js
  3. 35 0
      src/nodes/utils/SpriteSheetUV.js
  4. 0 90
      src/nodes/utils/SpriteSheetUVNode.js

+ 0 - 1
src/nodes/Nodes.js

@@ -49,7 +49,6 @@ export { default as RemapNode } from './utils/RemapNode.js';
 export { default as RotateNode } from './utils/RotateNode.js';
 export { default as SetNode } from './utils/SetNode.js';
 export { default as SplitNode } from './utils/SplitNode.js';
-export { default as SpriteSheetUVNode } from './utils/SpriteSheetUVNode.js';
 export { default as StorageArrayElementNode } from './utils/StorageArrayElementNode.js';
 export { default as ReflectorNode } from './utils/ReflectorNode.js';
 export { default as RTTNode } from './utils/RTTNode.js';

+ 1 - 1
src/nodes/TSL.js

@@ -40,7 +40,7 @@ export * from './utils/UVUtils.js';
 export * from './utils/SpriteUtils.js';
 export * from './utils/ViewportUtils.js';
 export * from './utils/RotateNode.js';
-export * from './utils/SpriteSheetUVNode.js';
+export * from './utils/SpriteSheetUV.js';
 export * from './utils/Timer.js';
 export * from './utils/TriplanarTextures.js';
 export * from './utils/ReflectorNode.js';

+ 35 - 0
src/nodes/utils/SpriteSheetUV.js

@@ -0,0 +1,35 @@
+import { uv } from '../accessors/UV.js';
+import { Fn, float, vec2 } from '../tsl/TSLBase.js';
+
+/**
+ * TSL function for computing texture coordinates for animated sprite sheets.
+ *
+ * ```js
+ * const uvNode = spritesheetUV( vec2( 6, 6 ), uv(), time.mul( animationSpeed ) );
+ *
+ * material.colorNode = texture( spriteSheet, uvNode );
+ * ```
+ *
+ * @tsl
+ * @function
+ * @param {Node<vec2>} countNode - The node that defines the number of sprites in the x and y direction (e.g 6x6).
+ * @param {?Node<vec2>} [uvNode=uv()] - The uv node.
+ * @param {?Node<float>} [frameNode=float(0)] - The node that defines the current frame/sprite.
+ * @returns {Node<vec2>}
+ */
+export const spritesheetUV = /*@__PURE__*/ Fn( ( [ countNode, uvNode = uv(), frameNode = float( 0 ) ] ) => {
+
+	const width = countNode.x;
+	const height = countNode.y;
+
+	const frameNum = frameNode.mod( width.mul( height ) ).floor();
+
+	const column = frameNum.mod( width );
+	const row = height.sub( frameNum.add( 1 ).div( width ).ceil() );
+
+	const scale = countNode.reciprocal();
+	const uvFrameOffset = vec2( column, row );
+
+	return uvNode.add( uvFrameOffset ).mul( scale );
+
+} );

+ 0 - 90
src/nodes/utils/SpriteSheetUVNode.js

@@ -1,90 +0,0 @@
-import Node from '../core/Node.js';
-import { uv } from '../accessors/UV.js';
-import { nodeProxy, float, vec2 } from '../tsl/TSLBase.js';
-
-/**
- * Can be used to compute texture coordinates for animated sprite sheets.
- *
- * ```js
- * const uvNode = spritesheetUV( vec2( 6, 6 ), uv(), time.mul( animationSpeed ) );
- *
- * material.colorNode = texture( spriteSheet, uvNode );
- * ```
- *
- * @augments Node
- */
-class SpriteSheetUVNode extends Node {
-
-	static get type() {
-
-		return 'SpriteSheetUVNode';
-
-	}
-
-	/**
-	 * Constructs a new sprite sheet uv node.
-	 *
-	 * @param {Node<vec2>} countNode - The node that defines the number of sprites in the x and y direction (e.g 6x6).
-	 * @param {Node<vec2>} [uvNode=uv()] - The uv node.
-	 * @param {Node<float>} [frameNode=float()] - The node that defines the current frame/sprite.
-	 */
-	constructor( countNode, uvNode = uv(), frameNode = float( 0 ) ) {
-
-		super( 'vec2' );
-
-		/**
-		 * The node that defines the number of sprites in the x and y direction (e.g 6x6).
-		 *
-		 * @type {Node<vec2>}
-		 */
-		this.countNode = countNode;
-
-		/**
-		 * The uv node.
-		 *
-		 * @type {Node<vec2>}
-		 */
-		this.uvNode = uvNode;
-
-		/**
-		 * The node that defines the current frame/sprite.
-		 *
-		 * @type {Node<float>}
-		 */
-		this.frameNode = frameNode;
-
-	}
-
-	setup() {
-
-		const { frameNode, uvNode, countNode } = this;
-
-		const { width, height } = countNode;
-
-		const frameNum = frameNode.mod( width.mul( height ) ).floor();
-
-		const column = frameNum.mod( width );
-		const row = height.sub( frameNum.add( 1 ).div( width ).ceil() );
-
-		const scale = countNode.reciprocal();
-		const uvFrameOffset = vec2( column, row );
-
-		return uvNode.add( uvFrameOffset ).mul( scale );
-
-	}
-
-}
-
-export default SpriteSheetUVNode;
-
-/**
- * TSL function for creating a sprite sheet uv node.
- *
- * @tsl
- * @function
- * @param {Node<vec2>} countNode - The node that defines the number of sprites in the x and y direction (e.g 6x6).
- * @param {?Node<vec2>} [uvNode=uv()] - The uv node.
- * @param {?Node<float>} [frameNode=float()] - The node that defines the current frame/sprite.
- * @returns {SpriteSheetUVNode}
- */
-export const spritesheetUV = /*@__PURE__*/ nodeProxy( SpriteSheetUVNode ).setParameterLength( 3 );

粤ICP备19079148号