Luce packages
LuceLuce BaseLuciaOS

dymokomi/luced-3d

Source of release 0.11.7

0.11.7 / src / viewport_placing.luc

## The viewport side of placing (place_prompt.luc): a click becomes a world
## point and the workspace takes it; the cursor's would-be corner is the
## rubber band's end. The Knife's points land as knife_placing.luc says. Hit tests are Base calls: the edited mesh's points by
## screen distance (EditOverlay.pick), the displayed surfaces
## (SurfaceHits.cast), the ground plane (SurfaceHits.plane).
from luce_ui.ui import Painter
from luce_3d.graphics import Vector3, EditOverlay, Renderer, SurfaceHits, SurfaceHit, WireRenderer
from workspace import Workspace
from place_prompt import PlacePrompt
import theme
import view_projection as gm
import placing_commands
import knife_placing
from snapping import SnapMark, grid_step, on_grid

## Screen pixels within which a click lands on a mesh point or closes a
## drawing on its first corner.
let reach = 11.0

pub class ViewportPlacing:
    let workspace: Workspace
    ## Whether the last pointer move moved the rubber band (a frame is due).
    pub var moved: bool = false

    pub func init(self, workspace: Workspace):
        self.workspace = workspace

    ## A click while drawing: a corner, or the finish on the first corner
    ## (the Knife's stroke closes there, back at its first point).
    pub func click(self, placing: PlacePrompt, overlay: EditOverlay, renderer: Renderer?, x: float, y: float, width: float, height: float, control: bool = false, shift: bool = false) -> unit!:
        let w = self.workspace
        if placing.corners.length >= 3 and near_on_screen(w, placing.corners[0], x, y, width, height):
            if placing.gesture == "knife":
                placing_commands.add_corner(w, placing.corners[0], knife_placing.ray_at(w, x, y, width, height))
            placing_commands.finish_placing(w)
            return
        let hit = self.corner_at(placing, overlay, renderer, x, y, width, height, control, shift)
        if hit.found:
            placing_commands.add_corner(w, hit.point, knife_placing.ray_at(w, x, y, width, height))

    ## The pointer moved while drawing: the rubber band follows.
    pub func hover(self, placing: PlacePrompt, overlay: EditOverlay, renderer: Renderer?, x: float, y: float, width: float, height: float, control: bool = false, shift: bool = false) -> unit!:
        let hit = self.corner_at(placing, overlay, renderer, x, y, width, height, control, shift)
        placing.cursor = hit.point if hit.found else none
        self.moved = true

    ## Where a corner lands, as snapping allows (snapping.luc): an
    ## edited-mesh point within reach, else the surface under the cursor,
    ## else the ground through the first corner (on the grid).
    func corner_at(self, placing: PlacePrompt, overlay: EditOverlay, renderer: Renderer?, x: float, y: float, width: float, height: float, control: bool, shift: bool) -> SurfaceHit!:
        let w = self.workspace
        if placing.gesture == "knife":
            return knife_placing.corner_at(w, placing, overlay, renderer, x, y, width, height, control, shift)
        let snaps = w.snaps
        let direction = view_direction(w, x, y, width, height)
        if let mesh = w.geometry:
            if snaps.points() and mesh.point_count() > 0:
                var face = -2
                if let drawn = renderer:
                    face = drawn.pick_face(mesh, w.view.camera, x, y, width, height) catch failure:
                        recover -2
                overlay.show(mesh, 0, none, -1)
                let point = overlay.pick(w.view.camera, x, y, width, height, face)
                if point >= 0:
                    w.snap_mark = SnapMark(mesh.point(point), "Point")
                    return SurfaceHit(true, mesh.point(point), Vector3(0.0, 1.0, 0.0), 0.0)
        if snaps.surfaces() or w.snap_target >= 0:
            let hit = w.landing_at(gm.origin(w, x, y, width, height), direction)
            if hit.found:
                w.snap_mark = SnapMark(hit.point, "Surface")
                return hit
        let base = placing.corners[0] if placing.corners.length > 0 else Vector3()
        let ground = SurfaceHits.plane(gm.origin(w, x, y, width, height), direction, base, Vector3(0.0, 1.0, 0.0))
        if ground.found and snaps.grid():
            let step = grid_step(w.view.distance)
            let on = Vector3(on_grid(ground.point.x, step), ground.point.y, on_grid(ground.point.z, step))
            w.snap_mark = SnapMark(on, "Grid")
            return SurfaceHit(true, on, ground.normal, ground.distance)
        return ground

    ## The drawing so far, and the rubber band to the cursor.
    pub func draw(self, painter: Painter) -> unit!:
        let placing = self.workspace.placing else return
        if placing.corners.length == 0:
            return
        let points: list[Vector3] = []
        for at in 1..<placing.corners.length:
            points.append(placing.corners[at - 1])
            points.append(placing.corners[at])
        if let cursor = placing.cursor:
            points.append(placing.corners[-1])
            points.append(cursor)
        if points.length > 0:
            WireRenderer.lines(points, self.workspace.view.camera, painter.target(), theme.accent(), 2.0)

## The view ray's direction through (x, y).
func view_direction(w: Workspace, x: float, y: float, width: float, height: float) -> Vector3!:
    return gm.ray(w, x, y, width, height).normalized()

## Whether `point` shows within reach of (x, y), in front of the camera.
func near_on_screen(w: Workspace, point: Vector3, x: float, y: float, width: float, height: float) -> bool!:
    let screen = gm.project(w, point, width, height)
    return screen[2] > 0.0 and (screen[0] - x) * (screen[0] - x) + (screen[1] - y) * (screen[1] - y) <= reach * reach