Skip to content

Commit aabec17

Browse files
committed
feat: add support of quality for textures and shadows in scene loader in babylonjs-editor-tools
1 parent acf274d commit aabec17

File tree

3 files changed

+26
-6
lines changed

3 files changed

+26
-6
lines changed

tools/src/loading/loader.ts

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,17 +41,27 @@ export type ScriptMap = Record<
4141
* Using "medium" or "low" quality levels will reduce the memory usage and improve the performance of the scene
4242
* especially on mobiles where memory is limited.
4343
*/
44-
export type SceneLoaderQualitySelector = "low" | "medium" | "high";
44+
export type SceneLoaderQualitySelector = "very-low" | "low" | "medium" | "high";
4545

4646
export type SceneLoaderOptions = {
4747
/**
4848
* Defines the quality of the scene.
4949
* This will affect the quality of textures that will be loaded in terms of dimensions.
5050
* The editor computes automatic "high (untouched)", "medium (half)", and "low (quarter)" quality levels for textures.
5151
* Using "medium" or "low" quality levels will reduce the memory usage and improve the performance of the scene
52-
* especially on mobiles where memory is limited.
52+
* especially on mobiles where memory is limited. The "very-low" quality level is even more aggressive with shadows quality.
5353
*/
5454
quality?: SceneLoaderQualitySelector;
55+
56+
/**
57+
* Same as "quality" but only applied to textures. If set, this has priority over "quality".
58+
*/
59+
texturesQuality?: SceneLoaderQualitySelector;
60+
/**
61+
* Same as "quality" but only applied to shadows. If set, this has priority over "quality".
62+
*/
63+
shadowsQuality?: SceneLoaderQualitySelector;
64+
5565
/**
5666
* Defines the function called to notify the loading progress in interval [0, 1]
5767
*/
@@ -62,12 +72,17 @@ declare module "@babylonjs/core/scene" {
6272
// eslint-disable-next-line @typescript-eslint/naming-convention
6373
interface Scene {
6474
loadingQuality: SceneLoaderQualitySelector;
75+
loadingTexturesQuality: SceneLoaderQualitySelector;
76+
loadingShadowsQuality: SceneLoaderQualitySelector;
6577
}
6678
}
6779

6880
export async function loadScene(rootUrl: any, sceneFilename: string, scene: Scene, scriptsMap: ScriptMap, options?: SceneLoaderOptions) {
6981
scene.loadingQuality = options?.quality ?? "high";
7082

83+
scene.loadingTexturesQuality = options?.texturesQuality ?? scene.loadingQuality;
84+
scene.loadingShadowsQuality = options?.shadowsQuality ?? scene.loadingQuality;
85+
7186
await AppendSceneAsync(`${rootUrl}${sceneFilename}`, scene, {
7287
pluginExtension: ".babylon",
7388
onProgress: (event) => {

tools/src/loading/shadows.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,20 @@ import { getPowerOfTwoUntil } from "../tools/scalar";
88
const shadowsGeneratorParser = GetParser(SceneComponentConstants.NAME_SHADOWGENERATOR);
99

1010
AddParser(SceneComponentConstants.NAME_SHADOWGENERATOR, (parsedData: any, scene: Scene, container: AssetContainer, rootUrl: string) => {
11-
if (scene.loadingQuality !== "high") {
11+
if (scene.loadingShadowsQuality !== "high") {
1212
parsedData.shadowGenerators?.forEach((shadowGenerator: any) => {
13-
switch (scene.loadingQuality) {
13+
switch (scene.loadingShadowsQuality) {
1414
case "medium":
1515
shadowGenerator.mapSize = shadowGenerator.mapSize * 0.5;
1616
break;
1717

1818
case "low":
1919
shadowGenerator.mapSize = shadowGenerator.mapSize * 0.25;
2020
break;
21+
22+
case "very-low":
23+
shadowGenerator.mapSize = shadowGenerator.mapSize * 0.125;
24+
break;
2125
}
2226

2327
shadowGenerator.mapSize = Math.max(128, getPowerOfTwoUntil(shadowGenerator.mapSize));

tools/src/loading/texture.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import { getPowerOfTwoUntil } from "../tools/scalar";
1111
const textureParser = SerializationHelper._TextureParser;
1212

1313
SerializationHelper._TextureParser = (sourceProperty: any, scene: Scene, rootUrl: string): Nullable<BaseTexture> => {
14-
if (scene.loadingQuality === "high" || !sourceProperty.metadata?.baseSize) {
14+
if (scene.loadingTexturesQuality === "high" || !sourceProperty.metadata?.baseSize) {
1515
return textureParser(sourceProperty, scene, rootUrl);
1616
}
1717

@@ -22,7 +22,7 @@ SerializationHelper._TextureParser = (sourceProperty: any, scene: Scene, rootUrl
2222

2323
let suffix = "";
2424

25-
switch (scene.loadingQuality) {
25+
switch (scene.loadingTexturesQuality) {
2626
case "medium":
2727
let midWidth = (width * 0.66) >> 0;
2828
let midHeight = (height * 0.66) >> 0;
@@ -36,6 +36,7 @@ SerializationHelper._TextureParser = (sourceProperty: any, scene: Scene, rootUrl
3636
break;
3737

3838
case "low":
39+
case "very-low":
3940
let lowWidth = (width * 0.33) >> 0;
4041
let lowHeight = (height * 0.33) >> 0;
4142

0 commit comments

Comments
 (0)