Skip to content

Commit 5906537

Browse files
committed
Update Godot version to 3.5.beta
* Change the defaults of some shader params in attenuated circle shader. * Change the defaults of some variables exported. * Move the changing of radius and strength from TerrainTool to TerrainToolUI. * Make chunk size snap to powers of 2 * Update the radius and strength changes on shader. * Improve transforming of vertices for better placement in the terrain chunk mesh.
1 parent 9eb22f9 commit 5906537

File tree

12 files changed

+99
-106
lines changed

12 files changed

+99
-106
lines changed

AttenuatedCircle.shader

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ shader_type spatial;
22
// by Sairam
33
uniform float radius = 3.0;
44
uniform float attenuated_radius = 0.01;
5-
uniform float attenuation_strength = 1.0;
6-
uniform float attenuated_size_multiplier = 1.0;
5+
uniform float attenuation_strength : hint_range(0.0, 1.0) = 1.0;
6+
uniform float attenuated_size_multiplier : hint_range(0.0, 5.0) = 1.0;
77
uniform float max_power = 1;
88
uniform float min_power = 0;
99

AttenuatedCircle.tres

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55
[resource]
66
shader = ExtResource( 1 )
77
shader_param/radius = 3.0
8-
shader_param/attenuated_radius = 0.01
9-
shader_param/attenuation_strength = 1.164
10-
shader_param/attenuated_size_multiplier = 1.778
8+
shader_param/attenuated_radius = 0.0
9+
shader_param/attenuation_strength = 1.0
10+
shader_param/attenuated_size_multiplier = 1.0
1111
shader_param/max_power = 1.0
1212
shader_param/min_power = 0.0
1313
shader_param/color = Color( 0, 1, 1, 0.498039 )

CircleAttenuation.gd

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,13 @@ func _update_attentuation_size():
3939

4040
func get_power_at_position(position: Vector3, sharp = false) -> float:
4141
var _translation = global_transform.origin
42+
# prints(position, translation)
4243
var _distance_squared = _translation.distance_squared_to(position)
4344
if _distance_squared >= radius_squared:
45+
# prints(position, _translation, "_distance_squared >= radius_squared")
4446
return min_power
4547
if _distance_squared <= attentuation_radius_squared:
48+
# prints(position, "_distance_squared <= attentuation_radius_squared")
4649
return max_power
4750

4851
# thanks to JestemStefan and QbieShay

FPSDebug.gd

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ extends Control
44
func _process(_delta):
55
$Label.text = "FPS: %d\n" % Engine.get_frames_per_second()
66
$Label.text += "Memory: %d\n" % OS.get_static_memory_usage()
7-
# $Label.text += "Position: %s\n" % $"../Tools/TerrainTool".translation
7+
$Label.text += "Position: %s\n" % $"../Tools/TerrainTool".translation
88
# var aabb = $"../Tools/TerrainTool".get_transformed_aabb()
99
# aabb.position.y -= 50
1010
# aabb.size.y += 100

GUI.tscn

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,13 +74,14 @@ margin_bottom = 16.0
7474
size_flags_horizontal = 3
7575
max_value = 1.0
7676
step = 0.01
77+
value = 1.0
7778

7879
[node name="Label2" type="Label" parent="PanelContainer/VBoxContainer/Strength"]
7980
margin_left = 609.0
8081
margin_top = 1.0
8182
margin_right = 633.0
8283
margin_bottom = 15.0
83-
text = "000"
84+
text = "001"
8485

8586
[node name="RotationDegrees" type="HBoxContainer" parent="PanelContainer/VBoxContainer"]
8687
margin_top = 52.0

Game.tscn

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@
1313

1414
[sub_resource type="PlaneMesh" id=1]
1515
material = ExtResource( 5 )
16-
size = Vector2( 16, 16 )
17-
subdivide_width = 32
18-
subdivide_depth = 32
16+
size = Vector2( 8, 8 )
17+
subdivide_width = 16
18+
subdivide_depth = 16
1919

2020
[sub_resource type="BoxShape" id=2]
2121
extents = Vector3( 8, 0.25, 8 )

PanelContainer.gd

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ signal radius_changed(radius)
55
signal strength_changed(strength)
66
signal rotation_changed(rotation)
77

8+
var strength = 1
9+
var radius = 3
10+
811
func _ready():
912
$VBoxContainer/Radius/Label2.text = "%04d" % $VBoxContainer/Radius/Radius.value
1013
$VBoxContainer/Strength/Label2.text = "%.2f" % $VBoxContainer/Strength/Strength.value
@@ -16,13 +19,24 @@ func _on_Button_pressed(state):
1619

1720
func _on_HSlider_value_changed(value):
1821
emit_signal("radius_changed", value)
22+
radius = value
1923
$VBoxContainer/Radius/Label2.text = "%03d" % value
2024

2125

2226
func _on_Strength_value_changed(value):
2327
emit_signal("strength_changed", value)
28+
strength = value
2429
$VBoxContainer/Strength/Label2.text = "%.2f" % value
2530

2631
func _on_RotationDegrees_value_changed(value):
2732
emit_signal("rotation_changed", value)
2833
$VBoxContainer/RotationDegrees/Label2.text = "%03d" % value
34+
35+
func _unhandled_input(event):
36+
if event is InputEventMouseMotion:
37+
if event.shift and Input.is_action_pressed("left_button"):
38+
$VBoxContainer/Radius/Radius.value = radius+event.relative.x*0.5
39+
get_tree().set_input_as_handled()
40+
elif event.control and Input.is_action_pressed("left_button"):
41+
$VBoxContainer/Strength/Strength.value = strength+event.relative.x*0.01
42+
get_tree().set_input_as_handled()

TerrainChunked.gd

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ static func previous_power_of_two(x: int) -> int:
3131

3232

3333
var _size: int = 64
34-
export var chunk_size = 16
34+
var _chunk_size: int = 16
3535
var chunks = {}
3636
var height_map: Image = Image.new()
3737
var quad_tree = QuadTreeNode.new()
@@ -45,6 +45,7 @@ func _ready():
4545
if Engine.editor_hint:
4646
return
4747
quad_tree.extents = Vector3(_size/2, 2, _size/2)
48+
$QuadTreeNode.extents = Vector3(_size/2, 2, _size/2)
4849
quad_tree.capacity = 4
4950
quad_tree.max_levels = 6
5051
height_map.create(_size, _size, false, Image.FORMAT_RGBA8)
@@ -54,12 +55,12 @@ func _ready():
5455
if get_node_or_null(terrain_tool_node):
5556
terrain_tool = get_node(terrain_tool_node)
5657
# print(range(-_size/2-8, 64-/2+8, 16))
57-
var r = range((-_size+chunk_size)/2, (_size+chunk_size)/2, chunk_size)
58-
# var r = range((-2*chunk_size+chunk_size)/2, (2*chunk_size+chunk_size)/2, chunk_size)
58+
var r = range((-_size+_chunk_size)/2, (_size+_chunk_size)/2, _chunk_size)
59+
# var r = range((-2*_chunk_size+_chunk_size)/2, (2*_chunk_size+_chunk_size)/2, _chunk_size)
5960
for i in r:
6061
for j in r:
6162
var image_offset: Vector2 = Vector2(range_lerp(i, -_size/2, _size/2, 0, _size), range_lerp(j, -_size/2, _size/2, 0, _size))
62-
var chunk = Chunk.new(Vector3(i, 0, j), chunk_size, 0, height_map, image_offset)
63+
var chunk = Chunk.new(Vector3(i, 0, j), _chunk_size, 1, height_map, image_offset)
6364
add_child(chunk)
6465
chunks[Vector3(i, 0, j)] = chunk
6566
chunk.translation = Vector3(i, 0, j)
@@ -108,8 +109,9 @@ func _process(delta):
108109
var new_aabb = aabb
109110
var old_aabb = (chunk.global_transform as Transform).xform_inv(new_aabb)
110111
# print("chunk: ", chunk, " (", chunk.get_transformed_aabb(), "), aabb: ", aabb, ", new: ", new_aabb, ", old: ", old_aabb)
111-
old_aabb.position.y -= 50
112-
old_aabb.size.y += 100
112+
# old_aabb.position.y -= 50
113+
# old_aabb.size.y += 100
114+
# print(old_aabb)
113115
(chunk as Chunk)._process_chunk(delta, terrain_tool, old_aabb)
114116

115117
terrain_tool.reset_values()
@@ -130,17 +132,27 @@ func _get_property_list():
130132
"hint": PROPERTY_HINT_RANGE,
131133
"hint_string": "16,8192"
132134
})
135+
properties.append({
136+
"name": "chunk_size",
137+
"type": TYPE_INT,
138+
"hint": PROPERTY_HINT_RANGE,
139+
"hint_string": "8,64"
140+
})
133141
return properties
134142

135143
func _get(property):
136144
match property:
137145
"size":
138146
return _size
147+
"chunk_size":
148+
return _chunk_size
139149

140150
func _set(property, value):
141151
match property:
142152
"size":
143153
_size = snap_to_nearest_power_of_two(_size, value)
144-
154+
return true
155+
"chunk_size":
156+
_chunk_size = snap_to_nearest_power_of_two(_chunk_size, value)
145157
return true
146158

TerrainTool.gd

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,27 +25,28 @@ func _unhandled_input(event):
2525
if event is InputEventMouseMotion:
2626
mouse_pointer = event.position
2727
if Input.is_action_pressed("left_button") and (!event.control and !event.shift):
28+
# print("is being clicked?")
2829
clicked = true
2930
strength = Input.get_action_strength("left_button") * strength_factor * 2
3031
attenuated_circle.attentuation_strength = strength
3132
position = translation
3233
if mesh is CylinderMesh:
3334
radius = mesh.bottom_radius
3435
attenuated_circle.radius = radius
35-
elif event.control and Input.is_action_pressed("left_button"):
36-
set_strength_value(strength_factor+event.relative.x*0.02)
37-
elif event.shift and Input.is_action_pressed("left_button"):
38-
print("shift")
39-
set_radius_value(radius+event.relative.x*0.02)
36+
# elif event.control and Input.is_action_pressed("left_button"):
37+
# set_strength_value(strength_factor+event.relative.x*0.01)
38+
# elif event.shift and Input.is_action_pressed("left_button"):
39+
## print("shift")
40+
# set_radius_value(radius+event.relative.x*0.02)
4041

41-
42-
if Input.is_action_just_pressed("left_button"):
42+
43+
if Input.is_action_just_pressed("left_button") and !Input.is_key_pressed(KEY_SHIFT) and !Input.is_key_pressed(KEY_CONTROL):
4344
position = translation
4445
for audio in sound_dict.values():
4546
audio.stop()
4647
sound_dict[current_state].play()
4748

48-
if Input.is_action_just_released("left_button"):
49+
if Input.is_action_just_released("left_button") and !Input.is_key_pressed(KEY_SHIFT) and !Input.is_key_pressed(KEY_CONTROL):
4950
for audio in sound_dict.values():
5051
audio.stop()
5152
clicked = false
@@ -59,16 +60,20 @@ func _physics_process(_delta):
5960
if not ray.empty():
6061
show()
6162
translation = ray.get("position", translation)
63+
# print(translation)
6264
else:
6365
hide()
6466

65-
if clicked and Input.is_action_pressed("left_button"):
67+
if clicked and Input.is_action_pressed("left_button") and (!Input.is_key_pressed(KEY_CONTROL) and !Input.is_key_pressed(KEY_SHIFT)):
68+
# print("running?")
6669
strength = Input.get_action_strength("left_button") * strength_factor * 2
6770
attenuated_circle.attentuation_strength = strength
71+
mesh.surface_get_material(0).set_shader_param("attenuation_strength", strength_factor)
6872
position = translation
6973
if mesh is CylinderMesh:
7074
radius = mesh.bottom_radius
7175
attenuated_circle.radius = radius
76+
mesh.surface_get_material(0).set_shader_param("radius", float(radius))
7277

7378
func reset_values():
7479
position = null
@@ -77,14 +82,16 @@ func set_state(state):
7782
current_state = state
7883

7984
func set_strength_value(value):
80-
strength_factor = value
85+
strength_factor = clamp(value, 0, 1)
86+
mesh.surface_get_material(0).set_shader_param("attenuation_strength", strength_factor)
8187

8288
func set_radius_value(value):
8389
if mesh is CylinderMesh:
8490
mesh.bottom_radius = value
8591
mesh.top_radius = value
8692
radius = value
8793
attenuated_circle.radius = radius
94+
mesh.surface_get_material(0).set_shader_param("radius", float(radius))
8895

8996
func get_strength_at_position(_position, sharp_edges):
9097
return attenuated_circle.get_power_at_position(_position, sharp_edges)

World.tscn

Lines changed: 14 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -11,62 +11,16 @@
1111
[ext_resource path="res://Materials/Terrain.material" type="Material" id=9]
1212
[ext_resource path="res://GUI.tscn" type="PackedScene" id=10]
1313
[ext_resource path="res://Materials/Drawing.material" type="Material" id=11]
14+
[ext_resource path="res://AttenuatedCircle.tres" type="Material" id=12]
15+
[ext_resource path="res://addons/sairam.quadtree/QuadTreeNode.gd" type="Script" id=13]
1416

15-
[sub_resource type="Shader" id=4]
16-
code = "shader_type spatial;
17-
// by Sairam
18-
uniform float radius = 3.0;
19-
uniform float attenuated_radius = 0.01;
20-
uniform float attenuation_strength = 1.0;
21-
uniform float attenuated_size_multiplier = 1.0;
22-
uniform float max_power = 1;
23-
uniform float min_power = 0;
24-
25-
uniform vec4 color : hint_color = vec4(0.0, 1.0, 1.0, 0.5);
26-
27-
varying vec3 vertex_1;
28-
29-
float get_power_at_position(vec3 point) {
30-
vec3 origin = vec3(0.0, 0.0, 0.0);
31-
float dist = pow(distance(origin, point), 2.0);
32-
33-
if (dist >= radius*radius) {
34-
return min_power;
35-
}
36-
if (dist <= attenuated_radius*attenuated_radius) {
37-
return max_power;
38-
}
39-
40-
float ratio = 1.0 - ((dist - attenuated_radius*attenuated_radius) / (radius*radius - attenuated_radius*attenuated_radius));
41-
return mix(min_power, max_power, ratio) * attenuation_strength*attenuated_size_multiplier;
42-
}
43-
44-
void vertex() {
45-
vertex_1 = VERTEX;
46-
}
47-
48-
void fragment() {
49-
ALBEDO = color.xyz;
50-
ALPHA = get_power_at_position(vertex_1) * color.a;
51-
}"
52-
53-
[sub_resource type="ShaderMaterial" id=5]
54-
shader = SubResource( 4 )
55-
shader_param/radius = 3.0
56-
shader_param/attenuated_radius = 0.01
57-
shader_param/attenuation_strength = 1.164
58-
shader_param/attenuated_size_multiplier = 1.778
59-
shader_param/max_power = 1.0
60-
shader_param/min_power = 0.0
61-
shader_param/color = Color( 0, 1, 1, 0.498039 )
62-
63-
[sub_resource type="CylinderMesh" id=6]
64-
material = SubResource( 5 )
17+
[sub_resource type="CylinderMesh" id=1]
18+
material = ExtResource( 12 )
6519
top_radius = 4.0
6620
bottom_radius = 4.0
6721
height = 0.01
6822

69-
[sub_resource type="SpatialMaterial" id=7]
23+
[sub_resource type="SpatialMaterial" id=2]
7024
flags_unshaded = true
7125
params_line_width = 24.8
7226
albedo_color = Color( 0, 0, 0, 1 )
@@ -78,10 +32,13 @@ script = ExtResource( 8 )
7832

7933
[node name="Terrain" type="Spatial" parent="."]
8034
script = ExtResource( 1 )
81-
chunk_size = 8
8235
terrain_tool_node = NodePath("../Tools/TerrainTool")
8336
terrain_material = ExtResource( 9 )
84-
size = 128
37+
size = 64
38+
chunk_size = 8
39+
40+
[node name="QuadTreeNode" type="Spatial" parent="Terrain"]
41+
script = ExtResource( 13 )
8542

8643
[node name="Camera" parent="." instance=ExtResource( 2 )]
8744
transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 5, 0 )
@@ -92,16 +49,15 @@ zooming/max_distance = 300.0
9249
[node name="Tools" type="Spatial" parent="."]
9350

9451
[node name="TerrainTool" type="MeshInstance" parent="Tools"]
95-
mesh = SubResource( 6 )
52+
mesh = SubResource( 1 )
9653
material/0 = null
9754
script = ExtResource( 7 )
9855

9956
[node name="AttenuatedCircle" type="Spatial" parent="Tools/TerrainTool"]
10057
script = ExtResource( 6 )
10158
radius = 3.0
102-
attentuation_radius = 0.01
103-
attentuation_size_multiplier = 0.5
104-
min_power = 0.01
59+
attentuation_radius = 0.0
60+
attentuation_size_multiplier = 1.0
10561

10662
[node name="Raise" type="AudioStreamPlayer" parent="Tools/TerrainTool"]
10763
stream = ExtResource( 4 )
@@ -113,13 +69,11 @@ volume_db = -3.098
11369

11470
[node name="ImmediateGeometry" type="ImmediateGeometry" parent="Tools"]
11571
transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0.1, 0 )
116-
visible = false
11772
material_override = ExtResource( 11 )
11873

11974
[node name="ImmediateGeometry2" type="ImmediateGeometry" parent="Tools"]
12075
transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0.1, 0 )
121-
visible = false
122-
material_override = SubResource( 7 )
76+
material_override = SubResource( 2 )
12377

12478
[node name="Control2" parent="." instance=ExtResource( 10 )]
12579
mouse_filter = 2

0 commit comments

Comments
 (0)