How to scan a surface of a GameObject and get the distance of every edge to camera

Hey guys,

I have a GameObject (e.g. stairs like in the picture below).

85407-stairs.png

When my main camera is pointed toward that Object I want to scan the surface. The result should be an array that represents the screen (with a resolution of e.g. 20x20) and the distances between the camera and the surfaces / edges of the Game Object.
Simplified (if no object is recognized, the distance i 0):

00009999900000000000
09999999900000000000
00999888880000000000
00088888777000000000
00000777777770000000
00000077666666000000
00000006666666600000
00000000666555550000

So what I need to do is:

  1. Get the distances between camera and every surface / edge that is in sight of the camera
    I first thought about multiple raycasts, but that is not performant. Then I considered spherecastAll(), but that only recognizes one collider (It’s not an option to get a collider for every part of the object).

  2. Transform to my resolution (I should be able to accomplish that)

Can anyone help?

Thanks in advance!

Sounds like you want to access the depth texture of the camera: Unity - Manual: Cameras and depth textures

You could render this to a RenderTexture and then blit to a regular Texture2D for resizing/whatever other transformations you want to do to the pixel array.