You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
ctx.res.Textures() turns an image path or raw pixels into a TextureID that sprites, UI, materials, and decals can reference. A load returns the ID the same frame and never blocks; the async decode and GPU upload finish in the background, and the renderer starts drawing the texture once it is ready. Use it to stream art on demand, build textures at runtime, and update dynamic images pixel by pixel.
Use Cases
Swapping a sprite or UI image on the fly: texture_load!(ctx.res, "res://ui/icon.png") and assign the returned TextureID.
Procedural art: paint an RGBA8 buffer and upload it with create_from_rgba, for example a generated minimap or noise texture.
Dynamic textures that change over time: write_rgba to replace the whole image, or write_rgba_region to patch just the dirty rectangle (a paintable canvas, a fog-of-war overlay).
Decoding downloaded or embedded images: create_from_bytes for PNG/JPEG bytes or engine PTEX data.
Preloading during a loading screen: texture_reserve! to pin textures so they stay resident, and texture_is_loaded! to poll when the async upload finishes.
Freeing memory: texture_drop! to release a texture the game no longer shows.
Showing a camera feed in an image, sprite, or material: camera_texture!(ctx.res, stream).
Saving any loaded texture to disk: texture_save_image!(ctx.res, texture, "user://shot.png").
Saving what a camera sees: camera_save_image!(ctx, camera, "user://camera.png").
Ownership And Choice
The resource cache owns decoded texture data; scripts and nodes carry TextureID handles. Inject a typed ID from a scene path for a fixed per-instance texture. Load through ctx.res when the path or bytes become known at runtime. Reuse the returned ID instead of loading in on_update; the cache keeps repeated paths stable, but repeated calls still hide ownership and intent.
Context
Script context path: ctx.res
Module access: ctx.res.Textures()
Texture loads return a TextureID immediately and do not block the frame; the renderer uses the texture once async decode/upload completes.
Webcam and camera-stream textures resolve through this module too; see Webcam.
Camera output stays live. Assign its TextureID once; do not copy it each frame.
Pass a CameraStream2D, CameraStream3D, or UiCameraStream ID, not its source camera ID.
Lifecycle examples stay inside lifecycle! because script hooks get API from the macro expansion.
Runtime Bytes
Use runtime bytes when texture data is already in memory.
Call
Return
Notes
ctx.res.Textures().create_from_rgba(w, h, rgba)
TextureID
Raw RGBA8 bytes; length must be w * h * 4.
ctx.res.Textures().create_from_bytes(bytes)
TextureID
Decodes image bytes or PTEX.
texture_create_from_rgba!(ctx.res, w, h, rgba)
TextureID
Macro form.
texture_create_from_bytes!(ctx.res, bytes)
TextureID
Macro form.
ctx.res.Textures().write_rgba(id, w, h, rgba)
bool
Replace mutable RGBA8 data.
ctx.res.Textures().write_rgba_region(id, x, y, w, h, rgba)
bool
Update one tight RGBA8 subregion.
ctx.res.Textures().save_image(id, path)
bool
Queue image encode/save; GPU textures read back async.
The camera-stream node owns render resolution, aspect mode, and
post-processing. Its camera field selects what it sees. The returned
TextureID references that output directly, so image users receive each frame
without another API call.
Save the same live camera output as a PNG:
let queued = camera_save_image!(
ctx,self.security_camera,"user://screenshots/security.png");
Pass a Camera2D or Camera3D ID directly. No CameraStream node is needed.
The default output size matches the active viewport. Use
camera_save_image_sized!(ctx, camera, path, width, height) for an explicit
size.
true means the request entered the render queue. Camera output uses a hidden
one-shot render target plus async GPU readback, so the file appears after
rendering completes. Use user:// for writable game data. The file extension
selects PNG, JPEG, WebP, BMP, TGA, or ICO encoding.