Advertisement
Stevie_D

DitzyNinja Procedural Terrain Generation, updated for Godot 4.x

May 13th, 2024 (edited)
261
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
GDScript 1.25 KB | Gaming | 0 0
  1. @tool
  2. extends Node3D
  3. @export var generate_mesh_button : bool:
  4.     set(val):
  5.         generate_terrain()
  6.  
  7. @export var size : int = 64
  8. @export var subdivide : int = 63
  9. @export var amplitude : int = 5
  10.  
  11. @export var noise : FastNoiseLite = FastNoiseLite.new()
  12.  
  13. func _ready():
  14.     generate_terrain()
  15.  
  16. func generate_terrain():
  17.     var plane_mesh = PlaneMesh.new()
  18.     plane_mesh.size = Vector2(size,size)
  19.     plane_mesh.subdivide_depth = subdivide
  20.     plane_mesh.subdivide_width = subdivide
  21.        
  22.     var surface_tool = SurfaceTool.new()
  23.     surface_tool.create_from(plane_mesh,0)
  24.     var data = surface_tool.commit_to_arrays()
  25.     var vertices = data[ArrayMesh.ARRAY_VERTEX]
  26.    
  27.     for i in vertices.size():
  28.         var vertex = vertices[i]
  29.         vertices[i].y = noise.get_noise_2d(vertex.x,vertex.z) * amplitude
  30.     data[ArrayMesh.ARRAY_VERTEX] = vertices
  31.    
  32.     var array_mesh = ArrayMesh.new()
  33.     array_mesh.add_surface_from_arrays(Mesh.PRIMITIVE_TRIANGLES,data)
  34.    
  35.     surface_tool.create_from(array_mesh,0)
  36.     surface_tool.generate_normals()
  37.    
  38.     $MeshInstance3D.mesh = surface_tool.commit()
  39.     $CollisionShape3D.shape = array_mesh.create_trimesh_shape()
  40.    
  41. # Updated for Godot 4.x from "Procedural Terrain Generation: Displacement & Collisions"
  42. # by DitzyNinja's Godojo, at https://www.youtube.com/watch?v=OUnJEaatl2Q
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement