Advertisement
Stevie_D

HTerrain procgen script

Nov 7th, 2023 (edited)
46
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
GDScript 3.57 KB | Gaming | 0 0
  1. # Written by zylann and available at: https://hterrain-plugin.readthedocs.io/en/latest/#procedural-generation
  2. # lerp notation updated by Steve Downing
  3.  
  4. extends Node3D
  5.  
  6. @export var noise_multiplier: float
  7.  
  8. # Import classes
  9. const HTerrain = preload("res://addons/zylann.hterrain/hterrain.gd")
  10. const HTerrainData = preload("res://addons/zylann.hterrain/hterrain_data.gd")
  11. const HTerrainTextureSet = preload("res://addons/zylann.hterrain/hterrain_texture_set.gd")
  12.  
  13. # You may want to change paths to your own textures
  14. var grass_texture = load("res://addons/zylann.hterrain/textures/grass.png")
  15. var sand_texture = load("res://addons/zylann.hterrain/textures/sand.png")
  16. var leaves_texture = load("res://addons/zylann.hterrain/textures/leaves.png")
  17.  
  18.  
  19. func _ready():
  20.     # Create terrain resource and give it a size.
  21.     # It must be either 513, 1025, 2049 or 4097.
  22.     var terrain_data = HTerrainData.new()
  23.     terrain_data.resize(513)
  24.  
  25.     var noise = FastNoiseLite.new()
  26.     #var noise_multiplier = 50.0
  27.  
  28.     # Get access to terrain maps
  29.     var heightmap: Image = terrain_data.get_image(HTerrainData.CHANNEL_HEIGHT)
  30.     var normalmap: Image = terrain_data.get_image(HTerrainData.CHANNEL_NORMAL)
  31.     var splatmap: Image = terrain_data.get_image(HTerrainData.CHANNEL_SPLAT)
  32.  
  33.     # Generate terrain maps
  34.     # Note: this is an example with some arbitrary formulas,
  35.     # you may want to come up with your owns
  36.     for z in heightmap.get_height():
  37.         for x in heightmap.get_width():
  38.             # Generate height
  39.             var h = noise_multiplier * noise.get_noise_2d(x, z)
  40.  
  41.             # Getting normal by generating extra heights directly from noise,
  42.             # so map borders won't have seams in case you stitch them
  43.             var h_right = noise_multiplier * noise.get_noise_2d(x + 0.1, z)
  44.             var h_forward = noise_multiplier * noise.get_noise_2d(x, z + 0.1)
  45.             var normal = Vector3(h - h_right, 0.1, h_forward - h).normalized()
  46.  
  47.             # Generate texture amounts
  48.             var splat = splatmap.get_pixel(x, z)
  49.             var slope = 4.0 * normal.dot(Vector3.UP) - 2.0
  50.             # Sand on the slopes
  51.             var sand_amount = clamp(1.0 - slope, 0.0, 1.0)
  52.             # Leaves below sea level
  53.             var leaves_amount = clamp(0.0 - h, 0.0, 1.0)
  54.             splat = splat.lerp(Color(0,1,0,0), sand_amount)
  55.             splat = splat.lerp(Color(0,0,1,0), leaves_amount)
  56.  
  57.             heightmap.set_pixel(x, z, Color(h, 0, 0))
  58.             normalmap.set_pixel(x, z, HTerrainData.encode_normal(normal))
  59.             splatmap.set_pixel(x, z, splat)
  60.  
  61.     # Commit modifications so they get uploaded to the graphics card
  62.     var modified_region = Rect2(Vector2(), heightmap.get_size())
  63.     terrain_data.notify_region_change(modified_region, HTerrainData.CHANNEL_HEIGHT)
  64.     terrain_data.notify_region_change(modified_region, HTerrainData.CHANNEL_NORMAL)
  65.     terrain_data.notify_region_change(modified_region, HTerrainData.CHANNEL_SPLAT)
  66.  
  67.     # Create texture set
  68.     # NOTE: usually this is not made from script, it can be built with editor tools
  69.     var texture_set = HTerrainTextureSet.new()
  70.     texture_set.set_mode(HTerrainTextureSet.MODE_TEXTURES)
  71.     texture_set.insert_slot(-1)
  72.     texture_set.set_texture(0, HTerrainTextureSet.TYPE_ALBEDO_BUMP, grass_texture)
  73.     texture_set.insert_slot(-1)
  74.     texture_set.set_texture(1, HTerrainTextureSet.TYPE_ALBEDO_BUMP, sand_texture)
  75.     texture_set.insert_slot(-1)
  76.     texture_set.set_texture(2, HTerrainTextureSet.TYPE_ALBEDO_BUMP, leaves_texture)
  77.  
  78.     # Create terrain node
  79.     var terrain = HTerrain.new()
  80.     terrain.set_shader_type(HTerrain.SHADER_CLASSIC4_LITE)
  81.     terrain.set_data(terrain_data)
  82.     terrain.set_texture_set(texture_set)
  83.     add_child(terrain)
  84.  
  85.     # No need to call this, but you may need to if you edit the terrain later on
  86.     #terrain.update_collider()
  87.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement