Get the depth map of a set of meshes

Hi Everyone,

We are trying to get the depth map of a set of meshes, and running into couple of problems.

(1) Getting the depth map of a set of meshes.

We realized that using following code could get the depth map:

    var depthRender = scene.enableDepthRenderer(camera, true);
    var renderDepthTexture = depthRender.getDepthMap();

However, it seems that the depth map include all objects in the scene. Is there any possibility of get the depth map of a customized set of meshes (instead of all meshes)?

It seems that there is a setMaterialForRendering method for TargetRenderer, however it looks like it doesn’t work as expected?

I quickly created a playground: https://playground.babylonjs.com/#KHIVV1#1. And I am wondering that is there anyway to get the depth map, but only for the sphere (not the ground).

(2) How to normalize the depth map, to augment the details of the meshes?

There are a couple of notes (such as this one) that indicate that by adjusting the clipping planes of camera (camera.minZ, and camera.maxZ), we could augment the details of the depth map. However, making the clipping distance smaller will also create challenge for the meshes to be navigated in the 3D canvas.

I am wondering if there is anyway to simply set the minZ as the shortest distance to the selected meshes, and maxZ as the longest distance to the selected meshes?

We have tried to augment the pixel with following pseudo code:

renderDepthTexture.readPixels(undefined, undefined, undefined, false, false).then((data) => {                    
    Tools.DumpData(
        renderDepthTexture.getRenderWidth(), 
        renderDepthTexture.getRenderHeight(), 
        data, (dataUrl) => {
               // min_pixel = minimal value of pixel except 0
               // max_pixel = maximum value of pixel except 255
               // each_pixel = 255 * (read_pixel - min_pixel) / (max_pixel - min_pixel)                   
        });
    });
});

Thank you so much! All suggestions are welcome!

You can use the renderList property of renderDepthTexture to render only the meshes you want:

Regarding 2, if the camera can move inside the scene (and even more if the meshes themselves can move), you don’t really have a shortest/longest distance to a mesh. If the camera and the meshes don’t move at all, then you can use the bounding boxes of the meshes to calculate the minimal / maximal distances you are after.

3 Likes