Geometría procedural

There are many ways to procedurally generate geometry in Godot. In this tutorial series we will explore a few of them. Each technique has its own benefits and drawbacks, so it is best to understand each one and how it can be useful in a given situation.

¿Qué es geometría?

La geometría es una forma elegante de decir formas. En los gráficos por ordenador, la geometría se representa típicamente por una serie de posiciones llamadas "vértices". En Godot, la geometría es representada por Meshes.

¿Qué es una Mesh?

Muchas cosas en Godot tienen un mesh en su nombre: el Mesh, el ArrayMesh, el MeshInstance, el MultiMesh, y el MultiMeshInstance. Aunque todos están relacionados, tienen usos ligeramente diferentes.

Meshes y ArrayMeshes son recursos que se dibujan usando un nodo de MeshInstance. Recursos como Meshes y ArrayMeshes no pueden ser añadidos a la escena directamente. Un MeshInstance representa una instancia de un mesh en tu escena. Puedes reutilizar una solo mesh en múltiples MeshInstance para que se dibuje en diferentes partes de tu escena con diferentes materiales o transformaciones (escala, rotación, posición, etc.).

If you are going to draw the same object many times, it can be helpful to use a MultiMesh with a MultiMeshInstance. MultiMeshInstances draw meshes thousands of times very cheaply by taking advantage of hardware instancing. The drawback with using a MultiMeshInstance is that each of your mesh's surfaces are limited to one material for all instances. It uses an instance array to store different colors and transformations for each instance, but all the instances of each surface use the same material.

Qué es una Mesh

Un Mesh está compuesto de una o más caras. Una cara es un conjunto compuesto de múltiples sub-arrays que contienen vértices, normales, UVs, etc. Normalmente el proceso de construcción de superficies y mallas está oculto para el usuario en el VisualServer, pero con ArrayMeshes, el usuario puede construir una Mesh manualmente pasando un array que contiene la información de la superficie.

Caras

Each surface has its own material. Alternatively, you can override the material for all surfaces in the Mesh when you use a MeshInstance using the material_override property.

Array de las caras

The surface array is an array of length ArrayMesh.ARRAY_MAX. Each position in the array is filled with a sub-array containing per-vertex information. For example, the array located at ArrayMesh.ARRAY_NORMAL is a PoolVector3Array of vertex normals. See Mesh.ArrayType for more information.

The surface array can be indexed or non-indexed. Creating a non-indexed array is as easy as not assigning an array at the index ArrayMesh.ARRAY_INDEX. A non-indexed array stores unique vertex information for every triangle, meaning that when two triangles share a vertex, the vertex is duplicated in the array. An indexed surface array only stores vertex information for each unique vertex and then also stores an array of indices which maps out how to construct the triangles from the vertex array. In general, using an indexed array is faster, but it means you have to share vertex data between triangles, which is not always desired (e.g. when you want per-face normals).

Herramientas

Godot provides different ways of accessing and working with geometry. More information on each will be provided in the following tutorials.

ArrayMesh

The ArrayMesh resource extends Mesh to add a few different quality of life functions and, most importantly, the ability to construct a Mesh surface through scripting.

Para más información sobre ArrayMesh, lee ArrayMesh tutorial.

MeshDataTool

El MeshDataTool es un recurso que convierte los datos del Mesh en arrays de vértices, caras y bordes que pueden ser modificados en tiempo de ejecución.

Para más información sobre MeshDataTool, lee MeshDataTool tutorial.

SurfaceTool

The SurfaceTool allows the creation of Meshes using an OpenGL 1.x immediate mode style interface.

Para más información sobre SurfaceTool, por favor lee el SurfaceTool tutorial.

ImmediateGeometry

ImmediateGeometry is a node that uses an immediate mode style interface (like SurfaceTool) to draw objects. The difference between ImmediateGeometry and the SurfaceTool is that ImmediateGeometry is a node itself that can be added to the scene tree and is drawn directly from the code, while the SurfaceTool generates a Mesh that needs to be added to a MeshInstance to be seen.

ImmediateGeometry is useful for prototyping because of its straightforward API, but it is slow because the geometry is rebuilt every frame. It is most useful for adding simple geometry for visual debugging (e.g. by drawing lines to visualize physics raycasts etc.).

Para más información sobre ImmediateGeometry, por favor lee el ImmediateGeometry tutorial.

¿Cuál método debería usar?

Which approach you use depends on what you are trying to do and what kind of procedure you are comfortable with.

Tanto SurfaceTool como ArrayMesh son excelentes para generar geometría estática (meshes) que no cambian con el tiempo.

Using an ArrayMesh is slightly faster than using a SurfaceTool, but the API is a little more challenging. Additionally, SurfaceTool has a few quality of life methods such as generate_normals() and index().

ImmediateGeometry regenerates the mesh every frame, so it is much slower than ArrayMesh or SurfaceTool. However, if you need the geometry to change every frame anyway, it provides a much easier interface that may even be a little faster than generating an ArrayMesh every frame.

The MeshDataTool is not fast, but it gives you access to all kinds of properties of the mesh that you don't get with the others (edges, faces, etc.). It is incredibly useful when you need that sort of data to transform the mesh, but it is not a good idea to use it if that extra information is not needed. The MeshDataTool is best used if you are going to be using an algorithm that requires access to the face or edge array.