Long ago I made a script that generates chunks for heightmap terrain. I never figured out how to displace an existing mesh, only how to generate a grid with its vertices globally bumped upward by noise. I'd like to know how to displace all vertices on a mesh in GDScript, in whichever direction their normals are facing.

For example: If I have a MeshInstance3D with a SphereMesh subdivided to the desired resolution of my planet: How would a script loop through all vertices and push or pull each vertice toward or away from the center of the sphere, based on the intensity of noise at its original location?

I'm not doing vertex displacement in a shader because I want the shape to have collisions and pathfinding, apart from other issues with large scale displacement in shaders. Please also specify how to generate the StaticBody3D/CollisionShape3D for the deformed mesh.

  • xyz replied to this.
    • Edited

    MirceaKitsune Initialize a MeshDataTool object with the original mesh. Iterate through vertices and for each vertex get its position and normal. Sample a 3d noise using the vertex position and move the vertex along its normal for the distance proportional to the noise sample. Pass the mesh from the data tool to a SurfaceTool object and let it auto calculate normals. Get the final ArrayMesh object from the SurfaceTool and assign it to a mesh instance node.

    To create a concave collision shape resource, call create_trimesh_shape() on the final mesh and assign the returned shape to a collision shape node.