diff --git a/src/diagram/diagramConnection.ts b/src/diagram/diagramConnection.ts index 60c9161..263dacf 100644 --- a/src/diagram/diagramConnection.ts +++ b/src/diagram/diagramConnection.ts @@ -1,4 +1,5 @@ import { + AbstractMesh, CreateGreasedLine, GreasedLineMesh, GreasedLineTools, @@ -37,6 +38,7 @@ export class DiagramConnection { } this.buildConnection(); } + private readonly scene: Scene; private toAnchor: TransformNode; private fromAnchor: TransformNode; @@ -85,7 +87,6 @@ export class DiagramConnection { } else { this.points = [Vector3.Zero(), Vector3.Zero()]; } - } private setPoints() { @@ -96,9 +97,7 @@ export class DiagramConnection { } private buildConnection() { - this.logger.debug('buildConnection'); - this.logger.debug(this._to); - this.logger.debug(this._from); + this.logger.debug(`buildConnection from ${this._from} to ${this._to}`); this.recalculate(); this._mesh = CreateGreasedLine(this.id, @@ -115,25 +114,33 @@ export class DiagramConnection { this.recalculate(); this.setPoints(); }); - this.scene.onNewMeshAddedObservable.add((mesh) => { - if (mesh && mesh.id) { - if (!this.toAnchor || !this.fromAnchor) { - this.logger.debug('render'); - if (mesh?.id == this?._to) { - this.logger.debug("Found to anchor"); - this.toAnchor = mesh; - this._mesh.metadata.to = this.to; - } - if (mesh?.id == this?._from) { - this.logger.debug("Found from anchor"); - this.fromAnchor = mesh; - this._mesh.metadata.from = this.from; - } + this.scene.onNewMeshAddedObservable.add(this.onMeshAdded, -1, true, this); + return; + } + + private onMeshAdded = (mesh: AbstractMesh) => { + if (mesh && mesh.id) { + if (!this.toAnchor || !this.fromAnchor) { + if (mesh?.id == this?._to) { + this.logger.debug("Found to anchor"); + this.toAnchor = mesh; + this._mesh.metadata.to = this.to; + } + if (mesh?.id == this?._from) { + this.logger.debug("Found from anchor"); + this.fromAnchor = mesh; + this._mesh.metadata.from = this.from; + } + if (this.toAnchor && this.fromAnchor) { + this.logger.debug(`connection built from ${this._from} to ${this._to}`); + this.removeObserver(); } } + } + } - }, -1, true, this); - return; - + private removeObserver() { + this.logger.debug("removing observer"); + this.scene.onNewMeshAddedObservable.removeCallback(this.onMeshAdded); } } diff --git a/src/diagram/meshConverter.ts b/src/diagram/meshConverter.ts index 5233f4a..51a2be9 100644 --- a/src/diagram/meshConverter.ts +++ b/src/diagram/meshConverter.ts @@ -44,22 +44,25 @@ export class MeshConverter { } let mesh: AbstractMesh = scene.getMeshById(entity.id); if (mesh) { - log.debug('mesh already exists'); + this.logger.debug(`mesh ${mesh.id} already exists`); } else { if (entity.template == "#connection-template") { const connection: DiagramConnection = new DiagramConnection(entity.from, entity.to, scene); - - } - mesh = scene.getMeshById("tool-" + entity.template + "-" + entity.color); - if (mesh) { - if (mesh.isAnInstance) { - log.debug('error: mesh is an instance'); - } else { - mesh = new InstancedMesh(entity.id, (mesh as Mesh)); - } + this.logger.debug(`connection.mesh = ${connection.mesh.id}`); + mesh = connection.mesh; } else { - log.debug('no mesh found for ' + entity.template + "-" + entity.color); + mesh = scene.getMeshById("tool-" + entity.template + "-" + entity.color); + if (mesh) { + if (mesh.isAnInstance) { + this.logger.error(`mesh ${mesh.id} is an instance`); + } else { + mesh = new InstancedMesh(entity.id, (mesh as Mesh)); + } + } else { + this.logger.warn('no mesh found for ' + entity.template + "-" + entity.color); + } } + } if (mesh) { mesh.metadata = {template: entity.template};