Textures
Clay backend implementation of texture management. Handles loading, creating, and managing GPU textures with reference counting.
Features:
- Automatic texture caching and reference counting
- Support for loading from files, URLs, or raw bytes
- Render texture creation for off-screen rendering
- Texture filtering and wrapping configuration
- PNG export functionality
- Multi-texture batching support
The class maintains a cache of loaded textures to avoid duplicate loads and uses reference counting to manage texture lifetime.
Instance Members
Loads a texture from a file path or URL. Implements automatic caching and reference counting to avoid duplicate loads.
Features:
- Supports both local files and HTTP(S) URLs
- Automatic texture caching with reference counting
- Synchronous or asynchronous loading
- Optional alpha premultiplication
- Queues callbacks if texture is already loading
Name | Type | Default | Description |
---|---|---|---|
path |
String | File path (relative to assets) or URL to load | |
options |
Null<LoadTextureOptions> | (optional) | Loading options (sync/async, premultiply alpha, etc.) |
_done |
Function | Callback when texture is loaded or failed (null on failure) |
loadFromBytes(bytes: haxe.io.Bytes, type: ceramic.ImageType, ?options: Null<LoadTextureOptions>, _done: Function): Void
Creates a texture from raw image bytes. Useful for dynamically generated images or data loaded from custom sources.
Name | Type | Default | Description |
---|---|---|---|
bytes |
haxe.io.Bytes | Raw image data | |
type |
ceramic.ImageType | Image format (PNG, JPEG, GIF) | |
options |
Null<LoadTextureOptions> | (optional) | Loading options (sync/async, premultiply alpha) |
_done |
Function | Callback when texture is created or failed (null on failure) |
Indicates whether hot-reloading of texture files is supported. Clay backend supports watching texture files for changes.
Returns | Description |
---|---|
Bool | Always returns true for Clay backend |
Creates a texture from raw pixel data. The pixels should be in RGBA format with 8 bits per channel.
Name | Type | Description |
---|---|---|
width |
Int | Texture width in pixels |
height |
Int | Texture height in pixels |
pixels |
ceramic.UInt8Array | Raw RGBA pixel data (width * height * 4 bytes) |
Returns | Description |
---|---|
Texture | Created texture with reference count of 1 |
createRenderTarget(width: Int, height: Int, depth: Bool, stencil: Bool, antialiasing: Int): Texture
Creates a render texture for off-screen rendering. Render textures can be used as drawing targets for post-processing effects.
Name | Type | Description |
---|---|---|
width |
Int | Texture width in pixels |
height |
Int | Texture height in pixels |
depth |
Bool | Whether to create a depth buffer |
stencil |
Bool | Whether to create a stencil buffer |
antialiasing |
Int | Antialiasing samples (0 = disabled, WebGL2+ required on web) |
Returns | Description |
---|---|
Texture | Created render texture with reference count of 1 |
Reads pixel data from a texture. Retrieves the current pixel contents from GPU memory.
Name | Type | Default | Description |
---|---|---|---|
texture |
Texture | The texture to read from | |
result |
Null<ceramic.UInt8Array> | (optional) | Optional array to store results (created if null) |
Returns | Description |
---|---|
ceramic.UInt8Array | Array containing RGBA pixel data |
Updates texture pixels on the GPU. Uploads new pixel data to an existing texture.
Name | Type | Description |
---|---|---|
texture |
Texture | The texture to update |
pixels |
ceramic.UInt8Array | New RGBA pixel data (must match texture dimensions) |
Destroys a texture and releases GPU resources. Decrements reference count and only destroys when count reaches zero.
Name | Type | Description |
---|---|---|
texture |
Texture | The texture to destroy |
Gets the GPU texture ID.
Name | Type | Description |
---|---|---|
texture |
Texture | The texture |
Returns | Description |
---|---|
TextureId | OpenGL texture ID |
Gets the texture width in pixels.
Name | Type | Description |
---|---|---|
texture |
Texture | The texture |
Returns | Description |
---|---|
Int | Width in pixels |
Gets the texture height in pixels.
Name | Type | Description |
---|---|---|
texture |
Texture | The texture |
Returns | Description |
---|---|
Int | Height in pixels |
Gets the actual texture width on GPU (may be power of 2).
Name | Type | Description |
---|---|---|
texture |
Texture | The texture |
Returns | Description |
---|---|
Int | Actual width in GPU memory |
Gets the actual texture height on GPU (may be power of 2).
Name | Type | Description |
---|---|---|
texture |
Texture | The texture |
Returns | Description |
---|---|
Int | Actual height in GPU memory |
Gets the texture index for multi-texture batching.
Name | Type | Description |
---|---|---|
texture |
Texture | The texture |
Returns | Description |
---|---|
Int | Texture slot index |
Sets the texture filtering mode. LINEAR provides smooth interpolation, NEAREST provides pixelated look.
Name | Type | Description |
---|---|---|
texture |
Texture | The texture to configure |
filter |
Anonymous | Filter mode (LINEAR or NEAREST) |
Sets the horizontal texture wrapping mode. Controls how the texture repeats or clamps at U coordinates outside 0-1.
Name | Type | Description |
---|---|---|
texture |
Texture | The texture to configure |
wrap |
ceramic.TextureWrap | Wrap mode (CLAMP, REPEAT, or MIRROR) |
Sets the vertical texture wrapping mode. Controls how the texture repeats or clamps at V coordinates outside 0-1.
Name | Type | Description |
---|---|---|
texture |
Texture | The texture to configure |
wrap |
ceramic.TextureWrap | Wrap mode (CLAMP, REPEAT, or MIRROR) |
Returns the maximum texture width supported by the GPU.
Returns | Description |
---|---|
Int | Maximum width in pixels (2048 on web, GPU-specific on native) |
Returns the maximum texture height supported by the GPU.
Returns | Description |
---|---|
Int | Maximum height in pixels (2048 on web, GPU-specific on native) |
Returns the maximum number of textures that can be used in a single batch. Values above 1 indicate multi-texture batching support for improved performance.
Returns | Description |
---|---|
Int | Maximum texture units (capped at 32) |
textureToPng(texture: Texture, ?reversePremultiplyAlpha: Bool = true, ?path: String, done: Function): Void
Exports a texture to PNG format. Native implementation using STB image write.
Name | Type | Default | Description |
---|---|---|---|
texture |
Texture | The texture to export | |
reversePremultiplyAlpha |
Bool | true |
Whether to reverse premultiplied alpha |
path |
String | (optional) | Optional file path to save to (returns bytes if null) |
done |
Function | Callback with PNG data bytes (null if path provided) |
pixelsToPng(width: Int, height: Int, pixels: ceramic.UInt8Array, ?path: String, done: Function): Void
Exports raw pixel data to PNG format. Native implementation using STB image write.
Name | Type | Default | Description |
---|---|---|---|
width |
Int | Image width in pixels | |
height |
Int | Image height in pixels | |
pixels |
ceramic.UInt8Array | Raw RGBA pixel data | |
path |
String | (optional) | Optional file path to save to (returns bytes if null) |
done |
Function | Callback with PNG data bytes (null if path provided) |
Private Members
Counter for unique render texture IDs
Counter for unique pixel texture IDs
Counter for unique byte-loaded texture IDs
Map of loading textures to their callbacks
Cache of loaded textures by path/ID
Reference count for each loaded texture
Queries GPU for maximum texture size if not already cached. @private
Queries GPU for maximum texture units if not already cached. @private