Draw
Instance Members
Renders an array of visual objects.
On iOS, this method checks if the app is in background to prevent GPU operations that could cause crashes when the app is not active.
Name | Type | Description |
---|---|---|
visuals |
Array<ceramic.Visual> | Array of Visual objects to render |
Swaps the front and back buffers (unused in Clay backend). Buffer swapping is handled automatically by Clay framework.
Initializes the vertex and index buffers for rendering.
This method sets up the buffer management system and prepares the first set of buffers for use. Should be called before any rendering operations begin.
Begins a rendering pass by enabling vertex attributes.
Enables the core vertex attributes used by all shaders:
- Position (x, y, z)
- Texture coordinates (u, v)
- Color (r, g, b, a)
Additional attributes are enabled dynamically based on the active shader.
Clears the current render target to transparent white.
This method clears the color buffer with a transparent white background. When rendering to a texture, this marks the render target as updated.
Clears the current render target and applies the application background color.
This method clears the color buffer with the application's configured background color (fully opaque). Used for the main screen clearing.
Enables alpha blending for transparent rendering.
This activates the GPU's blending functionality, allowing pixels to be combined based on their alpha values. Must be enabled for transparency to work correctly.
Disables alpha blending.
When disabled, pixels are rendered opaque regardless of alpha values. This can improve performance when rendering fully opaque content.
Sets the active texture unit slot for multi-texturing.
Modern GPUs support multiple texture units, allowing shaders to sample from multiple textures simultaneously. This method selects which texture unit subsequent texture operations will affect.
Name | Type | Description |
---|---|---|
slot |
Int | The texture unit index (0-based) |
Sets the primitive type for rendering.
Determines how vertices are interpreted:
- TRIANGLE: Every 3 vertices form a triangle (default)
- LINE: Every 2 vertices form a line
Name | Type | Description |
---|---|---|
primitiveType |
ceramic.RenderPrimitiveType | The primitive type to use for subsequent draw calls |
Gets the currently active texture unit slot.
Returns | Description |
---|---|
Int | The index of the active texture unit |
Sets the active render target for subsequent drawing operations.
This method switches between rendering to a texture (off-screen) or to the main screen buffer. When switching targets, it handles:
- MSAA buffer blitting for antialiased render targets
- Projection and view matrix updates
- Viewport configuration
- Optional automatic clearing
When renderTarget is null, rendering switches back to the main screen.
Name | Type | Default | Description |
---|---|---|---|
renderTarget |
ceramic.RenderTexture | The render texture to render to, or null for main screen | |
force |
Bool | false |
Force the render target switch even if it's the same target |
Activates a shader program for rendering.
This method:
- Sets the shader as the active GPU program
- Uploads projection and modelview matrices as uniforms
- Configures vertex layout based on shader attributes
- Determines if multi-texture batching is supported
- Calculates maximum vertices per batch based on attribute size
The vertex size calculation includes:
- 3 floats for position (x, y, z)
- Custom float attributes defined by the shader
- 1 float for texture slot (if multi-texturing is enabled)
Name | Type | Description |
---|---|---|
shader |
Shader | The shader to activate |
setBlendFuncSeparate(srcRgb: BlendMode, dstRgb: BlendMode, srcAlpha: BlendMode, dstAlpha: BlendMode): Void
Sets separate blend functions for RGB and alpha channels.
This allows fine control over how colors are blended, with different blend modes for color (RGB) and transparency (alpha) channels.
Common blend modes:
- ONE: Use source value as-is
- ZERO: Ignore value (multiply by 0)
- SRC_ALPHA: Multiply by source alpha
- ONE_MINUS_SRC_ALPHA: Multiply by (1 - source alpha)
Name | Type | Description |
---|---|---|
srcRgb |
BlendMode | Blend factor for source RGB |
dstRgb |
BlendMode | Blend factor for destination RGB |
srcAlpha |
BlendMode | Blend factor for source alpha |
dstAlpha |
BlendMode | Blend factor for destination alpha |
Called before drawing a quad (currently unused).
Reserved for future optimizations or quad-specific setup.
Name | Type | Description |
---|---|---|
quad |
ceramic.Quad | The quad about to be drawn |
Called after drawing a quad (currently unused).
Reserved for future optimizations or quad-specific cleanup.
Name | Type |
---|---|
mesh |
ceramic.Mesh |
Enables scissor testing to clip rendering to a rectangular area.
Scissor testing restricts rendering to pixels within the specified rectangle. Any pixels outside this area are discarded by the GPU.
The coordinates are transformed by the current modelview matrix and adjusted for screen density. When rendering to a texture, the Y coordinate is flipped to account for texture coordinate differences.
Name | Type | Description |
---|---|---|
x |
Float | Left edge of the scissor rectangle in logical coordinates |
y |
Float | Top edge of the scissor rectangle in logical coordinates |
width |
Float | Width of the scissor rectangle in logical pixels |
height |
Float | Height of the scissor rectangle in logical pixels |
Disables scissor testing.
After calling this, rendering is no longer clipped to a rectangular area.
Enables stencil testing for masked rendering.
This configures the stencil test to only render pixels where the stencil buffer equals 1. Used after drawing to the stencil buffer to render content only within the stenciled area.
Stencil configuration:
- Test: Passes when stencil buffer equals 1
- Mask: Stencil buffer is read-only (0x00)
- Color: All color channels are written
Disables stencil testing for normal rendering.
Resets stencil configuration to default values where all pixels pass the stencil test and the stencil buffer can be written to.
Stencil configuration:
- Test: Always passes
- Mask: Stencil buffer is writable (0xFF)
- Color: All color channels are written
Begins drawing to the stencil buffer for masking operations.
This sets up the GPU to write to the stencil buffer instead of the color buffer. Pixels drawn will mark areas in the stencil buffer with a value of 1, creating a mask for subsequent rendering.
Stencil configuration:
- Clears stencil buffer to 0xFF
- Writes 1 to stencil buffer where pixels are drawn
- Disables color output (only stencil is affected)
- Always passes stencil test during mask creation
Binds a texture to the current texture unit.
Makes the texture available for sampling in shaders. The texture will be bound to the currently active texture unit.
Name | Type | Description |
---|---|---|
backendItem |
Texture | The texture to bind |
Binds no texture or a default white texture.
On web targets, binds a 1x1 white texture because WebGL requires a texture to be bound. On native targets, unbinds any texture.
Used when rendering untextured geometry or solid colors.
Name | Type |
---|---|
backendItem |
Texture |
textureId |
TextureId |
Returns |
---|
Bool |
Name | Type |
---|---|
backendItem |
Texture |
Returns |
---|
TextureId |
Name | Type |
---|---|
backendItem |
Texture |
Returns |
---|
Int |
Name | Type |
---|---|
backendItem |
Texture |
Returns |
---|
Int |
Name | Type |
---|---|
backendItem |
Texture |
Returns |
---|
Int |
Name | Type |
---|---|
backendItem |
Texture |
Returns |
---|
Int |
Returns |
---|
Int |
Adds a vertex position to the current batch.
On C++ targets, uses direct memory access for performance. On other targets, uses array access.
Name | Type | Description |
---|---|---|
x |
Float | X coordinate in screen space |
y |
Float | Y coordinate in screen space |
z |
Float | Z coordinate for depth ordering (0-1 range typically) |
Name | Type |
---|---|
x |
Float |
y |
Float |
z |
Float |
textureSlot |
Float |
Name | Type |
---|---|
index |
Int |
value |
Float |
Name | Type |
---|---|
i |
Int |
Name | Type |
---|---|
uvX |
Float |
uvY |
Float |
Adds a vertex color to the current batch.
Colors are stored as floating-point values from 0.0 to 1.0. The color will be interpolated across the triangle/line.
Name | Type | Description |
---|---|---|
r |
Float | Red component (0.0 to 1.0) |
g |
Float | Green component (0.0 to 1.0) |
b |
Float | Blue component (0.0 to 1.0) |
a |
Float | Alpha component (0.0 to 1.0) |
Returns |
---|
Bool |
Checks if the current batch should be flushed before adding more vertices.
Returns true if adding the specified number of vertices or indices would exceed buffer capacity, indicating that the current batch should be sent to the GPU before continuing.
Name | Type | Description |
---|---|---|
numVerticesAfter |
Int | Number of vertices to be added |
numIndicesAfter |
Int | Number of indices to be added |
customFloatAttributesSize |
Int | Size of custom attributes (unused) |
Returns | Description |
---|---|
Bool | True if flush is needed, false otherwise |
Returns |
---|
Int |
Returns |
---|
Int |
Flushes the current batch of vertices to the GPU.
This is the core rendering method that:
- Creates GPU buffers from the accumulated vertex data
- Configures vertex attributes for the shader
- Handles multi-texture batching if supported
- Sets up custom shader attributes
- Issues the draw call to render all triangles/lines
- Cleans up temporary buffers
- Prepares for the next batch
The method uses temporary GPU buffers that are deleted after rendering to avoid memory leaks. Buffer data is uploaded as STREAM_DRAW for optimal performance with dynamic geometry.
Creates a new Draw backend instance. Initializes the Ceramic renderer for handling visual rendering.
Private Members
Maximum number of vertices that can be stored in a single buffer.
Maximum number of indices that can be stored in a single buffer.
Maximum number of buffer sets to cycle through. Buffer cycling prevents GPU stalls by using multiple buffer sets.
Vertex attribute location for position data (x, y, z).
Vertex attribute location for texture coordinate data (u, v).
Vertex attribute location for color data (r, g, b, a).
The Ceramic renderer instance used for high-level rendering operations.
Begins a rendering frame (currently unused). Reserved for future use or platform-specific initialization.
Ends a rendering frame (currently unused). Reserved for future use or platform-specific cleanup.
Prepares the next set of vertex buffers for use.
This implements a buffer cycling system to avoid GPU stalls. Instead of reusing the same buffer immediately (which could cause the GPU to wait), it cycles through multiple buffer sets.
Buffer allocation:
- Position buffer: Full vertex capacity (MAX_VERTS_SIZE)
- UV buffer: 2/3 of vertex capacity (optimized for quads)
- Color buffer: Full vertex capacity (4 floats per vertex)
- Index buffer: MAX_INDICES * 2 capacity
On C++ targets, additional ArrayBufferView objects are created for efficient memory access without copying.
Resets all vertex buffer indexes to zero.
This prepares the buffers for a new batch of vertices. Called when starting a new draw batch or after flushing.
Updates the projection matrix for orthographic rendering.
Creates an orthographic projection matrix that maps logical coordinates to normalized device coordinates (-1 to 1).
The projection uses:
- Origin at top-left (0,0)
- X increases rightward
- Y increases downward
- Z range from -1000 to 1000 for layering
Name | Type | Description |
---|---|---|
width |
Float | Viewport width in logical pixels |
height |
Float | Viewport height in logical pixels |
updateViewMatrix(density: Float, width: Float, height: Float, ?transform: ceramic.Transform, ?flipY: Float = 1): Void
Updates the view matrix for camera transformations.
The view matrix handles:
- Camera position and rotation (via transform parameter)
- Screen density scaling for high-DPI displays
- Y-axis flipping for render-to-texture (textures have inverted Y)
The matrix is inverted at the end because the view matrix represents the inverse of the camera transform.
Name | Type | Default | Description |
---|---|---|---|
density |
Float | Screen density multiplier (e.g., 2 for retina) | |
width |
Float | Viewport width in logical pixels | |
height |
Float | Viewport height in logical pixels | |
transform |
ceramic.Transform | (optional) | Optional camera transform |
flipY |
Float | 1 |
-1 to flip Y axis (for render targets), 1 for normal |
Name | Type |
---|---|
m |
ceramic.Float32Array |
Name | Type |
---|---|
m |
ceramic.Float32Array |
transform |
ceramic.Transform |
Metadata
Name | Parameters |
---|---|
:allow |
backend.Backend |
:access |
backend.Backend |