129 lines
3.4 KiB
GDScript
129 lines
3.4 KiB
GDScript
extends Node3D
|
|
|
|
enum CanopyVariant {
|
|
GREEN,
|
|
DARK_GREEN,
|
|
YELLOW_GREEN,
|
|
}
|
|
|
|
const COLORS := {
|
|
CanopyVariant.GREEN: Color(0.18, 0.39, 0.16),
|
|
CanopyVariant.DARK_GREEN: Color(0.10, 0.28, 0.12),
|
|
CanopyVariant.YELLOW_GREEN: Color(0.34, 0.47, 0.16),
|
|
}
|
|
|
|
const WIND_SHADER := preload("res://world/jajce/materials/wind.gdshader")
|
|
const WIND_STRENGTH := 0.07
|
|
const WIND_SPEED := 0.55
|
|
|
|
|
|
func _ready() -> void:
|
|
for child in get_children():
|
|
child.queue_free()
|
|
|
|
var hash_seed := hash(global_position)
|
|
var rng := RandomNumberGenerator.new()
|
|
rng.seed = hash_seed
|
|
var canopy_variant := rng.randi() % COLORS.size() as CanopyVariant
|
|
var canopy_radius := 1.0 + rng.randf() * 0.55
|
|
var wind_phase := rng.randf_range(0.0, TAU)
|
|
|
|
var trunk_mesh := CylinderMesh.new()
|
|
trunk_mesh.top_radius = 0.22
|
|
trunk_mesh.bottom_radius = 0.32
|
|
trunk_mesh.height = 3.4
|
|
trunk_mesh.radial_segments = 9
|
|
|
|
var trunk_mat := StandardMaterial3D.new()
|
|
trunk_mat.albedo_color = Color(0.27, 0.16, 0.09)
|
|
trunk_mat.roughness = 0.95
|
|
trunk_mesh.material = trunk_mat
|
|
|
|
var trunk := MeshInstance3D.new()
|
|
trunk.name = "Trunk"
|
|
trunk.mesh = trunk_mesh
|
|
trunk.position = Vector3(0, 1.7, 0)
|
|
add_child(trunk)
|
|
|
|
_add_branch(Vector3(-0.18, 2.45, 0.0), Vector3(0.0, 0.0, 42.0), 1.45, trunk_mat)
|
|
_add_branch(Vector3(0.2, 2.6, 0.08), Vector3(24.0, 0.0, -38.0), 1.25, trunk_mat)
|
|
|
|
var canopy_color: Color = COLORS[canopy_variant]
|
|
var crown_height := 3.7 + (canopy_radius - 1.2) * 0.45
|
|
_add_canopy(
|
|
"Canopy",
|
|
Vector3(0.0, crown_height, 0.0),
|
|
Vector3(canopy_radius * 1.12, canopy_radius, canopy_radius),
|
|
canopy_color,
|
|
wind_phase
|
|
)
|
|
_add_canopy(
|
|
"CanopyLeft",
|
|
Vector3(-0.82, crown_height - 0.15, 0.1),
|
|
Vector3(canopy_radius * 0.78, canopy_radius * 0.72, canopy_radius * 0.76),
|
|
canopy_color.lightened(0.05),
|
|
wind_phase + 0.12
|
|
)
|
|
_add_canopy(
|
|
"CanopyRight",
|
|
Vector3(0.78, crown_height - 0.05, -0.12),
|
|
Vector3(canopy_radius * 0.74, canopy_radius * 0.68, canopy_radius * 0.72),
|
|
canopy_color.darkened(0.04),
|
|
wind_phase - 0.1
|
|
)
|
|
_add_canopy(
|
|
"CanopyTop",
|
|
Vector3(0.08, crown_height + 0.72, 0.04),
|
|
Vector3(canopy_radius * 0.72, canopy_radius * 0.66, canopy_radius * 0.7),
|
|
canopy_color.lightened(0.08),
|
|
wind_phase + 0.05
|
|
)
|
|
|
|
|
|
func _add_branch(
|
|
branch_position: Vector3,
|
|
branch_rotation_degrees: Vector3,
|
|
branch_height: float,
|
|
material: StandardMaterial3D
|
|
) -> void:
|
|
var mesh := CylinderMesh.new()
|
|
mesh.top_radius = 0.08
|
|
mesh.bottom_radius = 0.14
|
|
mesh.height = branch_height
|
|
mesh.radial_segments = 7
|
|
mesh.material = material
|
|
var branch := MeshInstance3D.new()
|
|
branch.name = "Branch"
|
|
branch.mesh = mesh
|
|
branch.position = branch_position
|
|
branch.rotation_degrees = branch_rotation_degrees
|
|
add_child(branch)
|
|
|
|
|
|
func _add_canopy(
|
|
canopy_name: String,
|
|
canopy_position: Vector3,
|
|
canopy_scale: Vector3,
|
|
canopy_color: Color,
|
|
wind_phase: float
|
|
) -> void:
|
|
var mesh := SphereMesh.new()
|
|
mesh.radius = 1.0
|
|
mesh.height = 2.0
|
|
mesh.radial_segments = 12
|
|
mesh.rings = 6
|
|
var material := ShaderMaterial.new()
|
|
material.shader = WIND_SHADER
|
|
material.set_shader_parameter("albedo", canopy_color)
|
|
material.set_shader_parameter("roughness", 0.9)
|
|
material.set_shader_parameter("wind_strength", WIND_STRENGTH)
|
|
material.set_shader_parameter("wind_speed", WIND_SPEED)
|
|
material.set_shader_parameter("wind_phase", wind_phase)
|
|
mesh.material = material
|
|
var canopy := MeshInstance3D.new()
|
|
canopy.name = canopy_name
|
|
canopy.mesh = mesh
|
|
canopy.position = canopy_position
|
|
canopy.scale = canopy_scale
|
|
add_child(canopy)
|