Initial web worker commit.

This commit is contained in:
Michael Mainguy 2023-08-24 14:13:29 -05:00
parent 96d0e01d77
commit 50c972eb97
14 changed files with 200 additions and 151 deletions

View File

@ -15,9 +15,8 @@ import {AppConfig} from "./util/appConfig";
import {GamepadManager} from "./controllers/gamepadManager"; import {GamepadManager} from "./controllers/gamepadManager";
import {CustomEnvironment} from "./util/customEnvironment"; import {CustomEnvironment} from "./util/customEnvironment";
import {Controllers} from "./controllers/controllers"; import {Controllers} from "./controllers/controllers";
import {Introduction} from "./tutorial/introduction"; import workerUrl from "./worker?worker&url";
import {IndexdbPersistenceManager} from "./integration/indexdbPersistenceManager"; import {DiagramEventType} from "./diagram/diagramEntity";
export class App { export class App {
//preTasks = [havokModule]; //preTasks = [havokModule];
@ -43,22 +42,44 @@ export class App {
const logger = log.getLogger('App'); const logger = log.getLogger('App');
const engine = new Engine(canvas, true); const engine = new Engine(canvas, true);
const scene = new Scene(engine); const scene = new Scene(engine);
const config = new AppConfig();
const persistenceManager = new IndexdbPersistenceManager("diagram"); //const persistenceManager = new IndexdbPersistenceManager("diagram");
const worker = new Worker(workerUrl, {type: 'module'});
const controllers = new Controllers(); const controllers = new Controllers();
const toolbox = new Toolbox(scene, controllers); const toolbox = new Toolbox(scene, controllers);
const diagramManager = new DiagramManager(scene, controllers, toolbox);
diagramManager.setPersistenceManager(persistenceManager);
const config = new AppConfig(persistenceManager); const diagramManager = new DiagramManager(scene, controllers, toolbox, config);
diagramManager.onDiagramEventObservable.add((evt) => {
worker.postMessage(evt);
}, 2);
worker.onmessage = (evt) => {
if (evt.data.entity) {
console.log(evt.data.entity);
diagramManager.onDiagramEventObservable.notifyObservers({
type: DiagramEventType.ADD,
entity: evt.data.entity
}, 1);
}
if (evt.data.config) {
console.log(evt.data.config);
config.onConfigChangedObservable.notifyObservers(evt.data.config, 1);
}
}
worker.postMessage({type: 'init'});
//diagramManager.setPersistenceManager(persistenceManager);
const environment = new CustomEnvironment(scene, "default", config); const environment = new CustomEnvironment(scene, "default", config);
persistenceManager.initialize().then(() => { /*persistenceManager.initialize().then(() => {
if (!config.current?.demoCompleted) { if (!config.current?.demoCompleted) {
const intro = new Introduction(scene, config); const intro = new Introduction(scene, config);
intro.start(); intro.start();
} }
}); });
*/
const camera: ArcRotateCamera = new ArcRotateCamera("Camera", -Math.PI / 2, Math.PI / 2, 4, const camera: ArcRotateCamera = new ArcRotateCamera("Camera", -Math.PI / 2, Math.PI / 2, 4,
new Vector3(0, 1.6, 0), scene); new Vector3(0, 1.6, 0), scene);

View File

@ -157,7 +157,7 @@ export class Base {
type: DiagramEventType.ADD, type: DiagramEventType.ADD,
entity: toDiagramEntity(newMesh) entity: toDiagramEntity(newMesh)
} }
this.diagramManager.onDiagramEventObservable.notifyObservers(event); this.diagramManager.onDiagramEventObservable.notifyObservers(event, 2);
} }
} }
@ -202,7 +202,7 @@ export class Base {
entity: entity entity: entity
} }
this.diagramManager.onDiagramEventObservable.notifyObservers(event); this.diagramManager.onDiagramEventObservable.notifyObservers(event, 2);
const body = mesh?.physicsBody; const body = mesh?.physicsBody;
if (body) { if (body) {
body.setMotionType(PhysicsMotionType.DYNAMIC); body.setMotionType(PhysicsMotionType.DYNAMIC);

View File

@ -30,12 +30,15 @@ export class DiagramConnection {
this.toAnchor = toMesh; this.toAnchor = toMesh;
} else { } else {
if (fromMesh) { if (fromMesh) {
this.toAnchor = new TransformNode(this.id + "_to", this.scene); const to = new TransformNode(this.id + "_to", this.scene);
this.toAnchor.id = this.id + "_to"; to.ignoreNonUniformScaling = true;
this.toAnchor.position = fromMesh.absolutePosition.clone(); to.id = this.id + "_to";
to.position = fromMesh.absolutePosition.clone();
if (pointerInfo) { if (pointerInfo) {
this.toAnchor.setParent(pointerInfo.pickInfo.gripTransform); to.setParent(pointerInfo.pickInfo.gripTransform);
} }
this.toAnchor = to;
} }
} }
this.buildConnection(); this.buildConnection();

View File

@ -1,4 +1,4 @@
import {Color3, Vector3} from "@babylonjs/core"; import {Color3} from "@babylonjs/core";
import {EditMenuState} from "../menus/editMenuState"; import {EditMenuState} from "../menus/editMenuState";
export enum DiagramEventType { export enum DiagramEventType {
@ -28,12 +28,13 @@ export type DiagramEntity = {
from?: string; from?: string;
to?: string; to?: string;
last_seen?: Date; last_seen?: Date;
position?: Vector3; position?: { x: number, y: number, z: number };
rotation?: Vector3; rotation?: { x: number, y: number, z: number };
template?: string; template?: string;
text?: string; text?: string;
scale?: Vector3; scale?: { x: number, y: number, z: number };
parent?: string; parent?: string;
diagramlistingid?: string;
} }

View File

@ -1,11 +1,11 @@
import {DiagramEvent, DiagramEventType} from "./diagramEntity"; import {DiagramEvent, DiagramEventType} from "./diagramEntity";
import log from "loglevel"; import log from "loglevel";
import {applyPhysics} from "./functions/diagramShapePhysics"; import {applyPhysics} from "./functions/diagramShapePhysics";
import {ActionManager, Color3, PhysicsMotionType, Scene} from "@babylonjs/core"; import {ActionManager, PhysicsMotionType, Scene} from "@babylonjs/core";
import {TextLabel} from "./textLabel"; import {TextLabel} from "./textLabel";
import {Toolbox} from "../toolbox/toolbox"; import {Toolbox} from "../toolbox/toolbox";
import {DiaSounds} from "../util/diaSounds"; import {DiaSounds} from "../util/diaSounds";
import {IPersistenceManager} from "../integration/iPersistenceManager";
import {fromDiagramEntity} from "./functions/fromDiagramEntity"; import {fromDiagramEntity} from "./functions/fromDiagramEntity";
@ -14,8 +14,7 @@ export function diagramEventHandler(event: DiagramEvent,
toolbox: Toolbox, toolbox: Toolbox,
physicsEnabled: boolean, physicsEnabled: boolean,
actionManager: ActionManager, actionManager: ActionManager,
sounds: DiaSounds, sounds: DiaSounds) {
persistenceManager: IPersistenceManager) {
const entity = event.entity; const entity = event.entity;
let mesh; let mesh;
if (entity) { if (entity) {
@ -42,13 +41,15 @@ export function diagramEventHandler(event: DiagramEvent,
break; break;
case DiagramEventType.DROP: case DiagramEventType.DROP:
if (mesh.metadata.template.indexOf('#') > -1) { if (mesh.metadata.template.indexOf('#') > -1) {
persistenceManager.modify(mesh); //@TODO refactor
// persistenceManager.modify(entity);
TextLabel.updateTextNode(mesh, entity.text); TextLabel.updateTextNode(mesh, entity.text);
} }
break; break;
case DiagramEventType.ADD: case DiagramEventType.ADD:
persistenceManager.add(mesh); //@TODO refactor
//persistenceManager.add(event.entity);
if (!mesh.actionManager) { if (!mesh.actionManager) {
mesh.actionManager = actionManager; mesh.actionManager = actionManager;
} }
@ -58,7 +59,8 @@ export function diagramEventHandler(event: DiagramEvent,
break; break;
case DiagramEventType.MODIFY: case DiagramEventType.MODIFY:
persistenceManager.modify(mesh); //@TODO refactor
//persistenceManager.modify(mesh);
if (physicsEnabled) { if (physicsEnabled) {
applyPhysics(sounds, mesh, scene); applyPhysics(sounds, mesh, scene);
} }
@ -67,16 +69,19 @@ export function diagramEventHandler(event: DiagramEvent,
case DiagramEventType.CHANGECOLOR: case DiagramEventType.CHANGECOLOR:
if (!event.oldColor) { if (!event.oldColor) {
if (!event.newColor) { if (!event.newColor) {
persistenceManager.changeColor(null, Color3.FromHexString(event.entity.color)); //@TODO refactor
//persistenceManager.changeColor(null, Color3.FromHexString(event.entity.color));
this.logger.info("Received color change event, sending entity color as new color"); this.logger.info("Received color change event, sending entity color as new color");
} else { } else {
this.logger.info("Received color change event, no old color, sending new color"); this.logger.info("Received color change event, no old color, sending new color");
persistenceManager.changeColor(null, event.newColor); //@TODO refactor
//persistenceManager.changeColor(null, event.newColor);
} }
} else { } else {
if (event.newColor) { if (event.newColor) {
this.logger.info("changing color from " + event.oldColor + " to " + event.newColor); this.logger.info("changing color from " + event.oldColor + " to " + event.newColor);
persistenceManager.changeColor(event.oldColor, event.newColor); //@TODO refactor
//persistenceManager.changeColor(event.oldColor, event.newColor);
} else { } else {
this.logger.error("changing color from " + event.oldColor + ", but no new color found"); this.logger.error("changing color from " + event.oldColor + ", but no new color found");
} }
@ -85,7 +90,8 @@ export function diagramEventHandler(event: DiagramEvent,
break; break;
case DiagramEventType.REMOVE: case DiagramEventType.REMOVE:
if (mesh) { if (mesh) {
persistenceManager.remove(mesh) //@TODO refactor
//persistenceManager.remove(mesh.id)
mesh?.physicsBody?.dispose(); mesh?.physicsBody?.dispose();
mesh.dispose(); mesh.dispose();
sounds.exit.play(); sounds.exit.play();

View File

@ -1,6 +1,5 @@
import {AbstractMesh, Color3, InstancedMesh, Mesh, Observable, PhysicsMotionType, Scene} from "@babylonjs/core"; import {AbstractMesh, InstancedMesh, Mesh, Observable, Scene} from "@babylonjs/core";
import {DiagramEntity, DiagramEvent, DiagramEventType} from "./diagramEntity"; import {DiagramEvent, DiagramEventType} from "./diagramEntity";
import {IPersistenceManager} from "../integration/iPersistenceManager";
import log from "loglevel"; import log from "loglevel";
import {Controllers} from "../controllers/controllers"; import {Controllers} from "../controllers/controllers";
import {DiaSounds} from "../util/diaSounds"; import {DiaSounds} from "../util/diaSounds";
@ -13,13 +12,11 @@ import {deepCopy} from "../util/functions/deepCopy";
import {applyPhysics} from "./functions/diagramShapePhysics"; import {applyPhysics} from "./functions/diagramShapePhysics";
import {applyScaling} from "./functions/applyScaling"; import {applyScaling} from "./functions/applyScaling";
import {toDiagramEntity} from "./functions/toDiagramEntity"; import {toDiagramEntity} from "./functions/toDiagramEntity";
import {fromDiagramEntity} from "./functions/fromDiagramEntity";
export class DiagramManager { export class DiagramManager {
public readonly onDiagramEventObservable: Observable<DiagramEvent> = new Observable(); public readonly onDiagramEventObservable: Observable<DiagramEvent> = new Observable();
private readonly logger = log.getLogger('DiagramManager'); private readonly logger = log.getLogger('DiagramManager');
private persistenceManager: IPersistenceManager = null;
private readonly toolbox: Toolbox; private readonly toolbox: Toolbox;
private readonly scene: Scene; private readonly scene: Scene;
private readonly sounds: DiaSounds; private readonly sounds: DiaSounds;
@ -28,9 +25,10 @@ export class DiagramManager {
private presentationManager: PresentationManager; private presentationManager: PresentationManager;
private _config: AppConfig; private _config: AppConfig;
constructor(scene: Scene, controllers: Controllers, toolbox: Toolbox) { constructor(scene: Scene, controllers: Controllers, toolbox: Toolbox, config: AppConfig) {
this.sounds = new DiaSounds(scene); this.sounds = new DiaSounds(scene);
this.scene = scene; this.scene = scene;
this._config = config;
this.toolbox = toolbox; this.toolbox = toolbox;
this.controllers = controllers; this.controllers = controllers;
this.presentationManager = new PresentationManager(this.scene); this.presentationManager = new PresentationManager(this.scene);
@ -40,9 +38,12 @@ export class DiagramManager {
this.logger.warn("onDiagramEventObservable already has Observers, you should be careful"); this.logger.warn("onDiagramEventObservable already has Observers, you should be careful");
} }
this.toolbox.colorChangeObservable.add((evt) => { this.toolbox.colorChangeObservable.add((evt) => {
this.persistenceManager.changeColor(Color3.FromHexString(evt.oldColor), Color3.FromHexString(evt.newColor)); console.log(evt);
this.onDiagramEventObservable.notifyObservers({type: DiagramEventType.CHANGECOLOR}, 2);
//@TODO Refactor
//this.persistenceManager.changeColor(Color3.FromHexString(evt.oldColor), Color3.FromHexString(evt.newColor));
}, -1, true, this, false); }, -1, true, this, false);
this.onDiagramEventObservable.add(this.onDiagramEvent, -1, true, this); this.onDiagramEventObservable.add(this.onDiagramEvent, 1, true, this);
this.logger.debug("DiagramManager constructed"); this.logger.debug("DiagramManager constructed");
scene.onMeshRemovedObservable.add((mesh) => { scene.onMeshRemovedObservable.add((mesh) => {
@ -54,7 +55,7 @@ export class DiagramManager {
this.onDiagramEventObservable.notifyObservers({ this.onDiagramEventObservable.notifyObservers({
type: DiagramEventType.REMOVE, type: DiagramEventType.REMOVE,
entity: toDiagramEntity(m) entity: toDiagramEntity(m)
}); }, -1);
} }
}); });
} }
@ -65,11 +66,13 @@ export class DiagramManager {
public get config(): AppConfig { public get config(): AppConfig {
return this._config; return this._config;
} }
public setPersistenceManager(persistenceManager: IPersistenceManager) {
//@TODO Refactor
/*public setPersistenceManager(persistenceManager: IPersistenceManager) {
this.persistenceManager = persistenceManager; this.persistenceManager = persistenceManager;
this._config = new AppConfig(persistenceManager); this._config = new AppConfig(persistenceManager);
this.persistenceManager.updateObserver.add(this.onRemoteEvent, -1, true, this); this.persistenceManager.updateObserver.add(this.onRemoteEvent, -1, true, this);
} }*/
public createCopy(mesh: AbstractMesh, copy: boolean = false): AbstractMesh { public createCopy(mesh: AbstractMesh, copy: boolean = false): AbstractMesh {
let newMesh; let newMesh;
if (!mesh.isAnInstance) { if (!mesh.isAnInstance) {
@ -90,30 +93,18 @@ export class DiagramManager {
if (this.config.current?.physicsEnabled) { if (this.config.current?.physicsEnabled) {
applyPhysics(this.sounds, newMesh, this.scene); applyPhysics(this.sounds, newMesh, this.scene);
} }
this.persistenceManager.add(newMesh); //@TODO Refactor
this.onDiagramEventObservable.notifyObservers({
type: DiagramEventType.ADD,
entity: toDiagramEntity(newMesh)
}, 2);
//this.persistenceManager.add(toDiagramEntity(newMesh));
return newMesh; return newMesh;
} }
private onRemoteEvent(event: DiagramEntity) {
this.logger.debug(event);
const toolMesh = this.scene.getMeshById("tool-" + event.template + "-" + event.color);
if (!toolMesh && (event.template != '#connection-template')) {
log.debug('no mesh found for ' + event.template + "-" + event.color, 'adding it');
this.toolbox.updateToolbox(event.color);
}
const mesh = fromDiagramEntity(event, this.scene);
mesh.actionManager = this.diagramEntityActionManager.manager;
if (event.parent) {
mesh.parent = this.scene.getMeshById(event.parent);
}
if (this.config.current?.physicsEnabled) {
applyPhysics(this.sounds, mesh, this.scene, PhysicsMotionType.DYNAMIC);
}
}
private onDiagramEvent(event: DiagramEvent) { private onDiagramEvent(event: DiagramEvent) {
diagramEventHandler( diagramEventHandler(
event, this.scene, this.toolbox, this.config.current.physicsEnabled, event, this.scene, this.toolbox, this.config.current.physicsEnabled,
this.diagramEntityActionManager.manager, this.sounds, this.persistenceManager); this.diagramEntityActionManager.manager, this.sounds);
} }
} }

View File

@ -1,5 +1,5 @@
import {DiagramEntity} from "../diagramEntity"; import {DiagramEntity} from "../diagramEntity";
import {AbstractMesh, Color3, InstancedMesh, Mesh, Quaternion, Scene, StandardMaterial} from "@babylonjs/core"; import {AbstractMesh, Color3, InstancedMesh, Mesh, Quaternion, Scene, StandardMaterial, Vector3} from "@babylonjs/core";
import {DiagramConnection} from "../diagramConnection"; import {DiagramConnection} from "../diagramConnection";
import {TextLabel} from "../textLabel"; import {TextLabel} from "../textLabel";
import log from "loglevel"; import log from "loglevel";
@ -40,20 +40,20 @@ export function fromDiagramEntity(entity: DiagramEntity, scene: Scene): Abstract
if (mesh) { if (mesh) {
mesh.metadata = {template: entity.template}; mesh.metadata = {template: entity.template};
if (entity.position) { if (entity.position) {
mesh.position = entity.position; mesh.position = xyztovec(entity.position);
} }
if (entity.rotation) { if (entity.rotation) {
if (mesh.rotationQuaternion) { if (mesh.rotationQuaternion) {
mesh.rotationQuaternion = Quaternion.FromEulerAngles(entity.rotation.x, entity.rotation.y, entity.rotation.z); mesh.rotationQuaternion = Quaternion.FromEulerAngles(entity.rotation.x, entity.rotation.y, entity.rotation.z);
} else { } else {
mesh.rotation = entity.rotation; mesh.rotation = xyztovec(entity.rotation);
} }
} }
if (entity.parent) { if (entity.parent) {
mesh.parent = scene.getNodeById(entity.parent); mesh.parent = scene.getNodeById(entity.parent);
} }
if (entity.scale) { if (entity.scale) {
mesh.scaling = entity.scale; mesh.scaling = xyztovec(entity.scale);
} }
if (!mesh.material) { if (!mesh.material) {
const material = new StandardMaterial("material-" + entity.id, scene); const material = new StandardMaterial("material-" + entity.id, scene);
@ -75,3 +75,8 @@ export function fromDiagramEntity(entity: DiagramEntity, scene: Scene): Abstract
} }
return mesh; return mesh;
} }
function xyztovec(xyz: { x, y, z }): Vector3 {
return new Vector3(xyz.x, xyz.y, xyz.z);
}

View File

@ -1,4 +1,4 @@
import {AbstractMesh} from "@babylonjs/core"; import {AbstractMesh, Vector3} from "@babylonjs/core";
import {DiagramEntity} from "../diagramEntity"; import {DiagramEntity} from "../diagramEntity";
import log from "loglevel"; import log from "loglevel";
import {v4 as uuidv4} from 'uuid'; import {v4 as uuidv4} from 'uuid';
@ -16,14 +16,14 @@ export function toDiagramEntity(mesh: AbstractMesh): DiagramEntity {
mesh.id = "id" + uuidv4(); mesh.id = "id" + uuidv4();
} }
entity.id = mesh.id; entity.id = mesh.id;
entity.position = mesh.position; entity.position = vectoxys(mesh.position);
entity.rotation = mesh.rotation; entity.rotation = vectoxys(mesh.rotation);
entity.last_seen = new Date(); entity.last_seen = new Date();
entity.template = mesh?.metadata?.template; entity.template = mesh?.metadata?.template;
entity.text = mesh?.metadata?.text; entity.text = mesh?.metadata?.text;
entity.from = mesh?.metadata?.from; entity.from = mesh?.metadata?.from;
entity.to = mesh?.metadata?.to; entity.to = mesh?.metadata?.to;
entity.scale = mesh.scaling; entity.scale = vectoxys(mesh.scaling);
if (mesh.material) { if (mesh.material) {
entity.color = (mesh.material as any).diffuseColor.toHexString(); entity.color = (mesh.material as any).diffuseColor.toHexString();
} else { } else {
@ -31,3 +31,7 @@ export function toDiagramEntity(mesh: AbstractMesh): DiagramEntity {
} }
return entity; return entity;
} }
function vectoxys(v: Vector3): { x, y, z } {
return {x: v.x, y: v.y, z: v.z};
}

View File

@ -1,4 +1,4 @@
import {AbstractMesh, Color3, Observable} from "@babylonjs/core"; import {Color3, Observable} from "@babylonjs/core";
import {DiagramEntity} from "../diagram/diagramEntity"; import {DiagramEntity} from "../diagram/diagramEntity";
import {AppConfigType} from "../util/appConfigType"; import {AppConfigType} from "../util/appConfigType";
@ -33,11 +33,11 @@ export interface IPersistenceManager {
removeDiagram(diagram: DiagramListing); removeDiagram(diagram: DiagramListing);
add(mesh: AbstractMesh); add(mesh: DiagramEntity);
remove(mesh: AbstractMesh); remove(id: string);
modify(mesh: AbstractMesh); modify(mesh: DiagramEntity);
initialize(); initialize();

View File

@ -1,11 +1,9 @@
import {DiagramListing, DiagramListingEvent, DiagramListingEventType, IPersistenceManager} from "./iPersistenceManager"; import {DiagramListing, DiagramListingEvent, DiagramListingEventType, IPersistenceManager} from "./iPersistenceManager";
import {AbstractMesh, Observable, Vector3} from "@babylonjs/core"; import {Observable} from "@babylonjs/core";
import {DiagramEntity} from "../diagram/diagramEntity"; import {DiagramEntity} from "../diagram/diagramEntity";
import Dexie from "dexie"; import Dexie from "dexie";
import log from "loglevel"; import log from "loglevel";
import {AppConfigType} from "../util/appConfigType"; import {AppConfigType} from "../util/appConfigType";
import {toDiagramEntity} from "../diagram/functions/toDiagramEntity";
export class IndexdbPersistenceManager implements IPersistenceManager { export class IndexdbPersistenceManager implements IPersistenceManager {
private readonly logger = log.getLogger('IndexdbPersistenceManager'); private readonly logger = log.getLogger('IndexdbPersistenceManager');
@ -29,18 +27,14 @@ export class IndexdbPersistenceManager implements IPersistenceManager {
this.currentDiagramId = diagram.id; this.currentDiagramId = diagram.id;
} }
public add(mesh: AbstractMesh) { public add(entity: DiagramEntity) {
if (!mesh) { if (!entity) {
this.logger.error("Adding null mesh, early return"); this.logger.error("Adding null mesh, early return");
return; return;
} }
const entity = <any>toDiagramEntity(mesh);
entity.position = this.vectoxys(mesh.position);
entity.rotation = this.vectoxys(mesh.rotation);
entity.scale = this.vectoxys(mesh.scaling);
entity.diagramlistingid = this.currentDiagramId; entity.diagramlistingid = this.currentDiagramId;
this.db["entities"].add(entity); this.db["entities"].add(entity);
this.logger.debug('add', mesh, entity); this.logger.debug('add', entity);
} }
public addDiagram(diagram: DiagramListing) { public addDiagram(diagram: DiagramListing) {
@ -52,12 +46,12 @@ export class IndexdbPersistenceManager implements IPersistenceManager {
this.diagramListingObserver.notifyObservers(event); this.diagramListingObserver.notifyObservers(event);
} }
public remove(mesh: AbstractMesh) { public remove(id: string) {
if (!mesh) { if (!id) {
this.logger.error("Removing null mesh, early return"); this.logger.error("Removing null mesh, early return");
return; return;
} }
this.db["entities"].delete(mesh.id); this.db["entities"].delete(id);
} }
public setConfig(config: AppConfigType) { public setConfig(config: AppConfigType) {
@ -136,9 +130,6 @@ export class IndexdbPersistenceManager implements IPersistenceManager {
); );
} }
this.getFilteredEntities().each((e) => { this.getFilteredEntities().each((e) => {
e.position = this.xyztovec(e.position);
e.rotation = this.xyztovec(e.rotation);
e.scale = this.xyztovec(e.scale);
this.logger.debug('adding', e); this.logger.debug('adding', e);
this.updateObserver.notifyObservers(e); this.updateObserver.notifyObservers(e);
}); });
@ -146,21 +137,13 @@ export class IndexdbPersistenceManager implements IPersistenceManager {
this.logger.info("initialize finished"); this.logger.info("initialize finished");
} }
public modify(mesh) { public modify(entity: DiagramEntity) {
if (!mesh) {
this.logger.error("Modifying null mesh, early return");
return;
}
const entity = <any>toDiagramEntity(mesh);
if (!entity) { if (!entity) {
this.logger.error("Modifying null mesh, early return"); this.logger.error("Modifying null mesh, early return");
return; return;
} }
entity.position = this.vectoxys(mesh.position); this.db["entities"].update(entity.id, entity);
entity.rotation = this.vectoxys(mesh.rotation); this.logger.debug('modify', entity);
entity.scale = this.vectoxys(mesh.scaling);
this.db["entities"].update(mesh.id, entity);
this.logger.debug('modify', mesh, entity);
} }
public changeColor(oldColor, newColor) { public changeColor(oldColor, newColor) {
@ -199,12 +182,4 @@ export class IndexdbPersistenceManager implements IPersistenceManager {
} }
); );
} }
private vectoxys(v: Vector3): { x, y, z } {
return {x: v.x, y: v.y, z: v.z};
}
private xyztovec(xyz: { x, y, z }): Vector3 {
return new Vector3(xyz.x, xyz.y, xyz.z);
}
} }

View File

@ -20,7 +20,7 @@ import {InputTextView} from "../information/inputTextView";
import {DiaSounds} from "../util/diaSounds"; import {DiaSounds} from "../util/diaSounds";
import {TextLabel} from "../diagram/textLabel"; import {TextLabel} from "../diagram/textLabel";
import {DiagramConnection} from "../diagram/diagramConnection"; import {DiagramConnection} from "../diagram/diagramConnection";
import {GLTF2Export} from "@babylonjs/serializers";
import {toDiagramEntity} from "../diagram/functions/toDiagramEntity"; import {toDiagramEntity} from "../diagram/functions/toDiagramEntity";
import {AbstractMenu} from "./abstractMenu"; import {AbstractMenu} from "./abstractMenu";
import {Controllers} from "../controllers/controllers"; import {Controllers} from "../controllers/controllers";
@ -119,7 +119,7 @@ export class EditMenu extends AbstractMenu {
this.diagramManager.onDiagramEventObservable.notifyObservers({ this.diagramManager.onDiagramEventObservable.notifyObservers({
type: DiagramEventType.MODIFY, type: DiagramEventType.MODIFY,
entity: toDiagramEntity(mesh), entity: toDiagramEntity(mesh),
}); }, -1);
} }
toggle() { toggle() {
@ -186,7 +186,7 @@ export class EditMenu extends AbstractMenu {
this.diagramManager.onDiagramEventObservable.notifyObservers({ this.diagramManager.onDiagramEventObservable.notifyObservers({
type: DiagramEventType.MODIFY, type: DiagramEventType.MODIFY,
entity: toDiagramEntity(newMesh) entity: toDiagramEntity(newMesh)
}); }, 2);
} else { } else {
this.logger.error("no paint color selectced"); this.logger.error("no paint color selectced");
@ -200,7 +200,7 @@ export class EditMenu extends AbstractMenu {
this.diagramManager.onDiagramEventObservable.notifyObservers({ this.diagramManager.onDiagramEventObservable.notifyObservers({
type: DiagramEventType.ADD, type: DiagramEventType.ADD,
entity: toDiagramEntity(this.connection.mesh) entity: toDiagramEntity(this.connection.mesh)
}); }, -1);
this.connection = null; this.connection = null;
} else { } else {
this.connection = new DiagramConnection(mesh.id, null, this.scene, pointerInfo); this.connection = new DiagramConnection(mesh.id, null, this.scene, pointerInfo);
@ -214,7 +214,8 @@ export class EditMenu extends AbstractMenu {
entity: entity:
toDiagramEntity(mesh) toDiagramEntity(mesh)
} }
this.diagramManager.onDiagramEventObservable.notifyObservers(event);
this.diagramManager.onDiagramEventObservable.notifyObservers(event, -1);
} }
private modifyMesh(mesh: AbstractMesh) { private modifyMesh(mesh: AbstractMesh) {
@ -228,7 +229,7 @@ export class EditMenu extends AbstractMenu {
this.diagramManager.onDiagramEventObservable.notifyObservers({ this.diagramManager.onDiagramEventObservable.notifyObservers({
type: DiagramEventType.MODIFY, type: DiagramEventType.MODIFY,
entity: toDiagramEntity(mesh), entity: toDiagramEntity(mesh),
} }, -1
) )
this.logger.debug(mesh.scaling); this.logger.debug(mesh.scaling);
}); });
@ -306,7 +307,9 @@ export class EditMenu extends AbstractMenu {
this.showNewRelic(); this.showNewRelic();
break; break;
case "export": case "export":
GLTF2Export.GLBAsync(this.scene, 'diagram.glb', { import("@babylonjs/serializers").then((serializers) => {
serializers.GLTF2Export.GLBAsync(this.scene, 'diagram.glb', {
shouldExportNode: function (node) { shouldExportNode: function (node) {
if (node?.metadata?.template) { if (node?.metadata?.template) {
return true; return true;
@ -318,6 +321,10 @@ export class EditMenu extends AbstractMenu {
}).then((gltf) => { }).then((gltf) => {
gltf.downloadFiles(); gltf.downloadFiles();
}); });
});
break; break;
default: default:
this.logger.error("Unknown button"); this.logger.error("Unknown button");

View File

@ -1,22 +1,26 @@
import {Observable} from "@babylonjs/core"; import {Observable} from "@babylonjs/core";
import {IPersistenceManager} from "../integration/iPersistenceManager"; //import {IPersistenceManager} from "../integration/iPersistenceManager";
import {AppConfigType} from "./appConfigType"; import {AppConfigType} from "./appConfigType";
export class AppConfig { export class AppConfig {
public readonly onConfigChangedObservable = new Observable<AppConfigType>(); public readonly onConfigChangedObservable = new Observable<AppConfigType>();
private _currentConfig: AppConfigType; private _currentConfig: AppConfigType;
private persistenceManager: IPersistenceManager;
constructor(persistenceManager: IPersistenceManager) { // private persistenceManager: IPersistenceManager;
this.persistenceManager = persistenceManager;
this.persistenceManager.configObserver.add(this.load, -1, false, this, false); constructor() {
this.onConfigChangedObservable.add((config, state) => {
console.log(state);
this._currentConfig = config;
}, 2);
//this.persistenceManager = persistenceManager;
//this.persistenceManager.configObserver.add(this.load, -1, false, this, false);
} }
public get current(): AppConfigType { public get current(): AppConfigType {
if (!this._currentConfig) { if (!this._currentConfig) {
this.persistenceManager.getConfig().then((config) => {
if (!config) {
this._currentConfig = { this._currentConfig = {
id: 1, id: 1,
gridSnap: .1, gridSnap: .1,
@ -28,26 +32,22 @@ export class AppConfig {
physicsEnabled: false, physicsEnabled: false,
demoCompleted: false, demoCompleted: false,
} }
this.save();
} else {
this._currentConfig = config;
}
});
} }
return this._currentConfig; return this._currentConfig;
} }
public set current(config: AppConfigType) { public set current(config: AppConfigType) {
this._currentConfig = config; this.onConfigChangedObservable.notifyObservers(config, 2);
this.save(); this.save();
} }
public save() { public save() {
this.persistenceManager.setConfig(this._currentConfig); //this.persistenceManager.setConfig(this._currentConfig);
} }
public load(config: AppConfigType) { public load(config: AppConfigType) {
this._currentConfig = config; this._currentConfig = config;
this.onConfigChangedObservable.notifyObservers(this._currentConfig); this.onConfigChangedObservable.notifyObservers(this._currentConfig, 1);
} }
} }

View File

@ -32,7 +32,7 @@ export class CustomEnvironment {
this._groundMeshObservable.notifyObservers(ground); this._groundMeshObservable.notifyObservers(ground);
}); });
const photo = new PhotoDome('sky', const photo = new PhotoDome('sky',
'/assets/textures/outdoor_field2.jpeg', {}, '/assets/textures/outdoor_field4.jpeg', {},
scene); scene);
try { try {
const sounds = new DiaSounds(scene); const sounds = new DiaSounds(scene);

36
src/worker.ts Normal file
View File

@ -0,0 +1,36 @@
import {IndexdbPersistenceManager} from "./integration/indexdbPersistenceManager";
import {DiagramEvent, DiagramEventType} from "./diagram/diagramEntity";
const persistenceManager = new IndexdbPersistenceManager("diagram");
const ctx: Worker = self as any;
ctx.onmessage = (event) => {
console.log(event.data);
if (event.data.type == 'init') {
persistenceManager.updateObserver.add((event) => {
ctx.postMessage({entity: event});
});
persistenceManager.configObserver.add((event) => {
ctx.postMessage({config: event});
});
persistenceManager.initialize().then(() => {
console.log('initialized');
});
} else {
const data = (event.data as DiagramEvent);
switch (data.type) {
case DiagramEventType.ADD:
persistenceManager.add(data.entity);
break;
case DiagramEventType.MODIFY:
persistenceManager.modify(data.entity);
break;
case DiagramEventType.REMOVE:
persistenceManager.remove(data.entity.id);
break;
}
}
};