From 209325b937117f96703b5b0043b53857d9177429 Mon Sep 17 00:00:00 2001 From: Kalila <69767640+digisomni@users.noreply.github.com> Date: Thu, 8 Feb 2024 14:05:40 +1100 Subject: [PATCH] WIP --- src/modules/scene/LODManager.ts | 80 ++++++----------- src/modules/scene/LightmapManager.ts | 12 +++ staging/lightmap_support_glb.js | 89 +++++++++++++++++++ ...ap_support.js => lightmap_support_gltf.js} | 0 types/vircadia_gameUse.ts | 40 +++++++++ 5 files changed, 166 insertions(+), 55 deletions(-) create mode 100644 src/modules/scene/LightmapManager.ts create mode 100644 staging/lightmap_support_glb.js rename staging/{lightmap_support.js => lightmap_support_gltf.js} (100%) create mode 100644 types/vircadia_gameUse.ts diff --git a/src/modules/scene/LODManager.ts b/src/modules/scene/LODManager.ts index 50d41038..8c22ebd1 100644 --- a/src/modules/scene/LODManager.ts +++ b/src/modules/scene/LODManager.ts @@ -16,42 +16,17 @@ 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, @@ -59,7 +34,7 @@ export const DistanceTargets: { [key in LODLevels]: number } = { 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, @@ -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, @@ -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; @@ -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, @@ -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, @@ -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[]; }[] = []; @@ -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, @@ -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, @@ -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( @@ -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; } @@ -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 @@ -347,7 +317,7 @@ export class LODManager { break; } - case LODModes.SIZE: { + case MeshTypes.LOD.Modes.SIZE: { let sizeTarget = SizeTargets[level as keyof typeof SizeTargets]; diff --git a/src/modules/scene/LightmapManager.ts b/src/modules/scene/LightmapManager.ts new file mode 100644 index 00000000..115731d4 --- /dev/null +++ b/src/modules/scene/LightmapManager.ts @@ -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 diff --git a/staging/lightmap_support_glb.js b/staging/lightmap_support_glb.js new file mode 100644 index 00000000..6f914c6e --- /dev/null +++ b/staging/lightmap_support_glb.js @@ -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; +}; diff --git a/staging/lightmap_support.js b/staging/lightmap_support_gltf.js similarity index 100% rename from staging/lightmap_support.js rename to staging/lightmap_support_gltf.js diff --git a/types/vircadia_gameUse.ts b/types/vircadia_gameUse.ts new file mode 100644 index 00000000..7b4bb4cd --- /dev/null +++ b/types/vircadia_gameUse.ts @@ -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 + } +}