From 17206abca72871f2d395fd8006485f654847d128 Mon Sep 17 00:00:00 2001 From: Michael Mainguy Date: Fri, 23 Aug 2024 10:12:07 -0500 Subject: [PATCH] Added target sphere to pick preview --- src/menus/connectionPreview.ts | 33 +++++++++++++++++++++++++++------ 1 file changed, 27 insertions(+), 6 deletions(-) diff --git a/src/menus/connectionPreview.ts b/src/menus/connectionPreview.ts index 16b32a9..688d68e 100644 --- a/src/menus/connectionPreview.ts +++ b/src/menus/connectionPreview.ts @@ -4,6 +4,7 @@ import { MeshBuilder, Observable, Observer, + Ray, Scene, TransformNode, Vector3, @@ -33,14 +34,34 @@ export class ConnectionPreview { this._parent = input.pointer; this._fromId = fromMesh.id; this._transform = new TransformNode("transform", this._scene); + this._transform.position = point.clone(); this._transform.setParent(this._parent); this._fromPoint = fromMesh.getAbsolutePosition(); - this._options = { - points: [this._fromPoint, this._transform.absolutePosition], - updatable: true, - useAlphaForLines: false, - }; + + const target = MeshBuilder.CreateSphere("target", {segments: 8, diameter: .02}); + target.parent = this._transform; + const ray: Ray = new Ray(fromMesh.getAbsolutePosition(), target.absoluteScaling); + const pick = this._scene.pickWithRay(ray, (mesh) => { + return mesh.id === "target"; + }); + + target.isPickable = false; + + if (pick.pickedPoint) { + console.log('picked'); + this._options = { + points: [this._fromPoint, pick.pickedPoint], + updatable: true, + useAlphaForLines: false, + }; + } else { + this._options = { + points: [this._fromPoint, this._transform.absolutePosition], + updatable: true, + useAlphaForLines: false, + }; + } this._line = MeshBuilder.CreateLines("connectionPreview", this._options, this._scene); this._options.instance = this._line; this._renderObserver = this._scene.onBeforeRenderObservable.add(() => { @@ -53,7 +74,7 @@ export class ConnectionPreview { public dispose() { this._scene.onBeforeRenderObservable.remove(this._renderObserver); this._parent = null; - this._transform.dispose(); + this._transform.dispose(false, true); this._line.dispose(); }