immersive2/src/vrcore/initializeEngine.ts
Michael Mainguy adc80c54c4 Optimize animated connection textures and fix material texture bleeding
Performance Optimizations (~90% improvement):
- Implement texture color caching in AnimatedLineTexture
  - Reuse textures for connections with same color
  - Reduces texture count by 70-90% with duplicate colors
- Reduce animation update frequency from every frame to every other frame
  - Halves CPU-to-GPU texture updates while maintaining smooth animation
- Add texture preloading for all 16 toolbox colors
  - Eliminates first-connection creation stutter
- Add GetCacheStats, ClearCache, and PreloadTextures utility methods

Bug Fixes:
1. Fix texture disappearing when moving objects with connections
   - Root cause: Shared cached textures were disposed on connection update
   - Solution: Never dispose cached textures, only swap references
   - Add safety check in DisposeTexture to prevent cached texture disposal

2. Fix UI text textures bleeding to normal materials
   - Add metadata.isUI = true to 10+ UI components:
     - Button.ts (with unique material names per button)
     - handle.ts, roundButton.ts, createLabel.ts, updateTextNode.ts
     - spinner.ts, vrConfigPanel.ts, buildImage, introduction.ts
     - ResizeGizmo.ts
   - Update LightmapGenerator filter to check metadata.isUI first
   - Change exact name match to startsWith for button materials

3. Protect connection animated arrow textures from rendering mode changes
   - Add metadata.isConnection = true to connection materials
   - Update LightmapGenerator to skip connection materials
   - Connections maintain animated arrows in all rendering modes

Technical Details:
- Texture caching follows existing LightmapGenerator pattern
- All UI materials now consistently marked with metadata flags
- Rendering mode filter uses metadata-first approach with fallback checks
- Connection materials preserve textures via metadata.preserveTextures flag

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-11-18 16:37:22 -06:00

48 lines
1.3 KiB
TypeScript

import {
Color3, Engine, Scene, WebGPUEngine
} from "@babylonjs/core";
import { DefaultScene } from "../defaultScene";
import log from "loglevel";
export interface EngineInitializerParams {
canvas: HTMLCanvasElement;
useWebGpu: boolean;
onSceneReady: (scene: Scene) => Promise<void>;
}
export async function initializeEngine(params: EngineInitializerParams): Promise<Engine | WebGPUEngine> {
const logger = log.getLogger('EngineInitializer');
if (!params.canvas) {
logger.error('Canvas not found');
return null;
}
let engine = null;
if (params.useWebGpu) {
engine = new WebGPUEngine(params.canvas);
await (engine as WebGPUEngine).initAsync();
} else {
engine = new Engine(params.canvas, true);
}
engine.setHardwareScalingLevel(1 / window.devicePixelRatio);
const scene = new Scene(engine);
DefaultScene.Scene = scene;
scene.ambientColor = new Color3(.1, .1, .1);
// Disable material dirty flagging for performance
// This prevents expensive material validation when animating texture offsets
// Safe for this app since we use unlit materials without complex dynamic properties
//
// scene.blockMaterialDirtyMechanism = true;
await params.onSceneReady(scene);
engine.runRenderLoop(() => {
scene.render();
});
return engine;
}