GuilhermeGSousa / godot-motion-matching-demo

2 stars 0 forks source link

Replace DebugDraw with intermediate mesh #3

Open fire opened 3 weeks ago

fire commented 3 weeks ago

Replace the sphere and arrow meshes.

fire commented 3 weeks ago

Initial work. Buggy.

extends MeshInstance3D

@export var character : MMCharacter

func _ready():
    mesh = ImmediateMesh.new()

func draw_sphere(position, radius, color):
    mesh.surface_set_color(color)
    mesh.surface_add_vertex(position + Vector3(-radius, -radius, 0))
    mesh.surface_set_color(color)
    mesh.surface_add_vertex(position + Vector3(radius, -radius, 0))
    mesh.surface_set_color(color)
    mesh.surface_add_vertex(position + Vector3(radius, radius, 0))

    mesh.surface_set_color(color)
    mesh.surface_add_vertex(position + Vector3(-radius, -radius, 0))
    mesh.surface_set_color(color)
    mesh.surface_add_vertex(position + Vector3(radius, radius, 0))
    mesh.surface_set_color(color)
    mesh.surface_add_vertex(position + Vector3(-radius, radius, 0))

func draw_arrow(start_pos, end_pos, color, thickness):
    var direction = (end_pos - start_pos).normalized()
    mesh.surface_set_color(color)
    mesh.surface_add_vertex(start_pos + Vector3(-thickness, -thickness, 0))
    mesh.surface_set_color(color)
    mesh.surface_add_vertex(start_pos + Vector3(thickness, -thickness, 0))
    mesh.surface_set_color(color)
    mesh.surface_add_vertex(end_pos + Vector3(thickness, thickness, 0))

    mesh.surface_set_color(color)
    mesh.surface_add_vertex(start_pos + Vector3(-thickness, -thickness, 0))
    mesh.surface_set_color(color)
    mesh.surface_add_vertex(end_pos + Vector3(thickness, thickness, 0))
    mesh.surface_set_color(color)
    mesh.surface_add_vertex(end_pos + Vector3(-thickness, thickness, 0))

func _process(_delta: float) -> void:
    if not character:
        return

    var skeleton_state = character.get_skeleton_state()
    mesh.clear_surfaces()
    mesh.surface_begin(Mesh.PRIMITIVE_TRIANGLES)

    for bone_state in skeleton_state:
        var pos = bone_state["position"]
        var arrow_dir = bone_state["velocity"].normalized()
        var arrow_length = 0.1

        var sphere_color = Color(173/255.0, 255/255.0, 47/255.0)  # GREEN_YELLOW
        draw_sphere(pos, 0.01, sphere_color)

        var arrow_color = Color(30/255.0, 144/255.0, 255/255.0)  # DODGER_BLUE
        draw_arrow(pos, pos + arrow_dir * arrow_length, arrow_color, 0.01)

    mesh.surface_end()
fire commented 3 weeks ago

See also https://github.com/GuilhermeGSousa/godot-motion-matching-demo/blob/engine-module-vrm/addons/vrm/vrm_collider.gd#L119 for capture and sphere

GuilhermeGSousa commented 3 weeks ago

If you feel like its there feel free to open a PR with your changes, I'd love to play around with everything you've been working on. Also, can't wait to get rid of DebugDraw, I get crashes every so often because godot can't load the plugin