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.
This commit is contained in:
Dan Baker 2025-06-29 10:58:18 +01:00
parent ea5006e8a2
commit 3959333534
46 changed files with 559 additions and 77 deletions

Binary file not shown.

Binary file not shown.

View file

@ -4,12 +4,12 @@ importer="scene"
importer_version=1
type="PackedScene"
uid="uid://c3objh5he4fv7"
path="res://.godot/imported/plant_bush.glb-e9806d63c67fa60c2bf9125a46d34412.scn"
path="res://.godot/imported/plant_bush.glb-4eaf384fbf61c7e3b82505202bc9874e.scn"
[deps]
source_file="res://Entities/Tree/assets/temp/plant_bush.glb"
dest_files=["res://.godot/imported/plant_bush.glb-e9806d63c67fa60c2bf9125a46d34412.scn"]
source_file="res://Entities/Bush/assets/plant_bush.glb"
dest_files=["res://.godot/imported/plant_bush.glb-4eaf384fbf61c7e3b82505202bc9874e.scn"]
[params]

View file

@ -4,12 +4,12 @@ importer="scene"
importer_version=1
type="PackedScene"
uid="uid://cefyfwcx88gn8"
path="res://.godot/imported/plant_bushDetailed.glb-b49c41eb141c3e76fb272adf14466b5a.scn"
path="res://.godot/imported/plant_bushDetailed.glb-8633db2639023d65ffc045b449119f56.scn"
[deps]
source_file="res://Entities/Tree/assets/temp/plant_bushDetailed.glb"
dest_files=["res://.godot/imported/plant_bushDetailed.glb-b49c41eb141c3e76fb272adf14466b5a.scn"]
source_file="res://Entities/Bush/assets/plant_bushDetailed.glb"
dest_files=["res://.godot/imported/plant_bushDetailed.glb-8633db2639023d65ffc045b449119f56.scn"]
[params]
@ -32,6 +32,17 @@ animation/trimming=false
animation/remove_immutable_tracks=true
animation/import_rest_as_RESET=false
import_script/path=""
_subresources={}
_subresources={
"meshes": {
"plant_bushDetailed_Mesh plant_bushDetailed": {
"generate/lightmap_uv": 0,
"generate/lods": 0,
"generate/shadow_meshes": 0,
"lods/normal_merge_angle": 60.0,
"save_to_file/enabled": true,
"save_to_file/path": "res://Entities/Bush/assets/bush_large.res"
}
}
}
gltf/naming_version=1
gltf/embedded_image_handling=1

View file

@ -4,12 +4,12 @@ importer="scene"
importer_version=1
type="PackedScene"
uid="uid://bqh742nyfy67t"
path="res://.godot/imported/plant_bushLarge.glb-444f7e2fefa42d9967360c2460aba973.scn"
path="res://.godot/imported/plant_bushLarge.glb-034f46e010fc48537badbba708af03b9.scn"
[deps]
source_file="res://Entities/Tree/assets/temp/plant_bushLarge.glb"
dest_files=["res://.godot/imported/plant_bushLarge.glb-444f7e2fefa42d9967360c2460aba973.scn"]
source_file="res://Entities/Bush/assets/plant_bushLarge.glb"
dest_files=["res://.godot/imported/plant_bushLarge.glb-034f46e010fc48537badbba708af03b9.scn"]
[params]

View file

@ -4,12 +4,12 @@ importer="scene"
importer_version=1
type="PackedScene"
uid="uid://xj45kysvl047"
path="res://.godot/imported/plant_bushLargeTriangle.glb-9e831461ae4497bcbdb95549623cbaf6.scn"
path="res://.godot/imported/plant_bushLargeTriangle.glb-4c2a6e5fedd13bffdd9cfb0a212c89c8.scn"
[deps]
source_file="res://Entities/Tree/assets/temp/plant_bushLargeTriangle.glb"
dest_files=["res://.godot/imported/plant_bushLargeTriangle.glb-9e831461ae4497bcbdb95549623cbaf6.scn"]
source_file="res://Entities/Bush/assets/plant_bushLargeTriangle.glb"
dest_files=["res://.godot/imported/plant_bushLargeTriangle.glb-4c2a6e5fedd13bffdd9cfb0a212c89c8.scn"]
[params]

View file

@ -4,12 +4,12 @@ importer="scene"
importer_version=1
type="PackedScene"
uid="uid://cjoyr61xdo3hx"
path="res://.godot/imported/plant_bushSmall.glb-636059e56114bd2f2c7902ac6a574e3a.scn"
path="res://.godot/imported/plant_bushSmall.glb-2218874f9d8b685dac1fd79232f82be8.scn"
[deps]
source_file="res://Entities/Tree/assets/temp/plant_bushSmall.glb"
dest_files=["res://.godot/imported/plant_bushSmall.glb-636059e56114bd2f2c7902ac6a574e3a.scn"]
source_file="res://Entities/Bush/assets/plant_bushSmall.glb"
dest_files=["res://.godot/imported/plant_bushSmall.glb-2218874f9d8b685dac1fd79232f82be8.scn"]
[params]
@ -32,6 +32,17 @@ animation/trimming=false
animation/remove_immutable_tracks=true
animation/import_rest_as_RESET=false
import_script/path=""
_subresources={}
_subresources={
"meshes": {
"plant_bushSmall_Mesh plant_bushSmall": {
"generate/lightmap_uv": 0,
"generate/lods": 0,
"generate/shadow_meshes": 0,
"lods/normal_merge_angle": 60.0,
"save_to_file/enabled": true,
"save_to_file/path": "res://Entities/Bush/assets/bush_small.res"
}
}
}
gltf/naming_version=1
gltf/embedded_image_handling=1

View file

@ -4,12 +4,12 @@ importer="scene"
importer_version=1
type="PackedScene"
uid="uid://dsch27pqxgud5"
path="res://.godot/imported/plant_bushTriangle.glb-68d6459f20861ebce284d042b5ea0419.scn"
path="res://.godot/imported/plant_bushTriangle.glb-518273ba96c29be659e875adddc31676.scn"
[deps]
source_file="res://Entities/Tree/assets/temp/plant_bushTriangle.glb"
dest_files=["res://.godot/imported/plant_bushTriangle.glb-68d6459f20861ebce284d042b5ea0419.scn"]
source_file="res://Entities/Bush/assets/plant_bushTriangle.glb"
dest_files=["res://.godot/imported/plant_bushTriangle.glb-518273ba96c29be659e875adddc31676.scn"]
[params]

View file

@ -1,11 +1,15 @@
[gd_scene load_steps=13 format=3 uid="uid://bwcevwwphdvq"]
[gd_scene load_steps=17 format=3 uid="uid://bwcevwwphdvq"]
[ext_resource type="Script" uid="uid://bq7hia2dit80y" path="res://Entities/GroundTile/ground_tile.gd" id="1_uwxqs"]
[ext_resource type="Script" uid="uid://bq7hia2dit80y" path="res://Entities/GroundTile/scripts/ground_tile.gd" id="1_uwxqs"]
[ext_resource type="ArrayMesh" uid="uid://duj6747nq4qsk" path="res://Stages/Test3D/assets/stylizedGrassMeshes/grass.res" id="3_8mhad"]
[ext_resource type="Script" uid="uid://cacp8ncwuofuj" path="res://Entities/GroundTile/scripts/grass.gd" id="3_224hx"]
[ext_resource type="Material" uid="uid://b1miqvl8lus75" path="res://Stages/Test3D/GrassMaterialOverride.tres" id="3_f37ob"]
[ext_resource type="Script" uid="uid://btju6b83mvgvk" path="res://Entities/GroundTile/scripts/grass_multimesh.gd" id="4_3wpcb"]
[ext_resource type="Script" uid="uid://bt67yhdkwtqy5" path="res://Entities/GroundTile/scripts/bushes.gd" id="6_224hx"]
[ext_resource type="Script" uid="uid://cqko4m7cbxsfb" path="res://Entities/GroundTile/scripts/trees.gd" id="7_7lc7k"]
[ext_resource type="Script" uid="uid://dri5tubavplji" path="res://Entities/GroundTile/scripts/bush_multimesh.gd" id="7_jysav"]
[ext_resource type="Script" uid="uid://d3s0u7rm1y7i6" path="res://Entities/GroundTile/scripts/flower_multimesh.gd" id="8_jysav"]
[ext_resource type="Script" uid="uid://18vxtm3ua4x0" path="res://Entities/GroundTile/scripts/flowers.gd" id="8_q0r4p"]
[sub_resource type="ViewportTexture" id="ViewportTexture_h4g11"]
viewport_path = NodePath("DebugText/DebugTextViewport")
@ -67,5 +71,33 @@ script = ExtResource("4_3wpcb")
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0.0111763, 0)
mesh = SubResource("PlaneMesh_f37ob")
[node name="Bushes" type="Node3D" parent="."]
script = ExtResource("6_224hx")
[node name="BushMultimesh" type="MultiMeshInstance3D" parent="Bushes"]
cast_shadow = 0
multimesh = SubResource("MultiMesh_3wpcb")
script = ExtResource("7_jysav")
[node name="BushTarget" type="MeshInstance3D" parent="Bushes"]
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0.0111763, 0)
mesh = SubResource("PlaneMesh_f37ob")
[node name="Flowers" type="Node3D" parent="."]
script = ExtResource("8_q0r4p")
[node name="FlowerMultimesh" type="MultiMeshInstance3D" parent="Flowers"]
cast_shadow = 0
multimesh = SubResource("MultiMesh_3wpcb")
script = ExtResource("8_jysav")
[node name="FloewerTarget" type="MeshInstance3D" parent="Flowers"]
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0.0111763, 0)
mesh = SubResource("PlaneMesh_f37ob")
[node name="Trees" type="Node3D" parent="."]
script = ExtResource("7_7lc7k")
[node name="Special" type="Node3D" parent="."]
[node name="Rocks" type="Node3D" parent="Special"]

View file

@ -0,0 +1,56 @@
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

View file

@ -0,0 +1 @@
uid://dri5tubavplji

View file

@ -0,0 +1,36 @@
# GrassController.gd
extends Node3D
class_name BushController
@onready var bush_multimesh: MultiMeshInstance3D = $BushMultimesh
var parent_node: GroundTile = null
var bush_density: float = 0.5
var bush_instance_range: int = 10
func _ready() -> void:
parent_node = get_parent() as GroundTile
func spawn_bushes_for_cell(value):
if value == null:
return
bush_density = value.vegetation_density
update_bush_density()
if bush_multimesh and bush_multimesh.has_method("setup_multimesh"):
bush_multimesh.setup_multimesh()
func update_bush_density() -> void:
if parent_node == null:
return
var rng = parent_node.get_rng()
if bush_density > 0.8:
bush_instance_range = rng.randi_range(5, 10)
elif bush_density > 0.6:
bush_instance_range = rng.randi_range(2, 7)
elif bush_density > 0.3:
bush_instance_range = rng.randi_range(0, 1)
else:
bush_instance_range = rng.randi_range(0, 0)

View file

@ -0,0 +1 @@
uid://bt67yhdkwtqy5

View file

@ -0,0 +1,59 @@
extends MultiMeshInstance3D
var mm: MultiMesh
var parent_node: FlowerController
static var flower_mesh: Mesh = null
var material: StandardMaterial3D
func _ready() -> void:
parent_node = get_parent() as FlowerController
if parent_node == null:
Log.pr("Error: Parent node is not a FlowerController!")
# Load mesh once and reuse
if flower_mesh == null:
flower_mesh = load("res://Entities/Plant/assets/flower_tall.res")
func setup_multimesh() -> void:
if parent_node == null:
return
if flower_mesh == null:
Log.pr("Error: Could not load flower 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 = flower_mesh
var flower_material = ColorData.flower_materials[Season.current]["flower"]
var stem_material = ColorData.flower_materials[Season.current]["stem"]
mm.mesh.surface_set_material(0, stem_material)
mm.mesh.surface_set_material(1, flower_material)
# Configure instance count
mm.instance_count = parent_node.flower_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.2, 0.4)
basis = basis.scaled(Vector3(random_scale, random_scale, random_scale))
var tx = Transform3D(basis, random_pos)
mm.set_instance_transform(i, tx)
multimesh = mm

View file

@ -0,0 +1 @@
uid://d3s0u7rm1y7i6

View file

@ -0,0 +1,36 @@
# GrassController.gd
extends Node3D
class_name FlowerController
@onready var flower_multimesh: MultiMeshInstance3D = $FlowerMultimesh
var parent_node: GroundTile = null
var flower_density: float = 0.5
var flower_instance_range: int = 10
func _ready() -> void:
parent_node = get_parent() as GroundTile
func spawn_flowers_for_cell(value):
if value == null:
return
flower_density = value.vegetation_density
update_flower_density()
if flower_multimesh and flower_multimesh.has_method("setup_multimesh"):
flower_multimesh.setup_multimesh()
func update_flower_density() -> void:
if parent_node == null:
return
var rng = parent_node.get_rng()
if flower_density > 0.8:
flower_instance_range = rng.randi_range(1, 3)
elif flower_density > 0.6:
flower_instance_range = rng.randi_range(3, 4)
elif flower_density > 0.3:
flower_instance_range = rng.randi_range(4, 7)
else:
flower_instance_range = rng.randi_range(5, 10)

View file

@ -0,0 +1 @@
uid://18vxtm3ua4x0

View file

@ -25,6 +25,8 @@ func setup_multimesh() -> void:
mm = MultiMesh.new()
mm.transform_format = MultiMesh.TRANSFORM_3D
mm.mesh = grass_mesh
update_shader_parameter('top_color', ColorData.grass_materials[Season.current]['top'])
update_shader_parameter('bottom_color', ColorData.grass_materials[Season.current]['base'])
# Configure instance count
mm.instance_count = parent_node.grass_instance_range
@ -51,3 +53,18 @@ func setup_multimesh() -> void:
multimesh = mm
cast_shadow = GeometryInstance3D.SHADOW_CASTING_SETTING_OFF
func update_shader_parameter(param_name: String, value) -> void:
if material_override == null:
Log.pr("Error: No material override found")
return
# Check if it's a ShaderMaterial
if material_override is ShaderMaterial:
var shader_material = material_override as ShaderMaterial
shader_material.set_shader_parameter(param_name, value)
else:
Log.pr("Error: Material override is not a ShaderMaterial")
return

View file

@ -3,6 +3,9 @@ extends Node3D
@onready var debug_text: Label = $DebugText/DebugTextViewport/DebugTextLabel
@onready var tree_spawner = $Trees
@onready var grass_spawner = $Grass
@onready var bush_spawner = $Bushes
@onready var flower_spawner = $Flowers
@onready var ground = $Ground
var grid_x: int
var grid_z: int
var cell_info: CellDataResource = null
@ -16,8 +19,10 @@ func _ready() -> void:
spawners_ready = true
if cell_info != null:
spawn_content()
update_text_label()
spawn_content()
update_text_label()
ground.material_override.albedo_color = ColorData.grass_materials[Season.current]['base']
func get_rng() -> RandomClass:
if cached_rng == null and cell_info:
@ -44,6 +49,11 @@ func spawn_content():
tree_spawner.spawn_trees_for_cell(cell_info)
if grass_spawner:
grass_spawner.spawn_grass_for_cell(cell_info)
if bush_spawner:
bush_spawner.spawn_bushes_for_cell(cell_info)
if flower_spawner:
flower_spawner.spawn_flowers_for_cell(cell_info)
func update_text_label() -> void:
debug_text.text = str(cell_info.vegetation_density)

Binary file not shown.

View file

@ -4,12 +4,12 @@ importer="scene"
importer_version=1
type="PackedScene"
uid="uid://c8kl0a1nstbuc"
path="res://.godot/imported/flower_purpleB.glb-69a5b0606a51601fdd91762f296ce88b.scn"
path="res://.godot/imported/flower_tall.glb-105aa1c03f7501793bbf8ad0e150a0ed.scn"
[deps]
source_file="res://Entities/Tree/assets/temp/flower_purpleB.glb"
dest_files=["res://.godot/imported/flower_purpleB.glb-69a5b0606a51601fdd91762f296ce88b.scn"]
source_file="res://Entities/Plant/assets/flower_tall.glb"
dest_files=["res://.godot/imported/flower_tall.glb-105aa1c03f7501793bbf8ad0e150a0ed.scn"]
[params]
@ -32,6 +32,17 @@ animation/trimming=false
animation/remove_immutable_tracks=true
animation/import_rest_as_RESET=false
import_script/path=""
_subresources={}
_subresources={
"meshes": {
"flower_tall_Mesh flower_purpleB": {
"generate/lightmap_uv": 0,
"generate/lods": 0,
"generate/shadow_meshes": 0,
"lods/normal_merge_angle": 60.0,
"save_to_file/enabled": true,
"save_to_file/path": "res://Entities/Plant/assets/flower.res"
}
}
}
gltf/naming_version=1
gltf/embedded_image_handling=1

Binary file not shown.

View file

@ -104,5 +104,5 @@ func update_material_in_mesh_instance(mesh_instance: MeshInstance3D, material_na
func apply_material_with_queue(mesh_instance: MeshInstance3D, index: int, material: StandardMaterial3D):
ColorData.queue_material_application(mesh_instance, index, material)
MaterialManager.queue_material_application(mesh_instance, index, material)
return

View file

@ -33,7 +33,7 @@ func spawn_model():
# Instantiate the model from the TreeDataResource
model_instance = tree_data.model.instantiate()
tree_data.apply_seasonal_colors(model_instance, "spring")
tree_data.apply_seasonal_colors(model_instance, Season.current)
add_child(model_instance)