Create Mesh Async

If the mesh you’re creating is huge, you might want to create it asynchronously. In this example, we will create a simple mesh with a single quad. Let’s implement a mesh generator in a separate file:

// quad_mesh.das

require engine.gen_geometry.mesh_structures

[mesh_generator]
def generate_quad_mesh(width : float = 1., height : float = 1.) : MeshGeometry {
    var vertices = [float3(0, 0, 0), float3(0, height, 0), float3(width, height, 0), float3(width, 0, 0)]
    var normals = [float3(0, 0, -1), float3(0, 0, -1), float3(0, 0, -1), float3(0, 0, -1)]
    var uvs = [float2(0, 0), float2(0, height), float2(width, height), float2(width, 0)]

    var triangles = [uint3(0u, 1u, 2u), uint3(2u, 3u, 0u)]

    return <- MeshGeometry(vertices <- vertices, normals <- normals, uv <- uvs, triangles <- triangles)
}

We require the engine.gen_geometry.mesh_structures module that contains MeshGeometry type and [mesh_generator] annotation. Note that you must not require engine.core - the script will not compile, and it is not needed anyway. The [mesh_generator] function annotation is necessary so the function can be found and correctly processed by the engine. Only one function per file is allowed to have this annotation.

Inside the function, we define the vertices and triangles of the quad. The vertices are defined by three arrays - positions (vertices), normal vectors (normals) and corresponding uv coordinates (uv). The triangles are defined as an array of uint3, each containing the indices of the vertices that form a triangle. All of these parameters are bundled into MeshGeometry struct and returned.

Now let’s show how to use this generator in your game script:

// main.das

let mesh = generate_mesh("%file/quad_mesh.das", (height = 5.))

generate_mesh takes the path to the generator and a named tuple of parameters. Note that you can skip including your generator (with require quad_mesh) - you need to just pass raw file path. Default arguments are supported, so in this example the generator function will be called as generate_quad_mesh(1., 5.). generate_mesh returns MeshId that will correspond to the generated mesh as soon as it is done.