tiramisu/asset

Asset Management System - loading and caching for textures, models, and audio.

Provides declarative asset loading with progress tracking, caching, and batch loading capabilities with LRU eviction.

Quick Example

import tiramisu/asset

// Load a texture
let load_effect = asset.load_texture("player.png")
  |> promise.map(fn(result) {
    case result {
      Ok(texture) -> TextureLoaded(texture)
      Error(err) -> LoadFailed(err)
    }
  })
  |> effect.from_promise

// Use with cache
let cache = asset.new_cache()
let cache = asset.insert_texture(cache, "player.png", texture)
let texture = asset.get_texture(cache, "player.png")

Types

Asset cache/registry with LRU eviction

pub opaque type AssetCache

Asset loading error

pub type AssetError {
  AssetLoadError(url: String, reason: String)
  AssetNotFound(url: String)
  InvalidAssetType(url: String)
}

Constructors

  • AssetLoadError(url: String, reason: String)
  • AssetNotFound(url: String)
  • InvalidAssetType(url: String)

Types of asset that can be loaded

pub type AssetType {
  ModelAsset(url: String)
  TextureAsset(url: String)
  AudioAsset(url: String)
  STLAsset(url: String)
}

Constructors

  • ModelAsset(url: String)

    GLTF/GLB 3D model

  • TextureAsset(url: String)

    Texture image (PNG, JPG, etc.)

  • AudioAsset(url: String)

    Audio file (MP3, WAV, OGG)

  • STLAsset(url: String)

    STL 3D model

Result of batch loading

pub type BatchLoadResult {
  BatchLoadResult(cache: AssetCache, errors: List(AssetError))
}

Constructors

Asset cache configuration

pub type CacheConfig {
  CacheConfig(max_size: Int, current_time: Int)
}

Constructors

  • CacheConfig(max_size: Int, current_time: Int)
pub type GLTFData {
  GLTFData(
    scene: object3d.Object3D,
    animations: List(object3d.AnimationClip),
  )
}

Constructors

STL loading error

pub type LoadError {
  LoadError(String)
  InvalidUrl(String)
  ParseError(String)
}

Constructors

  • LoadError(String)
  • InvalidUrl(String)
  • ParseError(String)

Progress information for batch loading

pub type LoadProgress {
  LoadProgress(loaded: Int, total: Int, current_url: String)
}

Constructors

  • LoadProgress(loaded: Int, total: Int, current_url: String)

A loaded asset (opaque to enforce type safety)

pub opaque type LoadedAsset

Values

pub fn cache_size(cache: AssetCache) -> Int

Get the number of cached asset

pub fn cached_urls(cache: AssetCache) -> List(String)

Get all cached URLs

pub fn clear_cache(cache: AssetCache) -> AssetCache

Clear all cached asset

pub fn dispose_geometry(geometry: scene.BufferGeometry) -> Nil

Dispose of a geometry and free GPU memory

pub fn dispose_object3d(object: object3d.Object3D) -> Nil

Dispose of an Object3D and all its resources (geometry, materials, textures, children)

pub fn dispose_texture(texture: scene.Texture) -> Nil

Dispose of a texture and free GPU memory

pub fn get_audio(
  cache: AssetCache,
  url: String,
) -> Result(audio.AudioBuffer, AssetError)

Get an audio buffer from the cache

pub fn get_model(
  cache: AssetCache,
  url: String,
) -> Result(GLTFData, AssetError)

Get a GLTF model from the cache (updates LRU timestamp)

pub fn get_model_scene(
  cache: AssetCache,
  url: String,
) -> Result(object3d.Object3D, AssetError)

Get the scene object from a cached GLTF model

pub fn get_stl(
  cache: AssetCache,
  url: String,
) -> Result(scene.BufferGeometry, AssetError)

Get an STL geometry from the cache

pub fn get_texture(
  cache: AssetCache,
  url: String,
) -> Result(scene.Texture, AssetError)

Get a texture from the cache

pub fn insert_asset(
  cache: AssetCache,
  url: String,
  asset: LoadedAsset,
) -> AssetCache

Insert a loaded asset into the cache manually If cache exceeds max_size, evicts least recently used asset

pub fn is_cached(cache: AssetCache, url: String) -> Bool

Check if an asset is cached

pub fn load_asset(
  asset: AssetType,
) -> promise.Promise(Result(LoadedAsset, AssetError))

Load a single asset

pub fn load_batch(
  asset: List(AssetType),
  on_progress: fn(LoadProgress) -> Nil,
) -> promise.Promise(BatchLoadResult)

Load multiple asset with progress tracking Returns a promise that resolves with the loaded asset and any errors

pub fn load_batch_simple(
  asset: List(AssetType),
) -> promise.Promise(BatchLoadResult)

Load multiple asset without progress tracking

pub fn load_gltf(
  url: String,
) -> promise.Promise(Result(GLTFData, LoadError))

Load a GLTF/GLB file from a URL using Promises

pub fn load_stl(
  url: String,
) -> promise.Promise(Result(scene.BufferGeometry, LoadError))

Load an STL file from a URL using Promises

pub fn load_texture(
  url: String,
) -> promise.Promise(Result(scene.Texture, LoadError))

Load a texture from a URL using Promises

pub fn new_cache() -> AssetCache

Create a new empty asset cache with default max size (100 asset)

pub fn new_cache_with_size(max_size: Int) -> AssetCache

Create a new empty asset cache with custom max size

pub fn try_get(
  cache: AssetCache,
  url: String,
) -> option.Option(LoadedAsset)

Try to get any asset (returns Option)

Search Document