Jump to content

Camera minZ and maxZ

Jose Vicente

Recommended Posts



I'm trying to understand how camera minZ and maxZ works, to define it correctly for my scene. If I'm, not wrong, those values define the position for near and far planes.

I'm not able to define them properly.  In  http://www.babylonjs-playground.com/#1JNDSK#3, I have defined a sphere placed at 0, 0, 0, with diameter = 2, and there is a camera located a 0, 0, 20.

Camera minZ is set to 38, and the sphere starts dissapearing. Lower values, like 37, doesn't affect the sphere.

I don't understand this behavior.  Sphere min Z value its Z = -1, and I can't undestand why minZ = 37 doesn't hides the sphere and value = minZ =38 hides partially.

Anyone can help?

Thanks in advance.

Best Regards.


Link to comment
Share on other sites


minZ and maxZ define the clip planes and are used to create the projectionmatrix: https://github.com/BabylonJS/Babylon.js/blob/master/src/Cameras/babylon.camera.ts#L410

A projection matrix is build like this: https://github.com/BabylonJS/Babylon.js/blob/master/src/Math/babylon.math.ts#L2706

minZ and MaxZ are used to scale the depth values from [minZ,maxZ] to [0,1]

Here is an example to better capture it:



If you want to get a clipPlane (not a nearPlane), you can use  scene.clipPlane;



Link to comment
Share on other sites

I built a fancy tester.  http://www.babylonjs-playground.com/#1JNDSK#10

Ain't I just something?  :)  Looking straight -z, on-screen position-reporting, fancy edges-rendering, severely-reduced mousewheel sensibility, severely-adjusted camera.speed which is barely working if at all.  Fancy stuff.  All designer label stuff.  heh

Don't be messing around in MY version... unless you are dressed for dinner.  High class PG.  Wash-up a bit and dress appropriately, eh?  ;)

Link to comment
Share on other sites

  • 10 months later...

Save doesn't seem to be working for some reason, but you can copy and paste this into playground.

This is a fun demo of a my random stars rather than a skybox.  They require a pretty crazy maxZ. This was also my playing around with creating instances.  With some random scaling and the for loop set to 10,000 it's even more beautiful ;)

var createScene = function () {

    // This creates a basic Babylon Scene object (non-mesh)
    var scene = new BABYLON.Scene(engine);
    scene.clearColor = true;

    // This creates and positions a free camera (non-mesh)
    var camera = new BABYLON.FreeCamera("camera1", new BABYLON.Vector3(0, 5, -10), scene);
    camera.maxZ = 32000;

    // This attaches the camera to the canvas
    camera.attachControl(canvas, true);

    // This creates a light, aiming 0,1,0 - to the sky (non-mesh)
    var light = new BABYLON.HemisphericLight("light1", new BABYLON.Vector3(0, 1, 0), scene);

    // Our built-in 'ground' shape. Params: name, width, depth, subdivs, scene
    var ground = BABYLON.Mesh.CreateGround("ground1", 6, 6, 2, scene);

    var matForStars = new BABYLON.StandardMaterial("starMat", scene);
			matForStars.emissiveColor = new BABYLON.Color3(1, 1, 1);
			matForStars.specularColor = new BABYLON.Color3(0, 0, 0);
			matForStars.diffuseColor = new BABYLON.Color3(0, 0, 0);

			var star = BABYLON.Mesh.CreateSphere("star", 1, 20, scene);
			star.material = matForStars;
			star.position.z = 14000;
			// Create some instances, make a bunch of stars
			var stars = new Array();
			for (var i = 0; i < 1000; i++) {
				var random1 = (Math.random() - 0.5) * 32000;		
				var random2 = (Math.random() - 0.5) * 32000;
				var random3 = (Math.random() - 0.5) * 32000;
				var magnitudeStar = Math.sqrt(random1 * random1 + random2 * random2 + random3 * random3);
				if (magnitudeStar < 14000 || magnitudeStar > 16000 ) {
				else {
					stars[i] = star.createInstance('Instance' + i);
					stars[i].position.x = random1;
					stars[i].position.y = random2;
					stars[i].position.z = random3;

    return scene;



Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Create New...