Procedural terrain generation help!

Godot Version

4.6

Question

I m learning procedural generation, so i took this tutorialabout procedural terrain generation, in it i m unable to understand some things in the script, pls if anyone can explain, i m not much good at scripting started just few weeks ago !

The script :

extends Node

var mesh : MeshInstance3D
@export var size_depth : int = 100
@export var size_width : int = 100
@export var mesh_resolution : int = 2

@export var noise : FastNoiseLite

func _ready() :
	generate()

func generate():
	var plane_mesh = PlaneMesh.new()
	plane_mesh.size = Vector2(size_depth, size_width)
	plane_mesh.subdivide_depth = mesh_resolution * size_depth
	plane_mesh.subdivide_width = mesh_resolution * size_width
	plane_mesh.material = preload("res://868829797d609603fb1bf05c84d052ce.jpg")
	
	var surface = SurfaceTool.new()
	var data = MeshDataTool.new()
	surface.create_from(plane_mesh, 0)
	
	var array_plane = surface.commit()
	data.create_from_surface(array_plane, 0)
	
	for i in range(data.get_vertex_count()):
		var vertex = data.get_vertex(i)
		vertex.y = randf_range(0 , 0.5)
		data.set_vertex(i ,vertex)
	
	array_plane.clear_surfaces()
	
	data.commit_to_surface(array_plane)
	surface.begin(Mesh.PRIMITIVE_TRIANGLES)
	surface.create_from(array_plane, 0)
	surface.generate_normals()
	
	mesh = MeshInstance3D.new()
	mesh.mesh = surface.commit()
	mesh.create_trimesh_collision()
	mesh.cast_shadow =  GeometryInstance3D.SHADOW_CASTING_SETTING_OFF
	mesh.add_to_group("navsource")
	add_child(mesh)

In this the problem is i m getting confused as he used the .commit too many times and which commit does what ?

and pls explain me this block of this code ;

var array_plane = surface.commit()
	data.create_from_surface(array_plane, 0)

for i in range(data.get_vertex_count()):
		var vertex = data.get_vertex(i)
		vertex.y = randf_range(0 , 0.5)
		data.set_vertex(i ,vertex)
	
	array_plane.clear_surfaces()
	
	data.commit_to_surface(array_plane)
	surface.begin(Mesh.PRIMITIVE_TRIANGLES)
	surface.create_from(array_plane, 0)
	surface.generate_normals()

So the thing goes from subdivided plane_mesh to surface. Then

surface.commit()

returns array_plane, which is used to get access to the data with the function

data.create_from_surface(array_plane, 0)

The data is modifed then

data.commit_to_surface(array_plane)

writes the modifed vertex data back to the array, Then the surface is set up as a list of triangles with

surface.begin(Mesh.PRIMITIVE_TRIANGLES ,

and the surface is recreated from the modified array_plane, and then surface.commit()returns a mesh.mesh for the GPU to draw.

1 Like

If you hold CTRL key and click on a built in godot function inside the script editor, like surface.commit(), it opens the documentation where it tells you what the function does or how it should be used.

This is one of Godots strengths and helps learning to use it. You can also click other things like SurfaceTool to quickly jump to its documentation.

I just wanted to mention this, as I think it is so helpful when learning Godot scripting.

1 Like