メインコンテンツまでスキップ

sprite: Sprite Sheet Coordinate Extraction

Grid-based texture atlas sampling

The sprite function extracts individual sprite coordinates from a sprite sheet organized in a regular grid. Given grid dimensions and a frame index, it calculates the UV coordinates for that specific sprite:

adjustedIndex=index+gridx\text{adjustedIndex} = \text{index} + \text{grid}_x f=1gridf = \frac{1}{\text{grid}} cell=(adjustedIndexgridyadjustedIndexfx)\text{cell} = \begin{pmatrix} \lfloor \text{adjustedIndex} \rfloor \\ \text{grid}_y - \lfloor \text{adjustedIndex} \cdot f_x \rfloor \end{pmatrix} spriteUV=fract((st+cell)f)\text{spriteUV} = \text{fract}((\text{st} + \text{cell}) \cdot f)

This maps the input UV coordinates to the appropriate sprite cell within the atlas.

Grid Layout System

ParameterDescriptionRange
stInput UV coordinates[0,1]2[0,1]^2
gridGrid dimensions (width, height)(1,)2(1, \infty)^2
indexSprite frame index[0,total)[0, \text{total})
ライブエディター
const fragment = () => {
      const tileIndex = floor(uv.x.mul(6)).add(floor(uv.y.mul(6)).mul(6))
      const localUV = fract(uv.mul(6))
      const spriteUV = sprite(localUV, vec2(3, 3), tileIndex.mod(9))
      const gradient = length(spriteUV.sub(0.5)).mul(2)
      const color = mix(vec3(gradient), vec3(1).sub(gradient), step(0.5, gradient))
      return vec4(color, 1)
}