How to make generated terrain smooth?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By KijeviGombooc

I generate a terrain based on a youtube tutorial. In short, I create a plane, change the y position of its vertices using tools, then generate a mesh out of it.
here is the code:

var planeMesh = PlaneMesh.new()
planeMesh.size = Vector2(chunkSize, chunkSize)
planeMesh.subdivide_depth = chunkSize * resolution
planeMesh.subdivide_width = chunkSize * resolution

var surfaceTool = SurfaceTool.new()
surfaceTool.create_from(planeMesh, 0)
var arrayPlane = surfaceTool.commit()
var dataTool = MeshDataTool.new()
var error = dataTool.create_from_surface(arrayPlane, 0)

for i in range(dataTool.get_vertex_count()):
	var vertex = dataTool.get_vertex(i)
	vertex.y = noise.get_noise_3d(vertex.x + x, vertex.y, vertex.z + z) * 60
	dataTool.set_vertex(i, vertex)

for s in range(arrayPlane.get_surface_count()):
	arrayPlane.surface_remove(s)

dataTool.commit_to_surface(arrayPlane)
surfaceTool.begin(Mesh.PRIMITIVE_TRIANGLES)
surfaceTool.create_from(arrayPlane, 0)
surfaceTool.generate_normals()

meshInstance = MeshInstance.new()
meshInstance.mesh = surfaceTool.commit()
meshInstance.set_surface_material(0, load("res://GFX/TerrainMat.tres"))
parent.add_child(meshInstance)

I want to make the shading be smooth. If I know well, the normals have to be calculated per vertex and not per side, but I am not sure.
how can I make the shading smooth?

:bust_in_silhouette: Reply From: ArdaE

Replace the following line:

surfaceTool.create_from(arrayPlane, 0)

with these lines:

surfaceTool.add_smooth_group(true)
surfaceTool.append_from(arrayPlane, 0, Transform.IDENTITY)

This should make your mesh smooth (tested in Godot 3.2.1).