Skip to content

sampleSprite

sampleSprite(tex, frame, options): Node<"vec4"> | TextureNode<"vec4">

Defined in: packages/nodes/src/sprite/sampleSprite.ts:34

Sample a sprite from a texture with frame-based UV mapping. Works with both animated sprites (pass uniform) and static sprites (pass fixed frame).

Parameters

tex

Texture

The sprite texture to sample

frame

Vec4Input

Frame bounds as [x, y, width, height] in UV space (0-1), or a vec4 uniform

options

Optional settings

alphaTest?

FloatInput

Returns

Node<"vec4"> | TextureNode<"vec4">

Sampled color (vec4)

Examples

// Static sprite (full texture)
const color = sampleSprite(texture, [0, 0, 1, 1])
// Static sprite (specific frame)
const frame = spriteSheet.getFrame('idle_0')
const color = sampleSprite(texture, [frame.x, frame.y, frame.width, frame.height])
// Animated sprite (frame uniform updates each tick)
const frameUniform = uniform(new Vector4(0, 0, 0.125, 0.125))
const color = sampleSprite(texture, frameUniform)
// With alpha discard
const color = sampleSprite(texture, frame, { alphaTest: 0.01 })