Improved tree placement

This commit is contained in:
2024-02-17 23:17:35 +01:00
parent 2bc4200f5b
commit cf876a0ab8
9 changed files with 120 additions and 31 deletions

View File

@ -1,24 +0,0 @@
extends Node3D
@export var scene: PackedScene
@export var noise: Noise
@export var size_x: float
@export var size_z: float
@export var step: float = 1
@export var density: float
@export var position_randomness := 1.0
func _ready():
if !visible:
return
for x in range(-size_x, size_x, step):
for z in range(-size_z, size_z, step):
if noise.get_noise_2d(x, z) < 1 - density * 2:
continue
var t := scene.instantiate() as Node3D
t.rotation.y = randf() * TAU
t.position.x = x + (randf() - 0.5) * position_randomness
t.position.z = z + (randf() - 0.5) * position_randomness
add_child(t)

View File

@ -0,0 +1,40 @@
class_name Generator
extends Node3D
@export var scene: PackedScene
@export var noise: Noise
@export var size_x: float
@export var size_z: float
@export var step: float = 1
@export var density: float
@export var scale_min := 1.0
@export var scale_max := 1.0
@export var tilt := 0.0
func _ready():
if !visible:
return
for x in range(-size_x, size_x, step):
for z in range(-size_z, size_z, step):
if noise.get_noise_2d(x, z) < 1 - density * 2:
continue
var instance := generate(x, z)
add_child(instance)
func generate(x: int, z: int) -> Node3D:
var t := scene.instantiate() as Node3D
t.position.x = x
t.position.z = z
var angle_xz := randf() * TAU
t.rotation.x = cos(angle_xz) * randf() * tilt
t.rotation.y = randf() * TAU
t.rotation.z = sin(angle_xz) * randf() * tilt
var uniform_scale := scale_min + randf() * (scale_max - scale_min)
t.scale = Vector3(uniform_scale, uniform_scale, uniform_scale)
return t

View File

@ -0,0 +1,22 @@
class_name TreeGenerator
extends Generator
@export var tree_materials: Array[Material]
@export var leaf_materials: Array[Material]
func _ready():
super._ready()
func generate(x: int, z: int) -> Node3D:
var instance := super.generate(x, z)
randomize_materials(instance)
return instance
func randomize_materials(instance: Node):
var tree := instance.find_child("tree") as MeshInstance3D
var tree_material := tree_materials[randi_range(0, tree_materials.size()-1)]
tree.set_surface_override_material(0, tree_material)
var leaves := instance.find_child("leaves") as MeshInstance3D
var leaf_material := leaf_materials[randi_range(0, leaf_materials.size()-1)]
leaves.set_surface_override_material(0, leaf_material)

View File

@ -0,0 +1,15 @@
[gd_scene load_steps=3 format=3 uid="uid://tr0tn0pkr1ea"]
[ext_resource type="PackedScene" uid="uid://8rgrrx6wbdvf" path="res://assets/tree/Callistemon.blend" id="1_ngx2x"]
[sub_resource type="CylinderShape3D" id="CylinderShape3D_6f3jh"]
height = 1.5
radius = 0.25
[node name="Callistemon" instance=ExtResource("1_ngx2x")]
[node name="StaticBody3D" type="StaticBody3D" parent="." index="1"]
[node name="CollisionShape3D" type="CollisionShape3D" parent="StaticBody3D" index="0"]
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0.75, 0)
shape = SubResource("CylinderShape3D_6f3jh")

View File

@ -0,0 +1,7 @@
[gd_resource type="StandardMaterial3D" format=3 uid="uid://dw754wh1xcwgr"]
[resource]
resource_name = "Leaves"
cull_mode = 2
albedo_color = Color(0.0780164, 0.910454, 0, 1)
roughness = 0.5

View File

@ -0,0 +1,7 @@
[gd_resource type="StandardMaterial3D" format=3 uid="uid://cxlib6jo32hxg"]
[resource]
resource_name = "Leaves"
cull_mode = 2
albedo_color = Color(0.85098, 0.909804, 0, 1)
roughness = 0.5

View File

@ -0,0 +1,7 @@
[gd_resource type="StandardMaterial3D" format=3 uid="uid://c8o7cihkhrqf6"]
[resource]
resource_name = "Leaves"
cull_mode = 2
albedo_color = Color(0.909804, 0.286275, 0, 1)
roughness = 0.5

View File

@ -0,0 +1,4 @@
[gd_resource type="StandardMaterial3D" format=3 uid="uid://bt6an0ynrpkpt"]
[resource]
albedo_color = Color(0.239216, 0.172549, 0.0470588, 1)