How to convert depthmap from depth renderer to a static texture?

Hello everyone,
As depth renderer is binding with the scene camera, the depthmap texture retrieved using getDepthMap is dynamic, I want to save the depthmap from depth renderer at some moments to a Texture. I am now coding like below to implement this:

// at some moment
var depth_arr = await depth_renderer.getDepthMap().readPixels(); // a float32 array
var depth_texture = RawTexture.CreateRTexture(depth_arr, 1920, 1080, this.scene, false, false, Texture.BILINEAR_SAMPLINGMODE, Engine.TEXTURETYPE_FLOAT);

the depth_texture constructed by CreateRTexture is strange:

Is there something wrong with my code?

Thanks for your help in advance!

Welcome aboard!

In WebGL, Texture.readPixels always return RGBA data. So, you should either use CreateRGBATexture instead of CreateRTexture of create a new buffer from depth_arr that will only contain the r component.

You can call doit() in the console of the browser to create the texture and see it in the inspector.

1 Like