Updates grass and tree spawning to use the parent's RNG, ensuring consistent random generation based on the seed. This removes redundant RNG instances and ensures that grass and trees are generated predictably for a given cell.
51 lines
1.4 KiB
GDScript
51 lines
1.4 KiB
GDScript
extends MultiMeshInstance3D
|
|
var mm: MultiMesh
|
|
var parent_node: GrassController
|
|
|
|
static var grass_mesh: Mesh = null
|
|
|
|
func _ready() -> void:
|
|
parent_node = get_parent() as GrassController
|
|
if parent_node == null:
|
|
Log.pr("Error: Parent node is not a GrassController!")
|
|
|
|
# Load mesh once and reuse
|
|
if grass_mesh == null:
|
|
grass_mesh = load("res://Stages/Test3D/assets/stylizedGrassMeshes/grass2_mesh.res")
|
|
|
|
func setup_multimesh() -> void:
|
|
if parent_node == null:
|
|
return
|
|
|
|
if grass_mesh == null:
|
|
Log.pr("Error: Could not load grass mesh")
|
|
return
|
|
|
|
# Reuse existing MultiMesh if possible, or create new one
|
|
if mm == null:
|
|
mm = MultiMesh.new()
|
|
mm.transform_format = MultiMesh.TRANSFORM_3D
|
|
mm.mesh = grass_mesh
|
|
|
|
# Configure instance count
|
|
mm.instance_count = parent_node.grass_instance_range
|
|
|
|
# Generate positions using shared RNG
|
|
for i in range(mm.instance_count):
|
|
var random_pos = Vector3(
|
|
parent_node.parent_node.rng.randf_range(-1.0, 1.0),
|
|
0.0,
|
|
parent_node.parent_node.rng.randf_range(-1.0, 1.0)
|
|
)
|
|
|
|
var random_rotation = parent_node.parent_node.rng.randf_range(0.0, TAU)
|
|
var basis = Basis(Vector3.UP, random_rotation)
|
|
|
|
var random_scale = parent_node.parent_node.rng.randf_range(0.05, 0.3)
|
|
basis = basis.scaled(Vector3(random_scale, random_scale, random_scale))
|
|
|
|
var tx = Transform3D(basis, random_pos)
|
|
mm.set_instance_transform(i, tx)
|
|
|
|
multimesh = mm
|
|
cast_shadow = GeometryInstance3D.SHADOW_CASTING_SETTING_OFF
|