0 votes

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?

in Engine by (148 points)

1 Answer

+1 vote

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

by (144 points)
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.