Skip to main content

DynamicMask

A class in Root.Township, Alta.UI.

Paints a UI mask texture from code: stamps RGBA32 textures into a CPU colour buffer, then uploads it with a compute shader. Needs a Mask and RawImage on the same object.

Constructors​

MemberWhat it is
DynamicMask()Unity makes components itself; use AddComponent or spawn a prefab instead of new.

Fields​

MemberWhat it is
static Logger loggerLogger for this class.
8 private and internal fields
MemberWhat it is
private ComputeShader blitShaderCompute shader that copies the buffer into the texture.
private Vector4[] colorBufferCPU side pixel buffer that drawing calls change.
private const float ColorMultiplier1/255, to turn byte colours into 0 to 1.
private ComputeBuffer computeBufferGPU buffer the pixels are uploaded through.
private Rect renderRectThe mask bounds, from 0,0 to the resolution.
private int resolutionXBacking field for ResolutionX (default 2048).
private int resolutionYBacking field for ResolutionY (default 2048).
private RenderTexture textureThe render texture shown by the RawImage.

Properties​

MemberWhat it is
RawImage Image { get; private set; }The RawImage that shows the mask texture.
Mask Mask { get; private set; }The UI Mask on this object.
int ResolutionX { get; }Mask width in pixels.
int ResolutionY { get; }Mask height in pixels.

Methods​

MemberWhat it is
bool AddTexture(Texture2D texture, Vector2 position, TextureMode mode)Stamps a texture centred on a pixel position, clipped to the mask, using the given blend mode. Fails for non-RGBA32 textures.
void Clear()Sets the whole buffer to transparent black.
void FillRect(Rect rect, Vector4 color)Fills a rectangle of the buffer with one colour.
void Initialize()Creates the render texture, colour buffer and compute buffer at the set resolution. Logs and stops without a blit shader.
bool IsReady()True once everything needed for drawing exists.
void UpdateMask()Uploads the buffer and runs the blit shader's CSMain kernel to write it into the texture. Call after drawing.
9 private and internal methods
MemberWhat it is
private void Awake()Calls Initialize.
private bool ClipRect(ref Rect targetRect, Rect boundaryRect)Clips a rectangle to the mask bounds. Returns false if they do not overlap.
private Rect GetLocalTextureRect(Rect globalTextureRect, Texture2D texture, Vector2 offset)Turns a clipped mask rectangle back into the texture's own pixel area.
private Rect GetTextureRect(Texture2D texture, Vector2 position)The area a texture covers on the mask when centred on a position.
private void MaskAdd(Texture2D texture, Rect globalTextureRect, Rect localTextureRect)Adds a texture's colours into the buffer, skipping fully transparent pixels.
private void MaskMultiply(Texture2D texture, Rect globalTextureRect, Rect localTextureRect)Multiplies the buffer by a texture's colours, skipping fully transparent pixels.
private void MaskReplace(Texture2D texture, Rect globalTextureRect, Rect localTextureRect)Overwrites the buffer with a texture's colours, skipping fully transparent pixels.
private void MaskSubtract(Texture2D texture, Rect globalTextureRect, Rect localTextureRect)Subtracts a texture's colours from the buffer, skipping fully transparent pixels.
private void OnDestroy()Releases the compute buffer and render texture.