Skip to content

Commit

Permalink
Restore geometry coloring after updates
Browse files Browse the repository at this point in the history
  • Loading branch information
DraTeots committed Feb 4, 2025
1 parent 7fffec2 commit b2abc3c
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 59 deletions.
10 changes: 5 additions & 5 deletions firebird-ng/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

79 changes: 46 additions & 33 deletions firebird-ng/src/app/pages/main-display/main-display.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ import {EventDisplay} from "phoenix-event-display";
import Stats from 'three/examples/jsm/libs/stats.module.js';
import {PerfStatsComponent} from "../../components/perf-stats/perf-stats.component";
import {PerfService} from "../../services/perf.service";
import {EventDisplayService} from "../../services/event-display.service";



Expand Down Expand Up @@ -156,11 +157,12 @@ export class MainDisplayComponent implements OnInit, AfterViewInit, OnDestroy {
private settings: UserConfigService,
private dataService: DataModelService,
private urlService: UrlService,
private _snackBar: MatSnackBar,
private snackBar: MatSnackBar,
private perfService: PerfService,
private eventDisplayService: EventDisplayService
) {}

// 1) MAIN INIT

async ngOnInit() {
// Initialize the ThreeService scene/camera/renderer/controls
this.threeService.init('eventDisplay');
Expand Down Expand Up @@ -201,27 +203,36 @@ export class MainDisplayComponent implements OnInit, AfterViewInit, OnDestroy {
}
});

// 2) LOAD GEOMETRY
try {
const { rootGeometry, threeGeometry } = await this.geomService.loadGeometry();
if (threeGeometry) {
// Optionally, attach geometry to a group "Geometries"
let geoGroup = this.threeService.scene.getObjectByName('Geometries') as THREE.Group;
if (!geoGroup) {
geoGroup = new THREE.Group();
geoGroup.name = 'Geometries';
this.threeService.scene.add(geoGroup);
}
geoGroup.add(threeGeometry);

// Process geometry if needed
this.threeGeometryProcessor.process(this.geomService.subdetectors);
// // 2) LOAD GEOMETRY
// try {
// const { rootGeometry, threeGeometry } = await this.geomService.loadGeometry();
// if (threeGeometry) {
// // Optionally, attach geometry to a group "Geometries"
// let geoGroup = this.threeService.scene.getObjectByName('Geometries') as THREE.Group;
// if (!geoGroup) {
// geoGroup = new THREE.Group();
// geoGroup.name = 'Geometries';
// this.threeService.scene.add(geoGroup);
// }
// geoGroup.add(threeGeometry);
//
// // Process geometry if needed
// this.threeGeometryProcessor.process(this.geomService.subdetectors);
//
// this.loaded = true;
// }
// } catch (err) {
// console.error('ERROR LOADING GEOMETRY', err);
// }

this.eventDisplayService.loadGeometry().catch(error=>{
const msg = `Error loading geometry: ${error}`;
console.error(msg);
this.showError(msg);
}).then(value=>{
console.log("[main-display] Geometry loaded");
})

this.loaded = true;
}
} catch (err) {
console.error('ERROR LOADING GEOMETRY', err);
}

// 3) LOAD DEX or other event data
// example: load Dex data, then attach to an "EventData" group
Expand Down Expand Up @@ -275,16 +286,7 @@ export class MainDisplayComponent implements OnInit, AfterViewInit, OnDestroy {
this.onRendererElementResize();
});

// Initialize Stats
//this.eventDisplayDiv.nativeElement.appendChild(this.stats.dom); // Add stats to your container

// Position the stats panel (example: bottom-left)
const statsStyle = this.stats.dom.style;
statsStyle.position = 'absolute'; // Essential for positioning
statsStyle.bottom = '10px'; // Adjust as needed
statsStyle.left = '10px'; // Adjust as needed
// If you want to make it always on top:
statsStyle.zIndex = '100'; // Or some other high value
// Include performance stats:
let this_obj = this;
this.threeService.profileBeginFunc = ()=>this_obj.perfService.updateStats(this_obj.threeService.renderer);
this.threeService.profileEndFunc = this.stats.end;
Expand All @@ -305,13 +307,24 @@ export class MainDisplayComponent implements OnInit, AfterViewInit, OnDestroy {
this.displayShellComponent?.toggleLeftPane();
this.isLeftPaneOpen = !this.isLeftPaneOpen;
}

toggleRightPane() {
this.displayShellComponent?.toggleRightPane();
}

togglePhoenixMenu() {
this.isPhoenixMenuOpen = !this.isPhoenixMenuOpen;
}

// Example function to show an error
showError(message: string) {
this.snackBar.open(message, 'Dismiss', {
duration: 5000, // Auto-dismiss after 5 seconds
verticalPosition: 'top', // Place at the top of the screen
panelClass: ['mat-mdc-snack-bar-error'] // Optional: Custom styling (MD3)
});
}

// 4) UI - window resize detection
@HostListener('window:resize', ['$event'])
onResize(event: any) {
Expand Down Expand Up @@ -496,7 +509,7 @@ export class MainDisplayComponent implements OnInit, AfterViewInit, OnDestroy {
trackInfo.trackNode.visible = false;
}
}
this._snackBar.open(`Showing event: ${eventName}`, 'Dismiss', {
this.snackBar.open(`Showing event: ${eventName}`, 'Dismiss', {
duration: 2000,
horizontalPosition: 'right',
verticalPosition: 'top'
Expand Down
38 changes: 20 additions & 18 deletions firebird-ng/src/app/services/event-display.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { DataModelService } from './data-model.service';
import { UserConfigService } from './user-config.service';
import { UrlService } from './url.service';

import { getColorOrDefault } from '../utils/three.utils';
import {disposeHierarchy, disposeNode, getColorOrDefault} from '../utils/three.utils';
import { ThreeGeometryProcessor } from '../data-pipelines/three-geometry.processor';
import { ThreeEventProcessor } from '../data-pipelines/three-event.processor';
import { DataModelPainter } from '../painters/data-model-painter';
Expand Down Expand Up @@ -36,7 +36,7 @@ export class EventDisplayService {

// Geometry
private threeGeometryProcessor = new ThreeGeometryProcessor();
private defaultColor: Color = new Color(0x2fd691);
private defaultColor: Color = new Color(0x68698D);
currentGeometry: string = 'All';
private animateEventAfterLoad: boolean = false;
private trackInfos: any | null = null; // Replace 'any' with the actual type
Expand Down Expand Up @@ -100,7 +100,7 @@ export class EventDisplayService {
);
}

stopAnimation(): void {
stopTimeAnimation(): void {
if (this.tween) {
this.tween.stop(); // Stops the tween if it is running
this.tween = null; // Remove reference
Expand All @@ -113,7 +113,7 @@ export class EventDisplayService {

animateCurrentTime(targetTime: number, duration: number): void {
if (this.tween) {
this.stopAnimation();
this.stopTimeAnimation();
}
this.tween = new TWEEN.Tween({ currentTime: this.eventTime() })
.to({ currentTime: targetTime }, duration)
Expand All @@ -125,7 +125,7 @@ export class EventDisplayService {
}

animateWithCollision() {
this.stopAnimation();
this.stopTimeAnimation();
this.rewindTime();
if (this.trackInfos) {
for (let trackInfo of this.trackInfos) {
Expand Down Expand Up @@ -155,7 +155,7 @@ export class EventDisplayService {
}

exitTimedDisplay() {
this.stopAnimation();
this.stopTimeAnimation();
this.rewindTime();
this.painter.paint(null);
this.animateEventAfterLoad = false;
Expand All @@ -173,25 +173,28 @@ export class EventDisplayService {

/**
* Load geometry
* @param initiallyVisible
* @param scale
*/
async loadGeometry(initiallyVisible = true, scale = 10) {
let { rootGeometry, threeGeometry } =
await this.geomService.loadGeometry();
async loadGeometry(scale = 10, clearGeometry=true) {
let { rootGeometry, threeGeometry } = await this.geomService.loadGeometry();
if (!threeGeometry) return;

const sceneGeometry = this.three.sceneGeometry;

// Set geometry scale
if (scale) {
threeGeometry.scale.setScalar(scale);
}

sceneGeometry.add(threeGeometry);
const sceneGeo = this.three.sceneGeometry;

// Now we want to change the materials
sceneGeometry.traverse((child: any) => {
// There should be only one geometry if clearGeometry=true
if(clearGeometry && sceneGeo.children.length > 0) {
disposeHierarchy(sceneGeo, /* disposeSelf= */ false);
}

sceneGeo.add(threeGeometry);

// Now we want to set default materials
sceneGeo.traverse((child: any) => {
if (child.type !== 'Mesh' || !child?.material?.isMaterial) {
return;
}
Expand All @@ -200,7 +203,6 @@ export class EventDisplayService {
child.userData['size'] = 1; //this.importManager.getObjectSize(child);

// Handle the material of the child

const color = getColorOrDefault(child.material, this.defaultColor);
const side = DoubleSide;

Expand Down Expand Up @@ -239,7 +241,7 @@ export class EventDisplayService {
this.threeGeometryProcessor.process(this.geomService.subdetectors);

// Now we want to change the materials
sceneGeometry.traverse((child: any) => {
sceneGeo.traverse((child: any) => {
if (!child?.material?.isMaterial) {
return;
}
Expand Down Expand Up @@ -288,7 +290,7 @@ export class EventDisplayService {
if (typeof Worker !== 'undefined') {
// Create a new
const worker = new Worker(
new URL('../../workers/event-loader.worker.ts', import.meta.url)
new URL('../workers/event-loader.worker.ts', import.meta.url)
);
worker.onmessage = ({ data }) => {
for (let key in data) {
Expand Down
2 changes: 1 addition & 1 deletion firebird-ng/src/app/services/three.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ export class ThreeService implements OnDestroy {
this.scene.add(this.sceneHelpers);

// 2) Create cameras
this.perspectiveCamera = new THREE.PerspectiveCamera(60, 1, 0.1, 10000);
this.perspectiveCamera = new THREE.PerspectiveCamera(60, 1, 1, 40000);
this.perspectiveCamera.position.set(0, 100, 200);

this.orthographicCamera = new THREE.OrthographicCamera(-1, 1, 1, -1, 0.1, 10000);
Expand Down
8 changes: 6 additions & 2 deletions firebird-ng/src/app/utils/three.utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -369,13 +369,17 @@ export function disposeOriginalMeshesAfterMerge(mergeResult: MergeResult): void
*
* @function disposeHierarchy
* @param {THREE.Object3D} node - The root node of the hierarchy to dispose of.
* @param disposeSelf - disposes this node too (if false - only children and their hierarchies will be disposed)
*/
export function disposeHierarchy(node: THREE.Object3D): void {
export function disposeHierarchy(node: THREE.Object3D, disposeSelf=true): void {
// Clone the children array and iterate in reverse order
node.children.slice().reverse().forEach(child => {
disposeHierarchy(child);
});
disposeNode(node);

if(disposeSelf) {
disposeNode(node);
}
}

/**
Expand Down

0 comments on commit b2abc3c

Please sign in to comment.