More initial work / experiments on the mesh data resource editor plugin.

This commit is contained in:
Relintai 2021-02-28 00:18:38 +01:00
parent 2e2ebee35a
commit 4e071994f6
3 changed files with 95 additions and 20 deletions

View File

@ -3,6 +3,8 @@ extends EditorSpatialGizmoPlugin
const MDIGizmo = preload("res://addons/mesh_data_resource_editor/MIDGizmo.gd") const MDIGizmo = preload("res://addons/mesh_data_resource_editor/MIDGizmo.gd")
var plugin
func _init(): func _init():
create_material("main", Color(1, 0, 0)) create_material("main", Color(1, 0, 0))
create_handle_material("handles") create_handle_material("handles")
@ -15,7 +17,14 @@ func get_priority():
func create_gizmo(spatial): func create_gizmo(spatial):
if spatial is MeshDataInstance: if spatial is MeshDataInstance:
print("new gizmo") var gizmo = MDIGizmo.new()
return MDIGizmo.new()
gizmo.plugin = plugin
plugin.register_gizmo(gizmo)
return gizmo
else: else:
return null return null
func is_handle_highlighted(gizmo, index):
pass

View File

@ -3,17 +3,15 @@ extends EditorSpatialGizmo
var gizmo_size = 3.0 var gizmo_size = 3.0
var plugin
var vertices : PoolVector3Array
var indices : PoolIntArray
func set_handle(index: int, camera: Camera, point: Vector2): func set_handle(index: int, camera: Camera, point: Vector2):
#print(index)
#print(point)
pass pass
func redraw(): func redraw():
#print("MIDGizmo redraw")
clear() clear()
var node : MeshDataInstance = get_spatial_node() var node : MeshDataInstance = get_spatial_node()
@ -28,25 +26,68 @@ func redraw():
var handles_material : SpatialMaterial = get_plugin().get_material("handles", self) var handles_material : SpatialMaterial = get_plugin().get_material("handles", self)
var handles : PoolVector3Array = mdr.array[ArrayMesh.ARRAY_VERTEX] vertices = mdr.array[ArrayMesh.ARRAY_VERTEX]
add_handles(handles, handles_material) #add_handles(vertices, handles_material)
var material = get_plugin().get_material("main", self) var material = get_plugin().get_material("main", self)
var lines : PoolVector3Array = PoolVector3Array()
var indices : PoolIntArray = mdr.array[ArrayMesh.ARRAY_INDEX] var indices : PoolIntArray = mdr.array[ArrayMesh.ARRAY_INDEX]
if indices.size() % 3 == 0: var lines : PoolVector3Array = PoolVector3Array()
for i in range(0, len(indices), 3):
lines.append(handles[i]) if vertices.size() % 3 == 0:
lines.append(handles[i + 1]) for i in range(0, len(vertices), 3):
lines.append(vertices[i])
lines.append(vertices[i + 1])
lines.append(handles[i + 1]) lines.append(vertices[i + 1])
lines.append(handles[i + 2]) lines.append(vertices[i + 2])
lines.append(handles[i + 2]) lines.append(vertices[i + 2])
lines.append(handles[i]) lines.append(vertices[i])
add_lines(lines, material, false) add_lines(lines, material, false)
func forward_spatial_gui_input(index, camera, event):
if event is InputEventMouseButton:
var gt : Transform = get_spatial_node().global_transform
var ray_from : Vector3 = camera.global_transform.origin
var gpoint : Vector2 = event.get_position()
var grab_threshold : float = 4
# var grab_threshold : float = 4 * EDSCALE;
if event.get_button_index() == BUTTON_LEFT:
if event.is_pressed():
var mouse_pos = event.get_position()
# if (_gizmo_select(p_index, _edit.mouse_pos))
# return true;
# select vertex
var closest_idx : int = -1
var closest_dist : float = 1e10
var vertices_size : int = vertices.size()
for i in range(vertices_size):
var joint_pos_3d : Vector3 = gt.xform(vertices[i])
var joint_pos_2d : Vector2 = camera.unproject_position(joint_pos_3d)
var dist_3d : float = ray_from.distance_to(joint_pos_3d)
var dist_2d : float = gpoint.distance_to(joint_pos_2d)
if (dist_2d < grab_threshold && dist_3d < closest_dist):
closest_dist = dist_3d;
closest_idx = i;
if (closest_idx >= 0):
print("f")
else:
print("nf")
return false
func _notification(what):
if what == NOTIFICATION_PREDELETE:
plugin.unregister_gizmo(self)

View File

@ -5,8 +5,33 @@ const MdiGizmoPlugin = preload("res://addons/mesh_data_resource_editor/MDIGizmoP
var gizmo_plugin = MdiGizmoPlugin.new() var gizmo_plugin = MdiGizmoPlugin.new()
var active_gizmos : Array = []
func _enter_tree(): func _enter_tree():
gizmo_plugin.plugin = self
add_spatial_gizmo_plugin(gizmo_plugin) add_spatial_gizmo_plugin(gizmo_plugin)
set_input_event_forwarding_always_enabled()
func _exit_tree(): func _exit_tree():
remove_spatial_gizmo_plugin(gizmo_plugin) remove_spatial_gizmo_plugin(gizmo_plugin)
#func forward_spatial_gui_input(camera, event):
# return forward_spatial_gui_input(0, camera, event)
func register_gizmo(gizmo):
active_gizmos.append(gizmo)
func unregister_gizmo(gizmo):
for i in range(active_gizmos.size()):
if active_gizmos[i] == gizmo:
active_gizmos.remove(i)
return
func forward_spatial_gui_input(index, camera, event):
for g in active_gizmos:
if g.forward_spatial_gui_input(index, camera, event):
return true
return false