Improved tree placement

This commit is contained in:
Eduard Urbach 2024-02-17 23:17:35 +01:00
parent 2bc4200f5b
commit cf876a0ab8
Signed by: akyoto
GPG Key ID: C874F672B1AF20C0
9 changed files with 120 additions and 31 deletions

View File

@ -1,4 +1,4 @@
[gd_scene load_steps=30 format=3 uid="uid://b40y7iuskv1ar"]
[gd_scene load_steps=35 format=3 uid="uid://b40y7iuskv1ar"]
[ext_resource type="Script" path="res://Main.gd" id="1_cw3ws"]
[ext_resource type="Script" path="res://network/Client.gd" id="2_8hxcx"]
@ -15,15 +15,20 @@
[ext_resource type="Script" path="res://network/PlayerUseSkill.gd" id="11_gyiep"]
[ext_resource type="PackedScene" uid="uid://cpdoq0oh84mfw" path="res://camera/Camera.tscn" id="12_aljdh"]
[ext_resource type="Environment" uid="uid://dixa0yso2s1u3" path="res://world/Environment.tres" id="14_kuej8"]
[ext_resource type="Script" path="res://world/Generate.gd" id="15_25nmg"]
[ext_resource type="Script" path="res://world/generator/Generator.gd" id="15_25nmg"]
[ext_resource type="PackedScene" uid="uid://cb2t7bvvf3gwh" path="res://enemy/slime/Slime.tscn" id="15_hgl78"]
[ext_resource type="Script" path="res://world/PlayerManager.gd" id="16_dp6bj"]
[ext_resource type="PackedScene" uid="uid://dagn5bf7ou3sd" path="res://ui/UI.tscn" id="17_43qhq"]
[ext_resource type="PackedScene" uid="uid://hnn0n1xc2qt7" path="res://assets/tree/Tree.blend" id="18_6ab5t"]
[ext_resource type="Material" uid="uid://bdsblfaxbipaa" path="res://world/grass/GrassMaterial.tres" id="18_tja64"]
[ext_resource type="MultiMesh" uid="uid://dog5aq5n2q025" path="res://assets/grass/grass.multimesh" id="19_ae26a"]
[ext_resource type="FastNoiseLite" uid="uid://d3f4lk8q04haa" path="res://world/trees/TreeNoise.tres" id="19_ctwmw"]
[ext_resource type="Script" path="res://world/generator/TreeGenerator.gd" id="19_kcwnm"]
[ext_resource type="PackedScene" uid="uid://tr0tn0pkr1ea" path="res://world/trees/Callistemon.tscn" id="19_x2ulu"]
[ext_resource type="Material" uid="uid://bt6an0ynrpkpt" path="res://world/trees/TreeMaterial.tres" id="20_seiv4"]
[ext_resource type="PackedScene" uid="uid://djilgnhedvmtm" path="res://world/house/House.tscn" id="20_xjraj"]
[ext_resource type="Material" uid="uid://dw754wh1xcwgr" path="res://world/trees/LeafMaterial1.tres" id="21_v66do"]
[ext_resource type="Material" uid="uid://cxlib6jo32hxg" path="res://world/trees/LeafMaterial2.tres" id="22_k67l0"]
[ext_resource type="Material" uid="uid://c8o7cihkhrqf6" path="res://world/trees/LeafMaterial3.tres" id="23_lxn2x"]
[sub_resource type="StandardMaterial3D" id="StandardMaterial3D_he5c5"]
diffuse_mode = 3
@ -139,16 +144,21 @@ size_x = 100.0
size_z = 100.0
step = 10.0
density = 0.3
position_randomness = 0.0
scale_max = 2.0
[node name="Trees" type="Node3D" parent="World"]
script = ExtResource("15_25nmg")
scene = ExtResource("18_6ab5t")
script = ExtResource("19_kcwnm")
tree_materials = Array[Material]([ExtResource("20_seiv4")])
leaf_materials = Array[Material]([ExtResource("21_v66do"), ExtResource("22_k67l0"), ExtResource("23_lxn2x")])
scene = ExtResource("19_x2ulu")
noise = ExtResource("19_ctwmw")
size_x = 100.0
size_z = 100.0
step = 5.0
density = 0.4
scale_min = 0.6
scale_max = 1.1
tilt = 0.15
[node name="Enemies" type="Node3D" parent="World"]
script = ExtResource("15_25nmg")
@ -157,7 +167,8 @@ noise = SubResource("FastNoiseLite_yp2fx")
size_x = 100.0
size_z = 100.0
step = 2.0
density = 0.25
density = 0.3
tilt = 0.15
[node name="Follow" parent="World" instance=ExtResource("12_aljdh")]

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)