Advertisement
lil_sue

multimeshinstancing3D (broken)

May 25th, 2025
908
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
GDScript 2.16 KB | Gaming | 0 0
  1. extends Node3D
  2.  
  3. @onready var high: MultiMeshInstance3D = $_LOD_1
  4. @onready var mid: MultiMeshInstance3D = $_LOD_2
  5. @onready var low: MultiMeshInstance3D = $_LOD_3
  6.  
  7. #how many objects in total
  8. @export var instance_count: int = 40
  9. #max number visible at once, -1 = all
  10. @export var visible_instance_count: int = -1
  11. # Overall universal scale of the three models
  12. @export var tree_scale: float = 1.0  # Editable in Inspector
  13.  
  14. func _ready() -> void:
  15.     apply_uniform_scale(tree_scale)
  16.    
  17.     _LOD_1_visible_vars()
  18.     _LOD_2_visible_vars()
  19.     _LOD_3_visible_vars()
  20.  
  21.     _populate_multimesh(high, 10)
  22.     _populate_multimesh(mid, 10)
  23.     _populate_multimesh(low, 10)
  24.  
  25. func apply_uniform_scale(new_scale: float) -> void:
  26.     tree_scale = new_scale
  27.     var uniform_scale = Vector3(tree_scale, tree_scale, tree_scale)
  28.  
  29.     high.scale = uniform_scale
  30.     mid.scale = uniform_scale
  31.     low.scale = uniform_scale
  32.  
  33. # LOD 1 Visibility
  34. func _LOD_1_visible_vars():
  35.     high.visibility_range_begin = 0.0
  36.     high.visibility_range_begin_margin = 4.5
  37.     high.visibility_range_end = 6.0
  38.     high.visibility_range_end_margin = 6.5
  39.  
  40. # LOD 2 Visibility
  41. func _LOD_2_visible_vars():
  42.     mid.visibility_range_begin = 6.0
  43.     mid.visibility_range_begin_margin = 12.5
  44.     mid.visibility_range_end = 18.0
  45.     mid.visibility_range_end_margin = 18.5
  46.  
  47. # LOD 3 Visibility
  48. func _LOD_3_visible_vars():
  49.     low.visibility_range_begin = 18.0
  50.     low.visibility_range_begin_margin = 22.5
  51.     low.visibility_range_end = 35.0
  52.     low.visibility_range_end_margin = 36.0
  53.  
  54. # Populates MultiMesh with instances
  55. func _populate_multimesh(multimesh_instance: MultiMeshInstance3D, count: int):
  56.     if multimesh_instance.multimesh == null:
  57.         return  # Ensure MultiMesh exists
  58.  
  59.     multimesh_instance.multimesh.instance_count = count
  60.  
  61.     for i in range(count):
  62.         var xform = Transform3D()
  63.         xform.origin = Vector3(randf() * 10, 5, randf() * 10)  # Ensures objects are visible
  64.         multimesh_instance.multimesh.set_instance_transform(i, xform)
  65.  
  66. # Debugging Visibility
  67. func _process(delta: float) -> void:
  68.     print("LOD 1 Instances: ", high.multimesh.instance_count)
  69.     print("LOD 2 Instances: ", mid.multimesh.instance_count)
  70.     print("LOD 3 Instances: ", low.multimesh.instance_count)
  71.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement