Skip to content

SpriteTexture

Since 2.2.1

SpriteTexture renders a PNG image file from the assets/ directory. It supports:

  • 9-slice scaling via setBorder(...) — corners stay crisp while the centre stretches.
  • Sprite region selection within the source image via setSprite(x, y, width, height).
  • Wrap modes for tiling or mirroring the centre region.
  • Colour tint via setColor(int).

Registry name: sprite_texture

INFO

Extends TransformTexture — supports rotate(), scale(), transform().


Usage

java
// Whole image, stretched
IGuiTexture icon = SpriteTexture.of("mymod:textures/gui/icon.png");

// 9-slice panel: 4 px border on all sides
IGuiTexture panel = SpriteTexture.of("mymod:textures/gui/panel.png")
    .setBorder(4);

// 9-slice with different left/top/right/bottom borders
IGuiTexture fancy = SpriteTexture.of("mymod:textures/gui/fancy.png")
    .setBorder(4, 4, 4, 4); // left, top, right, bottom

// Sub-region of a sprite sheet (x, y, width, height in pixels)
IGuiTexture frame = SpriteTexture.of("mymod:textures/gui/sheet.png")
    .setSprite(0, 0, 16, 16);

// Tiling background
IGuiTexture tile = SpriteTexture.of("mymod:textures/gui/tile.png")
    .setWrapMode(SpriteTexture.WrapMode.REPEAT);

// Tinted
IGuiTexture tinted = SpriteTexture.of("mymod:textures/gui/icon.png")
    .setColor(0xFF44AAFF);

LSS

css
/* Simple image */
background: sprite(mymod:textures/gui/icon.png);

/* With sprite region (x, y, width, height) */
background: sprite(mymod:textures/gui/sheet.png, 0, 0, 16, 16);

/* With border (left, top, right, bottom) */
background: sprite(mymod:textures/gui/panel.png, 0, 0, 64, 64, 4, 4, 4, 4);

/* With color tint */
background: sprite(mymod:textures/gui/icon.png, 0, 0, 16, 16, 0, 0, 0, 0, #FF44AAFF);

Wrap Modes

ValueDescription
CLAMPThe centre tile is stretched to fill the available area. Default.
REPEATThe centre tile is repeated (tiled).
MIRRORED_REPEATThe centre tile is tiled with alternating mirrors.

Wrap modes only apply to the centre region of a 9-slice sprite. Corners and edges are always stretched.


Fields

NameTypeDescription
imageLocationResourceLocationPath to the PNG file.
spritePositionPositionTop-left pixel of the source region. Default: (0, 0).
spriteSizeSizeWidth × height of the source region in pixels. (0, 0) = full image.
borderLTPositionLeft / top 9-slice border in pixels.
borderRBPositionRight / bottom 9-slice border in pixels.
colorintARGB colour tint. Default: 0xFFFFFFFF (no tint).
wrapModeWrapModeCentre tile wrap mode. Default: CLAMP.

Methods

MethodReturnsDescription
SpriteTexture.of(String)SpriteTextureStatic factory from a resource-location string.
SpriteTexture.of(ResourceLocation)SpriteTextureStatic factory from a ResourceLocation.
setImageLocation(ResourceLocation)SpriteTextureChanges the source image (clears the size cache).
setSprite(int x, int y, int w, int h)SpriteTextureSets the sprite region within the image.
setBorder(int left, int top, int right, int bottom)SpriteTextureSets per-side 9-slice borders.
setBorder(int)SpriteTextureSets all four borders to the same value.
setColor(int)SpriteTextureSets the ARGB tint colour.
setWrapMode(WrapMode)SpriteTextureSets the centre region wrap mode.
getImageSize()SizeReturns the natural size of the loaded image (client-only).
copy()SpriteTextureReturns a deep copy.

Released under the MIT License.