Scale the sampling UVs.
Also note there’s no much use of vertex displacement in a shader if you plan to walk on the terrain. You’ll need a convex collider on the cpu side which would require displacing the vertices directly in vertex data.
extends MeshInstance3D
var rings = 50
var radial_segments = 50
var radius = 1
func _ready():
var surface_array = []
surface_array.resize(Mesh.ARRAY_MAX)
# PackedVector**Arrays for mesh construction.
var verts = PackedVector3Array()
var uvs = PackedVector2Array()
var normals = PackedVector3Array()
var indices = PackedInt32Array()
var thisrow = 0
var prevrow = 0
var point = 0
for i in range(rings + 1):
var v = float(i) / rings
var w = sin(PI * v)
var y = cos(PI * v)
# Loop over segments in ring.
for j in range(radial_segments + 1):
var u = float(j) / radial_segments
var x = sin(u * PI * 2.0)
var z = cos(u * PI * 2.0)
var vert = Vector3(x * radius * w, y * radius, z * radius * w)
verts.append(vert)
normals.append(vert.normalized())
uvs.append(Vector2(u, v))
point += 1
# Create triangles in ring using indices.
if i > 0 and j > 0:
indices.append(prevrow + j - 1)
indices.append(prevrow + j)
indices.append(thisrow + j - 1)
indices.append(prevrow + j)
indices.append(thisrow + j)
indices.append(thisrow + j - 1)
prevrow = thisrow
thisrow = point
# Assign arrays to surface array.
surface_array[Mesh.ARRAY_VERTEX] = verts
surface_array[Mesh.ARRAY_TEX_UV] = uvs
surface_array[Mesh.ARRAY_NORMAL] = normals
surface_array[Mesh.ARRAY_INDEX] = indices
# Create mesh surface from mesh array.
# No blendshapes, lods, or compression used.
mesh.add_surface_from_arrays(Mesh.PRIMITIVE_TRIANGLES, surface_array)
ResourceSaver.save(mesh, "res://sphere.tres", ResourceSaver.FLAG_COMPRESS)
So complete code for ArrayMesh from tutorial in Doc , but when I create resource why it is not shown in 3D scene preview ?
does it need to be regenerated through code every time or loaded from resource ?
Here is demonstration
How can this be useful without preview to generate geometry ?
Is there way from blender to translate shape into vertex and then use it inside the code ?
Well assign the resource to instance’s mesh property.
Also if you want to alter geometry you don’t need to build it from scratch. You can start with any mesh resource, either imported or Godot’s built in primitive and use MeshDataTool to move its vertices.
So for making terrains, you can start with a plane, iterate through vertices, get x and z for each vertex, sample the heightmap for those coordinates and displace vertex y accordingly. It’s simple.