Skip to content

Commit

Permalink
Update lightmap manager.
Browse files Browse the repository at this point in the history
  • Loading branch information
digisomni committed May 1, 2024
1 parent d4f8484 commit 0a052f5
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 40 deletions.
3 changes: 2 additions & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,7 @@ module.exports = {
"@typescript-eslint/no-use-before-define": ["error"],
"no-useless-constructor": "off",
"@typescript-eslint/no-useless-constructor": ["error"],
"@typescript-eslint/no-misused-promises": ["off"],
"object-curly-spacing": "off",
"@typescript-eslint/object-curly-spacing": ["error", "always"],
quotes: "off",
Expand Down Expand Up @@ -327,7 +328,7 @@ module.exports = {
"jsx-quotes": ["error", "prefer-double"],
"key-spacing": "error",
// "keyword-spacing": "error", // TypeScript extension overrides.
"max-len": ["error", { code: 160, tabWidth: 4 }],
"max-len": ["error", { code: 270, tabWidth: 4 }],
"multiline-ternary": ["error", "always-multiline"],
"new-cap": "off",
"new-parens": "error",
Expand Down
90 changes: 51 additions & 39 deletions src/modules/scene/LightmapManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@
import {
type AbstractMesh,
type Scene,
BaseTexture,
PBRMaterial,
Texture,
ImageProcessingConfiguration
} from "@babylonjs/core";
import Log from "../debugging/log";
import { glTF as MeshTypes } from "../../../types/vircadia_gameUse";
Expand All @@ -26,10 +26,6 @@ export class LightmapManager {
// //// HANDLE MASTER LIGHTMAP DATA
// ////

// const postprocess = scene.imageProcessingConfiguration;
// postprocess.toneMappingEnabled = true;
// postprocess.toneMappingType = ImageProcessingConfiguration.TONEMAPPING_ACES;

let lightmapColorSpace = null;
let lightmapLevel = null;
let lightmapMode = null;
Expand All @@ -48,7 +44,7 @@ export class LightmapManager {

if (metadata.vircadia_lightmap_level) {
Log.debug(Log.types.ENTITIES, `Found lightmap level for all meshes as ${metadata.vircadia_lightmap_level}`);
lightmapLevel = Number(metadata.vircadia_lightmap_level);
lightmapLevel = 2; // Number(metadata.vircadia_lightmap_level);
}

if (metadata.vircadia_lightmap_color_space) {
Expand Down Expand Up @@ -109,52 +105,68 @@ export class LightmapManager {
}

const materialToUse = material as PBRMaterial;

if (materialToUse
&& materialToUse.albedoTexture
&& mesh.material
&& metadata.vircadia_lightmap_texcoord) {
&& Boolean(metadata.vircadia_lightmap_texcoord)) {

Texture.WhenAllReady([materialToUse.albedoTexture], () => {
(mesh.material as PBRMaterial).lightmapTexture = materialToUse.albedoTexture;
(mesh.material as PBRMaterial).useLightmapAsShadowmap = metadata.vircadia_lightmap_use_as_shadowmap ?? true;

if ((mesh.material as PBRMaterial).lightmapTexture && metadata.vircadia_lightmap_texcoord) {
(mesh.material as PBRMaterial).lightmapTexture!.coordinatesIndex = metadata.vircadia_lightmap_texcoord;
try {
const lightmapTexture: Nullable<BaseTexture> = materialToUse.albedoTexture;

if (lightmapTexture) {
(mesh.material as PBRMaterial).lightmapTexture = lightmapTexture;
(mesh.material as PBRMaterial).useLightmapAsShadowmap = metadata.vircadia_lightmap_use_as_shadowmap ?? true;

if ((mesh.material as PBRMaterial).lightmapTexture && metadata.vircadia_lightmap_texcoord) {
(mesh.material as PBRMaterial).lightmapTexture!.coordinatesIndex = metadata.vircadia_lightmap_texcoord;
}
}
} catch (e) {
Log.error(Log.types.ENTITIES, `Error setting lightmap texture for: ${mesh.name}, error: ${e}`);
}
});
} else {

Log.error(Log.types.ENTITIES, `Could not find material or albedo texture for: ${mesh.name}`);
}
}

mesh.material?.getActiveTextures().forEach((texture) => {
if (texture instanceof Texture) {
if (lightmapLevel) {
texture.level = lightmapLevel;
}

if (lightmapColorSpace) {
switch (lightmapColorSpace) {
case MeshTypes.Texture.ColorSpace.LINEAR:
Log.debug(Log.types.ENTITIES, `Setting color space for ${mesh.name} to linear.`);
texture.gammaSpace = false;
break;
case MeshTypes.Texture.ColorSpace.GAMMA:
Log.debug(Log.types.ENTITIES, `Setting color space for ${mesh.name} to gamma.`);
texture.gammaSpace = true;
break;
case MeshTypes.Texture.ColorSpace.SRGB:
Log.debug(Log.types.ENTITIES, `Setting color space for ${mesh.name} to sRGB.`);
texture.gammaSpace = true;
break;
default:
Log.debug(Log.types.ENTITIES, `Setting color space for ${mesh.name} to gamma.`);
texture.gammaSpace = true;
break;
if (mesh.material) {
mesh.material?.getActiveTextures().forEach((texture) => {
if (texture instanceof Texture) {
if (lightmapLevel) {
texture.level = lightmapLevel;
}

if (lightmapColorSpace) {
switch (lightmapColorSpace) {
case MeshTypes.Texture.ColorSpace.LINEAR:
Log.debug(Log.types.ENTITIES, `Setting color space for ${mesh.name} to linear.`);
texture.gammaSpace = false;
break;
case MeshTypes.Texture.ColorSpace.GAMMA:
Log.debug(Log.types.ENTITIES, `Setting color space for ${mesh.name} to gamma.`);
texture.gammaSpace = true;
break;
case MeshTypes.Texture.ColorSpace.SRGB:
Log.debug(Log.types.ENTITIES, `Setting color space for ${mesh.name} to sRGB.`);
texture.gammaSpace = true;
break;
default:
Log.debug(Log.types.ENTITIES, `Setting color space for ${mesh.name} to gamma.`);
texture.gammaSpace = true;
break;
}
}

if (lightmapLevel) {
texture.level = lightmapLevel;
}
}
}
});
}
});
}
});

return meshes;
Expand Down

0 comments on commit 0a052f5

Please sign in to comment.