nature-sim/Entities/GroundTile/scripts/bush_multimesh.gd
Dan Baker 3959333534 Adds bushes and flowers to ground tiles
Implements bush and flower spawning on ground tiles based on vegetation density.
Adds new assets for bushes and flowers, and introduces multi-mesh rendering for optimized performance.
Introduces seasonal color variations for vegetation using a shader for bushes and materials for flowers and grass.
Refactors material application into a MaterialManager to handle material assignments over multiple frames.
Moves ground tile scripts into a subfolder.
Adds floating particles to test scene.
2025-06-29 10:58:18 +01:00

56 lines
1.5 KiB
GDScript

extends MultiMeshInstance3D
var mm: MultiMesh
var parent_node: BushController
static var bush_mesh: Mesh = null
var material: ShaderMaterial
func _ready() -> void:
parent_node = get_parent() as BushController
if parent_node == null:
Log.pr("Error: Parent node is not a BushController!")
# Load mesh once and reuse
if bush_mesh == null:
bush_mesh = load("res://Entities/Bush/assets/bush_large.res")
func setup_multimesh() -> void:
if parent_node == null:
return
if bush_mesh == null:
Log.pr("Error: Could not load bush 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 = bush_mesh
material = ColorData.get_random_bush_material(Season.current)
mm.mesh.surface_set_material(0, material)
# Configure instance count
mm.instance_count = parent_node.bush_instance_range
# Get shared RNG from GroundTile
var rng = parent_node.parent_node.get_rng()
# Generate positions using shared RNG
for i in range(mm.instance_count):
var random_pos = Vector3(
rng.randf_range(-1.0, 1.0),
0.0,
rng.randf_range(-1.0, 1.0)
)
var random_rotation = rng.randf_range(0.0, TAU)
var basis = Basis(Vector3.UP, random_rotation)
var random_scale = rng.randf_range(0.3, 0.9)
basis = basis.scaled(Vector3(random_scale, random_scale, random_scale))
var tx = Transform3D(basis, random_pos)
mm.set_instance_transform(i, tx)
multimesh = mm