@openhps/core
Version:
Open Hybrid Positioning System - Core component
87 lines (82 loc) • 2.61 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.spritesheetUV = exports.default = void 0;
var _Node = _interopRequireDefault(require("../core/Node.js"));
var _UV = require("../accessors/UV.js");
var _TSLBase = require("../tsl/TSLBase.js");
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
/**
* 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.default {
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 = (0, _UV.uv)(), frameNode = (0, _TSLBase.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 = (0, _TSLBase.vec2)(column, row);
return uvNode.add(uvFrameOffset).mul(scale);
}
}
var _default = exports.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}
*/
const spritesheetUV = exports.spritesheetUV = /*@__PURE__*/(0, _TSLBase.nodeProxy)(SpriteSheetUVNode).setParameterLength(3);