Mesh
A flexible visual for drawing custom shapes composed of triangles.
Mesh allows you to create complex 2D geometry by defining vertices (points), indices (triangles), and optional attributes like colors and texture coordinates. This is the foundation for advanced visuals like deformable sprites, particle systems, and custom shape rendering.
Features:
- Custom vertex positions for any shape
- Per-vertex coloring with color interpolation
- Texture mapping with UV coordinates
- Custom shader attributes support
- Complex hit testing at triangle level
- Optimized rendering through batching
The mesh is defined by:
vertices
: Array of x,y coordinates for each vertexindices
: Array defining triangles (every 3 indices form a triangle)colors
: Optional per-vertex colorsuvs
: Texture coordinates when using a texture
// Create a colored triangle
var mesh = new Mesh();
mesh.vertices = [
100, 100, // Vertex 0
200, 100, // Vertex 1
150, 200 // Vertex 2
];
mesh.indices = [0, 1, 2];
mesh.colors = [
Color.RED,
Color.GREEN,
Color.BLUE
];
// Create a textured quad
var mesh = new Mesh();
mesh.color = Color.WHITE; // Use fill color instead of explicit colors array
mesh.texture = assets.texture('image');
mesh.vertices = [
0, 0, // Top-left
100, 0, // Top-right
100, 100, // Bottom-right
0, 100 // Bottom-left
];
mesh.indices = [
0, 1, 2, // First triangle
0, 2, 3 // Second triangle
];
mesh.uvs = [
0, 0, // Top-left UV
1, 0, // Top-right UV
1, 1, // Bottom-right UV
0, 1 // Bottom-left UV
];
Instance Members
colorMapping: MeshColorMapping
Defines how colors are applied to the mesh.
- MESH: Use the mesh's color array
- TEXTURE: Use texture colors only
- VERTICES: Multiply vertex colors with texture
customFloatAttributesSize: Int
The number of additional float values per vertex for custom shader attributes. Default is 0 (only x,y coordinates). Set this when using shaders that require extra per-vertex data like secondary UVs, vertex weights, etc. The total floats per vertex becomes: 2 + customFloatAttributesSize
complexHit: Bool
When set to true
, hit testing checks individual triangles instead of just bounds.
This provides accurate hit detection for complex shapes but is more expensive.
Use only when you need precise interaction with non-rectangular meshes.
Default is false (uses bounding box).
color: Color
Convenience property for setting a single color for the entire mesh. When set, updates the colors array with this color for all vertices. When getting, returns the first vertex color or WHITE if no colors are set. For multi-colored meshes, use the colors array directly.
An array of vertex positions as alternating x,y coordinates. Each vertex requires 2 floats (or 2 + customFloatAttributesSize if using custom attributes). Example: [x0, y0, x1, y1, x2, y2, ...] These define the shape of your mesh.
An array of vertex indices defining triangles. Every 3 consecutive indices form one triangle. Indices refer to positions in the vertices array (0-based). Example: [0, 1, 2, 0, 2, 3] defines two triangles sharing vertices 0 and 2.
colors: Array<AlphaColor>
An array of colors for each vertex. Colors are interpolated across triangles for smooth gradients. Each color includes alpha channel for transparency. Array length should match the number of vertices.
floatColors: Float32Array
High-precision color array using 4 floats per color (RGBA).
Use this instead of colors
when you need:
- Extra color precision beyond 8-bit per channel
- To avoid CPU premultiplication of alpha
- HDR color values
Format: [r0, g0, b0, a0, r1, g1, b1, a1, ...]
If set, this is used instead of the
colors
array.
texture: Texture
The texture to apply to this mesh.
When set, you must also provide UV coordinates in the uvs
array.
The texture's asset reference count is automatically managed.
Set to null for untextured meshes.
Texture coordinates for each vertex, ranging from 0.0 to 1.0. Required when using a texture. Array format: [u0, v0, u1, v1, ...]
- (0,0) = top-left of texture
- (1,1) = bottom-right of texture Values outside 0-1 range will wrap or clamp based on texture settings.
destroy(): Void
computeSize(): Void
Compute and set the mesh's width and height based on vertex positions. Scans all vertices to find the maximum x and y coordinates. Useful after modifying vertices to update the mesh bounds.
Compute vertices and indices to obtain a grid with cols
columns
and rows
rows at the requested width
and height
.
Name | Type | Default | Description |
---|---|---|---|
cols |
Int | The number of columnns in the grid | |
rows |
Int | The number of rows in the grid | |
width |
Float | -1 |
The width of the grid |
height |
Float | -1 |
The height of the grid |
Compute vertices, indices and uvs to obtain a grid with cols
columns
and rows
rows to fit the given texture or mesh's current texture.
Name | Type | Default | Description |
---|---|---|---|
cols |
Int | The number of columnns in the grid | |
rows |
Int | The number of rows in the grid | |
texture |
Texture | (optional) | The texture used to generate the grid. If not provided, will use mesh's current texture |
new(): Void
Create a new Mesh. The mesh starts empty - you must set vertices, indices, and other properties before it will render anything.
Private Members
Name | Type |
---|---|
_ |
Entity |
Test if a point hits this mesh. If complexHit is true, tests against individual triangles for accuracy. Otherwise uses the bounding box for performance.
Name | Type | Description |
---|---|---|
x |
Float | X coordinate to test |
y |
Float | Y coordinate to test |
matrix |
Transform | Transform matrix for coordinate conversion |
Returns | Description |
---|---|
Bool | True if the point hits the mesh |
Metadata
Name | Parameters |
---|---|
:build |
tracker.macros.EventsMacro.build() |
:autoBuild |
tracker.macros.EventsMacro.build() |
:build |
ceramic.macros.EntityMacro.buildForCompletion() |
:autoBuild |
ceramic.macros.EntityMacro.buildForCompletion() |
:allow |
ceramic.MeshPool |