Prozedurale Geometrie

Es gibt viele Möglichkeiten, Geometrie in Godot prozedural zu generieren. In dieser Anleitung werden wir einige davon untersuchen. Jede Technik hat ihre eigenen Vor- und Nachteile. Daher ist es am besten jede zu verstehen und zu wissen wie sie in einer bestimmten Situation nützlich sein kann.

Was ist Geometrie?

Geometrie ist eine ausgefallene Art, Form zu sagen. In der Computergrafik wird die Geometrie typischerweise durch ein Array von Positionen dargestellt, die als "Eckpunkte" bezeichnet werden. In Godot wird die Geometrie durch Meshes (Gitternetze) dargestellt.

Was ist ein Mesh?

Many things in Godot have mesh in their name: the Mesh, the ArrayMesh, the MeshInstance, the MultiMesh, and the MultiMeshInstance. While they are all related, they have slightly different uses.

Meshes and ArrayMeshes are resources that are drawn using a MeshInstance node. Resources like Meshes and ArrayMeshes cannot be added to the scene directly. A MeshInstance represents one instance of a mesh in your scene. You can reuse a single mesh in multiple MeshInstances to draw it in different parts of your scene with different materials or transformations (scale, rotation, position etc.).

If you are going to draw the same object many times, it can be helpful to use a MultiMesh with a MultiMeshInstance. The MultiMeshInstance draws meshes thousands of times very cheaply. It takes advantage of hardware instancing in order to do so. The drawback with using a MultiMeshInstance is that you 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 use the same material.

Was ein Mesh ist

A Mesh is composed of one or more surfaces. A surface is an array composed of multiple sub-arrays containing vertices, normals, UVs, etc. Normally the process of constructing surfaces and meshes is hidden from the user in the VisualServer, but with ArrayMeshes, the user can construct a Mesh manually by passing in an array containing the surface information.

Oberflächen

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

Oberflächen-Array

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.

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 triangle 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).

Werkzeuge

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

ArrayMesh

Die ArrayMesh-Ressource erweitert Mesh um einige verschiedene Funktionen zur Verbesserung der Lebensqualität und vor allem um die Möglichkeit, eine Mesh-Oberfläche durch Skripterstellung zu erstellen.

Für mehr Informationen zum ArrayMesh siehe ArrayMesh tutorial.

MeshDataTool

Das MeshDataTool ist eine Ressource, die Mesh-Daten in Arrays von Scheitelpunkten, Flächen und Kanten konvertiert, die zur Laufzeit geändert werden können.

Weitere Informationen zum MeshDataTool finden Sie in der MeshDataTool-Anleitung.

SurfaceTool

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

Weitere Informationen zum SurfaceTool finden Sie in der SurfaceTool-Anleitung.

GenerierteGeometrie

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. The SurfaceTool generates a Mesh that needs to be added a MeshInstance to be seen.

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

Für weitere Informationen zu ImmediateGeometry siehe ImmediateGeometry tutorial.

Welche sollte man nutzen?

Welche Methode Sie verwenden hängt davon ab, was Sie versuchen und mit welcher Art von Verfahren Sie vertraut sind.

Sowohl SurfaceTool als auch ArrayMesh eignen sich hervorragend zum Generieren statischer Geometrie (Meshes), die sich im Laufe der Zeit nicht ändern.

Using an ArrayMesh is slightly faster than using a SurfaceTool, but the API is more 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 if that 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.