Mesh primitives
Here you can find useful functions for requesting mesh primitives such as cubes, spheres, cylinders, etc. Furthermore, you can create solids of revolution and extruder meshes.
Constants
- QUAD_MESH_ID = request_mesh_primitive("%builtin_package/quad.mesh")
MeshId of the default quad mesh
- SPHERE_MESH_ID = request_mesh_primitive("%builtin_package/sphere.mesh")
MeshId of the default sphere mesh
- CUBE_MESH_ID = request_mesh_primitive("%builtin_package/cube.mesh")
MeshId of the default cube mesh
- PLANE_MESH_ID = request_mesh_primitive("%builtin_package/plane.mesh")
MeshId of the default plane mesh
- PRISM_MESH_ID = request_mesh_primitive("%builtin_package/prism.mesh")
MeshId of the default prism mesh
- CYLINDER_MESH_ID = request_mesh_primitive("%builtin_package/cylinder.mesh")
MeshId of the default cylinder mesh
- PYRAMID_MESH_ID = request_mesh_primitive("%builtin_package/pyramid.mesh")
MeshId of the default pyramid mesh
- CONE_MESH_ID = request_mesh_primitive("%builtin_package/cone.mesh")
MeshId of the default cone mesh
- CAPSULE_MESH_ID = request_mesh_primitive("%builtin_package/capsule.mesh")
MeshId of the default capsule mesh
- MIN_SIDES = 3
Minimum number of subdivisions for polygon mesh geometry
- MIN_SPHERE_SUBDIVISIONS = 3
Minimum number of subdivisions for sphere mesh geometry
- MIN_CELLS = 1
Minimum number of cells for plane mesh geometry
- MIN_CONE_SUBDIVISIONS = 3
Minimum number of subdivisions for cone mesh generation
- MIN_CAPSULE_SUBDIVISIONS = 3
Minimum number of subdivisions for capsule mesh generation
- MIN_SOLID_OF_REVOLUTION_SUBDIVISIONS = 3
Minimum number of subdivisions for solid of revolution mesh generation
- MIN_EXTRUDER_SUBDIVISIONS = 3
Minimum number of subdivisions for extruder mesh generation
- MIN_PIPE_SUBDIVISIONS = 3
Minimum number of subdivisions for pipe mesh generation
Structures
- QuadInit
QuadInit struct is used to initialize a quad mesh
- Fields:
width : float = 1f - The width of the quad
height : float = 1f - The height of the quad
- PolygonInit
PolygonInit struct is used to initialize a polygon mesh
- Fields:
sides : int = 24 - The number of sides of the polygon
radius : float = 0.5f - The radius of the polygon
- SphereInit
SphereInit struct is used to initialize a sphere mesh
- Fields:
radius : float = 0.5f - The radius of the sphere
subdivisions : int = 24 - The number of subdivisions. The more subdivisions, the smoother the sphere
- CubeInit
CubeInit struct is used to initialize a rectangular parallelepiped cuboid
- Fields:
width : float = 1f - The width of the cuboid
height : float = 1f - The height of the cuboid
depth : float = 1f - The depth of the cuboid
- PlaneInit
PlaneInit struct is used to initialize a plane mesh.
- Fields:
cells : int = 1 - The number of cells along one axis of the plane. The total number of cells will be
cells * cells
- PrismInit
PrismInit struct is used to initialize a regular right prism mesh
- Fields:
sides : int = 3 - The number of sides of the prism
height : float = 2f - The height of the prism
radius : float = 0.5f - The radius of the prism base
axis : string = “y” - Specifies the orientation of the prism, can be “x”, “y”, or “z”. Default is “y”
cap : bool = true - Specifies whether to generate caps for the prism. Default is true
- CylinderInit
CylinderInit struct is used to initialize a cylinder mesh
- Fields:
height : float = 2f - The height of the cylinder
radius : float = 0.5f - The radius of the cylinder
axis : string = “y” - Specifies the orientation of the cylinder, can be “x”, “y”, or “z”. Default is “y”
subdivisions : int = 24 - The number of subdivisions. The more subdivisions, the smoother the cylinder
cap : bool = true - Specifies whether to generate caps for the cylinder. Default is true
- PyramidInit
PyramidInit struct is used to initialize a pyramid mesh
- Fields:
sides : int = 4 - The number of sides of the pyramid
height : float = 1f - The height of the pyramid
radius : float = 0.5f - The radius of the pyramid at the base
frustumRadius : float = 0f - The radius of the pyramid at the top (if it is truncated). Default is 0, which means the pyramid is not truncated
axis : string = “y” - Specifies the orientation of the pyramid, can be “x”, “y”, or “z”. Default is “y”
cap : bool = true - Specifies whether to generate a cap for the pyramid. Default is true
tessellation : int = 1 - The number of subdivisions along the height of the pyramid, there will be at least the same amount of subdivisions along the base perimeter. Default is 1. Increase this value if you have problems with texture stretching, but keep in mind that it will increase the number of vertices and triangles.
- ConeInit
ConeInit struct is used to initialize a cone mesh
- Fields:
height : float = 1f - The height of the cone
radius : float = 0.5f - The radius of the cone at the base
frustumRadius : float = 0f - The radius of the cone at the top (if it is truncated). Default is 0, which means the cone is not truncated
axis : string = “y” - Specifies the orientation of the cone, can be “x”, “y”, or “z”. Default is “y”
subdivisions : int = 24 - The number of subdivisions. The more subdivisions, the smoother the cone
cap : bool = true - Specifies whether to generate a cap for the cone. Default is true
tessellation : int = 4 - The number of subdivisions along the height of the cone. Default is 4. Increase this value if you have problems with cone smoothness or texture stretching, but keep in mind that it will increase the number of vertices and triangles.
- CapsuleInit
CapsuleInit struct is used to initialize a capsule mesh
- Fields:
height : float = 2f - Total height of the capsule, including the cylinder and the two hemispheres
radius : float = 0.5f - The radius of the capsule
axis : string = “y” - Specifies the orientation of the capsule, can be “x”, “y”, or “z”. Default is “y”
subdivisions : int = 24 - Number of subdivisions. The more subdivisions, the smoother the capsule
- RevolutionInit
RevolutionInit struct is used to initialize a solid of revolution mesh
- Fields:
points : array<float2> - An array of 2D points (float2) that define the profile of the solid of revolution
subdivisions : int = 24 - The number of subdivisions. The more subdivisions, the smoother the solid of revolution
cap : bool = true - Specifies whether to generate caps for the mesh. Default is true
- ExtruderInit
ExtruderInit struct is used to initialize an extruder mesh
- Fields:
polygonPoints : array<float2> - Represents the profile of the extruder. Clockwise order of the polygon points makes the normals point outwards making the extruder visible from the outside, counterclockwise order makes the normals point inwards making the extruder visible from the inside like a corridor
points : array<float3> - Represents the path of the extruder
bendRadius : float = 1f - The radius of all bends in the extruder
subdivisions : int = 24 - The number of subdivisions. The more subdivisions, the smoother the extruder
cap : bool = true - Specifies whether to generate caps for the mesh or not. Default is true
closed : bool = false - Specifies whether the path is closed, forming a loop. Default is false
smoothNormals : bool = false - Specifies whether the extruder profile is smoothed or not. Default is false
- PipeInit
PipeInit struct is used to initialize a pipe mesh
- Fields:
points : array<float3> - The path of the pipe
radius : float = 1f - The radius of pipe’s section
bendRadius : float = 1f - The radius of all bends of the pipe
subdivisions : int = 24 - The number of subdivisions. The more subdivisions, the smoother the pipe
cap : bool = true - Specifies whether to generate caps for the mesh or not. Default is true
closed : bool = false - Specifies whether the path is closed, forming a loop. Default is false
Functions
create_polygon_mesh (init_params: PolygonInit = PolygonInit()) : MeshId
create_solid_of_revolution_mesh (var init_params: RevolutionInit) : MeshId
create_extruder_mesh (var init_params: ExtruderInit) : MeshId
create_quad_mesh_geometry (width: float; height: float) : MeshGeometry
create_polygon_mesh_geometry (var sides: int = 24; radius: float = 0.5f) : MeshGeometry
create_sphere_mesh_geometry (var subdivisions: int = 64; radius: float = 0.5f) : MeshGeometry
create_cube_mesh_geometry (sizeX: float = 1f; sizeY: float = 1f; sizeZ: float = 1f) : MeshGeometry
- create_quad_mesh(init_params: QuadInit): MeshId
Generates a quad mesh
The quad will lie in the XY plane with its normal pointing to the camera (in the negative Z direction). The center of the quad will have coordinates (0, 0)
- Arguments:
init_params : QuadInit - The data to build the quad mesh from
- Returns:
MeshId - MeshId of the resulting quad mesh
Usage example:
let quadMesh = create_quad_mesh(QuadInit(width = 16.0, height = 9.0))
let quadNode = create_node()
add_component(quadNode, new Mesh(meshId = quadMesh))
- create_polygon_mesh(init_params: PolygonInit = PolygonInit()): MeshId
Generates a polygon mesh
The polygon will lie in the XY plane with its normal pointing to the camera (in the negative Z direction). The center of the polygon will have coordinates (0, 0)
- Arguments:
init_params : PolygonInit - The data to build the polygon mesh from
- Returns:
MeshId - MeshId of the resulting polygon mesh
Usage example:
let polygonMesh = create_polygon_mesh(PolygonInit(sides = 6, radius = 1.0))
let polygonNode = create_node()
add_component(polygonNode, new Mesh(meshId = polygonMesh))
- create_sphere_mesh(init_params: SphereInit): MeshId
Generates a sphere mesh
Note that it’s recommended to make several nodes with the same sphere mesh and scale them instead of creating multiple meshes with different radii - each mesh will have its own draw call
- Arguments:
init_params : SphereInit - The data to build the sphere mesh from
- Returns:
MeshId - MeshId of the resulting sphere mesh
Usage example:
let sphereMesh = create_sphere_mesh(SphereInit(subdivisions = 64, radius = 1.f))
let sphereNode = create_node()
add_component(sphereNode, new Mesh(meshId = sphereMesh))
- create_cube_mesh(init_params: CubeInit): MeshId
Generates a cube mesh
Note that it’s recommended to make several nodes with the same cube mesh and scale them instead of creating multiple meshes with different sizes - each mesh will have its own draw call
- Arguments:
init_params : CubeInit - The data to build the cube mesh from
- Returns:
MeshId - MeshId of the resulting cuboid mesh
Usage example:
let stickMesh = create_cube_mesh(CubeInit(width = 0.1, height = 1.0, depth = 0.1))
let cubeNode = create_node()
add_component(cubeNode, new Mesh(meshId = stickMesh))
- create_plane_mesh(init_params: PlaneInit): MeshId
Generates a plane mesh
The plane will lie in the XZ (horizontal) plane with its normal pointing upwards (in the positive Y direction). The plane can be subdivided to cointain more than one cell
- Arguments:
init_params : PlaneInit - The data to build the plane mesh from
- Returns:
MeshId - MeshId of the resulting plane mesh
Usage example:
let planeMesh = create_plane_mesh(PlaneInit())
let planeNode = create_node()
add_component(planeNode, new Mesh(meshId = planeMesh))
- create_prism_mesh(init_params: PrismInit): MeshId
Generates a prism mesh
Note that it’s recommended to make several nodes with the same prism mesh and scale them instead of creating multiple meshes with different sizes - each mesh will have its own draw call
- Arguments:
init_params : PrismInit - The data to build the prism mesh from
- Returns:
MeshId - MeshId of the resulting prism mesh
Usage example:
let prismMesh = create_prism_mesh(PrismInit(sides = 5, height = 4.0, radius = 1.0))
let prismNode = create_node()
add_component(prismNode, new Mesh(meshId = prismMesh))
- create_cylinder_mesh(init_params: CylinderInit): MeshId
Generates a cylinder mesh
Note that it’s recommended to make several nodes with the same cylinder mesh and scale them instead of creating multiple meshes with different sizes - each mesh will have its own draw call
- Arguments:
init_params : CylinderInit - The data to build the cylinder mesh from
- Returns:
MeshId - MeshId of the resulting cylinder mesh
Usage example:
let cylinderMesh = create_cylinder_mesh(CylinderInit(height = 4.0, radius = 1.0))
let cylinderNode = create_node()
add_component(cylinderNode, new Mesh(meshId = cylinderMesh))
- create_pyramid_mesh(init_params: PyramidInit): MeshId
Generates a pyramid mesh
Note that it’s recommended to make several nodes with the same pyramid mesh and scale them instead of creating multiple meshes with different sizes - each mesh will have its own draw call
- Arguments:
init_params : PyramidInit - The data to build the pyramid mesh from
- Returns:
MeshId - MeshId of the resulting pyramid mesh
Usage example:
let pyramidMesh = create_pyramid_mesh(PyramidInit(sides = 5, height = 4.0, radius = 1.0))
let pyramidNode = create_node()
add_component(pyramidNode, new Mesh(meshId = pyramidMesh))
- create_cone_mesh(init_params: ConeInit): MeshId
Generates a cone mesh
Note that it’s recommended to make several nodes with the same cone mesh and scale them instead of creating multiple meshes with different sizes - each mesh will have its own draw call
- Arguments:
init_params : ConeInit - The data to build the cone mesh from
- Returns:
MeshId - MeshId of the resulting cone mesh
Usage example:
let coneMesh = create_cone_mesh(ConeInit(height = 4.0, radius = 1.0))
let coneNode = create_node()
add_component(coneNode, new Mesh(meshId = coneMesh))
- create_capsule_mesh(init_params: CapsuleInit): MeshId
Generates a capsule mesh
- Arguments:
init_params : CapsuleInit - The data to build the capsule mesh from
- Returns:
MeshId - MeshId of the resulting capsule mesh
Usage example:
let capsuleMesh = create_capsule_mesh(CapsuleInit(height = 4.0, radius = 0.5))
let capsuleNode = create_node()
add_component(capsuleNode, new Mesh(meshId = capsuleMesh))
- create_solid_of_revolution_mesh(init_params: RevolutionInit): MeshId
Generates a solid of revolution mesh
The function takes an array of float2 points and rotates it around Z-axis to create a solid of revolution mesh
- Arguments:
init_params : RevolutionInit - The data to build the solid of revolution mesh from
- Returns:
MeshId - MeshId of the resulting solid of revolution mesh
Usage example:
let coneMesh = create_solid_of_revolution_mesh(RevolutionInit(
points = [float2(0, 0), float2(1, 0), float2(0, 2)] // 2D profile of cone mesh
))
let coneNode = create_node()
add_component(coneNode, new Mesh(meshId = coneMesh))
- create_extruder_mesh(init_params: ExtruderInit): MeshId
Generates an extruder mesh
The function creates a mesh with pipe-like geometry by extruding a ‘polygonPoints’ profile along a path with ‘points’
- Arguments:
init_params : ExtruderInit - The data to build the extruder mesh from
- Returns:
MeshId - MeshId of the resulting extruder mesh
Usage example:
let extruderMesh = create_extruder_mesh(ExtruderInit(// Generates an extruder mesh with a square profile and a three-point path.
polygonPoints = [float2(-0.5, -0.5), float2(-0.5, 0.5), float2(0.5, 0.5), float2(0.5, -0.5)],
points = [float3(0, 0, 0), float3(10, 0, 0), float3(0, 20, 0)]
))
let extruderNode = create_node()
add_component(extruderNode, new Mesh(meshId = extruderMesh))
- create_pipe_mesh(init_params: PipeInit): MeshId
Generates a pipe mesh
- Arguments:
init_params : PipeInit - The data to build the pipe mesh from
- Returns:
MeshId - MeshId of the resulting pipe mesh
Usage example:
let pipeMesh = create_pipe_mesh(PipeInit(// Generates a pipe mesh with a three-point path.
points = [float3(0, 0, 0), float3(10, 0, 0), float3(0, 20, 0)]
))
let pipeNode = create_node()
add_component(pipeNode, new Mesh(meshId = pipeMesh))
- create_quad_mesh_geometry(width: float; height: float): MeshGeometry
Generates a quad mesh geometry
- Arguments:
width : float - The width of the quad
height : float - The height of the quad
- Returns:
MeshGeometry - A mesh geometry (vertices, normals, uvs, triangles) representing the quad
Usage example:
var quadGeometry = create_quad_mesh_geometry(2.0, 3.0) // Generate a quad with width 2.0 and height 3.0
update_mesh(quadMesh, quadGeometry) // Update the mesh with the new geometry
- create_polygon_mesh_geometry(sides: int = 24; radius: float = 0.5f): MeshGeometry
Generates a regular polygon mesh geometry with a specified number of sides and radius. It can also be used to generate geometry for a circle
- Arguments:
sides : int - The number of sides of the polygon
radius : float - The radius of the polygon
- Returns:
MeshGeometry - A mesh geometry (vertices, normals, uvs, triangles) representing the polygon
Usage example:
var polygonMesh = create_polygon_mesh_geometry(6, 1.0) // Generate a hexagon
update_mesh(mesh, polygonMesh) // Update the mesh with the new geometry
- create_sphere_mesh_geometry(subdivisions: int = 64; radius: float = 0.5f): MeshGeometry
Generates a sphere mesh geometry
- Arguments:
subdivisions : int - The number of subdivisions. The more subdivisions, the smoother the sphere. Default is 64
radius : float - The radius of the sphere. Default is 0.5
- Returns:
MeshGeometry - A mesh geometry (vertices, normals, uvs, triangles) representing the sphere
Usage example:
var sphereGeometry = create_sphere_mesh_geometry(32, 1.0) // Generate a sphere with 32 subdivisions and radius 1.0
update_mesh(cylinderMesh, sphereGeometry) // Update the mesh with the new geometry
- create_cube_mesh_geometry(sizeX: float = 1f; sizeY: float = 1f; sizeZ: float = 1f): MeshGeometry
Generates a rectangular cuboid mesh geometry with specified dimensions.
- Arguments:
sizeX : float - The size of the cube along the X axis
sizeY : float - The size of the cube along the Y axis
sizeZ : float - The size of the cube along the Z axis
- Returns:
MeshGeometry - A mesh geometry (vertices, normals, uvs, triangles) representing the cube
Usage example:
var cubeGeometry = create_cube_mesh_geometry(2.0, 2.0, 2.0) // Generate a cube with dimensions 2x2x2
update_mesh(cubeMesh, cubeGeometry) // Update the mesh with the new geometry
- create_plane_mesh_geometry(cells: int): MeshGeometry
Generates a plane mesh geometry.
- Arguments:
cells : int - The number of cells along one axis of the plane. The total number of cells will be
cells * cells
- Returns:
MeshGeometry - A mesh geometry (vertices, normals, uvs, triangles) representing the plane.
Usage example:
var planeGeometry = create_plane_mesh_geometry(10) // Generate a plane with 10x10 cells
update_mesh(planeMesh, planeGeometry) // Update the mesh with the new geometry
- create_cone_mesh_geometry(subdivisions: int = 24; height: float = 1f; radius: float = 0.5f; frustumRadius: float = 0f; cap: bool = true; smoothNormals: bool = true; axis: string = "y"; tessellation: int = 1): MeshGeometry
Generates a cone mesh geometry. It can also be used to generate geometry for a truncated cone, a pyramid, a truncated pyramid, a cylinder, or a prism
- Arguments:
subdivisions : int - The number of subdivisions. The more subdivisions, the smoother the cone
height : float - The height of the cone
radius : float - The radius of the cone at the base
frustumRadius : float - The radius of the cone at the top (if it is truncated). Default is 0, which means the cone is not truncated
cap : bool - Specifies whether to generate a cap for the cone. Default is true
smoothNormals : bool - Specifies whether to generate smooth normals (for a cone) or flat normals (for a pyramid). Default is true
axis : string - Specifies the orientation of the cone, can be “x”, “y”, or “z”. Default is “y”
tessellation : int - The number of subdivisions along the height of the cone. Default is 1. Increase this value if you have problems with cone smoothness or texture stretching, but keep in mind that it will increase the number of vertices and triangles.
- Returns:
MeshGeometry - A mesh geometry (vertices, normals, uvs, triangles) representing the cone
Usage example:
var pyramidGeometry = create_cone_mesh_geometry(3, 2., 1., 0.2, true, false, "y", 1) //Generate a truncated triangular pyramid
update_mesh(sphereMesh, pyramidGeometry) //Update the mesh with the new geometry
- create_capsule_mesh_geometry(subdivisions: int = 24; height: float = 2f; radius: float = 0.5f; axis: string = "y"): MeshGeometry
Generates a capsule mesh geometry
- Arguments:
subdivisions : int - The number of subdivisions. The more subdivisions, the smoother the capsule
height : float - The full height of the capsule (including the spherical caps)
radius : float - The radius of the capsule
axis : string - Specifies the orientation of the capsule, can be “x”, “y”, or “z”. Default is “y”
- Returns:
MeshGeometry - A mesh geometry (vertices, normals, uvs, triangles) representing the capsule
Usage example:
var capsuleGeometry = create_capsule_mesh_geometry(24, 1.0, 0.5, "y") // Generate a capsule mesh
update_mesh(capsuleMesh, capsuleGeometry) // Update the mesh with the new geometry
- create_solid_of_revolution_mesh_geometry(points: array<float2>; subdivisions: int; cap: bool = true): MeshGeometry
Generates a solid of revolution mesh geometry based on a set of 2D points
- Arguments:
points : array<float2> - An array of 2D points (float2) that define the profile of the solid of revolution
subdivisions : int - The number of subdivisions around the axis of revolution. The more subdivisions, the smoother the mesh
cap : bool - Specifies whether to generate caps for the solid of revolution. Default is true
- Returns:
MeshGeometry - A mesh geometry (vertices, normals, uvs, triangles) representing the solid of revolution
Usage example:
var profile = [float2(0., 0.), float2(1., 1.), float2(0., 2.)]
var solidMesh = create_solid_of_revolution_mesh_geometry(profile, 24, true)
update_mesh(solidMesh)
- create_extruder_mesh_geometry(polygonPoints: array<float2>; points: array<float3>; subdivisions: int; bendRadius: float; cap: bool = true; closed: bool = false; smoothNormals: bool = false): MeshGeometry
Generates a mesh geometry by extruding a polygon along a given path with optional bending and capping.
- Arguments:
polygonPoints : array<float2> - Represents the profile of the extruder. Clockwise order of the polygon points makes the normals point outwards making the extruder visible from the outside, counterclockwise order makes the normals point inwards making the extruder visible from the inside like a corridor
points : array<float3> - Represents the path of the extruder
subdivisions : int - The number of subdivisions. The more subdivisions, the smoother the extruder
bendRadius : float - The radius of all bends in the extruder
cap : bool - Specifies whether to generate caps at the ends of the extruded mesh. Default is true.
closed : bool - Specifies whether the path is closed, forming a loop. Default is false
smoothNormals : bool - Specifies whether the extruder profile is smoothed or not. Default is false
- Returns:
MeshGeometry - A mesh geometry (vertices, normals, uvs, triangles) representing the extruded polygon along the given path
Usage example:
var polygon = [float2(0, 0), float2(1, 0), float2(1, 1), float2(0, 1)]
var path = [float3(0, 0, 0), float3(0, 1, 0), float3(1, 1, 0)]
var extrudedMesh = create_extruder_mesh_geometry(polygon, path, 10, 0.5, true, false, true)
update_mesh(mesh, extrudedMesh) // Update the mesh with the new geometry
- create_pipe_mesh_geometry(points: array<float3>; subdivisions: int; radius: float; bendRadius: float; cap: bool = true; closed: bool = false): MeshGeometry
Generates a pipe mesh geometry along a given path defined by points.
- Arguments:
points : array<float3> - Represents the path of the pipe
subdivisions : int - The number of subdivisions. The more subdivisions, the smoother the pipe
radius : float - The radius of the pipe
bendRadius : float - The radius of all bends in the pipe
cap : bool - Specifies whether to generate caps at the ends of the pipe. Default is true
closed : bool - Specifies whether the pipe should be closed, forming a loop. Default is false
- Returns:
MeshGeometry - A mesh geometry (vertices, normals, uvs, triangles) representing the pipe
Usage example:
var pipeGeometry = create_pipe_mesh_geometry([float3(0, 0, 0), float3(1, 0, 0), float3(1, 1, 0)], 16, 0.1, 0.2, true, false)
update_mesh(pipeMesh, pipeGeometry) // Update the mesh with the new geometry