Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
digisomni committed Feb 8, 2024
1 parent 7b30aaa commit 209325b
Show file tree
Hide file tree
Showing 5 changed files with 166 additions and 55 deletions.
80 changes: 25 additions & 55 deletions src/modules/scene/LODManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,50 +16,25 @@ import {
Mesh
} from "@babylonjs/core";
import Log from "../debugging/log";

// TODO: Move these types and consts to a central types file for Vircadia: vircadia/types (repo)
export interface MeshMetadata {
vircadia_lod_mode?: LODModes;
vircadia_lod_auto?: boolean;
vircadia_lod_distance?: number;
vircadia_lod_size?: number;
vircadia_lod_hide?: number;
vircadia_billboard_mode?: string;
}

export enum BillboardModes {
BILLBOARDMODE_NONE = 0,
BILLBOARDMODE_X = 1,
BILLBOARDMODE_Y = 2,
BILLBOARDMODE_Z = 4,
BILLBOARDMODE_ALL = 7,
}

export const StringsAsBillboardModes: { [key: string]: BillboardModes } = {
none: BillboardModes.BILLBOARDMODE_NONE,
x: BillboardModes.BILLBOARDMODE_X,
y: BillboardModes.BILLBOARDMODE_Y,
z: BillboardModes.BILLBOARDMODE_Z,
all: BillboardModes.BILLBOARDMODE_ALL,
import { Mesh as MeshTypes } from "../../../types/vircadia_gameUse";

export const StringsAsBillboardModes: { [key: string]: MeshTypes.BillboardModes } = {
none: MeshTypes.BillboardModes.BILLBOARDMODE_NONE,
x: MeshTypes.BillboardModes.BILLBOARDMODE_X,
y: MeshTypes.BillboardModes.BILLBOARDMODE_Y,
z: MeshTypes.BillboardModes.BILLBOARDMODE_Z,
all: MeshTypes.BillboardModes.BILLBOARDMODE_ALL,
};

export enum LODLevels {
LOD0 = "LOD0",
LOD1 = "LOD1",
LOD2 = "LOD2",
LOD3 = "LOD3",
LOD4 = "LOD4",
}

export const DistanceTargets: { [key in LODLevels]: number } = {
export const DistanceTargets: { [key in MeshTypes.LOD.Levels]: number } = {
LOD0: 0,
LOD1: 15,
LOD2: 30,
LOD3: 60,
LOD4: 120,
};

export const SizeTargets: { [key in LODLevels]: number } = {
export const SizeTargets: { [key in MeshTypes.LOD.Levels]: number } = {
LOD0: 1.0,
LOD1: 0.25,
LOD2: 0.1,
Expand All @@ -72,7 +47,7 @@ export interface AutoTarget {
distance: number;
optimizeMesh: boolean;
}
export const AutoTargets: { [key in LODLevels]?: AutoTarget } = {
export const AutoTargets: { [key in MeshTypes.LOD.Levels]?: AutoTarget } = {
LOD0: {
quality: 0.9,
distance: DistanceTargets.LOD0,
Expand Down Expand Up @@ -100,11 +75,6 @@ export const AutoTargets: { [key in LODLevels]?: AutoTarget } = {
},
};

export enum LODModes {
DISTANCE = "distance",
SIZE = "size",
}

export class LODManager {
public static parseMeshName(name: string): {
prefix?: string;
Expand All @@ -124,10 +94,10 @@ export class LODManager {
}

private static getMetadataFromMesh(mesh: AbstractMesh | Mesh | InstancedMesh) {
const meshExtras = mesh.metadata?.gltf?.extras as MeshMetadata | undefined;
const parentExtras = mesh.parent?.metadata?.gltf?.extras as MeshMetadata | undefined;
const meshExtras = mesh.metadata?.gltf?.extras;
const parentExtras = mesh.parent?.metadata?.gltf?.extras;

const meshMetadata: MeshMetadata = {
const meshMetadata: MeshTypes.Metadata = {
vircadia_lod_mode: undefined,
vircadia_lod_auto: undefined,
vircadia_lod_distance: undefined,
Expand Down Expand Up @@ -183,8 +153,8 @@ export class LODManager {
}
}

private static setLODMode(mesh: Mesh, mode: LODModes): void {
if (mode === LODModes.SIZE) {
private static setLODMode(mesh: Mesh, mode: MeshTypes.LOD.Modes): void {
if (mode === MeshTypes.LOD.Modes.SIZE) {
mesh.useLODScreenCoverage = true;
Log.debug(
Log.types.ENTITIES,
Expand Down Expand Up @@ -243,7 +213,7 @@ export class LODManager {
suffix: string | undefined;
name: string | undefined;
mesh: Mesh;
metadata: MeshMetadata;
metadata: MeshTypes.Metadata;
lodLevel: string | undefined;
simplificationSettings: ISimplificationSettings[];
}[] = [];
Expand All @@ -253,11 +223,11 @@ export class LODManager {
const mesh = meshes[i];
const name = mesh.name;
const parse = LODManager.parseMeshName(name);
const metadata: MeshMetadata = LODManager.getMetadataFromMesh(mesh);
const metadata: MeshTypes.Metadata = LODManager.getMetadataFromMesh(mesh);
if (
// (mesh.constructor.name === "Mesh" ||
// mesh.constructor.name === "AbstractMesh") &&
parse?.lodLevel === LODLevels.LOD0
parse?.lodLevel === MeshTypes.LOD.Levels.LOD0
) {
roots.push({
prefix: parse?.prefix,
Expand All @@ -282,7 +252,7 @@ export class LODManager {
// Process metadata for root mesh.
LODManager.setLODMode(
roots[root].mesh,
roots[root].metadata.vircadia_lod_mode ?? LODModes.DISTANCE
roots[root].metadata.vircadia_lod_mode ?? MeshTypes.LOD.Modes.DISTANCE
);
LODManager.setLODHide(
roots[root].mesh,
Expand All @@ -303,7 +273,7 @@ export class LODManager {
parse.suffix === roots[root].suffix &&
parse.prefix === roots[root].prefix
) {
const metadata: MeshMetadata =
const metadata: MeshTypes.Metadata =
LODManager.getMetadataFromMesh(mesh);

LODManager.setBillboardMode(
Expand All @@ -312,14 +282,14 @@ export class LODManager {
);

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

if (
!level ||
(!(level in DistanceTargets) &&
!(level in SizeTargets) &&
!(level in AutoTargets)) ||
level === LODLevels.LOD0
level === MeshTypes.LOD.Levels.LOD0
) {
continue;
}
Expand All @@ -329,7 +299,7 @@ export class LODManager {
}

switch (mode) {
case LODModes.DISTANCE: {
case MeshTypes.LOD.Modes.DISTANCE: {
let distanceTarget =
DistanceTargets[
level as keyof typeof DistanceTargets
Expand All @@ -347,7 +317,7 @@ export class LODManager {

break;
}
case LODModes.SIZE: {
case MeshTypes.LOD.Modes.SIZE: {
let sizeTarget =
SizeTargets[level as keyof typeof SizeTargets];

Expand Down
12 changes: 12 additions & 0 deletions src/modules/scene/LightmapManager.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
//
// LightmapManager.ts
//
// Created by Kalila on 08 Feb 2024.
// Copyright 2024 Vircadia contributors.
// Copyright 2024 DigiSomni LLC.
//
// Distributed under the Apache License, Version 2.0.
// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
//

// WIP
89 changes: 89 additions & 0 deletions staging/lightmap_support_glb.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/* eslint-disable */

const createScene = function () {
const scene = new BABYLON.Scene(engine);
const camera = new BABYLON.ArcRotateCamera(
"camera2",
Math.PI / 2,
Math.PI / 4,
10,
new BABYLON.Vector3.Zero(),
scene
);
camera.attachControl(canvas, true);
const light = new BABYLON.HemisphericLight(
"light2",
new BABYLON.Vector3(0, 1, 0),
scene
);
light.intensity = 0.7;

BABYLON.SceneLoader.ImportMesh(
"",
"https://digisomni-singapore-1.ap-south-1.linodeobjects.com/falah-tech/virtual-collaboration/Testing/Vircadia/Lightmaps/Test3/",
"LightmapTest_jpg.glb",
scene,
function (newMeshes) {
newMeshes.forEach((mesh) => {
console.info(
"Mesh:",
mesh.name,
"has metadata:",
mesh.metadata
);

if (
mesh.metadata &&
mesh.metadata.gltf &&
mesh.metadata.gltf.extras &&
mesh.metadata.gltf.extras.vircadia_lightmap_default
) {
console.info(
"Found lightmap in metadata:",
mesh.metadata.gltf.extras.vircadia_lightmap_default
);
// Attempt to find the embedded lightmap texture by name
const lightmapName =
mesh.metadata.gltf.extras.vircadia_lightmap_default;

console.info(
"Before checking, here are the lightmaps:",
scene.textures
);
const embeddedLightmap = lightmaps.find(
(texture) => texture.name === lightmapName
);

console.info(
"Found embedded lightmap texture:",
embeddedLightmap
);
if (embeddedLightmap) {
mesh.material.lightmapTexture = embeddedLightmap;
mesh.material.useLightmapAsShadowmap = true;
mesh.material.lightmapTexture.coordinatesIndex =
mesh.metadata.gltf.extras.vircadia_lightmap_texcoord;
console.info(
"Successfully applied embedded lightmap texture:",
lightmapName
);
} else {
console.error(
"Failed to find embedded lightmap texture for:",
mesh.name,
"with lightmap name:",
lightmapName
);
}
}
// Check if the mesh name matches 'vircadia_lightmapData' and hide it
if (mesh.name === "vircadia_lightmapData") {
mesh.isVisible = false; // This hides the mesh
console.info("Mesh vircadia_lightmapData is now hidden");
}
});
}
);

return scene;
};
File renamed without changes.
40 changes: 40 additions & 0 deletions types/vircadia_gameUse.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
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;
// Lightmap
vircadia_lightmap_default?: string,
vircadia_lightmap_texcoord?: number
}

export namespace LOD {
export enum Modes {
DISTANCE = "distance",
SIZE = "size",
}

export enum Levels {
LOD0 = "LOD0",
LOD1 = "LOD1",
LOD2 = "LOD2",
LOD3 = "LOD3",
LOD4 = "LOD4",
}
}

export enum BillboardModes {
BILLBOARDMODE_NONE = 0,
BILLBOARDMODE_X = 1,
BILLBOARDMODE_Y = 2,
BILLBOARDMODE_Z = 4,
BILLBOARDMODE_ALL = 7,
}

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

0 comments on commit 209325b

Please sign in to comment.