Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- extends Node3D
- @onready var high: MultiMeshInstance3D = $_LOD_1
- @onready var mid: MultiMeshInstance3D = $_LOD_2
- @onready var low: MultiMeshInstance3D = $_LOD_3
- #how many objects in total
- @export var instance_count: int = 40
- #max number visible at once, -1 = all
- @export var visible_instance_count: int = -1
- # Overall universal scale of the three models
- @export var tree_scale: float = 1.0 # Editable in Inspector
- func _ready() -> void:
- apply_uniform_scale(tree_scale)
- _LOD_1_visible_vars()
- _LOD_2_visible_vars()
- _LOD_3_visible_vars()
- _populate_multimesh(high, 10)
- _populate_multimesh(mid, 10)
- _populate_multimesh(low, 10)
- func apply_uniform_scale(new_scale: float) -> void:
- tree_scale = new_scale
- var uniform_scale = Vector3(tree_scale, tree_scale, tree_scale)
- high.scale = uniform_scale
- mid.scale = uniform_scale
- low.scale = uniform_scale
- # LOD 1 Visibility
- func _LOD_1_visible_vars():
- high.visibility_range_begin = 0.0
- high.visibility_range_begin_margin = 4.5
- high.visibility_range_end = 6.0
- high.visibility_range_end_margin = 6.5
- # LOD 2 Visibility
- func _LOD_2_visible_vars():
- mid.visibility_range_begin = 6.0
- mid.visibility_range_begin_margin = 12.5
- mid.visibility_range_end = 18.0
- mid.visibility_range_end_margin = 18.5
- # LOD 3 Visibility
- func _LOD_3_visible_vars():
- low.visibility_range_begin = 18.0
- low.visibility_range_begin_margin = 22.5
- low.visibility_range_end = 35.0
- low.visibility_range_end_margin = 36.0
- # Populates MultiMesh with instances
- func _populate_multimesh(multimesh_instance: MultiMeshInstance3D, count: int):
- if multimesh_instance.multimesh == null:
- return # Ensure MultiMesh exists
- multimesh_instance.multimesh.instance_count = count
- for i in range(count):
- var xform = Transform3D()
- xform.origin = Vector3(randf() * 10, 5, randf() * 10) # Ensures objects are visible
- multimesh_instance.multimesh.set_instance_transform(i, xform)
- # Debugging Visibility
- func _process(delta: float) -> void:
- print("LOD 1 Instances: ", high.multimesh.instance_count)
- print("LOD 2 Instances: ", mid.multimesh.instance_count)
- print("LOD 3 Instances: ", low.multimesh.instance_count)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement