2262 lines
105 KiB
GDScript
Executable File
2262 lines
105 KiB
GDScript
Executable File
@tool
|
|
class_name PdView3D
|
|
extends SubViewportContainer
|
|
## Perspective 3D view: LMB-drag on empty space ORBITS (click deselects),
|
|
## RMB-drag PANS (right-click cancels a doodle; erasing is the
|
|
## dedicated Eraser tool), MMB-drag
|
|
## orbits (Shift+MMB pans), Alt+LMB-drag orbits while drawing/painting,
|
|
## wheel zooms. Shaded mesh with x-ray wireframe overlay, vertex handles,
|
|
## and the 3-arrow translate gizmo.
|
|
|
|
const GIZMO_LEN_PX := 90.0
|
|
|
|
enum Op { NONE, ORBIT, PAN, MOVE, GIZMO, PAINT, SHAPE, MARQUEE }
|
|
|
|
var doc: PdDocument
|
|
var main: Control
|
|
|
|
var sub: SubViewport
|
|
var root: Node3D
|
|
var cam: Camera3D
|
|
var mesh_root: Node3D
|
|
var overlay_mi: MeshInstance3D
|
|
var overlay_mesh: ImmediateMesh
|
|
var wire_mesh: ArrayMesh
|
|
var wire_mi: MeshInstance3D
|
|
var _wire_cache := {} # object id -> {k, pts, cols}
|
|
var _wire_ids: Array = [] # objects drawn from the cache last frame
|
|
var _wire_dirty := false
|
|
var overlay_mat: StandardMaterial3D
|
|
var grid_mi: MeshInstance3D
|
|
var grid_mesh: ImmediateMesh
|
|
var grid_mat: StandardMaterial3D
|
|
|
|
var target := Vector3.ZERO
|
|
var yaw := -0.6
|
|
var pitch := 0.5
|
|
var dist := 6.0
|
|
var _grid_key := Vector3(-1, -1, -1) # last built grid (visible, size, dist)
|
|
|
|
var op := Op.NONE
|
|
var pan_last := Vector2.ZERO
|
|
var press_px := Vector2.ZERO
|
|
var orbit_btn := 0 # mouse button that started an orbit
|
|
var press_moved := false # LMB press turned into a drag?
|
|
|
|
var move_before := []
|
|
var move_orig := {}
|
|
var move_orig_pos := {}
|
|
var move_hit0 := Vector3.ZERO
|
|
var move_valid := false
|
|
|
|
var gizmo_axis := -1
|
|
var gizmo_t0 := 0.0
|
|
var gizmo_c0 := Vector3.ZERO # selection center FROZEN at gizmo press
|
|
var gizmo_scalar := 0.0
|
|
var gizmo_ref_len := 1.0 # arm length (world) frozen at press (scale)
|
|
var gizmo_d0 := 1.0 # cursor distance from center at press (uniform scale)
|
|
var move_orig_scale := {} # object scale3 frozen at gizmo press
|
|
var _gizmo_label := "Move selection"
|
|
|
|
# rotate gizmo: rings, not arms - axis is a plain Vector3 (the
|
|
# ring's own normal) rather than the 0/1/2 index the move/scale code
|
|
# uses, since apply_rotate_gizmo takes a world-space axis directly.
|
|
var gizmo_rot_axis := Vector3.UP
|
|
var gizmo_rot_u := Vector3.RIGHT # two vectors spanning the ring's plane,
|
|
var gizmo_rot_v := Vector3.FORWARD # used to measure the drag angle
|
|
var gizmo_angle0 := 0.0
|
|
var gizmo_angle := 0.0
|
|
var gizmo_rot_origs := {} # {oi -> {"verts","pos"}} frozen at press
|
|
|
|
var hover := {}
|
|
var paint_hover := {} # last mesh hit under the cursor (Paint mode)
|
|
var mouse_px := Vector2(-1, -1)
|
|
var mouse_in := false
|
|
var _last_paint_key := "" # dedupes paint sweeps (face id or pixel cell)
|
|
var press_picked := false # this press picked something (UV-edit click-select)
|
|
var _ctx_menu: PopupMenu # right-click mesh-ops menu (edit modes)
|
|
var expand_btn: Button # fullscreen toggle for this view
|
|
|
|
# Ctrl+LMB rectangle multi-select (3D modeler viewports)
|
|
var marquee_start := Vector2.ZERO
|
|
var marquee_now := Vector2.ZERO
|
|
|
|
# 3D doodling (Draw mode): shapes are sketched on the plane under the
|
|
# cursor (a face) or a horizontal plane through the selection.
|
|
var shape_plane_n := Vector3.UP
|
|
var shape_plane_p := Vector3.ZERO
|
|
var shape_u := Vector3.RIGHT
|
|
var shape_v := Vector3.UP
|
|
var shape_pts := PackedVector2Array() # screen-space samples, like 2D
|
|
var _last_sides := true
|
|
var _ink_mat: StandardMaterial3D
|
|
var _last_shading := 0
|
|
var _last_filter := 0
|
|
|
|
static var _font: Font
|
|
|
|
|
|
func _init() -> void:
|
|
stretch = true
|
|
size_flags_horizontal = Control.SIZE_EXPAND_FILL
|
|
size_flags_vertical = Control.SIZE_EXPAND_FILL
|
|
mouse_filter = Control.MOUSE_FILTER_STOP
|
|
focus_mode = Control.FOCUS_ALL
|
|
|
|
|
|
func _ready() -> void:
|
|
sub = SubViewport.new()
|
|
sub.own_world_3d = true
|
|
sub.msaa_3d = Viewport.MSAA_2X
|
|
sub.gui_disable_input = true
|
|
sub.transparent_bg = false
|
|
add_child(sub)
|
|
|
|
var world := sub.find_world_3d()
|
|
var env := Environment.new()
|
|
env.background_mode = Environment.BG_COLOR
|
|
env.background_color = doc.bg_color if doc else Color("23262e")
|
|
env.ambient_light_source = Environment.AMBIENT_SOURCE_COLOR
|
|
env.ambient_light_color = Color(0.82, 0.82, 0.88)
|
|
env.ambient_light_energy = 0.85
|
|
world.environment = env
|
|
|
|
root = Node3D.new()
|
|
sub.add_child(root)
|
|
|
|
var light := DirectionalLight3D.new()
|
|
light.rotation_degrees = Vector3(-52, 38, 0)
|
|
light.light_energy = 1.1
|
|
light.shadow_enabled = false
|
|
root.add_child(light)
|
|
|
|
var fill := DirectionalLight3D.new()
|
|
fill.rotation_degrees = Vector3(-24, -145, 0)
|
|
fill.light_energy = 0.35
|
|
root.add_child(fill)
|
|
|
|
cam = Camera3D.new()
|
|
cam.fov = 55.0
|
|
cam.near = 0.05
|
|
cam.far = 500.0
|
|
root.add_child(cam)
|
|
|
|
mesh_root = Node3D.new()
|
|
root.add_child(mesh_root)
|
|
|
|
grid_mesh = ImmediateMesh.new()
|
|
grid_mat = StandardMaterial3D.new()
|
|
grid_mat.shading_mode = BaseMaterial3D.SHADING_MODE_UNSHADED
|
|
grid_mat.vertex_color_use_as_albedo = true
|
|
grid_mi = MeshInstance3D.new()
|
|
grid_mi.mesh = grid_mesh
|
|
grid_mi.material_override = grid_mat
|
|
grid_mi.cast_shadow = GeometryInstance3D.SHADOW_CASTING_SETTING_OFF
|
|
root.add_child(grid_mi)
|
|
|
|
overlay_mesh = ImmediateMesh.new()
|
|
overlay_mat = StandardMaterial3D.new()
|
|
overlay_mat.shading_mode = BaseMaterial3D.SHADING_MODE_UNSHADED
|
|
overlay_mat.vertex_color_use_as_albedo = true
|
|
overlay_mat.no_depth_test = true
|
|
overlay_mat.transparency = BaseMaterial3D.TRANSPARENCY_ALPHA
|
|
overlay_mat.cull_mode = BaseMaterial3D.CULL_DISABLED
|
|
overlay_mat.render_priority = 10
|
|
overlay_mi = MeshInstance3D.new()
|
|
overlay_mi.mesh = overlay_mesh
|
|
overlay_mi.material_override = overlay_mat
|
|
overlay_mi.cast_shadow = GeometryInstance3D.SHADOW_CASTING_SETTING_OFF
|
|
root.add_child(overlay_mi)
|
|
|
|
wire_mesh = ArrayMesh.new()
|
|
var wire_mat := overlay_mat.duplicate() as StandardMaterial3D
|
|
wire_mat.render_priority = 9
|
|
wire_mi = MeshInstance3D.new()
|
|
wire_mi.mesh = wire_mesh
|
|
wire_mi.material_override = wire_mat
|
|
wire_mi.cast_shadow = GeometryInstance3D.SHADOW_CASTING_SETTING_OFF
|
|
root.add_child(wire_mi)
|
|
|
|
doc.doc_changed.connect(_flag_meshes_dirty)
|
|
doc.settings_changed.connect(_on_settings)
|
|
_last_sides = doc.both_sides if doc else true
|
|
_update_cam()
|
|
_rebuild_grid()
|
|
|
|
|
|
## See pd_view2d.gd's _style_corner_btn - same flat, transparent-at-rest
|
|
## look for this view's own corner button, for consistency.
|
|
func _style_corner_btn(b: Button) -> void:
|
|
var normal := StyleBoxFlat.new()
|
|
normal.bg_color = Color(1, 1, 1, 0)
|
|
normal.set_corner_radius_all(4)
|
|
var hover := normal.duplicate()
|
|
hover.bg_color = Color(1, 1, 1, 0.12)
|
|
var pressed := normal.duplicate()
|
|
pressed.bg_color = Color(1, 1, 1, 0.2)
|
|
b.add_theme_stylebox_override("normal", normal)
|
|
b.add_theme_stylebox_override("hover", hover)
|
|
b.add_theme_stylebox_override("pressed", pressed)
|
|
b.add_theme_stylebox_override("focus", normal)
|
|
|
|
|
|
func setup(d: PdDocument, m: Control) -> void:
|
|
doc = d
|
|
main = m
|
|
# expand/retract toggle in the top-right corner: pressed = this
|
|
# viewport alone fills the whole view area
|
|
expand_btn = Button.new()
|
|
expand_btn.icon = PdIcons.get_icon("expand")
|
|
expand_btn.toggle_mode = true
|
|
expand_btn.focus_mode = Control.FOCUS_NONE
|
|
expand_btn.tooltip_text = "Expand view\nShow only this viewport at full size - press again to retract"
|
|
expand_btn.custom_minimum_size = Vector2(22, 22)
|
|
expand_btn.add_theme_constant_override("icon_max_width", 16)
|
|
expand_btn.modulate = Color(1, 1, 1, 0.75)
|
|
_style_corner_btn(expand_btn)
|
|
add_child(expand_btn)
|
|
expand_btn.set_anchors_and_offsets_preset(Control.PRESET_TOP_RIGHT)
|
|
expand_btn.offset_left = -28.0
|
|
expand_btn.offset_top = 5.0
|
|
expand_btn.offset_right = -6.0
|
|
expand_btn.offset_bottom = 27.0
|
|
expand_btn.pressed.connect(func(): main._toggle_expand(self))
|
|
if main.has_method("_sync_expand_btns"):
|
|
main._sync_expand_btns.call_deferred()
|
|
# in-viewport hint chip - child Control, drawn above the
|
|
# SubViewport, ignores the mouse so it never blocks the camera
|
|
_hint_panel = PanelContainer.new()
|
|
var hsb := StyleBoxFlat.new()
|
|
hsb.bg_color = Color(0, 0, 0, 0.45)
|
|
hsb.set_corner_radius_all(4)
|
|
hsb.content_margin_left = 7.0
|
|
hsb.content_margin_right = 7.0
|
|
hsb.content_margin_top = 3.0
|
|
hsb.content_margin_bottom = 3.0
|
|
_hint_panel.add_theme_stylebox_override("panel", hsb)
|
|
_hint_panel.mouse_filter = Control.MOUSE_FILTER_IGNORE
|
|
_hint_label = Label.new()
|
|
_hint_label.add_theme_font_size_override("font_size", 11)
|
|
_hint_label.add_theme_color_override("font_color", Color(1, 1, 1, 0.72))
|
|
_hint_label.mouse_filter = Control.MOUSE_FILTER_IGNORE
|
|
_hint_panel.add_child(_hint_label)
|
|
add_child(_hint_panel)
|
|
_hint_panel.visible = false
|
|
|
|
|
|
func _on_settings() -> void:
|
|
var env := sub.find_world_3d().environment
|
|
if env:
|
|
env.background_color = doc.bg_color
|
|
if _last_sides != doc.both_sides or _last_shading != doc.view_shading \
|
|
or _last_filter != doc.tex_filter:
|
|
_last_sides = doc.both_sides
|
|
_last_shading = doc.view_shading
|
|
_last_filter = doc.tex_filter
|
|
_rebuild_meshes()
|
|
_rebuild_grid()
|
|
|
|
|
|
func _notification(what: int) -> void:
|
|
if what == NOTIFICATION_RESIZED:
|
|
_place_hint()
|
|
elif what == NOTIFICATION_MOUSE_ENTER:
|
|
mouse_in = true
|
|
# the 3D view has no per-control cursor override, so a
|
|
# stale global drag/split shape left by the panel splitter
|
|
# would keep showing here - reset it on entry
|
|
Input.set_default_cursor_shape(Input.CURSOR_ARROW)
|
|
elif what == NOTIFICATION_MOUSE_EXIT:
|
|
mouse_in = false
|
|
paint_hover = {}
|
|
|
|
|
|
# ------------------------------------------------------------- camera ---
|
|
func _wpp() -> float:
|
|
# world units per pixel at the target distance
|
|
var h := maxf(1.0, sub.size.y)
|
|
return 2.0 * dist * tan(deg_to_rad(cam.fov) * 0.5) / h
|
|
|
|
|
|
func _cam_right() -> Vector3: return cam.global_transform.basis.x
|
|
func _cam_up() -> Vector3: return cam.global_transform.basis.y
|
|
func _cam_fwd() -> Vector3: return -cam.global_transform.basis.z
|
|
|
|
|
|
func _update_cam() -> void:
|
|
var cp := cos(pitch)
|
|
var dir := Vector3(sin(yaw) * cp, sin(pitch), cos(yaw) * cp)
|
|
cam.position = target + dir * dist
|
|
cam.look_at(target, Vector3.UP)
|
|
|
|
|
|
func _project(v: Vector3) -> Vector2:
|
|
return cam.unproject_position(v)
|
|
|
|
|
|
func frame_selection() -> void:
|
|
var c := doc.sel_center_w()
|
|
if c == Vector3.INF:
|
|
target = Vector3.ZERO
|
|
dist = 6.0
|
|
else:
|
|
target = c
|
|
var ab := AABB(c, Vector3.ZERO)
|
|
var any := false
|
|
for oi in (doc.sel_objs if doc.mode == PdDocument.Mode.OBJECT else [doc.active_obj]):
|
|
var o := doc.obj(oi)
|
|
if o != null and o.is_visible:
|
|
ab = ab.merge(o.aabb_w())
|
|
any = true
|
|
dist = clampf(ab.get_longest_axis_size() * 1.8 + 1.0, 1.5, 60.0) if any else 6.0
|
|
_update_cam()
|
|
|
|
|
|
# ------------------------------------------------------- mesh rebuild ---
|
|
## Mesh instances are REUSED: a rebuild updates meshes on the existing
|
|
## children and only grows/shrinks the pool. Recreating nodes per change
|
|
## would spam the frame with dying MeshInstances on every paint stamp and
|
|
## drag step.
|
|
## SHADED mode renders ink edges (edge_color) as a DEPTH-TESTED lines
|
|
## surface baked into the object mesh: far-side edges hide behind the
|
|
## faces, matching a real shaded render. The no-depth overlay drew them
|
|
## straight through the geometry ("wireframe off but edge lines are still
|
|
## visible for some edges from behind faces").
|
|
func _ink_material() -> StandardMaterial3D:
|
|
if _ink_mat == null:
|
|
_ink_mat = StandardMaterial3D.new()
|
|
_ink_mat.shading_mode = BaseMaterial3D.SHADING_MODE_UNSHADED
|
|
_ink_mat.vertex_color_use_as_albedo = true
|
|
return _ink_mat
|
|
|
|
|
|
func _append_ink_surface(m: ArrayMesh, o: PdPolyObj) -> void:
|
|
var pts := PackedVector3Array()
|
|
var cols := PackedColorArray()
|
|
for e in o.edges():
|
|
var ec := o.edge_color(e)
|
|
if ec.a <= 0.01:
|
|
continue
|
|
var ca := Color(ec.r, ec.g, ec.b, 0.95)
|
|
pts.append(o.wvert(e.x))
|
|
cols.append(ca)
|
|
pts.append(o.wvert(e.y))
|
|
cols.append(ca)
|
|
if pts.size() < 2:
|
|
return
|
|
var arrays := []
|
|
arrays.resize(Mesh.ARRAY_MAX)
|
|
arrays[Mesh.ARRAY_VERTEX] = pts
|
|
arrays[Mesh.ARRAY_COLOR] = cols
|
|
m.add_surface_from_arrays(Mesh.PRIMITIVE_LINES, arrays)
|
|
m.surface_set_material(m.get_surface_count() - 1, _ink_material())
|
|
|
|
|
|
func _rebuild_meshes() -> void:
|
|
var kids := []
|
|
for c in mesh_root.get_children():
|
|
if not c.is_queued_for_deletion():
|
|
kids.append(c)
|
|
var ki := 0
|
|
if doc.view_shading != 2:
|
|
for oi in doc.objects.size():
|
|
var o := doc.obj(oi)
|
|
if o == null or not o.is_visible:
|
|
continue
|
|
var mi: MeshInstance3D
|
|
if ki < kids.size():
|
|
mi = kids[ki]
|
|
else:
|
|
mi = MeshInstance3D.new()
|
|
mesh_root.add_child(mi)
|
|
ki += 1
|
|
# Pool reuse: an object whose render state is
|
|
# unchanged (geometry/UV epoch, shading flags, the
|
|
# very mesh instance AND the very texture object it
|
|
# rendered through) keeps its built mesh - painting
|
|
# updates the shared ImageTexture in place, and the
|
|
# texture-id meta catches the placeholder -> real
|
|
# canvas swap on first paint.
|
|
var tex_obj := o.get_texture(doc.show_checker)
|
|
if mi.get_meta("oid", 0) == o.get_instance_id() \
|
|
and mi.get_meta("ve", -1) == o.visual_epoch \
|
|
and mi.get_meta("sh", -1) == doc.view_shading \
|
|
and mi.get_meta("bs", true) == doc.both_sides \
|
|
and mi.get_meta("tf", -1) == doc.tex_filter \
|
|
and mi.get_meta("hc", true) == doc.show_checker \
|
|
and mi.get_meta("sm", false) == o.shade_smooth \
|
|
and mi.get_meta("tx", 0) == tex_obj.get_instance_id():
|
|
continue
|
|
var m := PdIo.build_mesh(o, PdPolyObj.BASE,
|
|
doc.both_sides, true, doc.tex_filter == 1,
|
|
not doc.show_checker)
|
|
if m == null:
|
|
mi.mesh = null
|
|
mi.set_meta("oid", 0)
|
|
continue
|
|
mi.mesh = m
|
|
mi.name = "obj%d" % oi
|
|
if doc.view_shading == 1:
|
|
_append_ink_surface(m, o)
|
|
mi.set_meta("oid", o.get_instance_id())
|
|
mi.set_meta("ve", o.visual_epoch)
|
|
mi.set_meta("sh", doc.view_shading)
|
|
mi.set_meta("bs", doc.both_sides)
|
|
mi.set_meta("tf", doc.tex_filter)
|
|
mi.set_meta("tx", o.get_texture(
|
|
doc.show_checker).get_instance_id())
|
|
mi.set_meta("hc", doc.show_checker)
|
|
mi.set_meta("sm", o.shade_smooth)
|
|
while ki < kids.size():
|
|
kids[ki].queue_free()
|
|
ki += 1
|
|
_rebuild_grid()
|
|
|
|
|
|
func _rebuild_grid() -> void:
|
|
# the grid only depends on visibility, cell size and the camera
|
|
# distance (which drives the LOD step) - skip rebuilds while all
|
|
# three are unchanged
|
|
var key := Vector3(1.0 if doc.grid_visible else 0.0, doc.grid_size,
|
|
dist)
|
|
if key == _grid_key:
|
|
return
|
|
_grid_key = key
|
|
grid_mesh.clear_surfaces()
|
|
if not doc.grid_visible:
|
|
return
|
|
var step := doc.grid_size
|
|
while step < dist * 0.015:
|
|
step *= 2.0
|
|
var n := int(ceil(dist * 1.6 / step))
|
|
grid_mesh.surface_begin(Mesh.PRIMITIVE_LINES)
|
|
# DARK grid ink: dark lines read like Blender's grid on the
|
|
# default background and also survive a light background; faint
|
|
# white lines only work on dark scenes. The colored X/Z axis
|
|
# lines below are untouched.
|
|
var minor := Color(0, 0, 0, 0.20)
|
|
var major := Color(0, 0, 0, 0.38)
|
|
for i in range(-n, n + 1):
|
|
var a := i * step
|
|
var c := major if i % 4 == 0 else minor
|
|
grid_mesh.surface_set_color(c)
|
|
grid_mesh.surface_add_vertex(Vector3(a, -0.002, -n * step))
|
|
grid_mesh.surface_set_color(c)
|
|
grid_mesh.surface_add_vertex(Vector3(a, -0.002, n * step))
|
|
grid_mesh.surface_set_color(c)
|
|
grid_mesh.surface_add_vertex(Vector3(-n * step, -0.002, a))
|
|
grid_mesh.surface_set_color(c)
|
|
grid_mesh.surface_add_vertex(Vector3(n * step, -0.002, a))
|
|
# axes
|
|
grid_mesh.surface_set_color(Color(1.0, 0.37, 0.37, 0.55))
|
|
grid_mesh.surface_add_vertex(Vector3(-n * step, -0.002, 0))
|
|
grid_mesh.surface_set_color(Color(1.0, 0.37, 0.37, 0.55))
|
|
grid_mesh.surface_add_vertex(Vector3(n * step, -0.002, 0))
|
|
grid_mesh.surface_set_color(Color(0.37, 0.6, 1.0, 0.55))
|
|
grid_mesh.surface_add_vertex(Vector3(0, -0.002, -n * step))
|
|
grid_mesh.surface_set_color(Color(0.37, 0.6, 1.0, 0.55))
|
|
grid_mesh.surface_add_vertex(Vector3(0, -0.002, n * step))
|
|
grid_mesh.surface_end()
|
|
|
|
|
|
# ------------------------------------------------------ overlay draw ---
|
|
## Whether the texture workspace draws the selected faces' outline on the
|
|
## 3D model. The Brush hides it (a wireframe over the model while brushing
|
|
## hides the texels being painted); face lock and every other paint tool
|
|
## (Fill, Eraser, Picker, Replace, Pixel) show it as the selection marker.
|
|
static func tex_outline_visible(d: PdDocument) -> bool:
|
|
return d != null and (d.face_lock or d.paint_tool != "Brush")
|
|
|
|
# The overlay is BUFFERED: every draw helper appends to per-primitive
|
|
# buffers which are flushed into AT MOST ONE LINES + ONE TRIANGLES mesh
|
|
# surface per frame. ImmediateMesh adds a new mesh SURFACE for every
|
|
# begin/end pair - per-segment surfaces would pile up by the hundreds
|
|
# each frame and trip the renderer's MAX_MESH_SURFACES guard on busy
|
|
# scenes. Buffering also cuts draw-data overhead dramatically.
|
|
var _ov_lines: Array = [] # [a: Vector3, b: Vector3, col]
|
|
var _ov_quads: Array = [] # [a, b, px, col] screen-constant width
|
|
var _ov_tris: Array = [] # [v: Vector3, col] flat triangle soup
|
|
|
|
|
|
func _ov_reset() -> void:
|
|
_ov_lines = []
|
|
_ov_quads = []
|
|
_ov_tris = []
|
|
|
|
|
|
## Plain hairline between two world points.
|
|
func _line3(a: Vector3, b: Vector3, col: Color) -> void:
|
|
_ov_lines.append([a, b, col])
|
|
|
|
|
|
## Screen-constant-width line (a camera-facing quad).
|
|
func _quad_line(a: Vector3, b: Vector3, px: float, col: Color) -> void:
|
|
_ov_quads.append([a, b, px, col])
|
|
|
|
|
|
## Camera-facing dot (8-segment fan).
|
|
func _dot(p: Vector3, px: float, col: Color) -> void:
|
|
var r := px * _wpp() * 0.5
|
|
var u := _cam_up() * r
|
|
var rt := _cam_right() * r
|
|
for i in 8:
|
|
var a0 := TAU * i / 8.0
|
|
var a1 := TAU * (i + 1) / 8.0
|
|
_ov_tris.append([p, col])
|
|
_ov_tris.append([p + rt * cos(a0) + u * sin(a0), col])
|
|
_ov_tris.append([p + rt * cos(a1) + u * sin(a1), col])
|
|
|
|
|
|
func _ov_flush() -> void:
|
|
if not _ov_lines.is_empty():
|
|
overlay_mesh.surface_begin(Mesh.PRIMITIVE_LINES)
|
|
for l in _ov_lines:
|
|
overlay_mesh.surface_set_color(l[2])
|
|
overlay_mesh.surface_add_vertex(l[0])
|
|
overlay_mesh.surface_set_color(l[2])
|
|
overlay_mesh.surface_add_vertex(l[1])
|
|
overlay_mesh.surface_end()
|
|
_ov_lines = []
|
|
if not _ov_quads.is_empty() or not _ov_tris.is_empty():
|
|
overlay_mesh.surface_begin(Mesh.PRIMITIVE_TRIANGLES)
|
|
for q in _ov_quads:
|
|
var a: Vector3 = q[0]
|
|
var b: Vector3 = q[1]
|
|
var px: float = q[2]
|
|
var col: Color = q[3]
|
|
var off1 := _cam_right() * px * _wpp() * 0.5
|
|
var off2 := _cam_up() * px * _wpp() * 0.5
|
|
for v in [a - off1 - off2, b - off1 - off2,
|
|
b + off1 + off2, a - off1 - off2,
|
|
b + off1 + off2, a + off1 + off2]:
|
|
overlay_mesh.surface_set_color(col)
|
|
overlay_mesh.surface_add_vertex(v)
|
|
for t in _ov_tris:
|
|
overlay_mesh.surface_set_color(t[1])
|
|
overlay_mesh.surface_add_vertex(t[0])
|
|
overlay_mesh.surface_end()
|
|
_ov_quads = []
|
|
_ov_tris = []
|
|
|
|
|
|
func _process(_delta: float) -> void:
|
|
if not is_visible_in_tree():
|
|
return
|
|
_update_cam()
|
|
_rebuild_overlay()
|
|
# in-viewport hint chip: update when the contextual text
|
|
# changes (mode / selection / substate driven)
|
|
var h := PdMain.context_hint(doc, "3d") if doc != null else ""
|
|
if h != _last_hint:
|
|
_last_hint = h
|
|
_sync_hint()
|
|
|
|
|
|
## doc_changed marks the meshes stale and flushes ONCE per frame (at the
|
|
## frame's deferred point). A direct rebuild per signal made every motion
|
|
## event of a drag re-triangulate and re-upload EVERY object.
|
|
var _meshes_dirty := false
|
|
|
|
|
|
func _flag_meshes_dirty() -> void:
|
|
_meshes_dirty = true
|
|
_flush_meshes.call_deferred()
|
|
|
|
|
|
func _flush_meshes() -> void:
|
|
if not _meshes_dirty or not is_inside_tree():
|
|
return
|
|
_meshes_dirty = false
|
|
_rebuild_meshes()
|
|
|
|
|
|
## Small muted hint chip at the bottom-left of the 3D viewport:
|
|
## e.g. "Right-click: options (EXTRUDE faces, fill, delete and more)" once
|
|
## a face is selected - the viewport teaches itself, no docs required. It
|
|
## must be a Control CHILD (not the container's own _draw): a
|
|
## SubViewportContainer's own drawing renders UNDER its SubViewport child.
|
|
var _hint_panel: PanelContainer
|
|
var _hint_label: Label
|
|
var _last_hint := ""
|
|
|
|
|
|
func _sync_hint() -> void:
|
|
if _hint_label == null:
|
|
return
|
|
_hint_label.text = _last_hint
|
|
_hint_panel.visible = _last_hint != ""
|
|
_place_hint()
|
|
|
|
|
|
## Pin the chip to the bottom-left at its natural size. Runs on every text
|
|
## change AND every resize, so a view that goes full screen (or is resized
|
|
## by the splitter) keeps the chip at its bottom edge.
|
|
func _place_hint() -> void:
|
|
if _hint_panel == null:
|
|
return
|
|
_hint_panel.reset_size()
|
|
_hint_panel.position = Vector2(4.0, size.y - _hint_panel.size.y - 4.0)
|
|
|
|
|
|
func _gizmo_axes() -> Array:
|
|
# [{axis, dir(Vector3), from(Vector2), to(Vector2), col}]
|
|
var c := doc.gizmo_anchor_w()
|
|
if c == Vector3.INF:
|
|
return []
|
|
var out := []
|
|
for i in 3:
|
|
var ax := Vector3.RIGHT
|
|
match i:
|
|
1: ax = Vector3.UP
|
|
2: ax = Vector3(0, 0, 1)
|
|
var tip := c + ax * GIZMO_LEN_PX * _wpp()
|
|
out.append({
|
|
"axis": i, "dir": ax, "tip_w": tip,
|
|
"from": _project(c), "to": _project(tip),
|
|
"col": PdDocument.AXIS_COLORS[i],
|
|
})
|
|
return out
|
|
|
|
|
|
const ROT_RING_SEGS := 48
|
|
|
|
## Two unit vectors spanning the plane perpendicular to `n` - the fixed
|
|
## reference frame a rotate drag measures its angle against (drawing and
|
|
## hit-testing use the same pair, so the ring you see is the ring you hit).
|
|
func _ring_basis(n: Vector3) -> Array:
|
|
var u := n.cross(Vector3.UP)
|
|
if u.length() < 0.1:
|
|
u = n.cross(Vector3.RIGHT)
|
|
u = u.normalized()
|
|
var v := n.cross(u).normalized()
|
|
return [u, v]
|
|
|
|
|
|
## The three rotate rings (X/Y/Z), sampled as polylines in both world and
|
|
## projected screen space so _rebuild_overlay and _gizmo_hit share one
|
|
## source of truth for where each ring actually is.
|
|
func _gizmo_rings() -> Array:
|
|
var c := doc.gizmo_anchor_w()
|
|
if c == Vector3.INF:
|
|
return []
|
|
var r := GIZMO_LEN_PX * _wpp()
|
|
var out := []
|
|
for i in 3:
|
|
var ax := Vector3.RIGHT
|
|
match i:
|
|
1: ax = Vector3.UP
|
|
2: ax = Vector3(0, 0, 1)
|
|
var uv := _ring_basis(ax)
|
|
var pts_w := PackedVector3Array()
|
|
var pts_s := PackedVector2Array()
|
|
for s in ROT_RING_SEGS + 1:
|
|
var a := TAU * float(s) / ROT_RING_SEGS
|
|
var w: Vector3 = c + ((uv[0] as Vector3) * cos(a) + (uv[1] as Vector3) * sin(a)) * r
|
|
pts_w.append(w)
|
|
pts_s.append(_project(w))
|
|
out.append({"axis": i, "dir": ax, "u": uv[0], "v": uv[1],
|
|
"pts_w": pts_w, "pts_s": pts_s, "col": PdDocument.AXIS_COLORS[i]})
|
|
return out
|
|
|
|
|
|
var _wire_now: Array = []
|
|
|
|
|
|
func _wire_begin() -> void:
|
|
_wire_now = []
|
|
|
|
|
|
## Wireframe lines of one object, rebuilt only when its geometry, transform
|
|
## or highlight state changed. The key hashes the vertex and face arrays
|
|
## natively, so no edit path can leave a stale wire behind.
|
|
func _wire_use(o: PdPolyObj, is_active: bool) -> void:
|
|
var id := o.get_instance_id()
|
|
var key := [hash(o.verts), hash(o.faces), o.position, o.scale3, is_active]
|
|
_wire_now.append(id)
|
|
var hit: Variant = _wire_cache.get(id)
|
|
if hit != null and (hit as Dictionary)["k"] == key:
|
|
return
|
|
var es := o.edges()
|
|
var pts := PackedVector3Array()
|
|
pts.resize(es.size() * 2)
|
|
var wv := PackedVector3Array()
|
|
wv.resize(o.nverts())
|
|
for i in wv.size():
|
|
wv[i] = o.wvert(i)
|
|
var n := 0
|
|
for e in es:
|
|
pts[n] = wv[e.x]
|
|
pts[n + 1] = wv[e.y]
|
|
n += 2
|
|
var cols := PackedColorArray()
|
|
cols.resize(pts.size())
|
|
cols.fill(Color(1, 1, 1, 0.34 if is_active else 0.15))
|
|
_wire_cache[id] = {"k": key, "pts": pts, "cols": cols}
|
|
_wire_dirty = true
|
|
|
|
|
|
func _wire_end() -> void:
|
|
if _wire_now != _wire_ids:
|
|
_wire_dirty = true
|
|
_wire_ids = _wire_now
|
|
for id in _wire_cache.keys():
|
|
if not _wire_ids.has(id):
|
|
_wire_cache.erase(id)
|
|
if not _wire_dirty:
|
|
return
|
|
_wire_dirty = false
|
|
wire_mesh.clear_surfaces()
|
|
var pts := PackedVector3Array()
|
|
var cols := PackedColorArray()
|
|
for id in _wire_ids:
|
|
var ent: Dictionary = _wire_cache[id]
|
|
pts.append_array(ent["pts"])
|
|
cols.append_array(ent["cols"])
|
|
if pts.is_empty():
|
|
return
|
|
var arr := []
|
|
arr.resize(Mesh.ARRAY_MAX)
|
|
arr[Mesh.ARRAY_VERTEX] = pts
|
|
arr[Mesh.ARRAY_COLOR] = cols
|
|
wire_mesh.add_surface_from_arrays(Mesh.PRIMITIVE_LINES, arr)
|
|
|
|
|
|
func _rebuild_overlay() -> void:
|
|
if overlay_mesh == null:
|
|
return
|
|
overlay_mesh.clear_surfaces()
|
|
_ov_reset()
|
|
_wire_begin()
|
|
var accent := PdDocument.ACCENT
|
|
|
|
for oi in doc.objects.size():
|
|
var o := doc.obj(oi)
|
|
if o == null or not o.is_visible:
|
|
continue
|
|
var is_active := oi == doc.active_obj
|
|
var wire := Color(1, 1, 1, 0.34 if is_active else 0.15)
|
|
if o.edge_colors.is_empty():
|
|
# plain wireframe: cached per object and uploaded natively
|
|
if doc.view_shading != 1:
|
|
_wire_use(o, is_active)
|
|
else:
|
|
# shaded-only mode hides the x-ray edge overlay
|
|
if doc.view_shading != 1:
|
|
for e in o.edges():
|
|
if o.edge_color(e).a > 0.01:
|
|
continue
|
|
_line3(o.wvert(e.x), o.wvert(e.y), wire)
|
|
for e in o.edges():
|
|
var ec := o.edge_color(e)
|
|
if ec.a > 0.01:
|
|
if doc.view_shading == 1:
|
|
continue # depth-tested ink surface in the mesh
|
|
_quad_line(o.wvert(e.x), o.wvert(e.y), 1.6, Color(ec.r, ec.g, ec.b, 0.95))
|
|
|
|
# Selected-face outline. FACE mode shows the translucent
|
|
# accent fill + outline. TEXTURE mode shows ONLY the outline
|
|
# (edge highlight), and hides even that while the BRUSH is
|
|
# painting (a wireframe over the model while brushing hides
|
|
# the very texels the user is judging) - face lock brings it
|
|
# back, because the outline then marks the locked face, and
|
|
# every other tool (Fill, Eraser, Picker, Replace, Pixel)
|
|
# keeps it as the selection indicator.
|
|
if is_active and (doc.mode == PdDocument.Mode.FACE \
|
|
or (doc.mode == PdDocument.Mode.TEXTURE \
|
|
and tex_outline_visible(doc))):
|
|
for fi in doc.sel_faces:
|
|
if fi >= o.nfaces():
|
|
continue
|
|
var tr := o.face_triangulation(fi)
|
|
var pts: PackedVector3Array = tr["verts"]
|
|
var tris: PackedInt32Array = tr["tris"]
|
|
if pts.size() < 3 or tris.size() < 3:
|
|
continue
|
|
if doc.mode == PdDocument.Mode.FACE:
|
|
var fill := Color(accent.r,
|
|
accent.g, accent.b, 0.28)
|
|
for t in range(0, tris.size() - 2, 3):
|
|
for k in 3:
|
|
_ov_tris.append([
|
|
pts[tris[t + k]],
|
|
fill])
|
|
var n := o.face_loop(fi).size()
|
|
var loop := o.face_pts_w(fi)
|
|
for i in n:
|
|
_quad_line(loop[i], loop[(i + 1) % n],
|
|
2.0, accent)
|
|
|
|
# selected edges
|
|
if is_active and doc.mode == PdDocument.Mode.EDGE:
|
|
for e in doc.sel_edges:
|
|
_quad_line(o.wvert(e.x), o.wvert(e.y), 2.6, accent)
|
|
|
|
# object selection outline
|
|
if doc.mode == PdDocument.Mode.OBJECT and doc.sel_objs.has(oi):
|
|
var ab := o.aabb_w()
|
|
var p := ab.position
|
|
var s := ab.size
|
|
var c8 := [
|
|
p, p + Vector3(s.x, 0, 0), p + Vector3(s.x, 0, s.z), p + Vector3(0, 0, s.z),
|
|
p + Vector3(0, s.y, 0), p + Vector3(s.x, s.y, 0),
|
|
p + Vector3(s.x, s.y, s.z), p + Vector3(0, s.y, s.z)]
|
|
var lines := [[0,1],[1,2],[2,3],[3,0],[4,5],[5,6],[6,7],[7,4],[0,4],[1,5],[2,6],[3,7]]
|
|
for l in lines:
|
|
_quad_line(c8[l[0]], c8[l[1]], 1.6, accent)
|
|
|
|
# vertex handles
|
|
var show_verts := is_active and doc.mode != PdDocument.Mode.OBJECT \
|
|
and doc.mode != PdDocument.Mode.TEXTURE and doc.mode != PdDocument.Mode.DRAW
|
|
if show_verts:
|
|
for vi in o.nverts():
|
|
var w := o.wvert(vi)
|
|
if doc.mode == PdDocument.Mode.VERTEX and doc.sel_verts.has(vi):
|
|
_dot(w, 7.0, Color(0, 0, 0, 0.85))
|
|
_dot(w, 5.0, PdDocument.SEL_VERT)
|
|
elif doc.mode == PdDocument.Mode.VERTEX:
|
|
_dot(w, 3.2, Color(1, 1, 1, 0.55))
|
|
if hover.get("kind") == "vert" and hover.get("obj") == oi \
|
|
and hover.get("idx") == vi:
|
|
_dot(w, 6.0, Color(1, 1, 1, 0.95))
|
|
elif doc.mode == PdDocument.Mode.EDGE and _edge_touches(vi):
|
|
_dot(w, 4.0, accent)
|
|
elif doc.mode == PdDocument.Mode.FACE and _face_touches(o, vi):
|
|
_dot(w, 4.0, PdDocument.SEL_VERT)
|
|
|
|
_wire_end()
|
|
|
|
# gizmo: only on a real, visible selection (see gizmo_anchor_w)
|
|
var gizmo_visible := doc.gizmo_on \
|
|
and doc.mode != PdDocument.Mode.DRAW \
|
|
and doc.mode != PdDocument.Mode.TEXTURE \
|
|
and doc.gizmo_anchor_w() != Vector3.INF
|
|
if gizmo_visible:
|
|
var c := doc.gizmo_anchor_w()
|
|
if c != Vector3.INF:
|
|
var scale_kind := doc.gizmo_kind == 1
|
|
var rotate_kind := doc.gizmo_kind == 2
|
|
if rotate_kind:
|
|
# rotate: three rings, no center handle (there is
|
|
# no uniform/free rotation - each ring is a single
|
|
# clean axis, same as Blender/Godot's own gizmo)
|
|
_dot(c, 4.0, Color(1, 1, 1, 0.55))
|
|
for rg in _gizmo_rings():
|
|
var hov: bool = (op == Op.GIZMO and gizmo_axis == rg["axis"]) \
|
|
or (op == Op.NONE and _gizmo_hit(mouse_px) == rg["axis"])
|
|
var col: Color = rg["col"]
|
|
var pts: PackedVector3Array = rg["pts_w"]
|
|
for s in pts.size() - 1:
|
|
_quad_line(pts[s], pts[s + 1], 4.0 if hov else 2.2, col)
|
|
else:
|
|
# center chip: MOVE shows a ring (direct element
|
|
# drag); SCALE shows a cube = uniform-scale handle
|
|
if scale_kind:
|
|
_cube(c, _wpp() * 5.0, Color(1, 1, 1, 0.85))
|
|
else:
|
|
_dot(c, 8.0, Color(0, 0, 0, 0.6))
|
|
_dot(c, 5.5, Color(1, 1, 1, 0.35))
|
|
for a in _gizmo_axes():
|
|
var hov: bool = (op == Op.GIZMO and gizmo_axis == a["axis"]) \
|
|
or (op == Op.NONE and _gizmo_hit(mouse_px) == a["axis"])
|
|
var col: Color = a["col"]
|
|
_quad_line(c, a["tip_w"], 4.0 if hov else 2.6, col)
|
|
var tip: Vector3 = a["tip_w"]
|
|
if scale_kind:
|
|
# axis handle: a solid CUBE tip (scale affordance)
|
|
_cube(tip, _wpp() * (8.0 if hov else 5.5), col)
|
|
else:
|
|
# cone head (move affordance)
|
|
var dn: Vector3 = (tip - c).normalized()
|
|
var perp := dn.cross(_cam_up()).normalized()
|
|
if perp.length() < 0.1:
|
|
perp = dn.cross(Vector3.UP).normalized()
|
|
var hr := _wpp() * 7.0
|
|
var hl := _wpp() * 15.0
|
|
var base := tip - dn * hl
|
|
for i in 7:
|
|
var a0 := PI * i / 3.0
|
|
var a1 := PI * (i + 1) / 3.0
|
|
var p1 := base + (perp * cos(a0) + _cam_up() * sin(a0)) * hr
|
|
var p2 := base + (perp * cos(a1) + _cam_up() * sin(a1)) * hr
|
|
_ov_tris.append([tip, col])
|
|
_ov_tris.append([p1, col])
|
|
_ov_tris.append([p2, col])
|
|
|
|
# Ctrl+drag marquee rectangle: a world quad at a fixed distance in
|
|
# front of the camera (the overlay material ignores depth)
|
|
if op == Op.MARQUEE:
|
|
_draw_marquee()
|
|
|
|
# brush / eraser ring cursor: shows the stamp footprint on the mesh
|
|
# surface (paint submode only - UV editing has no brush)
|
|
if doc.mode == PdDocument.Mode.TEXTURE and not doc.tex_edit \
|
|
and (doc.paint_tool == "Brush" \
|
|
or doc.paint_tool == "Eraser") \
|
|
and paint_hover.get("kind", "") == "face":
|
|
var bcol := Color(doc.paint_color.lightened(0.25), 0.95)
|
|
_ring(paint_hover["point"], paint_hover.get("normal", Vector3.UP),
|
|
doc.brush_size, 2.2, bcol)
|
|
_dot(paint_hover["point"], 2.5, Color(1, 1, 1, 0.85))
|
|
if doc.paint_tool == "Eraser":
|
|
_ring(paint_hover["point"],
|
|
paint_hover.get("normal", Vector3.UP),
|
|
doc.brush_size * 0.8, 1.2, Color(1, 1, 1, 0.5))
|
|
|
|
# pixel-art cursor: shows the exact world cell that will be painted
|
|
if doc.mode == PdDocument.Mode.TEXTURE and not doc.tex_edit \
|
|
and doc.paint_tool == "Pixel" \
|
|
and paint_hover.get("kind", "") == "face":
|
|
_pixel_cell_cursor(paint_hover["point"],
|
|
paint_hover.get("normal", Vector3.UP))
|
|
|
|
# picker / replace crosshair cursor
|
|
if doc.mode == PdDocument.Mode.TEXTURE and not doc.tex_edit \
|
|
and (doc.paint_tool == "Picker" \
|
|
or doc.paint_tool == "Replace") \
|
|
and paint_hover.get("kind", "") == "face":
|
|
var pcol := Color(doc.paint_color.lightened(0.25), 0.95)
|
|
_ring(paint_hover["point"],
|
|
paint_hover.get("normal", Vector3.UP),
|
|
maxf(0.06, doc.brush_size * 0.25), 1.6, pcol)
|
|
_dot(paint_hover["point"], 2.5, Color(1, 1, 1, 0.9))
|
|
if doc.paint_tool == "Replace":
|
|
_ring(paint_hover["point"],
|
|
paint_hover.get("normal", Vector3.UP),
|
|
maxf(0.09, doc.brush_size * 0.4), 1.0,
|
|
Color(1, 1, 1, 0.5))
|
|
|
|
# live doodle preview while dragging in Draw mode
|
|
if op == Op.SHAPE and not shape_pts.is_empty():
|
|
_draw_shape_preview_3d()
|
|
|
|
_ov_flush()
|
|
|
|
|
|
## The live Ctrl+drag selection rectangle, projected onto a camera-facing
|
|
## plane at half the orbit distance.
|
|
func _draw_marquee() -> void:
|
|
var r := Rect2(marquee_start, marquee_now - marquee_start).abs()
|
|
if r.size.length() < 2.0:
|
|
return
|
|
var d := maxf(0.5, dist * 0.5)
|
|
var c := [
|
|
r.position,
|
|
Vector2(r.end.x, r.position.y),
|
|
r.end,
|
|
Vector2(r.position.x, r.end.y)]
|
|
var w := PackedVector3Array()
|
|
for sp in c:
|
|
w.append(_ray(sp)[0] + _ray(sp)[1] * d)
|
|
var col := Color(PdDocument.ACCENT.r, PdDocument.ACCENT.g,
|
|
PdDocument.ACCENT.b, 0.16)
|
|
for idx in [[0, 1, 2], [0, 2, 3]]:
|
|
for k in 3:
|
|
_ov_tris.append([w[idx[k]], col])
|
|
var line := Color(PdDocument.ACCENT.r, PdDocument.ACCENT.g,
|
|
PdDocument.ACCENT.b, 0.85)
|
|
for i in 4:
|
|
_line3(w[i], w[(i + 1) % 4], line)
|
|
|
|
|
|
## The world-space texel block under the cursor, drawn as a quad on the
|
|
## face surface - what you see is exactly what the Pixel tool paints.
|
|
func _pixel_cell_cursor(point: Vector3, normal: Vector3) -> void:
|
|
var hit := paint_hover
|
|
if hit.get("kind", "") != "face":
|
|
return
|
|
var o := doc.obj(hit.get("obj", -1))
|
|
if o == null:
|
|
return
|
|
var fi: int = hit["idx"]
|
|
if fi < 0 or fi >= o.nfaces():
|
|
return
|
|
var n := maxi(1, int(doc.pixel_px))
|
|
var t := o.world_to_texel(fi, point)
|
|
var tl := Vector2(int(floorf(t.x - (n - 1) * 0.5)),
|
|
int(floorf(t.y - (n - 1) * 0.5)))
|
|
var nrm := normal.normalized() if normal.length() > 0.01 else Vector3.UP
|
|
var lift := nrm * 0.012
|
|
var col := Color(doc.paint_color.lightened(0.25), 0.95)
|
|
var corners := [
|
|
o.texel_to_world(fi, tl.x, tl.y),
|
|
o.texel_to_world(fi, tl.x + n, tl.y),
|
|
o.texel_to_world(fi, tl.x + n, tl.y + n),
|
|
o.texel_to_world(fi, tl.x, tl.y + n)]
|
|
for i in 4:
|
|
_quad_line(corners[i] + lift, corners[(i + 1) % 4] + lift, 1.8, col)
|
|
|
|
|
|
## A flat ring (annulus) lying on the surface around `center`, oriented to
|
|
## `normal` - the visible footprint of the Brush tool.
|
|
func _ring(center: Vector3, normal: Vector3, radius: float, px: float, col: Color) -> void:
|
|
var n := normal.normalized() if normal.length() > 0.01 else Vector3.UP
|
|
var t := Vector3.UP if absf(n.dot(Vector3.UP)) < 0.9 else Vector3.RIGHT
|
|
var u := n.cross(t).normalized()
|
|
var v := n.cross(u).normalized()
|
|
var c := center + n * 0.01
|
|
var w := maxf(radius * 0.03, _wpp() * px * 0.5)
|
|
var seg := 44
|
|
overlay_mesh.surface_begin(Mesh.PRIMITIVE_TRIANGLES)
|
|
for i in seg:
|
|
var a0 := TAU * i / seg
|
|
var a1 := TAU * (i + 1) / seg
|
|
var i0 := c + (u * cos(a0) + v * sin(a0)) * (radius - w)
|
|
var i1 := c + (u * cos(a1) + v * sin(a1)) * (radius - w)
|
|
var o0 := c + (u * cos(a0) + v * sin(a0)) * (radius + w)
|
|
var o1 := c + (u * cos(a1) + v * sin(a1)) * (radius + w)
|
|
for vtx in [i0, o0, o1, i0, o1, i1]:
|
|
overlay_mesh.surface_set_color(col)
|
|
overlay_mesh.surface_add_vertex(vtx)
|
|
overlay_mesh.surface_end()
|
|
|
|
|
|
func _edge_touches(vi: int) -> bool:
|
|
for e in doc.sel_edges:
|
|
if e.x == vi or e.y == vi:
|
|
return true
|
|
return false
|
|
|
|
|
|
func _face_touches(o: PdPolyObj, vi: int) -> bool:
|
|
for fi in doc.sel_faces:
|
|
if o.face_loop(fi).has(vi):
|
|
return true
|
|
return false
|
|
|
|
|
|
# ------------------------------------------------------- scale gizmo ---
|
|
## Draw a small 3D cube (12 edges) centered at `c` with half-size `h`.
|
|
func _cube(c: Vector3, h: float, col: Color) -> void:
|
|
var p := []
|
|
for i in 8:
|
|
var v := c
|
|
v.x += h if (i & 1) != 0 else -h
|
|
v.y += h if (i & 2) != 0 else -h
|
|
v.z += h if (i & 4) != 0 else -h
|
|
p.append(v)
|
|
var lines := [[0, 1], [2, 3], [4, 5], [6, 7], [0, 2], [1, 3],
|
|
[4, 6], [5, 7], [0, 4], [1, 5], [2, 6], [3, 7]]
|
|
for l in lines:
|
|
_quad_line(p[l[0]], p[l[1]], 1.8, col)
|
|
|
|
|
|
# ------------------------------------------------------------ picking ---
|
|
func _ray(mp: Vector2) -> Array:
|
|
return [cam.project_ray_origin(mp), cam.project_ray_normal(mp)]
|
|
|
|
|
|
func _pick_vert(mp: Vector2) -> Dictionary:
|
|
var best := {}
|
|
var best_d := 10.0
|
|
for oi in doc.objects.size():
|
|
var o := doc.obj(oi)
|
|
if o == null or not o.is_visible or o.is_locked:
|
|
continue
|
|
for vi in o.nverts():
|
|
var w := o.wvert(vi)
|
|
if cam.is_position_behind(w):
|
|
continue
|
|
var sp := _project(w)
|
|
if sp.x < -20 or sp.y < -20 or sp.x > sub.size.x + 20 or sp.y > sub.size.y + 20:
|
|
continue
|
|
var d := mp.distance_to(sp)
|
|
if d < best_d:
|
|
best_d = d
|
|
best = {"obj": oi, "idx": vi, "kind": "vert"}
|
|
return best
|
|
|
|
|
|
func _pick_edge(mp: Vector2) -> Dictionary:
|
|
var best := {}
|
|
var best_d := 7.0
|
|
for oi in doc.objects.size():
|
|
var o := doc.obj(oi)
|
|
if o == null or not o.is_visible or o.is_locked:
|
|
continue
|
|
for e in o.edges():
|
|
var a := o.wvert(e.x)
|
|
var b := o.wvert(e.y)
|
|
if cam.is_position_behind(a) and cam.is_position_behind(b):
|
|
continue
|
|
var d := PdGeo.dist_to_seg(mp, _project(a), _project(b))
|
|
if d < best_d:
|
|
best_d = d
|
|
best = {"obj": oi, "idx": e, "kind": "edge"}
|
|
return best
|
|
|
|
|
|
func _pick_face(mp: Vector2) -> Dictionary:
|
|
var r := _ray(mp)
|
|
var best := {}
|
|
var best_t := INF
|
|
for oi in doc.objects.size():
|
|
var o := doc.obj(oi)
|
|
if o == null or not o.is_visible or o.is_locked:
|
|
continue
|
|
for fi in o.nfaces():
|
|
# cached robust triangulation: matches what renders
|
|
# in both viewports, self-intersecting loops included
|
|
var tr := o.face_triangulation(fi)
|
|
var pts: PackedVector3Array = tr["verts"]
|
|
var tris: PackedInt32Array = tr["tris"]
|
|
if pts.size() < 3 or tris.size() < 3:
|
|
continue
|
|
for t in range(0, tris.size() - 2, 3):
|
|
var tt := PdGeo.ray_tri(r[0], r[1], pts[tris[t]], pts[tris[t + 1]], pts[tris[t + 2]])
|
|
if tt > 0.0 and tt < best_t:
|
|
best_t = tt
|
|
best = {"obj": oi, "idx": fi, "kind": "face", "dist": tt,
|
|
"point": r[0] + r[1] * tt,
|
|
"normal": o.face_normal_w(fi)}
|
|
return best
|
|
|
|
|
|
## A click that selects nothing because the shape under the cursor is
|
|
## locked would otherwise look like a dead click: say why.
|
|
func _explain_locked_click(mp: Vector2) -> void:
|
|
if not doc.has_visible_locked() or _pick_object(mp) >= 0:
|
|
return
|
|
var r := _ray(mp)
|
|
var best_t := INF
|
|
var best_name := ""
|
|
for oi in doc.objects.size():
|
|
var o := doc.obj(oi)
|
|
if o == null or not o.is_visible or not o.is_locked:
|
|
continue
|
|
for fi in o.nfaces():
|
|
var tr := o.face_triangulation(fi)
|
|
var pts: PackedVector3Array = tr["verts"]
|
|
var tris: PackedInt32Array = tr["tris"]
|
|
if pts.size() < 3 or tris.size() < 3:
|
|
continue
|
|
for t in range(0, tris.size() - 2, 3):
|
|
var tt := PdGeo.ray_tri(r[0], r[1], pts[tris[t]], pts[tris[t + 1]], pts[tris[t + 2]])
|
|
if tt > 0.0 and tt < best_t:
|
|
best_t = tt
|
|
best_name = o.obj_name
|
|
if best_name != "":
|
|
doc.warn("%s is locked. Unlock it (padlock in the hierarchy) to edit it." % best_name)
|
|
|
|
|
|
func _pick_object(mp: Vector2) -> int:
|
|
var f := _pick_face(mp)
|
|
if not f.is_empty():
|
|
return f["obj"]
|
|
var v := _pick_vert(mp)
|
|
if not v.is_empty():
|
|
return v["obj"]
|
|
var e := _pick_edge(mp)
|
|
if not e.is_empty():
|
|
return e["obj"]
|
|
return -1
|
|
|
|
|
|
func _gizmo_hit(mp: Vector2) -> int:
|
|
if not doc.gizmo_on or doc.mode == PdDocument.Mode.DRAW \
|
|
or doc.mode == PdDocument.Mode.TEXTURE \
|
|
or doc.gizmo_anchor_w() == Vector3.INF:
|
|
return -1
|
|
if doc.gizmo_kind == 2:
|
|
var best := -1
|
|
var best_d := 10.0
|
|
for rg in _gizmo_rings():
|
|
var pts: PackedVector2Array = rg["pts_s"]
|
|
for s in pts.size() - 1:
|
|
var d := PdGeo.dist_to_seg(mp, pts[s], pts[s + 1])
|
|
if d < best_d:
|
|
best_d = d
|
|
best = rg["axis"]
|
|
return best
|
|
var c := doc.gizmo_anchor_w()
|
|
var cp := _project(c)
|
|
if mp.distance_to(cp) < 10.0:
|
|
# -2 = center zone. SCALE gizmo: the center cube is the
|
|
# uniform-scale handle. MOVE gizmo: fall through to direct
|
|
# element dragging (the press handler checks gizmo_kind).
|
|
return -2
|
|
for a in _gizmo_axes():
|
|
if PdGeo.dist_to_seg(mp, cp, a["to"]) < 10.0:
|
|
return a["axis"]
|
|
return -1
|
|
|
|
|
|
func _update_hover(mp: Vector2) -> void:
|
|
if doc.mode == PdDocument.Mode.TEXTURE:
|
|
paint_hover = _pick_face(mp)
|
|
return
|
|
match doc.mode:
|
|
PdDocument.Mode.VERTEX:
|
|
_set_hover(_pick_vert(mp))
|
|
PdDocument.Mode.EDGE:
|
|
_set_hover(_pick_edge(mp))
|
|
PdDocument.Mode.FACE:
|
|
_set_hover(_pick_face(mp))
|
|
PdDocument.Mode.OBJECT:
|
|
var oi := _pick_object(mp)
|
|
_set_hover({} if oi < 0 else {"obj": oi, "kind": "obj"})
|
|
_:
|
|
_set_hover({})
|
|
|
|
|
|
func _set_hover(h: Dictionary) -> void:
|
|
if hover != h:
|
|
hover = h
|
|
|
|
|
|
# -------------------------------------------------------------- input ---
|
|
func _gui_input(event: InputEvent) -> void:
|
|
if event is InputEventMouseButton:
|
|
_mi(event)
|
|
elif event is InputEventMouseMotion:
|
|
_mm(event)
|
|
|
|
|
|
func _mi(e: InputEventMouseButton) -> void:
|
|
var mp := e.position
|
|
match e.button_index:
|
|
MOUSE_BUTTON_MIDDLE:
|
|
if e.pressed:
|
|
# MMB orbits; Shift+MMB pans
|
|
op = Op.PAN if Input.is_key_pressed(KEY_SHIFT) else Op.ORBIT
|
|
orbit_btn = MOUSE_BUTTON_MIDDLE
|
|
pan_last = mp
|
|
accept_event()
|
|
elif op == Op.ORBIT or op == Op.PAN:
|
|
op = Op.NONE
|
|
accept_event()
|
|
MOUSE_BUTTON_RIGHT:
|
|
if e.pressed:
|
|
# right-click cancels an in-progress doodle
|
|
if op == Op.SHAPE:
|
|
op = Op.NONE
|
|
shape_pts = PackedVector2Array()
|
|
accept_event()
|
|
return
|
|
op = Op.PAN
|
|
pan_last = mp
|
|
press_px = mp
|
|
accept_event()
|
|
elif op == Op.PAN:
|
|
# a bare right-click opens the mesh-ops CONTEXT
|
|
# MENU in the edit modes; a right-DRAG pans the
|
|
# camera. In the texture editor right-click does
|
|
# nothing here - erasing is the dedicated Eraser
|
|
# TOOL now, and the canvas has its own gestures.
|
|
if doc.mode != PdDocument.Mode.TEXTURE \
|
|
and press_px.distance_to(mp) < 4.0:
|
|
_open_ctx_menu(mp)
|
|
op = Op.NONE
|
|
accept_event()
|
|
MOUSE_BUTTON_LEFT:
|
|
if e.pressed:
|
|
_press_left(mp)
|
|
else:
|
|
_release_left(mp)
|
|
MOUSE_BUTTON_WHEEL_UP:
|
|
if e.pressed:
|
|
dist = clampf(dist * 0.9, 0.4, 120.0)
|
|
_update_cam()
|
|
accept_event()
|
|
MOUSE_BUTTON_WHEEL_DOWN:
|
|
if e.pressed:
|
|
dist = clampf(dist * 1.1, 0.4, 120.0)
|
|
_update_cam()
|
|
accept_event()
|
|
|
|
|
|
func _press_left(mp: Vector2) -> void:
|
|
press_px = mp
|
|
press_moved = false
|
|
grab_focus()
|
|
# Alt+LMB orbits in every mode - the way to turn the camera while
|
|
# a doodle or paint gesture owns plain LMB
|
|
if Input.is_key_pressed(KEY_ALT):
|
|
op = Op.ORBIT
|
|
orbit_btn = MOUSE_BUTTON_LEFT
|
|
pan_last = mp
|
|
accept_event()
|
|
return
|
|
# Ctrl+LMB drag = rectangle multi-select in the 3D modeler
|
|
# viewports (Draw and the texture editor keep their own gestures)
|
|
if Input.is_key_pressed(KEY_CTRL) \
|
|
and doc.mode != PdDocument.Mode.DRAW \
|
|
and doc.mode != PdDocument.Mode.TEXTURE:
|
|
op = Op.MARQUEE
|
|
marquee_start = mp
|
|
marquee_now = mp
|
|
accept_event()
|
|
return
|
|
var gax := _gizmo_hit(mp)
|
|
var center_sel := doc.sel_center_w()
|
|
if (gax >= 0 or gax == -2) and center_sel != Vector3.INF:
|
|
op = Op.GIZMO
|
|
doc.begin_gesture()
|
|
gizmo_axis = gax
|
|
_gizmo_label = "Scale selection" if doc.gizmo_kind == 1 \
|
|
else ("Rotate selection" if doc.gizmo_kind == 2 else "Move selection")
|
|
move_before = doc.snapshot()
|
|
_capture_origs()
|
|
if doc.gizmo_kind == 1:
|
|
# SCALE: freeze the arm length and (for uniform) the
|
|
# cursor distance from the center so the factor maps
|
|
# 1:1 to the drag and never feeds back on itself
|
|
gizmo_ref_len = maxf(0.0001, GIZMO_LEN_PX * _wpp())
|
|
if gax == -2:
|
|
var r0 := _ray(mp)
|
|
var pl := Plane(-_cam_fwd(), center_sel)
|
|
var hit0 = pl.intersects_ray(r0[0], r0[1])
|
|
gizmo_d0 = maxf(0.0001, (hit0 as Vector3)
|
|
.distance_to(center_sel) if hit0 != null \
|
|
else gizmo_ref_len)
|
|
else:
|
|
var ax0 := _axis_vec(gax)
|
|
var r1 := _ray(mp)
|
|
gizmo_t0 = PdGeo.closest_ray_line(
|
|
r1[0], r1[1], center_sel, ax0)["t_line"]
|
|
elif doc.gizmo_kind == 2:
|
|
# ROTATE: freeze the ring's own plane basis and the
|
|
# cursor's starting angle within it - every later
|
|
# frame measures the angle against THIS, never the
|
|
# live (already-rotated) selection, for the same
|
|
# anti-jitter reason move/scale freeze their refs.
|
|
gizmo_rot_axis = _axis_vec(gax)
|
|
var uv := _ring_basis(gizmo_rot_axis)
|
|
gizmo_rot_u = uv[0]
|
|
gizmo_rot_v = uv[1]
|
|
gizmo_rot_origs = _capture_rotate_origs()
|
|
var rpl := Plane(gizmo_rot_axis, center_sel)
|
|
var rr := _ray(mp)
|
|
var rhit = rpl.intersects_ray(rr[0], rr[1])
|
|
var rel0: Vector3 = (rhit as Vector3 - center_sel) if rhit != null else Vector3.ZERO
|
|
gizmo_angle0 = atan2(rel0.dot(gizmo_rot_v), rel0.dot(gizmo_rot_u))
|
|
gizmo_angle = 0.0
|
|
else:
|
|
var ax := _axis_vec(gax)
|
|
var r := _ray(mp)
|
|
# Freeze BOTH the reference point and the start parameter: every
|
|
# motion event is measured against this fixed line. Recomputing
|
|
# against the live (already moved) center fed the scalar back into
|
|
# itself and made the drag jitter in the 3D view.
|
|
gizmo_c0 = center_sel
|
|
gizmo_t0 = PdGeo.closest_ray_line(r[0], r[1], gizmo_c0, ax)["t_line"]
|
|
gizmo_scalar = 0.0
|
|
gizmo_c0 = center_sel
|
|
accept_event()
|
|
return
|
|
|
|
if doc.mode in [PdDocument.Mode.OBJECT, PdDocument.Mode.VERTEX,
|
|
PdDocument.Mode.EDGE, PdDocument.Mode.FACE]:
|
|
_explain_locked_click(mp)
|
|
|
|
match doc.mode:
|
|
PdDocument.Mode.DRAW:
|
|
op = Op.SHAPE
|
|
doc.begin_gesture()
|
|
_begin_shape(mp)
|
|
accept_event()
|
|
PdDocument.Mode.TEXTURE:
|
|
if doc.tex_edit:
|
|
# UV editing: NO brush is active - clicking a
|
|
# face just selects it so its island highlights
|
|
# in the texture canvas. Drag on empty space
|
|
# orbits; a plain click never deselects here.
|
|
var sel_hit := _pick_face(mp)
|
|
if sel_hit.is_empty():
|
|
op = Op.ORBIT
|
|
orbit_btn = MOUSE_BUTTON_LEFT
|
|
press_picked = false
|
|
else:
|
|
op = Op.ORBIT # drag orbits after the click
|
|
orbit_btn = MOUSE_BUTTON_LEFT
|
|
press_picked = true
|
|
# the face lives on the hit object: make
|
|
# THAT object active first, otherwise the
|
|
# selection is pruned to the previous
|
|
# active object and the click appears dead
|
|
doc.select_face_on(sel_hit["obj"],
|
|
sel_hit["idx"],
|
|
Input.is_key_pressed(KEY_SHIFT))
|
|
accept_event()
|
|
return
|
|
var hit := _pick_face(mp)
|
|
if hit.is_empty() and doc.paint_tool == "Brush":
|
|
hit = _pick_edge(mp)
|
|
if hit.is_empty():
|
|
# pressed on empty space: drag orbits, click
|
|
# does nothing
|
|
op = Op.ORBIT
|
|
orbit_btn = MOUSE_BUTTON_LEFT
|
|
else:
|
|
op = Op.PAINT
|
|
doc.begin_gesture()
|
|
move_before = doc.snapshot()
|
|
_last_paint_key = ""
|
|
doc.pixel_stroke_begin()
|
|
# clicking a face also SELECTS it, so its
|
|
# island highlights in the texture canvas
|
|
if hit.get("kind", "") == "face":
|
|
doc.select_face_on(hit["obj"], hit["idx"],
|
|
Input.is_key_pressed(KEY_SHIFT))
|
|
# FACE LOCK: remember the face the
|
|
# stroke started on (edge hits lock nothing)
|
|
var sfi := -1
|
|
if hit.get("kind", "") == "face" \
|
|
and hit.get("obj", -1) == doc.active_obj:
|
|
sfi = hit["idx"]
|
|
doc.paint_stroke_begin(doc.active_obj, sfi)
|
|
_paint_gesture(mp, false)
|
|
accept_event()
|
|
_:
|
|
var picked := _pick_element(mp)
|
|
if picked.is_empty():
|
|
# pressed on empty space: drag orbits, a
|
|
# plain click deselects (handled on release)
|
|
op = Op.ORBIT
|
|
orbit_btn = MOUSE_BUTTON_LEFT
|
|
else:
|
|
_apply_click_selection(picked)
|
|
op = Op.MOVE
|
|
doc.begin_gesture()
|
|
move_before = doc.snapshot()
|
|
_capture_origs()
|
|
# reference point: plane through selection center
|
|
var c := doc.sel_center_w()
|
|
if c == Vector3.INF:
|
|
move_valid = false
|
|
else:
|
|
var r := _ray(mp)
|
|
var plane := Plane(-_cam_fwd(), c)
|
|
var hit = plane.intersects_ray(r[0], r[1])
|
|
move_valid = hit != null
|
|
move_hit0 = hit if hit != null else Vector3.ZERO
|
|
accept_event()
|
|
|
|
|
|
func _axis_vec(i: int) -> Vector3:
|
|
match i:
|
|
1: return Vector3.UP
|
|
2: return Vector3(0, 0, 1)
|
|
return Vector3.RIGHT
|
|
|
|
|
|
func _pick_element(mp: Vector2) -> Dictionary:
|
|
match doc.mode:
|
|
PdDocument.Mode.VERTEX:
|
|
return _pick_vert(mp)
|
|
PdDocument.Mode.EDGE:
|
|
return _pick_edge(mp)
|
|
PdDocument.Mode.FACE:
|
|
return _pick_face(mp)
|
|
PdDocument.Mode.OBJECT:
|
|
var oi := _pick_object(mp)
|
|
return {} if oi < 0 else {"obj": oi, "idx": oi, "kind": "obj"}
|
|
return {}
|
|
|
|
|
|
func _apply_click_selection(picked: Dictionary) -> void:
|
|
var add := Input.is_key_pressed(KEY_SHIFT)
|
|
var oi: int = picked["obj"]
|
|
match doc.mode:
|
|
PdDocument.Mode.OBJECT:
|
|
if add:
|
|
doc.toggle_object(oi)
|
|
elif not doc.sel_objs.has(oi):
|
|
doc.select_object(oi)
|
|
# clicking an already-selected object KEEPS the whole
|
|
# multi-selection, so dragging it (gizmo on or off)
|
|
# moves every selected part
|
|
_:
|
|
if oi != doc.active_obj:
|
|
doc.active_obj = oi
|
|
doc.clear_element_sel()
|
|
match doc.mode:
|
|
PdDocument.Mode.VERTEX:
|
|
var vi: int = picked["idx"]
|
|
var arr := PackedInt32Array(doc.sel_verts)
|
|
if add:
|
|
if arr.has(vi):
|
|
arr.remove_at(arr.find(vi))
|
|
else:
|
|
arr.append(vi)
|
|
elif not arr.has(vi):
|
|
arr = PackedInt32Array([vi])
|
|
doc.select_verts(arr)
|
|
PdDocument.Mode.EDGE:
|
|
var ei: Vector2i = picked["idx"]
|
|
var arr := doc.sel_edges.duplicate()
|
|
if add:
|
|
if arr.has(ei):
|
|
arr.erase(ei)
|
|
else:
|
|
arr.append(ei)
|
|
elif not arr.has(ei):
|
|
arr = [ei]
|
|
doc.select_edges(arr)
|
|
PdDocument.Mode.FACE:
|
|
var fi: int = picked["idx"]
|
|
var arr := PackedInt32Array(doc.sel_faces)
|
|
if add:
|
|
if arr.has(fi):
|
|
arr.remove_at(arr.find(fi))
|
|
else:
|
|
arr.append(fi)
|
|
elif not arr.has(fi):
|
|
arr = PackedInt32Array([fi])
|
|
doc.select_faces(arr)
|
|
doc.notify_sel()
|
|
|
|
|
|
func _capture_origs() -> void:
|
|
move_orig = {}
|
|
move_orig_pos = {}
|
|
move_orig_scale = {}
|
|
if doc.mode == PdDocument.Mode.OBJECT:
|
|
for oi in doc.sel_objs:
|
|
var o := doc.obj(oi)
|
|
if o != null:
|
|
move_orig_pos[oi] = o.position
|
|
move_orig_scale[oi] = o.scale3
|
|
else:
|
|
var affected := doc.affected_verts()
|
|
for oi in affected:
|
|
var o := doc.obj(oi)
|
|
if o == null:
|
|
continue
|
|
var ids: PackedInt32Array = affected[oi]
|
|
var arr := PackedVector3Array()
|
|
arr.resize(ids.size())
|
|
for k in ids.size():
|
|
arr[k] = o.wvert(ids[k])
|
|
move_orig[oi] = {"verts": ids, "pos": arr}
|
|
if move_orig.is_empty() and doc.active_obj >= 0:
|
|
# element modes with NOTHING selected: the gizmo
|
|
# (e.g. after a hierarchy click) drags the whole
|
|
# active object instead
|
|
var o := doc.obj(doc.active_obj)
|
|
if o != null:
|
|
move_orig_pos[doc.active_obj] = o.position
|
|
move_orig_scale[doc.active_obj] = o.scale3
|
|
|
|
|
|
## Rotate always works on actual vertex positions (there is no per-object
|
|
## rotation property to write to - see apply_rotate_gizmo), so unlike
|
|
## _capture_origs, OBJECT mode here captures every vertex of each selected
|
|
## object rather than a frozen position/scale3, in the same {"verts",
|
|
## "pos"} shape the element-mode branch already uses.
|
|
func _capture_rotate_origs() -> Dictionary:
|
|
var out := {}
|
|
if doc.mode == PdDocument.Mode.OBJECT:
|
|
for oi in doc.sel_objs:
|
|
var o := doc.obj(oi)
|
|
if o == null:
|
|
continue
|
|
out[oi] = _whole_object_verts(o)
|
|
else:
|
|
var affected := doc.affected_verts()
|
|
for oi in affected:
|
|
var o := doc.obj(oi)
|
|
if o == null:
|
|
continue
|
|
var ids: PackedInt32Array = affected[oi]
|
|
var arr := PackedVector3Array()
|
|
arr.resize(ids.size())
|
|
for k in ids.size():
|
|
arr[k] = o.wvert(ids[k])
|
|
out[oi] = {"verts": ids, "pos": arr}
|
|
if out.is_empty() and doc.active_obj >= 0:
|
|
var o := doc.obj(doc.active_obj)
|
|
if o != null:
|
|
out[doc.active_obj] = _whole_object_verts(o)
|
|
return out
|
|
|
|
|
|
func _whole_object_verts(o: PdPolyObj) -> Dictionary:
|
|
var ids := PackedInt32Array()
|
|
var pos := PackedVector3Array()
|
|
ids.resize(o.nverts())
|
|
pos.resize(o.nverts())
|
|
for k in o.nverts():
|
|
ids[k] = k
|
|
pos[k] = o.wvert(k)
|
|
return {"verts": ids, "pos": pos}
|
|
|
|
|
|
func _apply_move_targets() -> void:
|
|
match op:
|
|
Op.MOVE:
|
|
if not move_valid:
|
|
return
|
|
var r := _ray(mouse_px)
|
|
var plane := Plane(-_cam_fwd(), move_hit0)
|
|
var hit = plane.intersects_ray(r[0], r[1])
|
|
if hit == null:
|
|
return
|
|
var delta: Vector3 = hit - move_hit0
|
|
if doc.mode == PdDocument.Mode.OBJECT:
|
|
for oi in move_orig_pos:
|
|
var o := doc.obj(oi)
|
|
if o == null:
|
|
continue
|
|
o.position = doc.snap_v3(move_orig_pos[oi] + delta) if doc.snap_enabled \
|
|
else move_orig_pos[oi] + delta
|
|
o.mark_geo_changed() # world-vert cache must follow
|
|
else:
|
|
for oi in move_orig:
|
|
var o := doc.obj(oi)
|
|
if o == null:
|
|
continue
|
|
var ids: PackedInt32Array = move_orig[oi]["verts"]
|
|
var orig: PackedVector3Array = move_orig[oi]["pos"]
|
|
for k in ids.size():
|
|
o.set_wvert(ids[k],
|
|
doc.snap_v3(orig[k] + delta) if doc.snap_enabled else orig[k] + delta)
|
|
Op.GIZMO:
|
|
if doc.gizmo_kind == 1:
|
|
# SCALE drag: factor from the frozen press-time
|
|
# reference - axis drags measure along the axis
|
|
# line, uniform (center cube) measures radial
|
|
# distance on the camera-facing plane
|
|
var factor := 1.0
|
|
if gizmo_axis == -2:
|
|
var r2 := _ray(mouse_px)
|
|
var pl2 := Plane(-_cam_fwd(), gizmo_c0)
|
|
var hit2 = pl2.intersects_ray(r2[0], r2[1])
|
|
if hit2 != null:
|
|
factor = (hit2 as Vector3) \
|
|
.distance_to(gizmo_c0) / gizmo_d0
|
|
else:
|
|
var ax2 := _axis_vec(gizmo_axis)
|
|
var r3 := _ray(mouse_px)
|
|
var t2: float = PdGeo.closest_ray_line(
|
|
r3[0], r3[1], gizmo_c0, ax2)["t_line"]
|
|
factor = 1.0 + (t2 - gizmo_t0) / gizmo_ref_len
|
|
if doc.snap_enabled:
|
|
factor = snappedf(factor, 0.05)
|
|
factor = clampf(factor, 0.001, 100.0)
|
|
var ax_apply := -1 if gizmo_axis == -2 else gizmo_axis
|
|
# the FROZEN press-time center: recomputing the
|
|
# center from the live (already snapped) verts
|
|
# each step made snap + scale jitter badly
|
|
if doc.mode == PdDocument.Mode.OBJECT or move_orig.is_empty():
|
|
doc.apply_scale_gizmo(ax_apply, factor,
|
|
move_orig_scale, gizmo_c0)
|
|
else:
|
|
doc.apply_scale_gizmo(ax_apply, factor,
|
|
move_orig, gizmo_c0)
|
|
elif doc.gizmo_kind == 2:
|
|
# ROTATE drag: angle of the cursor within the
|
|
# ring's own frozen plane, measured from the
|
|
# frozen press-time angle - never the live
|
|
# (already-rotated) selection, same anti-
|
|
# jitter rule as move/scale above.
|
|
var rpl := Plane(gizmo_rot_axis, gizmo_c0)
|
|
var rr := _ray(mouse_px)
|
|
var rhit = rpl.intersects_ray(rr[0], rr[1])
|
|
if rhit != null:
|
|
var rel: Vector3 = (rhit as Vector3) - gizmo_c0
|
|
var ang := atan2(rel.dot(gizmo_rot_v), rel.dot(gizmo_rot_u))
|
|
var delta := wrapf(ang - gizmo_angle0, -PI, PI)
|
|
if doc.snap_enabled:
|
|
delta = snappedf(delta, deg_to_rad(15.0))
|
|
gizmo_angle = delta
|
|
doc.apply_rotate_gizmo(gizmo_rot_axis, delta,
|
|
gizmo_rot_origs, gizmo_c0)
|
|
else:
|
|
var ax := _axis_vec(gizmo_axis)
|
|
var r := _ray(mouse_px)
|
|
# measured against the FROZEN press-time line, never
|
|
# the live selection center (which moves with the
|
|
# drag) - this is the anti-jitter fix
|
|
var t: float = PdGeo.closest_ray_line(r[0], r[1], gizmo_c0, ax)["t_line"]
|
|
gizmo_scalar = t - gizmo_t0
|
|
if doc.snap_enabled and doc.grid_size > 0.001:
|
|
gizmo_scalar = snappedf(gizmo_scalar, doc.grid_size)
|
|
if doc.mode == PdDocument.Mode.OBJECT or move_orig.is_empty():
|
|
for oi in move_orig_pos:
|
|
var o := doc.obj(oi)
|
|
if o != null:
|
|
o.position = move_orig_pos[oi] + ax * gizmo_scalar
|
|
o.mark_geo_changed() # world-vert cache must follow
|
|
else:
|
|
for oi in move_orig:
|
|
var o := doc.obj(oi)
|
|
if o == null:
|
|
continue
|
|
var ids: PackedInt32Array = move_orig[oi]["verts"]
|
|
var orig: PackedVector3Array = move_orig[oi]["pos"]
|
|
for k in ids.size():
|
|
o.set_wvert(ids[k], orig[k] + ax * gizmo_scalar)
|
|
doc.notify_doc()
|
|
|
|
|
|
func _mm(e: InputEventMouseMotion) -> void:
|
|
mouse_px = e.position
|
|
if e.relative.length() > 1.0:
|
|
press_moved = true
|
|
match op:
|
|
Op.ORBIT:
|
|
yaw -= e.relative.x * 0.008
|
|
pitch = clampf(pitch + e.relative.y * 0.008, -1.55, 1.55)
|
|
_update_cam()
|
|
Op.PAN:
|
|
var k := _wpp()
|
|
target -= _cam_right() * e.relative.x * k
|
|
target += _cam_up() * e.relative.y * k
|
|
_update_cam()
|
|
Op.MOVE, Op.GIZMO:
|
|
_apply_move_targets()
|
|
Op.PAINT:
|
|
_paint_gesture(e.position, false)
|
|
Op.SHAPE:
|
|
if doc.draw_kind == "Freehand":
|
|
if shape_pts.is_empty() \
|
|
or shape_pts[shape_pts.size() - 1].distance_to(e.position) > 3.0:
|
|
shape_pts.append(e.position)
|
|
else:
|
|
shape_pts = PackedVector2Array([press_px, e.position])
|
|
Op.MARQUEE:
|
|
marquee_now = e.position
|
|
Op.NONE:
|
|
if mouse_in:
|
|
_update_hover(e.position)
|
|
|
|
|
|
func _release_left(mp: Vector2) -> void:
|
|
var was_gesture := op == Op.MOVE or op == Op.GIZMO \
|
|
or op == Op.SHAPE or op == Op.PAINT
|
|
match op:
|
|
Op.MOVE, Op.GIZMO:
|
|
doc.commit(_gizmo_label, move_before)
|
|
doc.notify_sel()
|
|
Op.SHAPE:
|
|
_finish_shape_3d()
|
|
Op.MARQUEE:
|
|
_marquee_select(mp)
|
|
Op.PAINT:
|
|
pass # painting happens on press + drag sweeps
|
|
Op.ORBIT:
|
|
# a bare LEFT click on empty space deselects (the
|
|
# drag itself was the orbit) - but never right after
|
|
# a UV-edit click that just SELECTED a face
|
|
if orbit_btn == MOUSE_BUTTON_LEFT and not press_moved \
|
|
and not press_picked \
|
|
and not Input.is_key_pressed(KEY_SHIFT):
|
|
doc.clear_all_sel()
|
|
_:
|
|
pass
|
|
doc.paint_stroke_end() # face-lock stroke memory (harmless if none)
|
|
if was_gesture:
|
|
doc.end_gesture()
|
|
op = Op.NONE
|
|
move_before = []
|
|
shape_pts = PackedVector2Array()
|
|
_last_paint_key = ""
|
|
press_picked = false
|
|
orbit_btn = 0
|
|
press_moved = false
|
|
marquee_start = Vector2.ZERO
|
|
marquee_now = Vector2.ZERO
|
|
|
|
|
|
## Ctrl+drag rectangle multi-select: every element whose screen position
|
|
## falls inside the marquee is selected. Shift adds to the existing
|
|
## selection, otherwise the rect replaces it.
|
|
func _marquee_select(mp: Vector2) -> void:
|
|
var r := Rect2(marquee_start, marquee_now - marquee_start).abs()
|
|
if r.size.length() < 4.0:
|
|
return
|
|
var add := Input.is_key_pressed(KEY_SHIFT)
|
|
match doc.mode:
|
|
PdDocument.Mode.OBJECT:
|
|
var ids := PackedInt32Array(doc.sel_objs) if add \
|
|
else PackedInt32Array()
|
|
for oi in doc.objects.size():
|
|
var o := doc.obj(oi)
|
|
if o == null or not o.is_visible or o.is_locked:
|
|
continue
|
|
for vi in o.nverts():
|
|
if r.has_point(_project(o.wvert(vi))):
|
|
if not ids.has(oi):
|
|
ids.append(oi)
|
|
break
|
|
doc.sel_objs = ids
|
|
doc.active_obj = ids[ids.size() - 1] \
|
|
if not ids.is_empty() else -1
|
|
doc.notify_sel()
|
|
_:
|
|
var o := doc.active()
|
|
if o == null:
|
|
return
|
|
match doc.mode:
|
|
PdDocument.Mode.VERTEX:
|
|
var arr := PackedInt32Array(doc.sel_verts) if add \
|
|
else PackedInt32Array()
|
|
for vi in o.nverts():
|
|
if r.has_point(_project(o.wvert(vi))):
|
|
arr.append(vi)
|
|
doc.select_verts(arr)
|
|
PdDocument.Mode.EDGE:
|
|
var arr: Array = doc.sel_edges.duplicate() if add else []
|
|
for e in o.edges():
|
|
if r.has_point(_project(o.wvert(e.x))) \
|
|
or r.has_point(_project(o.wvert(e.y))):
|
|
if not arr.has(e):
|
|
arr.append(e)
|
|
doc.select_edges(arr)
|
|
PdDocument.Mode.FACE:
|
|
var arr := PackedInt32Array(doc.sel_faces) if add \
|
|
else PackedInt32Array()
|
|
for fi in o.nfaces():
|
|
var pts := o.face_pts_w(fi)
|
|
if pts.size() < 3:
|
|
continue
|
|
if r.has_point(_project(PdGeo.face_center(pts))):
|
|
arr.append(fi)
|
|
doc.select_faces(arr)
|
|
|
|
|
|
## Paint one hit under the cursor, honoring the active paint tool. During a
|
|
## sweep, repeats in the same spot are skipped: face tools key on the face
|
|
## (or pixel cell), the Brush keys on the hit point quantized to half the
|
|
## brush radius so a drag paints a continuous stroke.
|
|
func _paint_gesture(mp: Vector2, erase: bool) -> void:
|
|
var hit := _pick_face(mp)
|
|
if hit.is_empty() and (doc.paint_tool == "Brush" \
|
|
or doc.paint_tool == "Eraser"):
|
|
hit = _pick_edge(mp)
|
|
paint_hover = hit
|
|
if hit.is_empty():
|
|
return
|
|
var key := _paint_key(hit)
|
|
if key != "" and key == _last_paint_key:
|
|
return
|
|
_last_paint_key = key
|
|
doc.paint_apply(hit, erase)
|
|
|
|
|
|
func _paint_key(hit: Dictionary) -> String:
|
|
var o := doc.obj(hit.get("obj", -1))
|
|
if o == null:
|
|
return ""
|
|
if doc.paint_tool == "Brush" and hit.get("kind", "") == "face":
|
|
var p: Vector3 = doc.brush_point(hit)
|
|
var step := maxf(doc.brush_size * 0.5, 0.05)
|
|
var q := Vector3i((p / step).floor())
|
|
return "b%d_%d_%d" % [q.x, q.y, q.z]
|
|
if doc.paint_tool == "Pixel" and hit.get("kind", "") == "face":
|
|
# key on theTEXEL BLOCK under the cursor so a drag paints a
|
|
# trail of blocks instead of re-stamping the same one
|
|
var fi: int = hit["idx"]
|
|
if fi < 0 or fi >= o.nfaces():
|
|
return ""
|
|
var p: Vector3 = hit.get("point",
|
|
PdGeo.face_center(o.face_pts_w(fi)))
|
|
var t := o.world_to_texel(fi, p)
|
|
var n := maxi(1, int(doc.pixel_px))
|
|
var tl := Vector2i(int(floorf(t.x - (n - 1) * 0.5)),
|
|
int(floorf(t.y - (n - 1) * 0.5)))
|
|
return "px%d_%d_%d_%d" % [hit["obj"], fi, tl.x, tl.y]
|
|
return "%s_%s" % [hit["obj"], hit["idx"]]
|
|
|
|
|
|
func paint_click_3d(mp: Vector2, erase: bool) -> void:
|
|
_last_paint_key = ""
|
|
_paint_gesture(mp, erase)
|
|
|
|
|
|
# --------------------------------------------------- 3D shape drawing ---
|
|
## Start a doodle on the plane under the cursor: a mesh face if one is
|
|
## hovered, otherwise a horizontal plane through the selection (or the
|
|
## ground). The plane stays fixed for the whole gesture.
|
|
func _begin_shape(mp: Vector2) -> void:
|
|
shape_pts = PackedVector2Array([mp])
|
|
var hit := _pick_face(mp)
|
|
if not hit.is_empty():
|
|
shape_plane_n = (hit.get("normal", Vector3.UP) as Vector3).normalized()
|
|
shape_plane_p = hit["point"]
|
|
else:
|
|
shape_plane_n = Vector3.UP
|
|
var c := doc.sel_center_w()
|
|
shape_plane_p = Vector3(0.0, c.y, 0.0) if c != Vector3.INF \
|
|
else Vector3.ZERO
|
|
if shape_plane_n.length() < 0.5:
|
|
shape_plane_n = Vector3.UP
|
|
if shape_plane_n.dot(_cam_fwd()) > 0.0:
|
|
shape_plane_n = -shape_plane_n
|
|
# basis: u = screen-right on the plane, v = screen-down on the plane
|
|
var up := _cam_up() - shape_plane_n * _cam_up().dot(shape_plane_n)
|
|
if up.length() < 0.05:
|
|
up = Vector3.UP if absf(shape_plane_n.dot(Vector3.UP)) < 0.9 \
|
|
else Vector3.RIGHT
|
|
up = up - shape_plane_n * up.dot(shape_plane_n)
|
|
shape_v = -up.normalized()
|
|
shape_u = shape_plane_n.cross(shape_v).normalized()
|
|
|
|
|
|
## Screen point -> 2D coords on the doodle plane (world units), or null
|
|
## when the ray misses (grazing angles).
|
|
func _shape_pt2(sp: Vector2):
|
|
var r := _ray(sp)
|
|
var pl := Plane(shape_plane_n, shape_plane_p.dot(shape_plane_n))
|
|
var hit = pl.intersects_ray(r[0], r[1])
|
|
if hit == null:
|
|
return null
|
|
var d: Vector3 = hit - shape_plane_p
|
|
return Vector2(d.dot(shape_u), d.dot(shape_v))
|
|
|
|
|
|
func _finish_shape_3d() -> void:
|
|
if shape_pts.is_empty():
|
|
return
|
|
match doc.draw_kind:
|
|
"Freehand":
|
|
if shape_pts.size() < 3:
|
|
doc.warn("Drag to draw an outline: hold the button and sketch the shape, then release.")
|
|
return
|
|
var simplified := PdGeo.rdp(shape_pts, 5.0)
|
|
if simplified.size() >= 3 \
|
|
and simplified[0].distance_to(simplified[simplified.size() - 1]) < 16.0:
|
|
simplified[simplified.size() - 1] = simplified[0]
|
|
if simplified.size() < 3:
|
|
doc.warn("That doodle was too small or too straight to make a shape. Sketch a larger closed outline.")
|
|
return
|
|
var pts := PackedVector3Array()
|
|
for sp in simplified:
|
|
var p2 = _shape_pt2(sp)
|
|
if p2 == null:
|
|
doc.warn("Aim the cursor at the grid or a surface to draw there.")
|
|
return
|
|
pts.append(doc.snap_v3(shape_plane_p
|
|
+ shape_u * p2.x + shape_v * p2.y))
|
|
doc.add_polygon(_orient_to_plane(pts), "Doodle")
|
|
"Torus":
|
|
_finish_torus_3d()
|
|
_:
|
|
if shape_pts.size() != 2:
|
|
doc.warn("Drag to size the shape: hold the button, move, then release.")
|
|
return
|
|
var r := Rect2(shape_pts[0], shape_pts[1] - shape_pts[0]).abs()
|
|
if Input.is_key_pressed(KEY_SHIFT):
|
|
var s := maxf(r.size.x, r.size.y)
|
|
r = Rect2(r.position, Vector2(s, s))
|
|
if r.size.length() < 6.0:
|
|
doc.warn("That shape is too small. Drag further to size it.")
|
|
return
|
|
var gen := _finish_gen(doc.draw_kind)
|
|
var pts := PackedVector3Array()
|
|
for gp in gen:
|
|
# normalized (0..1) generator -> screen -> plane
|
|
var sp := r.position + Vector2(gp.x * r.size.x, gp.y * r.size.y)
|
|
var p2 = _shape_pt2(sp)
|
|
if p2 == null:
|
|
doc.warn("Aim the cursor at the grid or a surface to draw there.")
|
|
return
|
|
pts.append(doc.snap_v3(shape_plane_p
|
|
+ shape_u * p2.x + shape_v * p2.y))
|
|
doc.add_polygon(_orient_to_plane(pts), doc.draw_kind)
|
|
|
|
|
|
## Normalized (0..1) outline used when FINISHING a 3D shape. It mirrors
|
|
## the live-preview generators exactly, so what the drag shows is what
|
|
## gets created (finishing a Rectangle through ngon_pts(4) would give a
|
|
## DIAMOND while the preview drew a rect).
|
|
func _finish_gen(kind: String) -> PackedVector2Array:
|
|
var out: PackedVector2Array
|
|
match kind:
|
|
"Circle":
|
|
out = _norm_ellipse3d(48)
|
|
"Triangle":
|
|
out = _norm_ngon3d(3)
|
|
"Pentagon":
|
|
out = _norm_ngon3d(5)
|
|
"Hexagon":
|
|
out = _norm_ngon3d(6)
|
|
"Star":
|
|
out = _norm_star3d()
|
|
_:
|
|
out = PackedVector2Array([Vector2(0, 0), Vector2(1, 0),
|
|
Vector2(1, 1), Vector2(0, 1)])
|
|
if out.size() > 1 and out[0].is_equal_approx(out[out.size() - 1]):
|
|
out.resize(out.size() - 1) # strip the closing repeat
|
|
return out
|
|
|
|
|
|
## Torus solid on the doodle plane: drag defines the outer radius.
|
|
func _finish_torus_3d() -> void:
|
|
if shape_pts.size() != 2:
|
|
doc.warn("Drag to size the torus: hold the button, move, then release.")
|
|
return
|
|
var r := Rect2(shape_pts[0], shape_pts[1] - shape_pts[0]).abs()
|
|
if Input.is_key_pressed(KEY_SHIFT):
|
|
var s := maxf(r.size.x, r.size.y)
|
|
r = Rect2(r.position, Vector2(s, s))
|
|
if r.size.length() < 12.0:
|
|
doc.warn("That torus is too small. Drag further to set its size.")
|
|
return
|
|
var c2 = _shape_pt2(r.get_center())
|
|
var e_top = _shape_pt2(r.position + Vector2(r.size.x * 0.5, 0))
|
|
var e_side = _shape_pt2(r.position + Vector2(r.size.x, r.size.y * 0.5))
|
|
if c2 == null or e_top == null or e_side == null:
|
|
return
|
|
# outer radius = the larger plane-space extent of the drag rect
|
|
var R: float = maxf((e_top as Vector2).distance_to(c2),
|
|
(e_side as Vector2).distance_to(c2))
|
|
if doc.snap_enabled and doc.grid_size > 0.001:
|
|
R = snappedf(R, doc.grid_size)
|
|
if R < 0.05:
|
|
return
|
|
var rmin := maxf(R * 0.36, 0.03)
|
|
var data := PdGeo.make_torus(R, rmin, 16, 8)
|
|
var c_w: Vector3 = doc.snap_v3(shape_plane_p + shape_u * c2.x + shape_v * c2.y)
|
|
var out_v := PackedVector3Array()
|
|
for p in data["verts"]:
|
|
# local torus: XZ ring, Y axis -> plane basis (u, v, normal)
|
|
out_v.append(doc.snap_v3(
|
|
c_w + shape_u * p.x + shape_plane_n * p.y + shape_v * p.z))
|
|
doc.add_mesh_data({"verts": out_v, "faces": data["faces"]}, "Torus")
|
|
|
|
|
|
## Flip the loop so its normal agrees with the doodle plane (toward the
|
|
## viewer), keeping the face front-facing in the 3D view.
|
|
func _orient_to_plane(pts: PackedVector3Array) -> PackedVector3Array:
|
|
if PdGeo.face_normal(pts).dot(shape_plane_n) < 0.0:
|
|
pts.reverse()
|
|
return pts
|
|
|
|
|
|
## Live preview of the doodle while dragging (drawn into the overlay).
|
|
func _draw_shape_preview_3d() -> void:
|
|
var col := Color(PdDocument.ACCENT.r, PdDocument.ACCENT.g, PdDocument.ACCENT.b, 0.9)
|
|
if doc.draw_kind == "Freehand":
|
|
var pts := PackedVector3Array()
|
|
for sp in shape_pts:
|
|
var p2 = _shape_pt2(sp)
|
|
if p2 == null:
|
|
return
|
|
pts.append(shape_plane_p + shape_u * p2.x + shape_v * p2.y)
|
|
for i in range(1, pts.size()):
|
|
_quad_line(pts[i - 1], pts[i], 1.6, col)
|
|
if pts.size() > 2:
|
|
_quad_line(pts[pts.size() - 1], pts[0], 1.0,
|
|
Color(col.r, col.g, col.b, 0.4))
|
|
if not pts.is_empty():
|
|
_dot(pts[0], 3.0, Color(1, 1, 1, 0.9))
|
|
return
|
|
if shape_pts.size() != 2:
|
|
if not shape_pts.is_empty():
|
|
var p0 = _shape_pt2(shape_pts[0])
|
|
if p0 != null:
|
|
_dot(shape_plane_p + shape_u * p0.x + shape_v * p0.y,
|
|
3.0, Color(1, 1, 1, 0.9))
|
|
return
|
|
var r := Rect2(shape_pts[0], shape_pts[1] - shape_pts[0]).abs()
|
|
# Torus preview: outer and inner ring as two separate closed loops
|
|
if doc.draw_kind == "Torus":
|
|
var outer := PackedVector3Array()
|
|
for i in 49:
|
|
var a := TAU * i / 48.0
|
|
var gp := Vector2(0.5, 0.5) + Vector2(cos(a), sin(a)) * 0.5
|
|
var sp := r.position + Vector2(gp.x * r.size.x, gp.y * r.size.y)
|
|
var p2 = _shape_pt2(sp)
|
|
if p2 == null:
|
|
return
|
|
outer.append(shape_plane_p + shape_u * p2.x + shape_v * p2.y)
|
|
for i in range(1, outer.size()):
|
|
_quad_line(outer[i - 1], outer[i], 1.6, col)
|
|
var inner := PackedVector3Array()
|
|
for i in 49:
|
|
var a := TAU * i / 48.0
|
|
var gp := Vector2(0.5, 0.5) + Vector2(cos(a), sin(a)) * 0.18
|
|
var sp := r.position + Vector2(gp.x * r.size.x, gp.y * r.size.y)
|
|
var p2 = _shape_pt2(sp)
|
|
if p2 == null:
|
|
return
|
|
inner.append(shape_plane_p + shape_u * p2.x + shape_v * p2.y)
|
|
for i in range(1, inner.size()):
|
|
_quad_line(inner[i - 1], inner[i], 1.6, Color(col.r, col.g, col.b, 0.7))
|
|
if not outer.is_empty():
|
|
_dot(outer[0], 3.0, Color(1, 1, 1, 0.9))
|
|
return
|
|
var gen: PackedVector2Array
|
|
match doc.draw_kind:
|
|
"Circle":
|
|
gen = _norm_ellipse3d(48)
|
|
"Triangle":
|
|
gen = _norm_ngon3d(3)
|
|
"Pentagon":
|
|
gen = _norm_ngon3d(5)
|
|
"Hexagon":
|
|
gen = _norm_ngon3d(6)
|
|
"Star":
|
|
gen = _norm_star3d()
|
|
_:
|
|
gen = PackedVector2Array([Vector2(0, 0), Vector2(1, 0),
|
|
Vector2(1, 1), Vector2(0, 1)])
|
|
var pts := PackedVector3Array()
|
|
for gp in gen:
|
|
var sp := r.position + Vector2(gp.x * r.size.x, gp.y * r.size.y)
|
|
var p2 = _shape_pt2(sp)
|
|
if p2 == null:
|
|
return
|
|
pts.append(shape_plane_p + shape_u * p2.x + shape_v * p2.y)
|
|
var closed := pts.duplicate()
|
|
closed.append(pts[0])
|
|
for i in range(1, closed.size()):
|
|
_quad_line(closed[i - 1], closed[i], 1.6, col)
|
|
_dot(pts[0], 3.0, Color(1, 1, 1, 0.9))
|
|
|
|
|
|
func _norm_ellipse3d(n: int) -> PackedVector2Array:
|
|
var out := PackedVector2Array()
|
|
for i in n + 1:
|
|
var a := TAU * i / n
|
|
out.append(Vector2(0.5, 0.5) + Vector2(cos(a), sin(a)) * 0.5)
|
|
return out
|
|
|
|
|
|
func _norm_ngon3d(n: int) -> PackedVector2Array:
|
|
var out := PackedVector2Array()
|
|
for i in n + 1:
|
|
var a := -PI * 0.5 + TAU * i / n
|
|
out.append(Vector2(0.5, 0.5) + Vector2(cos(a), sin(a)) * 0.5)
|
|
return out
|
|
|
|
|
|
func _norm_star3d() -> PackedVector2Array:
|
|
var out := PackedVector2Array()
|
|
for i in 11:
|
|
var a := -PI * 0.5 + PI * i / 5.0
|
|
var k := 0.5 if i % 2 == 0 else 0.21
|
|
out.append(Vector2(0.5, 0.5) + Vector2(cos(a), sin(a)) * k)
|
|
return out
|
|
|
|
|
|
# ---------------------------------------------------- context menu ---
|
|
## Right-click mesh-ops menu in the 3D editor (picocad-style): extrude
|
|
## faces/edges, merge vertices, fill/clear paint, delete - per mode.
|
|
## Built fresh on every open so enable states are always live.
|
|
func _open_ctx_menu(mp: Vector2) -> void:
|
|
if _ctx_menu != null and is_instance_valid(_ctx_menu):
|
|
_ctx_menu.queue_free()
|
|
_ctx_menu = PopupMenu.new()
|
|
_ctx_menu.add_theme_font_size_override("font_size", 12)
|
|
var oi := doc.active_obj
|
|
var o := doc.obj(oi)
|
|
var entries: Array = []
|
|
match doc.mode:
|
|
PdDocument.Mode.OBJECT:
|
|
entries = [[0, "Duplicate", doc.sel_objs.size() > 0],
|
|
[1, "Delete", doc.sel_objs.size() > 0],
|
|
[-1, "", true],
|
|
[2, "Select All", doc.objects.size() > 0],
|
|
[3, "Deselect", doc.sel_objs.size() > 0]]
|
|
PdDocument.Mode.VERTEX:
|
|
entries = [[10, "Merge Vertices", doc.sel_verts.size() >= 2],
|
|
[11, "Delete Vertex(es)", doc.sel_verts.size() > 0],
|
|
[-1, "", true],
|
|
[12, "Select All", o != null],
|
|
[13, "Deselect", doc.sel_verts.size() > 0]]
|
|
PdDocument.Mode.EDGE:
|
|
entries = [[20, "Extrude Edges", doc.sel_edges.size() > 0],
|
|
[21, "Delete Edge(s)", doc.sel_edges.size() > 0],
|
|
[-1, "", true],
|
|
[22, "Select All", o != null],
|
|
[23, "Deselect", doc.sel_edges.size() > 0]]
|
|
PdDocument.Mode.FACE:
|
|
# multi-face selection: BOTH extrude modes - each
|
|
# face along its own normal, or the whole selection
|
|
# as one region (shared edges get no walls)
|
|
if doc.sel_faces.size() > 1:
|
|
entries = [[30, "Extrude Faces (Individual)", true],
|
|
[36, "Extrude Faces (Combined)", true],
|
|
[37, "Inset Faces", true],
|
|
[38, "Outset Faces", true],
|
|
[31, "Fill Face(s)", true],
|
|
[32, "Clear Face Paint", true],
|
|
[33, "Delete Face(s)", true],
|
|
[-1, "", true],
|
|
[34, "Select All", o != null],
|
|
[35, "Deselect", true]]
|
|
else:
|
|
entries = [[30, "Extrude Face", doc.sel_faces.size() > 0],
|
|
[37, "Inset Face", doc.sel_faces.size() > 0],
|
|
[38, "Outset Face", doc.sel_faces.size() > 0],
|
|
[31, "Fill Face(s)", doc.sel_faces.size() > 0],
|
|
[32, "Clear Face Paint", doc.sel_faces.size() > 0],
|
|
[33, "Delete Face(s)", doc.sel_faces.size() > 0],
|
|
[-1, "", true],
|
|
[34, "Select All", o != null],
|
|
[35, "Deselect", doc.sel_faces.size() > 0]]
|
|
_:
|
|
return
|
|
var any := false
|
|
for e in entries:
|
|
if int(e[0]) < 0:
|
|
_ctx_menu.add_separator()
|
|
continue
|
|
_ctx_menu.add_item(e[1], e[0])
|
|
_ctx_menu.set_item_disabled(_ctx_menu.get_item_count() - 1,
|
|
not e[2] or o == null)
|
|
any = true
|
|
if not any:
|
|
_ctx_menu.queue_free()
|
|
_ctx_menu = null
|
|
return
|
|
_ctx_menu.id_pressed.connect(func(id: int): _ctx_action(id))
|
|
add_child(_ctx_menu)
|
|
# popup at the mouse position, in SCREEN coordinates
|
|
_ctx_menu.position = Vector2i(get_global_rect().position + mp)
|
|
_ctx_menu.popup()
|
|
|
|
|
|
func _ctx_action(id: int) -> void:
|
|
var oi := doc.active_obj
|
|
match id:
|
|
0:
|
|
doc.duplicate_selected()
|
|
1:
|
|
doc.delete_selection()
|
|
2:
|
|
doc.select_all()
|
|
3:
|
|
doc.clear_all_sel()
|
|
10:
|
|
doc.merge_selected_verts()
|
|
11:
|
|
doc.delete_verts_list(oi, PackedInt32Array(doc.sel_verts))
|
|
12:
|
|
doc.select_all()
|
|
13:
|
|
doc.clear_element_sel()
|
|
doc.notify_sel()
|
|
20:
|
|
doc.extrude_selected_edges()
|
|
21:
|
|
doc.delete_edges_list(oi, doc.sel_edges.duplicate())
|
|
22:
|
|
doc.select_all()
|
|
23:
|
|
doc.clear_element_sel()
|
|
doc.notify_sel()
|
|
30:
|
|
doc.extrude_selected_faces()
|
|
36:
|
|
doc.extrude_selected_faces_combined()
|
|
37:
|
|
doc.inset_selected_faces(doc.inset_dist)
|
|
38:
|
|
doc.inset_selected_faces(-doc.inset_dist)
|
|
31:
|
|
for fi in doc.sel_faces:
|
|
doc.tex_fill_island(oi, fi, false)
|
|
32:
|
|
for fi in doc.sel_faces:
|
|
doc.tex_fill_island(oi, fi, true)
|
|
33:
|
|
doc.delete_faces_list(oi, PackedInt32Array(doc.sel_faces))
|
|
34:
|
|
doc.select_all()
|
|
35:
|
|
doc.clear_element_sel()
|
|
doc.notify_sel()
|