I made this script to generate a heightmap based off of a png file following a Tutorial for random terrain generation (https://www.youtube.com/watch?v=OUnJEaatl2Q&t=75s) and using some of my own code to enable it to use the png instead of random noise.
extends Node3D
@export var point_remap = 30
var point_array = []
@export var size:int = 3000
@export var subdivide:int = 255
@export var amplitude:int = 16
func create_grid(point_count_x,point_count_z,image):
for x in point_count_x:
for z in point_count_z:
var point_color = image.get_pixel(x,z)
var point_color_v = point_color.v
var point_position = Vector3((x-128)*8,point_color_v * point_remap*8,(z-128)*8)
point_array.append(point_position)
func draw_planes():
var plane_mesh = PlaneMesh.new()
plane_mesh.size = Vector2(size,size)
plane_mesh.subdivide_depth = subdivide
plane_mesh.subdivide_width = subdivide
var surface_tool = SurfaceTool.new()
surface_tool.create_from(plane_mesh,0)
var data = surface_tool.commit_to_arrays()
var vertices = data[ArrayMesh.ARRAY_VERTEX]
for i in vertices.size():
if i < 65536:
var vertex = vertices[i]
vertices[i].y = point_array[i].y
data[ArrayMesh.ARRAY_VERTEX] = vertices
var array_mesh = ArrayMesh.new()
array_mesh.add_surface_from_arrays(Mesh.PRIMITIVE_TRIANGLES,data)
surface_tool.create_from(array_mesh,0)
surface_tool.generate_normals()
$Terrain/MeshInstance.mesh = surface_tool.commit()
$Terrain/CollisionShape.shape = array_mesh.create_trimesh_shape()
func _ready() -> void:
var image = load_image("res://MountainTest.png")
create_grid(256,256,image)
draw_planes()
func load_image(path: String):
var noiseImage = Image.new()
noiseImage.load(path)
noiseImage.decompress()
return noiseImage
when I input my image of a mountain it took it and mangled it in a really weird way. The terrain it generated was split down the middle and stretched so i created a numbered grid image to check what it was doing. The terrain that generated looked all chopped up and flipped around
after i split that image diagonally, placed the top half at the bottom, and then flipped the whole thing horizontally I got it to be legible but even then it was still stretched. Ive been stumped as to how this thing is getting so distorted for a few days now and any ideas as to why this could be happening would be really appreciated. I want to try and fix it on my own but I don't even understand where to start.
Create randomly generated mesh using noise in the Godot engine.
Starter Project
zip file: https://github.com/TheGodojo/Procedural-Terrain-Displacement-and-Collisions-STARTER/archive/refs/heads/main.zip
git repo: https://github.com/TheGodojo/Procedural-Terrain-Displacement-and-Collisions-Starter
Completed Project
zip file: https://github.com/Th...