Added async / await to dice texture loading

This should fix load issues with the dice tray being blank
Also turnned off the default texture flip so all textures are now unflipped by default
This commit is contained in:
Mitchell McCaffrey 2020-05-28 18:08:26 +10:00
parent 49c18e8cf7
commit 765c84389f
40 changed files with 58 additions and 41 deletions

View File

@ -36,27 +36,13 @@ function DiceTrayOverlay({ isOpen }) {
function handleAssetLoadStart() { function handleAssetLoadStart() {
assetLoadStart(); assetLoadStart();
sceneKeepAwakeRef.current++;
} }
function handleAssetLoadFinish() { function handleAssetLoadFinish() {
assetLoadFinish(); assetLoadFinish();
sceneKeepAwakeRef.current--; forceRender();
} }
// Force render when loading assets
useEffect(() => {
if (isLoading) {
sceneKeepAwakeRef.current++;
}
return () => {
if (isLoading) {
// eslint-disable-next-line react-hooks/exhaustive-deps
sceneKeepAwakeRef.current--;
}
};
}, [isLoading]);
// Forces rendering for 1 second // Forces rendering for 1 second
function forceRender() { function forceRender() {
// Force rerender // Force rerender

View File

@ -9,6 +9,7 @@ import d20Source from "./shared/d20.glb";
import d100Source from "./shared/d100.glb"; import d100Source from "./shared/d100.glb";
import { lerp } from "../helpers/shared"; import { lerp } from "../helpers/shared";
import { importTextureAsync } from "../helpers/babylon";
const minDiceRollSpeed = 600; const minDiceRollSpeed = 600;
const maxDiceRollSpeed = 800; const maxDiceRollSpeed = 800;
@ -46,11 +47,11 @@ class Dice {
return mesh; return mesh;
} }
static loadMaterial(materialName, textures, scene) { static async loadMaterial(materialName, textures, scene) {
let pbr = new BABYLON.PBRMaterial(materialName, scene); let pbr = new BABYLON.PBRMaterial(materialName, scene);
pbr.albedoTexture = new BABYLON.Texture(textures.albedo); pbr.albedoTexture = await importTextureAsync(textures.albedo);
pbr.normalTexture = new BABYLON.Texture(textures.normal); pbr.normalTexture = await importTextureAsync(textures.normal);
pbr.metallicTexture = new BABYLON.Texture(textures.metalRoughness); pbr.metallicTexture = await importTextureAsync(textures.metalRoughness);
pbr.useRoughnessFromMetallicTextureAlpha = false; pbr.useRoughnessFromMetallicTextureAlpha = false;
pbr.useRoughnessFromMetallicTextureGreen = true; pbr.useRoughnessFromMetallicTextureGreen = true;
pbr.useMetallnessFromMetallicTextureBlue = true; pbr.useMetallnessFromMetallicTextureBlue = true;

View File

@ -11,6 +11,8 @@ import doubleAlbedo from "./doubleAlbedo.jpg";
import doubleMetalRoughness from "./doubleMetalRoughness.jpg"; import doubleMetalRoughness from "./doubleMetalRoughness.jpg";
import doubleNormal from "./doubleNormal.jpg"; import doubleNormal from "./doubleNormal.jpg";
import { importTextureAsync } from "../../helpers/babylon";
class DiceTray { class DiceTray {
_size; _size;
get size() { get size() {
@ -126,9 +128,11 @@ class DiceTray {
"dice_tray_mat_single", "dice_tray_mat_single",
this.scene this.scene
); );
singleMaterial.albedoTexture = new BABYLON.Texture(singleAlbedo); singleMaterial.albedoTexture = await importTextureAsync(singleAlbedo);
singleMaterial.normalTexture = new BABYLON.Texture(singleNormal); singleMaterial.normalTexture = await importTextureAsync(singleNormal);
singleMaterial.metallicTexture = new BABYLON.Texture(singleMetalRoughness); singleMaterial.metallicTexture = await importTextureAsync(
singleMetalRoughness
);
singleMaterial.useRoughnessFromMetallicTextureAlpha = false; singleMaterial.useRoughnessFromMetallicTextureAlpha = false;
singleMaterial.useRoughnessFromMetallicTextureGreen = true; singleMaterial.useRoughnessFromMetallicTextureGreen = true;
singleMaterial.useMetallnessFromMetallicTextureBlue = true; singleMaterial.useMetallnessFromMetallicTextureBlue = true;
@ -152,9 +156,11 @@ class DiceTray {
"dice_tray_mat_double", "dice_tray_mat_double",
this.scene this.scene
); );
doubleMaterial.albedoTexture = new BABYLON.Texture(doubleAlbedo); doubleMaterial.albedoTexture = await importTextureAsync(doubleAlbedo);
doubleMaterial.normalTexture = new BABYLON.Texture(doubleNormal); doubleMaterial.normalTexture = await importTextureAsync(doubleNormal);
doubleMaterial.metallicTexture = new BABYLON.Texture(doubleMetalRoughness); doubleMaterial.metallicTexture = await importTextureAsync(
doubleMetalRoughness
);
doubleMaterial.useRoughnessFromMetallicTextureAlpha = false; doubleMaterial.useRoughnessFromMetallicTextureAlpha = false;
doubleMaterial.useRoughnessFromMetallicTextureGreen = true; doubleMaterial.useRoughnessFromMetallicTextureGreen = true;
doubleMaterial.useMetallnessFromMetallicTextureBlue = true; doubleMaterial.useMetallnessFromMetallicTextureBlue = true;

Binary file not shown.

Before

Width:  |  Height:  |  Size: 1.9 MiB

After

Width:  |  Height:  |  Size: 1.9 MiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 1.1 MiB

After

Width:  |  Height:  |  Size: 1.2 MiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 1.2 MiB

After

Width:  |  Height:  |  Size: 1.2 MiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 1.8 MiB

After

Width:  |  Height:  |  Size: 1.8 MiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 1.1 MiB

After

Width:  |  Height:  |  Size: 1.1 MiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 1.1 MiB

After

Width:  |  Height:  |  Size: 1.1 MiB

View File

@ -10,7 +10,7 @@ class GalaxyDice extends Dice {
static async load(scene) { static async load(scene) {
if (!this.material) { if (!this.material) {
this.material = this.loadMaterial( this.material = await this.loadMaterial(
"galaxy_pbr", "galaxy_pbr",
{ albedo, metalRoughness, normal }, { albedo, metalRoughness, normal },
scene scene

Binary file not shown.

Before

Width:  |  Height:  |  Size: 2.4 MiB

After

Width:  |  Height:  |  Size: 2.4 MiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 1.8 MiB

After

Width:  |  Height:  |  Size: 1.7 MiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 285 KiB

After

Width:  |  Height:  |  Size: 292 KiB

View File

@ -6,6 +6,8 @@ import albedo from "./albedo.jpg";
import metalRoughness from "./metalRoughness.jpg"; import metalRoughness from "./metalRoughness.jpg";
import normal from "./normal.jpg"; import normal from "./normal.jpg";
import { importTextureAsync } from "../../helpers/babylon";
class GemstoneDice extends Dice { class GemstoneDice extends Dice {
static meshes; static meshes;
static material; static material;
@ -15,11 +17,11 @@ class GemstoneDice extends Dice {
return { mass: properties.mass * 1.5, friction: properties.friction }; return { mass: properties.mass * 1.5, friction: properties.friction };
} }
static loadMaterial(materialName, textures, scene) { static async loadMaterial(materialName, textures, scene) {
let pbr = new BABYLON.PBRMaterial(materialName, scene); let pbr = new BABYLON.PBRMaterial(materialName, scene);
pbr.albedoTexture = new BABYLON.Texture(textures.albedo); pbr.albedoTexture = await importTextureAsync(textures.albedo);
pbr.normalTexture = new BABYLON.Texture(textures.normal); pbr.normalTexture = await importTextureAsync(textures.normal);
pbr.metallicTexture = new BABYLON.Texture(textures.metalRoughness); pbr.metallicTexture = await importTextureAsync(textures.metalRoughness);
pbr.useRoughnessFromMetallicTextureAlpha = false; pbr.useRoughnessFromMetallicTextureAlpha = false;
pbr.useRoughnessFromMetallicTextureGreen = true; pbr.useRoughnessFromMetallicTextureGreen = true;
pbr.useMetallnessFromMetallicTextureBlue = true; pbr.useMetallnessFromMetallicTextureBlue = true;
@ -35,7 +37,7 @@ class GemstoneDice extends Dice {
static async load(scene) { static async load(scene) {
if (!this.material) { if (!this.material) {
this.material = this.loadMaterial( this.material = await this.loadMaterial(
"gemstone_pbr", "gemstone_pbr",
{ albedo, metalRoughness, normal }, { albedo, metalRoughness, normal },
scene scene

Binary file not shown.

Before

Width:  |  Height:  |  Size: 2.1 MiB

After

Width:  |  Height:  |  Size: 2.1 MiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 2.0 MiB

After

Width:  |  Height:  |  Size: 1.9 MiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 285 KiB

After

Width:  |  Height:  |  Size: 291 KiB

View File

@ -6,6 +6,8 @@ import albedo from "./albedo.jpg";
import mask from "./mask.png"; import mask from "./mask.png";
import normal from "./normal.jpg"; import normal from "./normal.jpg";
import { importTextureAsync } from "../../helpers/babylon";
class GlassDice extends Dice { class GlassDice extends Dice {
static meshes; static meshes;
static material; static material;
@ -15,10 +17,10 @@ class GlassDice extends Dice {
return { mass: properties.mass * 1.5, friction: properties.friction }; return { mass: properties.mass * 1.5, friction: properties.friction };
} }
static loadMaterial(materialName, textures, scene) { static async loadMaterial(materialName, textures, scene) {
let pbr = new BABYLON.PBRMaterial(materialName, scene); let pbr = new BABYLON.PBRMaterial(materialName, scene);
pbr.albedoTexture = new BABYLON.Texture(textures.albedo); pbr.albedoTexture = await importTextureAsync(textures.albedo);
pbr.normalTexture = new BABYLON.Texture(textures.normal); pbr.normalTexture = await importTextureAsync(textures.normal);
pbr.roughness = 0.25; pbr.roughness = 0.25;
pbr.metallic = 0; pbr.metallic = 0;
pbr.subSurface.isRefractionEnabled = true; pbr.subSurface.isRefractionEnabled = true;
@ -29,7 +31,7 @@ class GlassDice extends Dice {
pbr.subSurface.minimumThickness = 10; pbr.subSurface.minimumThickness = 10;
pbr.subSurface.maximumThickness = 10; pbr.subSurface.maximumThickness = 10;
pbr.subSurface.tintColor = new BABYLON.Color3(43 / 255, 1, 115 / 255); pbr.subSurface.tintColor = new BABYLON.Color3(43 / 255, 1, 115 / 255);
pbr.subSurface.thicknessTexture = new BABYLON.Texture(textures.mask); pbr.subSurface.thicknessTexture = await importTextureAsync(textures.mask);
pbr.subSurface.useMaskFromThicknessTexture = true; pbr.subSurface.useMaskFromThicknessTexture = true;
return pbr; return pbr;
@ -37,7 +39,7 @@ class GlassDice extends Dice {
static async load(scene) { static async load(scene) {
if (!this.material) { if (!this.material) {
this.material = this.loadMaterial( this.material = await this.loadMaterial(
"glass_pbr", "glass_pbr",
{ albedo, mask, normal }, { albedo, mask, normal },
scene scene

Binary file not shown.

Before

Width:  |  Height:  |  Size: 583 KiB

After

Width:  |  Height:  |  Size: 585 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 217 KiB

After

Width:  |  Height:  |  Size: 217 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 283 KiB

After

Width:  |  Height:  |  Size: 292 KiB

View File

@ -15,7 +15,7 @@ class IronDice extends Dice {
static async load(scene) { static async load(scene) {
if (!this.material) { if (!this.material) {
this.material = this.loadMaterial( this.material = await this.loadMaterial(
"iron_pbr", "iron_pbr",
{ albedo, metalRoughness, normal }, { albedo, metalRoughness, normal },
scene scene

Binary file not shown.

Before

Width:  |  Height:  |  Size: 1.0 MiB

After

Width:  |  Height:  |  Size: 1.1 MiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 1.3 MiB

After

Width:  |  Height:  |  Size: 1.3 MiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 309 KiB

After

Width:  |  Height:  |  Size: 316 KiB

View File

@ -10,7 +10,7 @@ class NebulaDice extends Dice {
static async load(scene) { static async load(scene) {
if (!this.material) { if (!this.material) {
this.material = this.loadMaterial( this.material = await this.loadMaterial(
"neubula_pbr", "neubula_pbr",
{ albedo, metalRoughness, normal }, { albedo, metalRoughness, normal },
scene scene

Binary file not shown.

Before

Width:  |  Height:  |  Size: 1.9 MiB

After

Width:  |  Height:  |  Size: 1.9 MiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 1.5 MiB

After

Width:  |  Height:  |  Size: 1.5 MiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 580 KiB

After

Width:  |  Height:  |  Size: 613 KiB

View File

@ -10,7 +10,7 @@ class SunriseDice extends Dice {
static async load(scene) { static async load(scene) {
if (!this.material) { if (!this.material) {
this.material = this.loadMaterial( this.material = await this.loadMaterial(
"sunrise_pbr", "sunrise_pbr",
{ albedo, metalRoughness, normal }, { albedo, metalRoughness, normal },
scene scene

Binary file not shown.

Before

Width:  |  Height:  |  Size: 1.5 MiB

After

Width:  |  Height:  |  Size: 1.5 MiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 939 KiB

After

Width:  |  Height:  |  Size: 972 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 283 KiB

After

Width:  |  Height:  |  Size: 292 KiB

View File

@ -10,7 +10,7 @@ class SunsetDice extends Dice {
static async load(scene) { static async load(scene) {
if (!this.material) { if (!this.material) {
this.material = this.loadMaterial( this.material = await this.loadMaterial(
"sunset_pbr", "sunset_pbr",
{ albedo, metalRoughness, normal }, { albedo, metalRoughness, normal },
scene scene

Binary file not shown.

Before

Width:  |  Height:  |  Size: 1.3 MiB

After

Width:  |  Height:  |  Size: 1.4 MiB

View File

@ -33,7 +33,7 @@ class WalnutDice extends Dice {
static async load(scene) { static async load(scene) {
if (!this.material) { if (!this.material) {
this.material = this.loadMaterial( this.material = await this.loadMaterial(
"walnut_pbr", "walnut_pbr",
{ albedo, metalRoughness, normal }, { albedo, metalRoughness, normal },
scene scene

Binary file not shown.

Before

Width:  |  Height:  |  Size: 2.1 MiB

After

Width:  |  Height:  |  Size: 2.1 MiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 879 KiB

After

Width:  |  Height:  |  Size: 911 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 1.5 MiB

After

Width:  |  Height:  |  Size: 1.5 MiB

20
src/helpers/babylon.js Normal file
View File

@ -0,0 +1,20 @@
import * as BABYLON from "babylonjs";
// Turn texture load into an async function so it can be awaited
export async function importTextureAsync(url) {
return new Promise((resolve, reject) => {
let texture = new BABYLON.Texture(
url,
null,
undefined,
false,
undefined,
() => {
resolve(texture);
},
() => {
reject("Unable to load texture");
}
);
});
}