Procedural geometry generation¶
Users often ask how to generate geometry from code. This is not very complicated, but it’s not obvious. Godot provides a few classes entirely dedicated to make it this easy. Still, the best tool for the job depends entirely on the use case.
This is the most common helper. SurfaceTool is a class you can instantiate to generate Meshes, specifically Mesh Surfaces.
It has a similar API to OpenGL 1.x, and it’s meant for static content. This means, the mesh is generated once and then used.
Here is a simple example of how to use it to add a single triangle.
Just explore the APIs and the possibilities.
Unlike SurfaceTool, ImmediateGeometry is an actual node. It’s similar in the «OpenGL 1.x» style API, but it’s actually designed to create content on the fly and modify it every frame efficiently.
Generating complex geometry (several thousand vertices) with this node is inefficient, even if it’s done only once. Instead, ImmediateGeometry is designed to generate simple geometry that changes every frame.
It’s used similar to SurfaceTool.
Lastly, the final way to do this is to create arrays themselves. This is the most efficient way to create static geometry, and is only recommended when SurfaceTool is not fast enough.
Similar code as before, but draw a square using indices: