Changing camera minZ and maxZ makes the skybox disappear

Hi, I’m trying to render a scene with a skybox created from a HDR file and the skybox shows correctly. But after I changed camera’s minZ and maxZ with calculated clip distance, the skybox can’t be seen and the background only shows the clear color. I tried to set the clearColor to (0, 0, 0, 0) , the background becomes transparent but the skybox still can’t be seen. I also changed the skyboxholder’s infiniteDistance to be true, still can’t see the skybox. Here’s the code.

await SceneLoader.AppendAsync(modelRootPath, modelFileName, this[$scene])
.then(() => {
const {min, max} =
this[$scene].meshes[0].getHierarchyBoundingVectors();
const modelRadius =
Math.max(max.x - min.x, max.y - min.y, max.z - min.z);
const far = 2 * Math.max(modelRadius, orbit.radius);
const near = far / 1000;
// after adding these two lines, the skybox disappear.
this[$camera].minZ = near;
this[$camera].maxZ = far;
});

this[$scene].stopAllAnimations();

this[$hdrTexture] = new HDRCubeTexture(
    scenario.lighting, this[$scene], 256, false, false, false);
this[$scene].environmentTexture = this[$hdrTexture];
this[$hdrTexture].setReflectionTextureMatrix(
    Matrix.RotationY(Tools.ToRadians(180)));

const skyboxHolder =
    this[$scene].createDefaultSkybox(this[$scene].environmentTexture!, false, 128);
skyboxHolder!.rotate(Axis.Y, Math.PI, Space.WORLD);
skyboxHolder!.infiniteDistance = true;

Could you help me find what’s the problem? Thanks!

You are probably setting your camera far distance or maxZ after the position of the bounding box so it is clipped. You should reduce the skybox size to fit in your camera maxz value.

1 Like

Yeah you are correct, since maxZ is not important and it’s only for calculating minZ, so i just use the origin maxZ and problem solved. Out of curiosity, what does the size of a skybox mean? I remember when i learn opengl, to implement a skybox i have to render a cube with 6 skybox texture and when camera moves only update it’s rotation to PVM matrix. So i guess babylonjs implement in a different way?

The size is the size of the cube so it needs to fit into the visible range to ensure it is not clipped.

1 Like