Skip to content

Commit a88ca21

Browse files
authored
Add block placement preview to Voxel demo, improve debug display (#1190)
- Allow placing/breaking blocks when further away (up to 5 blocks instead of 4). - Add a background to the debug display to improve readability on bright backgrounds. - Simplify coordinate display using the `%v` placeholder available in 4.3 onwards.
1 parent c05d050 commit a88ca21

File tree

4 files changed

+44
-28
lines changed

4 files changed

+44
-28
lines changed

3d/voxel/menu/debug.gd

+5-25
Original file line numberDiff line numberDiff line change
@@ -8,31 +8,11 @@ func _process(_delta: float) -> void:
88
if Input.is_action_just_pressed(&"debug"):
99
visible = not visible
1010

11-
text = "Position: " + _vector_to_string_appropriate_digits(player.transform.origin)
12-
text += "\nEffective render distance: " + str(voxel_world.effective_render_distance)
13-
text += "\nLooking: " + _cardinal_string_from_radians(player.transform.basis.get_euler().y)
14-
text += "\nMemory: " + "%3.0f" % (OS.get_static_memory_usage() / 1048576.0) + " MiB"
15-
text += "\nFPS: " + String.num_uint64(Engine.get_frames_per_second())
16-
17-
18-
# Avoids the problem of showing more digits than needed or available.
19-
func _vector_to_string_appropriate_digits(vector: Vector3) -> String:
20-
var factors: Array[int] = [1000, 1000, 1000]
21-
for i in 3:
22-
if abs(vector[i]) > 4096:
23-
@warning_ignore("integer_division")
24-
factors[i] = factors[i] / 10
25-
if abs(vector[i]) > 65536:
26-
@warning_ignore("integer_division")
27-
factors[i] = factors[i] / 10
28-
if abs(vector[i]) > 524288:
29-
@warning_ignore("integer_division")
30-
factors[i] = factors[i] / 10
31-
32-
return "(" + \
33-
str(round(vector.x * factors[0]) / factors[0]) + ", " + \
34-
str(round(vector.y * factors[1]) / factors[1]) + ", " + \
35-
str(round(vector.z * factors[2]) / factors[2]) + ")"
11+
text = "Position: %.1v" % player.transform.origin \
12+
+ "\nEffective render distance: " + str(voxel_world.effective_render_distance) \
13+
+ "\nLooking: " + _cardinal_string_from_radians(player.transform.basis.get_euler().y) \
14+
+ "\nMemory: " + "%3.0f" % (OS.get_static_memory_usage() / 1048576.0) + " MiB" \
15+
+ "\nFPS: %d" % Engine.get_frames_per_second()
3616

3717

3818
# Expects a rotation where 0 is North, on the range -PI to PI.

3d/voxel/player/player.gd

+8-1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ var _selected_block := 6
2020
@onready var selected_block_texture: TextureRect = $SelectedBlock
2121
@onready var voxel_world: Node = $"../VoxelWorld"
2222
@onready var crosshair: CenterContainer = $"../PauseMenu/Crosshair"
23+
@onready var aim_preview: MeshInstance3D = $AimPreview
2324

2425

2526
func _ready() -> void:
@@ -54,18 +55,24 @@ func _process(_delta: float) -> void:
5455

5556
# Block breaking/placing.
5657
if crosshair.visible and raycast.is_colliding():
58+
aim_preview.visible = true
59+
var ray_current_block_position := Vector3i((ray_position - ray_normal / 2).floor())
60+
aim_preview.global_position = Vector3(ray_current_block_position) + Vector3(0.5, 0.5, 0.5)
5761
var breaking := Input.is_action_just_pressed(&"break")
5862
var placing := Input.is_action_just_pressed(&"place")
5963
# Either both buttons were pressed or neither are, so stop.
6064
if breaking == placing:
6165
return
6266

6367
if breaking:
64-
var block_global_position := Vector3i((ray_position - ray_normal / 2).floor())
68+
var block_global_position := ray_current_block_position
6569
voxel_world.set_block_global_position(block_global_position, 0)
6670
elif placing:
71+
# Calculate the position of the block to be placed.
6772
var block_global_position := Vector3i((ray_position + ray_normal / 2).floor())
6873
voxel_world.set_block_global_position(block_global_position, _selected_block)
74+
else:
75+
aim_preview.visible = false
6976

7077

7178
func _physics_process(delta: float) -> void:

3d/voxel/player/player.tscn

+28-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
[gd_scene load_steps=6 format=3 uid="uid://1s4asqpay67m"]
1+
[gd_scene load_steps=10 format=3 uid="uid://1s4asqpay67m"]
22

33
[ext_resource type="Script" uid="uid://rm45k07vw817" path="res://player/player.gd" id="1"]
44
[ext_resource type="Texture2D" uid="uid://d3f34krqfgdjd" path="res://world/textures/texture_sheet.png" id="2"]
@@ -13,6 +13,27 @@ radius = 0.375
1313
atlas = ExtResource("2")
1414
region = Rect2(0, 0, 64, 64)
1515

16+
[sub_resource type="Gradient" id="Gradient_rkbax"]
17+
offsets = PackedFloat32Array(0.952381, 1)
18+
colors = PackedColorArray(0, 0, 0, 0.0941176, 0, 0, 0, 1)
19+
20+
[sub_resource type="GradientTexture2D" id="GradientTexture2D_g1dw6"]
21+
gradient = SubResource("Gradient_rkbax")
22+
width = 256
23+
height = 256
24+
fill = 2
25+
fill_from = Vector2(0.5, 0.5)
26+
27+
[sub_resource type="StandardMaterial3D" id="StandardMaterial3D_yw30f"]
28+
transparency = 1
29+
shading_mode = 0
30+
albedo_texture = SubResource("GradientTexture2D_g1dw6")
31+
uv1_scale = Vector3(3, 2, 1)
32+
33+
[sub_resource type="BoxMesh" id="BoxMesh_qjkh3"]
34+
material = SubResource("StandardMaterial3D_yw30f")
35+
size = Vector3(1.001, 1.001, 1.001)
36+
1637
[node name="Player" type="CharacterBody3D"]
1738
collision_layer = 0
1839
script = ExtResource("1")
@@ -31,7 +52,7 @@ near = 0.02
3152
far = 1000.0
3253

3354
[node name="RayCast3D" type="RayCast3D" parent="Head"]
34-
target_position = Vector3(0, 0, -4)
55+
target_position = Vector3(0, 0, -5)
3556

3657
[node name="SelectedBlock" type="TextureRect" parent="."]
3758
texture_filter = 1
@@ -48,3 +69,8 @@ grow_horizontal = 0
4869
grow_vertical = 0
4970
texture = SubResource("2")
5071
expand_mode = 1
72+
73+
[node name="AimPreview" type="MeshInstance3D" parent="."]
74+
top_level = true
75+
visible = false
76+
mesh = SubResource("BoxMesh_qjkh3")

3d/voxel/world/world.tscn

+3
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ offset_bottom = -20.0
3737
grow_horizontal = 2
3838
grow_vertical = 2
3939
theme = ExtResource("7")
40+
theme_override_colors/font_shadow_color = Color(0, 0, 0, 0.501961)
41+
theme_override_constants/shadow_offset_x = 2
42+
theme_override_constants/shadow_offset_y = 2
4043
theme_override_font_sizes/font_size = 48
4144
script = ExtResource("6")
4245

0 commit comments

Comments
 (0)