MeshUtils
Low-level utility class for generating mesh data arrays.
MeshUtils provides static methods for creating vertices, indices, and UV coordinates for grid-based meshes. These utilities are the foundation for procedural mesh generation and are used by MeshExtensions for higher-level operations.
Key features:
- Grid vertex generation with staggering support
- Triangle index generation with mirroring options
- UV coordinate mapping for grid texturing
- Support for custom vertex attributes
- Efficient array reuse
Grid coordinate system:
0,0 --- 1,0 --- 2,0
| | |
0,1 --- 1,1 --- 2,1
| | |
0,2 --- 1,2 --- 2,2
// Create a 5x5 grid mesh
var vertices = MeshUtils.createVerticesGrid(null, 5, 5, 200, 200);
var indices = MeshUtils.createIndicesGrid(null, 5, 5);
var uvs = MeshUtils.createUVsGrid(null, 5, 5);
var mesh = new Mesh();
mesh.color = Color.WHITE;
mesh.vertices = vertices;
mesh.indices = indices;
mesh.uvs = uvs;
Static Members
createVerticesGrid(?vertices: Array<Float>, columns: Int, rows: Int, width: Float, height: Float, ?staggerX: Float = 0, ?staggerY: Float = 0, ?attrLength: Int = 0, ?attrValues: Array<Float>): Array<Float>
Creates a grid of vertices with optional staggering and custom attributes.
Generates vertices arranged in a rectangular grid pattern. Each vertex consists of x,y coordinates followed by optional custom attributes. The total number of vertices is (columns+1) × (rows+1).
Staggering creates offset patterns useful for:
- Hexagonal grids (staggerX with odd rows offset)
- Diamond/isometric grids (both staggerX and staggerY)
- Wave effects and deformations
Vertex data layout: [x, y, ...customAttributes]
Name | Type | Default | Description |
---|---|---|---|
vertices |
Array<Float> | (optional) | Existing array to reuse, or null to create new array. If provided and larger than needed, will be truncated. |
columns |
Int | Number of columns in the grid (cells, not vertices) | |
rows |
Int | Number of rows in the grid (cells, not vertices) | |
width |
Float | Total width of the grid in pixels | |
height |
Float | Total height of the grid in pixels | |
staggerX |
Float | 0 |
Horizontal offset applied to odd-numbered rows. Use cellWidth*0.5 for hexagonal grids. |
staggerY |
Float | 0 |
Vertical offset applied to odd-numbered columns. Rarely used, but can create diamond patterns. |
attrLength |
Int | 0 |
Number of custom float attributes per vertex. Common values: 2 for UV, 4 for color, etc. |
attrValues |
Array<Float> | (optional) | Array of attribute values to assign. If null, attributes are initialized to 0. Length must equal (columns+1)×(rows+1)×attrLength. |
Returns | Description |
---|---|
Array<Float> | Array of vertex data with length (columns+1)×(rows+1)×(2+attrLength) * haxe // Simple 10x10 grid var vertices = MeshUtils.createVerticesGrid(null, 10, 10, 400, 400); * // Hexagonal grid with horizontal stagger var cellWidth = 40; var vertices = MeshUtils.createVerticesGrid( null, 10, 10, 400, 400, cellWidth * 0.5, 0 ); * // Grid with per-vertex colors (4 floats: r,g,b,a) var colors = [ // color data ]; var vertices = MeshUtils.createVerticesGrid( null, 10, 10, 400, 400, 0, 0, 4, colors ); |
createIndicesGrid(?indices: Array<Int>, columns: Int, rows: Int, ?mirrorX: Bool = false, ?mirrorY: Bool = false, ?mirrorFlip: Bool = false): Array<Int>
Creates triangle indices for a grid of vertices.
Generates indices that connect grid vertices into triangles, with each grid cell split into two triangles. The total number of triangles is columns×rows×2.
Triangle winding order (default, no mirroring):
TL --- TR
|\ |
| \ | Cell split: TL-TR-BL and TR-BR-BL
| \ |
| \ |
| \ |
BL --- BR
Mirroring options change the diagonal direction in alternating cells, which helps:
- Reduce visual patterns in deformed meshes
- Create more natural-looking terrain
- Improve shading on curved surfaces
Name | Type | Default | Description |
---|---|---|---|
indices |
Array<Int> | (optional) | Existing array to reuse, or null to create new array. If provided and larger than needed, will be truncated. |
columns |
Int | Number of columns in the grid (cells, not vertices) | |
rows |
Int | Number of rows in the grid (cells, not vertices) | |
mirrorX |
Bool | false |
Mirror triangle diagonal in odd-numbered columns. Creates horizontal alternation pattern. |
mirrorY |
Bool | false |
Mirror triangle diagonal in odd-numbered rows. Creates vertical alternation pattern. |
mirrorFlip |
Bool | false |
Inverts the mirroring pattern. If true, mirrors even cells instead of odd. |
Returns | Description |
---|---|
Array<Int> | Array of indices with length columns×rows×6 (2 triangles × 3 vertices per cell) * haxe // Standard grid triangulation var indices = MeshUtils.createIndicesGrid(null, 10, 10); * // Alternating pattern for natural terrain var indices = MeshUtils.createIndicesGrid( null, 10, 10, true, true // Mirror both X and Y ); * // Custom pattern with flipped mirroring var indices = MeshUtils.createIndicesGrid( null, 10, 10, true, false, true // Mirror X on even columns ); |
createUVsGrid(?uvs: Array<Float>, columns: Int, rows: Int, ?offsetX: Float = 0, ?offsetY: Float = 0): Array<Float>
Creates UV coordinates for a grid of vertices.
Generates UV coordinates that map linearly across the grid from 0 to 1, stretching any applied texture across the entire mesh. Each vertex gets a UV coordinate based on its grid position.
UV mapping:
- Top-left vertex: (0, 0)
- Top-right vertex: (1, 0)
- Bottom-left vertex: (0, 1)
- Bottom-right vertex: (1, 1)
Offsets allow texture scrolling and tiling effects.
Name | Type | Default | Description |
---|---|---|---|
uvs |
Array<Float> | (optional) | Existing array to reuse, or null to create new array. If provided and larger than needed, will be truncated. |
columns |
Int | Number of columns in the grid (cells, not vertices) | |
rows |
Int | Number of rows in the grid (cells, not vertices) | |
offsetX |
Float | 0 |
Horizontal UV offset for texture scrolling. Values > 1 create tiling if texture wrap is enabled. |
offsetY |
Float | 0 |
Vertical UV offset for texture scrolling. Values > 1 create tiling if texture wrap is enabled. |
Returns | Description |
---|---|
Array<Float> | Array of UV coordinates with length (columns+1)×(rows+1)×2 * haxe // Standard UV mapping (texture stretched across grid) var uvs = MeshUtils.createUVsGrid(null, 10, 10); * // Scrolling texture effect var uvs = MeshUtils.createUVsGrid( null, 10, 10, time * 0.1, 0 // Scroll horizontally over time ); * // Tiled texture (requires texture wrap mode) var uvs = MeshUtils.createUVsGrid( null, 10, 10, 0, 0 // UVs will go from 0 to 1 ); // Then scale UVs by tile count in shader or modify here |