From 7366b7e8562a40f80cb6e64e25498f7de0cc8143 Mon Sep 17 00:00:00 2001 From: Michael Mainguy Date: Fri, 8 Sep 2023 13:59:44 -0500 Subject: [PATCH] Updated text labels to be two sided and removed emissive. --- src/controllers/base.ts | 1 + src/controllers/functions/reparent.ts | 1 + src/diagram/diagramManager.ts | 13 +++++++--- src/diagram/textLabel.ts | 37 +++++++++++++++++++-------- src/menus/diagramListingMenu.ts | 9 +++++-- 5 files changed, 44 insertions(+), 17 deletions(-) diff --git a/src/controllers/base.ts b/src/controllers/base.ts index 03c53e5..b061d79 100644 --- a/src/controllers/base.ts +++ b/src/controllers/base.ts @@ -152,6 +152,7 @@ export class Base { transformNode.rotation = newMesh.rotation.clone(); } transformNode.setParent(this.controller.motionController.rootMesh); + newMesh.setParent(transformNode); this.grabbedMeshParentId = transformNode.id; this.grabbedMesh = newMesh; this.previousParentId = null; diff --git a/src/controllers/functions/reparent.ts b/src/controllers/functions/reparent.ts index b29331d..b7803f9 100644 --- a/src/controllers/functions/reparent.ts +++ b/src/controllers/functions/reparent.ts @@ -18,6 +18,7 @@ export function reparent(mesh: AbstractMesh, previousParentId: string, grabbedMe if (parent) { logger.warn('setting parent to null', grabbedMeshParentId, parent) //this.grabbedMeshParentId = null; + mesh.setParent(null); parent.dispose(); } else { mesh.setParent(null); diff --git a/src/diagram/diagramManager.ts b/src/diagram/diagramManager.ts index 93b0bc7..fe19db2 100644 --- a/src/diagram/diagramManager.ts +++ b/src/diagram/diagramManager.ts @@ -12,6 +12,7 @@ import {deepCopy} from "../util/functions/deepCopy"; import {applyPhysics} from "./functions/diagramShapePhysics"; import {applyScaling} from "./functions/applyScaling"; import {toDiagramEntity} from "./functions/toDiagramEntity"; +import {v4 as uuidv4} from 'uuid'; export class DiagramManager { @@ -70,12 +71,15 @@ export class DiagramManager { public createCopy(mesh: AbstractMesh, copy: boolean = false): AbstractMesh { let newMesh; if (!mesh.isAnInstance) { - newMesh = new InstancedMesh("new", (mesh as Mesh)); + newMesh = new InstancedMesh('id' + uuidv4(), (mesh as Mesh)); } else { - newMesh = new InstancedMesh("new", (mesh as InstancedMesh).sourceMesh); + newMesh = new InstancedMesh('id' + uuidv4(), (mesh as InstancedMesh).sourceMesh); } + newMesh.id = 'id' + uuidv4(); + newMesh.actionManager = this.diagramEntityActionManager.manager; newMesh.position = mesh.absolutePosition.clone(); + if (mesh.absoluteRotationQuaternion) { newMesh.rotation = mesh.absoluteRotationQuaternion.toEulerAngles().clone(); } else { @@ -88,11 +92,12 @@ export class DiagramManager { applyPhysics(this.sounds, newMesh, this.scene); } //@TODO Refactor - this.onDiagramEventObservable.notifyObservers({ + /*this.onDiagramEventObservable.notifyObservers({ type: DiagramEventType.ADD, entity: toDiagramEntity(newMesh) - }, 2); + }, 2);*/ //this.persistenceManager.add(toDiagramEntity(newMesh)); + return newMesh; } diff --git a/src/diagram/textLabel.ts b/src/diagram/textLabel.ts index cab5596..36074a1 100644 --- a/src/diagram/textLabel.ts +++ b/src/diagram/textLabel.ts @@ -1,20 +1,23 @@ -import {AbstractMesh, Color3, DynamicTexture, Mesh, MeshBuilder, StandardMaterial} from "@babylonjs/core"; +import {AbstractMesh, DynamicTexture, Material, MeshBuilder, StandardMaterial} from "@babylonjs/core"; import log from "loglevel"; export class TextLabel { private static logger: log.Logger = log.getLogger('TextLabel'); - public static updateTextNode(mesh: AbstractMesh, text: string): AbstractMesh { + public static updateTextNode(mesh: AbstractMesh, text: string) { if (!mesh) { this.logger.error("updateTextNode: mesh is null"); return null; } - let textNode = (mesh.getChildren((node) => { - return node.name == 'text' - })[0] as Mesh); - if (textNode) { - textNode.dispose(false, true); + const textNodes = mesh.getChildren((node) => { + return node.metadata?.label == true; + }); + if (textNodes && textNodes.length > 0) { + textNodes.forEach((node) => { + node.dispose(false, true); + }); } + if (!text) { return null; } @@ -44,18 +47,30 @@ export class TextLabel { }, mesh.getScene(), false); const mat = new StandardMaterial("mat", mesh.getScene()); mat.diffuseTexture = dynamicTexture; - mat.emissiveColor = Color3.White(); + //mat.emissiveColor = Color3.White(); dynamicTexture.drawText(text, null, null, font, "#000000", "#ffffff", true); //Create plane and set dynamic texture as material + //const plane = MeshBuilder.CreatePlane("text" + text, {width: planeWidth, height: height}, mesh.getScene()); + + + const plane1 = this.createPlane(mat, mesh, text, planeWidth, height); + const plane2 = this.createPlane(mat, mesh, text, planeWidth, height); + plane2.rotation.y = Math.PI; + + + } + + private static createPlane(mat: Material, mesh: AbstractMesh, text: string, planeWidth: number, height: number): AbstractMesh { const plane = MeshBuilder.CreatePlane("text" + text, {width: planeWidth, height: height}, mesh.getScene()); + plane.material = mat; - plane.billboardMode = Mesh.BILLBOARDMODE_ALL; - plane.metadata = {exportable: true}; + //plane.billboardMode = Mesh.BILLBOARDMODE_ALL; + plane.metadata = {exportable: true, label: true}; const yOffset = mesh.getBoundingInfo().boundingSphere.radius; plane.parent = mesh; - plane.position.y = yOffset; + plane.position.y = yOffset + height / 2; plane.scaling.y = 1 / mesh.scaling.y; plane.scaling.x = 1 / mesh.scaling.x; plane.scaling.z = 1 / mesh.scaling.z; diff --git a/src/menus/diagramListingMenu.ts b/src/menus/diagramListingMenu.ts index c5da741..3a607c8 100644 --- a/src/menus/diagramListingMenu.ts +++ b/src/menus/diagramListingMenu.ts @@ -1,10 +1,11 @@ -import {MeshBuilder, Scene, Vector3, WebXRDefaultExperience} from "@babylonjs/core"; +import {AbstractMesh, MeshBuilder, Scene, Vector3, WebXRDefaultExperience} from "@babylonjs/core"; import {AbstractMenu} from "./abstractMenu"; import {ControllerEventType, Controllers} from "../controllers/controllers"; import {AdvancedDynamicTexture, Button, Control, ScrollViewer, StackPanel, TextBlock} from "@babylonjs/gui"; import {setMenuPosition} from "../util/functions/setMenuPosition"; export class DiagramListingMenu extends AbstractMenu { + private mesh: AbstractMesh; constructor(scene: Scene, xr: WebXRDefaultExperience, controllers: Controllers) { super(scene, xr, controllers); this.buildMenu(); @@ -17,6 +18,8 @@ export class DiagramListingMenu extends AbstractMenu { public toggle() { setMenuPosition(this.handle.mesh, this.scene, new Vector3(0, .4, 0)); + this.mesh.isVisible = !this.mesh.isVisible; + (this.mesh.parent as AbstractMesh).isVisible = this.mesh.isVisible; } private buildMenu() { @@ -26,6 +29,7 @@ export class DiagramListingMenu extends AbstractMenu { width: 1, height: .5 }, this.scene); + this.mesh = configPlane; const configTexture = AdvancedDynamicTexture.CreateForMesh(configPlane, 2048, 1024); configTexture.background = "white"; @@ -61,6 +65,7 @@ export class DiagramListingMenu extends AbstractMenu { this.createHandle(configPlane); configPlane.position.y = .5; setMenuPosition(this.handle.mesh, this.scene, new Vector3(0, .4, 0)); - + this.mesh.isVisible = false; + (this.mesh.parent as AbstractMesh).isVisible = false; } } \ No newline at end of file