Added target sphere to pick preview

This commit is contained in:
Michael Mainguy 2024-08-23 10:12:07 -05:00
parent 263879d215
commit 17206abca7

View File

@ -4,6 +4,7 @@ import {
MeshBuilder, MeshBuilder,
Observable, Observable,
Observer, Observer,
Ray,
Scene, Scene,
TransformNode, TransformNode,
Vector3, Vector3,
@ -33,14 +34,34 @@ export class ConnectionPreview {
this._parent = input.pointer; this._parent = input.pointer;
this._fromId = fromMesh.id; this._fromId = fromMesh.id;
this._transform = new TransformNode("transform", this._scene); this._transform = new TransformNode("transform", this._scene);
this._transform.position = point.clone(); this._transform.position = point.clone();
this._transform.setParent(this._parent); this._transform.setParent(this._parent);
this._fromPoint = fromMesh.getAbsolutePosition(); this._fromPoint = fromMesh.getAbsolutePosition();
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 = { this._options = {
points: [this._fromPoint, this._transform.absolutePosition], points: [this._fromPoint, this._transform.absolutePosition],
updatable: true, updatable: true,
useAlphaForLines: false, useAlphaForLines: false,
}; };
}
this._line = MeshBuilder.CreateLines("connectionPreview", this._options, this._scene); this._line = MeshBuilder.CreateLines("connectionPreview", this._options, this._scene);
this._options.instance = this._line; this._options.instance = this._line;
this._renderObserver = this._scene.onBeforeRenderObservable.add(() => { this._renderObserver = this._scene.onBeforeRenderObservable.add(() => {
@ -53,7 +74,7 @@ export class ConnectionPreview {
public dispose() { public dispose() {
this._scene.onBeforeRenderObservable.remove(this._renderObserver); this._scene.onBeforeRenderObservable.remove(this._renderObserver);
this._parent = null; this._parent = null;
this._transform.dispose(); this._transform.dispose(false, true);
this._line.dispose(); this._line.dispose();
} }