DynamicImage¶
Library · DynamicImage · server and client scripts
DynamicImage is an image whose pixels a client script draws while the game runs, such as a paint canvas, a minimap or a crosshair. A dynamic image exists only on the client that makes it. Each side is 1 to 1024 pixels; a client has room for 64 dynamic images and 16777216 pixels in all.
Every pixel is four bytes, red, green, blue and alpha, from 0 to 255. Pixels run left to right, then top to bottom; coordinates count from 0 at the top left corner.
Show an image by assigning its Asset string to a texture property: Image.Image and the other image properties of UI elements, Decal.Texture, the PbrMaterial maps, particle textures, material texture parameters, and the avatar Face, Shirt and Pants. Sky textures do not show dynamic images. The engine sends changed pixels to the GPU once per frame, before drawing, so a script never asks for an update.
| Member | What it does |
|---|---|
Size |
The image size in pixels as a Vector2 |
Asset |
The texture asset id string, or nil after :Destroy() |
:WritePixels(data, region?) |
Copies RGBA bytes from data into the PixelRegion (Position, Size, Offset) |
:ReadPixels(region?, into?) |
Copies the PixelRegion into into or a new buffer, and returns the buffer |
:Clear(color?) |
Sets every pixel to color, or transparent black when omitted |
:SetPixel(position, color) |
Sets one pixel at a Vector2 coordinate |
:GetPixel(position) |
Returns the pixel's Color |
:FillRect(position, size, color, options?) |
Fills a rectangle |
:DrawLine(from, to, thickness, color, options?) |
Draws a line between two Vector2 coordinates |
:DrawCircle(center, radius, color, options?) |
Fills a circle around a Vector2 center |
:Blit(source, options?) |
Copies a rectangle of source using BlitOptions (Position, SourcePosition, Size, Blend) |
:Destroy() |
Frees the image now |
The PixelRegion defaults to the whole image (Position = Vector2.new(0, 0), Size covering the rest of the image, Offset = 0). WritePixels, ReadPixels, SetPixel and GetPixel refuse coordinates outside the image. FillRect, DrawLine, DrawCircle and Blit draw the part inside the image.
Colors are Color values with alpha from 0 to 1. Clear and SetPixel store the color as passed. FillRect, DrawLine, DrawCircle and Blit blend over existing pixels by default; pass { Blend = false } to overwrite the bytes.
new¶
DynamicImage.new(size) creates a transparent image.
Example
-- Client script: a crosshair over the screen.
local image = DynamicImage.new(Vector2.new(33, 33))
image:DrawLine(Vector2.new(16, 4), Vector2.new(16, 28), 2, Color.new(1, 1, 1))
image:DrawLine(Vector2.new(4, 16), Vector2.new(28, 16), 2, Color.new(1, 1, 1))
image:DrawCircle(Vector2.new(16, 16), 2, Color.new(1, 0.2, 0.2))
local screen = World.Spawn({ UIRoot = true })
screen.Parent = World.Viewport.Screen
local label = World.Spawn({
Box = {
Size = UIVector.fromPoints(33, 33),
Position = UIVector.new(0.5, 0.5, -16, -16),
},
Image = { Image = image.Asset or "" },
})
label.Parent = screen
Errors (1)
| Error | When |
|---|---|
new can only be called from a client script |
Called from a server script. |