My project requires the ability to toggle a skybox - this can be, depending on the situation, with ground projection or without. So I’m writing a function to do this.
Function code
const envTex = new CubeTexture("../res/img/env/" + fileName + ".env", scene, null, false, null, () => {
scene.environmentTexture = envTex;
const skyBox = MeshBuilder.CreateBox("skyBox", { size: 2000, sideOrientation: 1 }, scene);
skyBox.isPickable = false;
skyBox.infiniteDistance = true;
skyBox.ignoreCameraMaxZ = true;
skyBox.disableLighting = true;
const skyMaterial = new BackgroundMaterial("skyBoxMaterial", scene);
if(isGround) {
skyBox.position.y = 950;
skyMaterial.enableGroundProjection = true;
skyMaterial.projectedGroundRadius = 200;
skyMaterial.projectedGroundHeight = 7;
camera.upperBetaLimit = Math.PI / 2;
} else {
skyMaterial.microSurface = 0.93; // Doesn't work
}
skyMaterial.backFaceCulling = false;
skyMaterial.reflectionTexture = envTex.clone();
skyMaterial.reflectionTexture.coordinatesMode = 5;
skyBox.material = skyMaterial;
scene.lights[0].intensity = 0;
}
However, I’ve got two main issues:
- Firstly: the BackgroundMaterial doesn’t seem to support blur. I’ve seen that the
createDefaultSkybox
scene helper uses a PBRMaterial to get around this using itsmicroSurface
, but I assume PBRMaterials are quite a bit less performant. Is there any way to have blur with the BackgroundMaterial? - Secondly: the first time a .env is loaded (so when it’s not already cached) there’s a flash of black on the model due to setting the HemisphericLight’s intensity to zero, as I want it to be lit by the environment. So my assumption is that the skybox material / lighting is taking a moment to load. I can “fix” this by putting the light intensity change in a timeout, however that means that when the .env is already in cache then the model is double-lit for a moment. I’m already using the onload of the CubeTexture, so what could be causing this issue?
Any ideas greatly appreciated - thanks!