From 8b53eaa6eda448413ac3c23ead4a767ec5e768e6 Mon Sep 17 00:00:00 2001 From: teatov Date: Tue, 18 Feb 2025 01:31:26 +1000 Subject: [PATCH] add debug collision shape --- project.godot | 6 ++- resources/materials/debug/debug_player.tres | 15 ++++++ scenes/player.tscn | 17 +++++- scripts/debug/debug_collision_shapes.gd | 59 +++++++++++++++++++++ scripts/player/player.gd | 3 ++ shaders/wireframe.gdshader | 22 ++++++++ 6 files changed, 120 insertions(+), 2 deletions(-) create mode 100644 resources/materials/debug/debug_player.tres create mode 100644 scripts/debug/debug_collision_shapes.gd create mode 100644 shaders/wireframe.gdshader diff --git a/project.godot b/project.godot index 4ee847e..a4c99e1 100644 --- a/project.godot +++ b/project.godot @@ -30,7 +30,6 @@ Settings="*res://scripts/globals/settings.gd" [debug] gdscript/warnings/untyped_declaration=2 -gdscript/warnings/unsafe_property_access=2 gdscript/warnings/unsafe_call_argument=2 gdscript/warnings/integer_division=0 @@ -115,6 +114,11 @@ aim_right={ ] } +[layer_names] + +3d_physics/layer_5="player" +3d_physics/layer_6="projectiles" + [physics] common/physics_jitter_fix=0.0 diff --git a/resources/materials/debug/debug_player.tres b/resources/materials/debug/debug_player.tres new file mode 100644 index 0000000..75846d6 --- /dev/null +++ b/resources/materials/debug/debug_player.tres @@ -0,0 +1,15 @@ +[gd_resource type="StandardMaterial3D" load_steps=3 format=3 uid="uid://cc18ee0wbfoud"] + +[ext_resource type="Shader" path="res://shaders/wireframe.gdshader" id="1_ys87q"] + +[sub_resource type="ShaderMaterial" id="ShaderMaterial_ionb4"] +render_priority = 0 +shader = ExtResource("1_ys87q") +shader_parameter/albedo = Color(0, 0.666667, 1, 1) +shader_parameter/outline_width = 0.0 + +[resource] +next_pass = SubResource("ShaderMaterial_ionb4") +transparency = 1 +shading_mode = 0 +albedo_color = Color(0, 0.666667, 1, 0.435294) diff --git a/scenes/player.tscn b/scenes/player.tscn index 621a27e..917ab35 100644 --- a/scenes/player.tscn +++ b/scenes/player.tscn @@ -1,6 +1,7 @@ -[gd_scene load_steps=5 format=3 uid="uid://b73y71y3efmv"] +[gd_scene load_steps=7 format=3 uid="uid://b73y71y3efmv"] [ext_resource type="Script" path="res://scripts/player/player.gd" id="1_xt3i8"] +[ext_resource type="Material" uid="uid://cc18ee0wbfoud" path="res://resources/materials/debug/debug_player.tres" id="2_0p422"] [sub_resource type="CylinderShape3D" id="CylinderShape3D_apl1i"] height = 1.8 @@ -11,8 +12,14 @@ height = 1.8 [sub_resource type="BoxMesh" id="BoxMesh_fdktb"] size = Vector3(0.5, 0.5, 0.5) +[sub_resource type="CylinderShape3D" id="CylinderShape3D_m8fdo"] +height = 1.0 +radius = 1.5 + [node name="Player" type="CharacterBody3D"] +collision_layer = 16 script = ExtResource("1_xt3i8") +_collision_debug_material = ExtResource("2_0p422") [node name="CollisionShape3D" type="CollisionShape3D" parent="."] transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0.9, 0) @@ -25,3 +32,11 @@ mesh = SubResource("CapsuleMesh_dtg5r") [node name="MeshInstance3D2" type="MeshInstance3D" parent="."] transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1.17779, 0.357218) mesh = SubResource("BoxMesh_fdktb") + +[node name="AttackArea" type="Area3D" parent="."] +collision_layer = 0 +collision_mask = 32 + +[node name="CollisionShape3D" type="CollisionShape3D" parent="AttackArea"] +transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1, 0) +shape = SubResource("CylinderShape3D_m8fdo") diff --git a/scripts/debug/debug_collision_shapes.gd b/scripts/debug/debug_collision_shapes.gd new file mode 100644 index 0000000..5ae390e --- /dev/null +++ b/scripts/debug/debug_collision_shapes.gd @@ -0,0 +1,59 @@ +class_name DebugCollisionShapes +extends RefCounted + +var _mesh_nodes: Array[MeshInstance3D] = [] + + +func init(children: Array[Node], parent: Node, material: Material) -> void: + for child in children: + if child is not CollisionShape3D: + continue + var shape_node: CollisionShape3D = child as CollisionShape3D + if shape_node.shape == null: + continue + + var mesh_node := MeshInstance3D.new() + if shape_node.shape is BoxShape3D: + var shape := shape_node.shape as BoxShape3D + var mesh := BoxMesh.new() + mesh.size = shape.size + mesh_node.mesh = mesh + if shape_node.shape is CapsuleShape3D: + var shape := shape_node.shape as CapsuleShape3D + var mesh := CapsuleMesh.new() + mesh.radius = shape.radius + mesh.height = shape.height + mesh.radial_segments = 8 + mesh.rings = 1 + mesh_node.mesh = mesh + if shape_node.shape is SphereShape3D: + var shape := shape_node.shape as SphereShape3D + var mesh := SphereMesh.new() + mesh.radius = shape.radius + mesh.height = shape.radius * 2 + mesh.radial_segments = 8 + mesh.rings = 4 + mesh_node.mesh = mesh + if shape_node.shape is CylinderShape3D: + var shape := shape_node.shape as CylinderShape3D + var mesh := CylinderMesh.new() + mesh.top_radius = shape.radius + mesh.bottom_radius = shape.radius + mesh.height = shape.height + mesh.radial_segments = 8 + mesh.rings = 0 + mesh_node.mesh = mesh + + mesh_node.material_override = material + mesh_node.cast_shadow = GeometryInstance3D.SHADOW_CASTING_SETTING_OFF + mesh_node.visible = false + parent.add_child(mesh_node) + mesh_node.global_transform = (child as Node3D).global_transform + _mesh_nodes.append(mesh_node) + + Debugger.mode_changed.connect(_on_debugger_mode_changed) + + +func _on_debugger_mode_changed(mode: Debugger.Mode) -> void: + for mesh_node in _mesh_nodes: + mesh_node.visible = mode == Debugger.Mode.FULL diff --git a/scripts/player/player.gd b/scripts/player/player.gd index 2a0c024..7b7f1a7 100644 --- a/scripts/player/player.gd +++ b/scripts/player/player.gd @@ -2,16 +2,19 @@ class_name Player extends CharacterBody3D @export var _respawn_height: float = -5 +@export var _collision_debug_material: Material var movement: PlayerMovement = PlayerMovement.new() var aiming: PlayerAiming = PlayerAiming.new() var _respawn_point: Vector3 +var _debug_collision_shapes := DebugCollisionShapes.new() func _ready() -> void: _respawn_point = global_position Referencer.player = self + _debug_collision_shapes.init(get_children(), self, _collision_debug_material) func _process(_delta: float) -> void: diff --git a/shaders/wireframe.gdshader b/shaders/wireframe.gdshader new file mode 100644 index 0000000..da11580 --- /dev/null +++ b/shaders/wireframe.gdshader @@ -0,0 +1,22 @@ +// https://godotshaders.com/shader/wireframe/ +shader_type spatial; +render_mode unshaded, wireframe, cull_disabled; + +uniform vec3 albedo : source_color = vec3(1.0, 1.0, 1.0); +uniform float outline_width : hint_range(0.0, 10.0, 0.1) = 0.0; + +void vertex() { + vec4 clip_position = + PROJECTION_MATRIX * (MODELVIEW_MATRIX * vec4(VERTEX, 1.0)); + vec3 clip_normal = + mat3(PROJECTION_MATRIX) * (mat3(MODELVIEW_MATRIX) * NORMAL); + + vec2 offset = normalize(clip_normal.xy) / VIEWPORT_SIZE * clip_position.w * + outline_width * 2.0; + + clip_position.xy += offset; + + POSITION = clip_position; +} + +void fragment() { ALBEDO = albedo.rgb; }