Starnuto di topo
Starnuto di topo

Reputation: 3569

BabylonJs target object disappears

In BabylonJs , if a camera is positioned at a certain distance from an object, so that its distance is proportional to the object's size, the overall scene should be roughly the same.

I created this simple test (can be copied and pasted into http://www.babylonjs-playground.com/ to be run), that produces a scene with a sphere of size 1000, seen from a camera positioned at (0, 0, 10000):

var createScene = function () {

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

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

    // This targets the camera to scene origin
    camera.setTarget(BABYLON.Vector3.Zero());

    // 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(1, -1, -1), scene);

    // Default intensity is 1. Let's dim the light a small amount
    light.intensity = 0.7;

    // Our built-in 'sphere' shape. Params: name, subdivs, size, scene
    var sphere = BABYLON.Mesh.CreateSphere("Sphere", 16, 1000, scene);  

    return scene;
};

Note that if the size of the sphere is reduced to 100 and the distance of the camera is reduced accordingly (so the camera is positioned at (0, 0, 1000)), the scene looks still the same.

If the experiment is repeated reducing by another factor, that is, size of the sphere reduced to 10 and camera positioned to (0, 0, 100), still nothing changes.

So far, so good. But if I attempt to increase the size of the sphere and the distance, then everything disappears: if the size of the sphere is 10000 and camera positioned to (0, 0, 100000), the scene looks empty. Why? Is it possible to change this behavior? How? What am I missing?

Thanks!

Upvotes: 0

Views: 2584

Answers (1)

David Basalla
David Basalla

Reputation: 3106

This is most likely related to the 'clipping plane' of the camera. In 3D graphics, it's common to limit the distance that a camera can 'look' into the distance (along its z-axis) so that it does not have to 'draw' objects far in the background.

You can change this value to extend the draw distance of your camera by adding this to your code:

camera.maxZ = 100000;

To visualise this, you could also add a plane geometry underneath the sphere. You could then set the maxZ attribute to different values and see where it stops drawing the plane.

Upvotes: 1

Related Questions