Skip to content

Commit

Permalink
Init v0.1 of LightmapManager.
Browse files Browse the repository at this point in the history
  • Loading branch information
digisomni committed Feb 8, 2024
1 parent 209325b commit 0162a5d
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 221 deletions.
5 changes: 5 additions & 0 deletions src/modules/entity/components/components/ModelComponent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import { updateContentLoadingProgress } from "@Modules/scene/LoadingScreen";
import { applicationStore } from "@Stores/index";
import Log from "@Modules/debugging/log";
import { LODManager } from "@Modules/scene/LODManager";
import { LightmapManager } from "@Modules/scene/LightmapManager";

const InteractiveModelTypes = [
{ name: "chair", condition: /^(?:animate_sitting|animate_seat)/iu },
Expand Down Expand Up @@ -70,6 +71,10 @@ export class ModelComponent extends MeshComponent {
let meshes = result.meshes;
// LOD Handling
meshes = LODManager.setLODLevels(meshes);
// Lightmap Handling
if (this._gameObject?.getScene()) {
meshes = LightmapManager.applySceneLightmapsToMeshes(meshes, this._gameObject.getScene());
}

this.mesh = meshes[0];
this.renderGroupId = DEFAULT_MESH_RENDER_GROUP_ID;
Expand Down
25 changes: 8 additions & 17 deletions src/modules/scene/LODManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import {
Mesh
} from "@babylonjs/core";
import Log from "../debugging/log";
import { Mesh as MeshTypes } from "../../../types/vircadia_gameUse";
import { glTF as MeshTypes } from "../../../types/vircadia_gameUse";

export const StringsAsBillboardModes: { [key: string]: MeshTypes.BillboardModes } = {
none: MeshTypes.BillboardModes.BILLBOARDMODE_NONE,
Expand Down Expand Up @@ -97,14 +97,7 @@ export class LODManager {
const meshExtras = mesh.metadata?.gltf?.extras;

Check warning on line 97 in src/modules/scene/LODManager.ts

View workflow job for this annotation

GitHub Actions / ⚒️ Build

Unsafe assignment of an `any` value

Check warning on line 97 in src/modules/scene/LODManager.ts

View workflow job for this annotation

GitHub Actions / ⚒️ Build

Unsafe member access .gltf on an `any` value

Check warning on line 97 in src/modules/scene/LODManager.ts

View workflow job for this annotation

GitHub Actions / ⚒️ Build

Unsafe assignment of an `any` value

Check warning on line 97 in src/modules/scene/LODManager.ts

View workflow job for this annotation

GitHub Actions / ⚒️ Build

Unsafe member access .gltf on an `any` value

Check warning on line 97 in src/modules/scene/LODManager.ts

View workflow job for this annotation

GitHub Actions / 🎉 Deploy

Unsafe assignment of an `any` value

Check warning on line 97 in src/modules/scene/LODManager.ts

View workflow job for this annotation

GitHub Actions / 🎉 Deploy

Unsafe member access .gltf on an `any` value

Check warning on line 97 in src/modules/scene/LODManager.ts

View workflow job for this annotation

GitHub Actions / desktop-build (ubuntu-20.04)

Unsafe assignment of an `any` value

Check warning on line 97 in src/modules/scene/LODManager.ts

View workflow job for this annotation

GitHub Actions / desktop-build (ubuntu-20.04)

Unsafe member access .gltf on an `any` value

Check warning on line 97 in src/modules/scene/LODManager.ts

View workflow job for this annotation

GitHub Actions / desktop-build (macos-latest)

Unsafe assignment of an `any` value

Check warning on line 97 in src/modules/scene/LODManager.ts

View workflow job for this annotation

GitHub Actions / desktop-build (macos-latest)

Unsafe member access .gltf on an `any` value

Check warning on line 97 in src/modules/scene/LODManager.ts

View workflow job for this annotation

GitHub Actions / desktop-build (windows-latest)

Unsafe assignment of an `any` value

Check warning on line 97 in src/modules/scene/LODManager.ts

View workflow job for this annotation

GitHub Actions / desktop-build (windows-latest)

Unsafe member access .gltf on an `any` value
const parentExtras = mesh.parent?.metadata?.gltf?.extras;

Check warning on line 98 in src/modules/scene/LODManager.ts

View workflow job for this annotation

GitHub Actions / ⚒️ Build

Unsafe assignment of an `any` value

Check warning on line 98 in src/modules/scene/LODManager.ts

View workflow job for this annotation

GitHub Actions / ⚒️ Build

Unsafe member access .gltf on an `any` value

Check warning on line 98 in src/modules/scene/LODManager.ts

View workflow job for this annotation

GitHub Actions / ⚒️ Build

Unsafe assignment of an `any` value

Check warning on line 98 in src/modules/scene/LODManager.ts

View workflow job for this annotation

GitHub Actions / ⚒️ Build

Unsafe member access .gltf on an `any` value

Check warning on line 98 in src/modules/scene/LODManager.ts

View workflow job for this annotation

GitHub Actions / 🎉 Deploy

Unsafe assignment of an `any` value

Check warning on line 98 in src/modules/scene/LODManager.ts

View workflow job for this annotation

GitHub Actions / 🎉 Deploy

Unsafe member access .gltf on an `any` value

Check warning on line 98 in src/modules/scene/LODManager.ts

View workflow job for this annotation

GitHub Actions / desktop-build (ubuntu-20.04)

Unsafe assignment of an `any` value

Check warning on line 98 in src/modules/scene/LODManager.ts

View workflow job for this annotation

GitHub Actions / desktop-build (ubuntu-20.04)

Unsafe member access .gltf on an `any` value

Check warning on line 98 in src/modules/scene/LODManager.ts

View workflow job for this annotation

GitHub Actions / desktop-build (macos-latest)

Unsafe assignment of an `any` value

Check warning on line 98 in src/modules/scene/LODManager.ts

View workflow job for this annotation

GitHub Actions / desktop-build (macos-latest)

Unsafe member access .gltf on an `any` value

Check warning on line 98 in src/modules/scene/LODManager.ts

View workflow job for this annotation

GitHub Actions / desktop-build (windows-latest)

Unsafe assignment of an `any` value

Check warning on line 98 in src/modules/scene/LODManager.ts

View workflow job for this annotation

GitHub Actions / desktop-build (windows-latest)

Unsafe member access .gltf on an `any` value

const meshMetadata: MeshTypes.Metadata = {
vircadia_lod_mode: undefined,
vircadia_lod_auto: undefined,
vircadia_lod_distance: undefined,
vircadia_lod_size: undefined,
vircadia_lod_hide: undefined,
vircadia_billboard_mode: undefined
};
const meshMetadata = new MeshTypes.Metadata();

if (meshExtras === null || meshExtras === undefined && parentExtras !== null || parentExtras !== undefined) {
meshMetadata.vircadia_lod_mode = parentExtras?.vircadia_lod_mode;

Check warning on line 103 in src/modules/scene/LODManager.ts

View workflow job for this annotation

GitHub Actions / ⚒️ Build

Unsafe assignment of an `any` value

Check warning on line 103 in src/modules/scene/LODManager.ts

View workflow job for this annotation

GitHub Actions / ⚒️ Build

Unsafe member access .vircadia_lod_mode on an `any` value

Check warning on line 103 in src/modules/scene/LODManager.ts

View workflow job for this annotation

GitHub Actions / ⚒️ Build

Unsafe assignment of an `any` value

Check warning on line 103 in src/modules/scene/LODManager.ts

View workflow job for this annotation

GitHub Actions / ⚒️ Build

Unsafe member access .vircadia_lod_mode on an `any` value

Check warning on line 103 in src/modules/scene/LODManager.ts

View workflow job for this annotation

GitHub Actions / 🎉 Deploy

Unsafe assignment of an `any` value

Check warning on line 103 in src/modules/scene/LODManager.ts

View workflow job for this annotation

GitHub Actions / 🎉 Deploy

Unsafe member access .vircadia_lod_mode on an `any` value

Check warning on line 103 in src/modules/scene/LODManager.ts

View workflow job for this annotation

GitHub Actions / desktop-build (ubuntu-20.04)

Unsafe assignment of an `any` value

Check warning on line 103 in src/modules/scene/LODManager.ts

View workflow job for this annotation

GitHub Actions / desktop-build (ubuntu-20.04)

Unsafe member access .vircadia_lod_mode on an `any` value

Check warning on line 103 in src/modules/scene/LODManager.ts

View workflow job for this annotation

GitHub Actions / desktop-build (macos-latest)

Unsafe assignment of an `any` value

Check warning on line 103 in src/modules/scene/LODManager.ts

View workflow job for this annotation

GitHub Actions / desktop-build (macos-latest)

Unsafe member access .vircadia_lod_mode on an `any` value

Check warning on line 103 in src/modules/scene/LODManager.ts

View workflow job for this annotation

GitHub Actions / desktop-build (windows-latest)

Unsafe assignment of an `any` value

Check warning on line 103 in src/modules/scene/LODManager.ts

View workflow job for this annotation

GitHub Actions / desktop-build (windows-latest)

Unsafe member access .vircadia_lod_mode on an `any` value
Expand All @@ -127,9 +120,9 @@ export class LODManager {

private static setBillboardMode(
mesh: Mesh | AbstractMesh | InstancedMesh,
modeAsString: string | undefined
modeAsString: string | null
): void {
if (modeAsString !== undefined) {
if (modeAsString !== null) {
const mode = StringsAsBillboardModes[modeAsString];
mesh.scaling.x *= -1;
mesh.billboardMode = mode;
Expand All @@ -142,9 +135,9 @@ export class LODManager {

private static setLODHide(
mesh: Mesh,
hideAtDistance: number | undefined
hideAtDistance: number | null
): void {
if (hideAtDistance !== undefined) {
if (hideAtDistance !== null) {
mesh.addLODLevel(hideAtDistance, null);
Log.debug(
Log.types.ENTITIES,
Expand Down Expand Up @@ -282,7 +275,6 @@ export class LODManager {
);

const level = parse.lodLevel;
let mode = MeshTypes.LOD.Modes.DISTANCE;

if (
!level ||
Expand All @@ -294,9 +286,7 @@ export class LODManager {
continue;
}

if (metadata.vircadia_lod_mode) {
mode = metadata.vircadia_lod_mode;
}
const mode = metadata.vircadia_lod_mode ?? MeshTypes.LOD.Modes.DISTANCE;

switch (mode) {
case MeshTypes.LOD.Modes.DISTANCE: {
Expand All @@ -317,6 +307,7 @@ export class LODManager {

break;
}
// FIXME: ??? TS
case MeshTypes.LOD.Modes.SIZE: {
let sizeTarget =
SizeTargets[level as keyof typeof SizeTargets];
Expand Down
53 changes: 52 additions & 1 deletion src/modules/scene/LightmapManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,55 @@
// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
//

// WIP
import {
type AbstractMesh,
type Scene,
} from "@babylonjs/core";
import Log from "../debugging/log";
import { glTF as MeshTypes } from "../../../types/vircadia_gameUse";

export class LightmapManager {
public static applySceneLightmapsToMeshes(meshes: AbstractMesh[], scene: Scene): AbstractMesh[] {
meshes.forEach((mesh) => {
if (
mesh.metadata &&
mesh.metadata.gltf &&
mesh.metadata.gltf.extras &&
mesh.metadata.gltf.extras.vircadia_lightmap_default
) {
Log.debug(
Log.types.ENTITIES,
`Mesh ${mesh.name} has lightmap metadata: ${mesh.metadata.gltf.extras.vircadia_lightmap_default}`

Check failure on line 30 in src/modules/scene/LightmapManager.ts

View workflow job for this annotation

GitHub Actions / ⚒️ Build

Invalid type "any" of template literal expression

Check failure on line 30 in src/modules/scene/LightmapManager.ts

View workflow job for this annotation

GitHub Actions / ⚒️ Build

Invalid type "any" of template literal expression

Check failure on line 30 in src/modules/scene/LightmapManager.ts

View workflow job for this annotation

GitHub Actions / ⚒️ Build

Invalid type "any" of template literal expression

Check failure on line 30 in src/modules/scene/LightmapManager.ts

View workflow job for this annotation

GitHub Actions / ⚒️ Build

Invalid type "any" of template literal expression

Check failure on line 30 in src/modules/scene/LightmapManager.ts

View workflow job for this annotation

GitHub Actions / 🎉 Deploy

Invalid type "any" of template literal expression

Check failure on line 30 in src/modules/scene/LightmapManager.ts

View workflow job for this annotation

GitHub Actions / 🎉 Deploy

Invalid type "any" of template literal expression

Check failure on line 30 in src/modules/scene/LightmapManager.ts

View workflow job for this annotation

GitHub Actions / desktop-build (ubuntu-20.04)

Invalid type "any" of template literal expression

Check failure on line 30 in src/modules/scene/LightmapManager.ts

View workflow job for this annotation

GitHub Actions / desktop-build (ubuntu-20.04)

Invalid type "any" of template literal expression

Check failure on line 30 in src/modules/scene/LightmapManager.ts

View workflow job for this annotation

GitHub Actions / desktop-build (macos-latest)

Invalid type "any" of template literal expression

Check failure on line 30 in src/modules/scene/LightmapManager.ts

View workflow job for this annotation

GitHub Actions / desktop-build (macos-latest)

Invalid type "any" of template literal expression

Check failure on line 30 in src/modules/scene/LightmapManager.ts

View workflow job for this annotation

GitHub Actions / desktop-build (windows-latest)

Invalid type "any" of template literal expression

Check failure on line 30 in src/modules/scene/LightmapManager.ts

View workflow job for this annotation

GitHub Actions / desktop-build (windows-latest)

Invalid type "any" of template literal expression
);
const lightmapMaterialName = mesh.metadata.gltf.extras.vircadia_lightmap_default;
// Search for the material by name
const material = scene.materials.find(
(m) => m.name === lightmapMaterialName
);

if (material && material.albedoTexture) {
mesh.material.lightmapTexture = material.albedoTexture;
mesh.material.useLightmapAsShadowmap = true;
mesh.material.lightmapTexture.coordinatesIndex =
mesh.metadata.gltf.extras.vircadia_lightmap_texcoord;
Log.info(
Log.types.ENTITIES,
`Applied albedo texture as lightmap texture for: ${mesh.name}`
);
} else {
Log.error(
Log.types.ENTITIES,
`Could not find material or albedo texture for: ${mesh.name}`
);
}
}

if (mesh.name.startsWith(MeshTypes.Lightmap.DATA_MESH_NAME)) {
mesh.dispose(false, true); // This deletes the mesh without removing child textures or materials
Log.debug(Log.types.ENTITIES, `Deleting lightmap data mesh: ${mesh.name}`);
}
});

return meshes;
}
}
89 changes: 0 additions & 89 deletions staging/lightmap_support_glb.js

This file was deleted.

103 changes: 0 additions & 103 deletions staging/lightmap_support_gltf.js

This file was deleted.

33 changes: 22 additions & 11 deletions types/vircadia_gameUse.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,25 @@
export namespace Mesh {
export interface Metadata {
vircadia_lod_mode?: LOD.Modes;
vircadia_lod_auto?: boolean;
vircadia_lod_distance?: number;
vircadia_lod_size?: number;
vircadia_lod_hide?: number;
vircadia_billboard_mode?: string;
export namespace glTF {
export interface MetadataInterface {
vircadia_lod_mode: LOD.Modes | null;
vircadia_lod_auto: boolean | null;
vircadia_lod_distance: number | null;
vircadia_lod_size: number | null;
vircadia_lod_hide: number | null;
vircadia_billboard_mode: string | null;
// Lightmap
vircadia_lightmap_default?: string,
vircadia_lightmap_texcoord?: number
vircadia_lightmap_default: string | null;
vircadia_lightmap_texcoord: number | null;
}

export class Metadata implements MetadataInterface {
public vircadia_lod_mode = null;
public vircadia_lod_auto = null;
public vircadia_lod_distance = null;
public vircadia_lod_size = null;
public vircadia_lod_hide = null;
public vircadia_billboard_mode = null;
public vircadia_lightmap_default = null;
public vircadia_lightmap_texcoord = null;
}

export namespace LOD {
Expand All @@ -35,6 +46,6 @@ export namespace Mesh {
}

export namespace Lightmap {
export const LightmapDataMesh = "vircadia_lightmapData_"; // + lightmap name
export const DATA_MESH_NAME = "vircadia_lightmapData";
}
}

0 comments on commit 0162a5d

Please sign in to comment.