From c7887d7d8fe34dcdbf40b6993b81136c7990562a Mon Sep 17 00:00:00 2001 From: Michael Mainguy Date: Thu, 13 Nov 2025 09:44:56 -0600 Subject: [PATCH] Optimize lightmap rendering using emissive texture approach MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Changed from lightmapTexture with lighting enabled to emissiveTexture with lighting disabled for better performance. The new approach provides the same lighting illusion without expensive per-pixel lighting calculations. - Added LightmapGenerator.ENABLED toggle for performance testing - Updated buildColor.ts to use emissiveColor + emissiveTexture with disableLighting = true - Updated buildMissingMaterial() to match new rendering approach - Fixed buildTool.ts to access emissiveColor instead of diffuseColor for material color detection 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude --- .../functions/buildMeshFromDiagramEntity.ts | 16 ++++++++++++---- src/toolbox/functions/buildColor.ts | 16 +++++++++++----- src/toolbox/functions/buildTool.ts | 7 +++++-- src/util/lightmapGenerator.ts | 3 +++ 4 files changed, 31 insertions(+), 11 deletions(-) diff --git a/src/diagram/functions/buildMeshFromDiagramEntity.ts b/src/diagram/functions/buildMeshFromDiagramEntity.ts index 71e6c05..33a0446 100644 --- a/src/diagram/functions/buildMeshFromDiagramEntity.ts +++ b/src/diagram/functions/buildMeshFromDiagramEntity.ts @@ -194,10 +194,18 @@ export function buildMissingMaterial(name: string, scene: Scene, color: string): const colorObj = Color3.FromHexString(color); const newMaterial = new StandardMaterial(name, scene); newMaterial.id = name; - newMaterial.diffuseColor = colorObj; - newMaterial.disableLighting = false; - newMaterial.lightmapTexture = LightmapGenerator.generateLightmapForColor(colorObj, scene); - newMaterial.useLightmapAsShadowmap = false; + + if (LightmapGenerator.ENABLED) { + // Lightmap as emissive texture (lighting illusion, no lighting calculations) + newMaterial.emissiveColor = colorObj; + newMaterial.emissiveTexture = LightmapGenerator.generateLightmapForColor(colorObj, scene); + newMaterial.disableLighting = true; + } else { + // Flat emissive-only rendering (no lighting illusion) + newMaterial.emissiveColor = colorObj; + newMaterial.disableLighting = true; + } + newMaterial.alpha = 1; return newMaterial; } \ No newline at end of file diff --git a/src/toolbox/functions/buildColor.ts b/src/toolbox/functions/buildColor.ts index 1ceb69b..97b46e0 100644 --- a/src/toolbox/functions/buildColor.ts +++ b/src/toolbox/functions/buildColor.ts @@ -17,11 +17,17 @@ export async function buildColor(color: Color3, scene: Scene, parent: TransformN const width = .1; const height = .1; const material = new StandardMaterial("material-" + color.toHexString(), scene); - material.diffuseColor = color; - material.disableLighting = false; - material.lightmapTexture = LightmapGenerator.generateLightmapForColor(color, scene); - material.useLightmapAsShadowmap = false; - material.specularPower = 64; + + if (LightmapGenerator.ENABLED) { + // Lightmap as emissive texture (lighting illusion, no lighting calculations) + material.emissiveColor = color; + material.emissiveTexture = LightmapGenerator.generateLightmapForColor(color, scene); + material.disableLighting = true; + } else { + // Flat emissive-only rendering (no lighting illusion) + material.emissiveColor = color; + material.disableLighting = true; + } const colorBoxMesh = MeshBuilder.CreatePlane("toolbox-color-" + color.toHexString(), { width: width, diff --git a/src/toolbox/functions/buildTool.ts b/src/toolbox/functions/buildTool.ts index e70cc93..53c55b8 100644 --- a/src/toolbox/functions/buildTool.ts +++ b/src/toolbox/functions/buildTool.ts @@ -9,8 +9,11 @@ export async function buildTool(tool: ToolType, colorParent: AbstractMesh, mater let color = "#000000"; switch (material.getClassName()) { case "StandardMaterial": - id = toolId(tool, (material as StandardMaterial).diffuseColor); - color = (material as StandardMaterial).diffuseColor.toHexString(); + const stdMat = material as StandardMaterial; + // Use emissiveColor (set for both lightmap and non-lightmap modes) + const materialColor = stdMat.emissiveColor || stdMat.diffuseColor; + id = toolId(tool, materialColor); + color = materialColor.toHexString(); break; case "PBRMaterial": id = toolId(tool, (material as PBRMaterial).albedoColor); diff --git a/src/util/lightmapGenerator.ts b/src/util/lightmapGenerator.ts index 07402f1..e7d0757 100644 --- a/src/util/lightmapGenerator.ts +++ b/src/util/lightmapGenerator.ts @@ -5,6 +5,9 @@ export class LightmapGenerator { private static lightmapCache: Map = new Map(); private static readonly DEFAULT_RESOLUTION = 512; + // Toggle to enable/disable lightmap usage (for performance testing) + public static ENABLED = true; + /** * Generates or retrieves cached lightmap for a given color * @param color The base color for the lightmap