Optimize lightmap rendering using emissive texture approach

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 <noreply@anthropic.com>
This commit is contained in:
Michael Mainguy 2025-11-13 09:44:56 -06:00
parent 3f02fc7ea5
commit c7887d7d8f
4 changed files with 31 additions and 11 deletions

View File

@ -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;
}

View File

@ -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,

View File

@ -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);

View File

@ -5,6 +5,9 @@ export class LightmapGenerator {
private static lightmapCache: Map<string, DynamicTexture> = 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