54 lines
1.6 KiB
GDScript
54 lines
1.6 KiB
GDScript
# GrassMultiMesh.gd
|
|
extends MultiMeshInstance3D
|
|
var mm: MultiMesh
|
|
var parent_node: GrassController
|
|
var rng: RandomClass = RandomClass.new()
|
|
|
|
func _ready() -> void:
|
|
parent_node = get_parent() as GrassController
|
|
if parent_node == null:
|
|
Log.pr("Error: Parent node is not a GrassController!")
|
|
|
|
func setup_multimesh() -> void:
|
|
if parent_node == null:
|
|
Log.pr("Error: Parent node not available in setup_multimesh")
|
|
return
|
|
|
|
# Use the same RNG seed as the parent for consistency
|
|
if parent_node.parent_node and parent_node.parent_node.cell_info:
|
|
rng.set_seed(parent_node.parent_node.cell_info.cell_seed)
|
|
|
|
# Load the mesh resource directly
|
|
var mesh = load("res://Stages/Test3D/assets/stylizedGrassMeshes/grass2_mesh.res")
|
|
if mesh == null:
|
|
Log.pr("Error: Could not load grass mesh")
|
|
return
|
|
|
|
# Create new MultiMesh instance
|
|
mm = MultiMesh.new()
|
|
|
|
# Configure the MultiMesh
|
|
mm.transform_format = MultiMesh.TRANSFORM_3D
|
|
mm.instance_count = parent_node.grass_instance_range
|
|
mm.mesh = mesh
|
|
|
|
# Generate random positions for grass using seeded 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.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)
|
|
|
|
# Assign the MultiMesh to this node
|
|
multimesh = mm
|
|
cast_shadow = GeometryInstance3D.SHADOW_CASTING_SETTING_OFF
|