xyz
Well I managed to finally get something to work without any extra processing.
Blender Compositor has a Depth pass output. I tried to use it before but apparently didn't know what I was doing.
Anyway, it saves as an EXR so it's a 'picture' of the depth in world coordinates (ie meters in my Blender world).
I had problems trying to import EXR files in Godot, kept getting corrupted-file errors but I think that was how I chose to format my save EXR file.
Anyway, the values are capped at about 216 (meters) so i think I'm ok with half-floats!
For those interested, here is my exact work-flow: (Blender 3.6 and Godot 3.5)
In Blender, enable Compositor in the Post-Processing Pipeline in Output properties.
In ViewLayers tab, enable Passes/Data/Include/Z. Disable everything else.
In the Compositor, connect the RenderLayers Depth output to an Output Node (File Output). Put in the proper path and filename pointing to a resource folder in Godot. Make sure it is selected for the next step!
In the Compositor sidebar in the Node tab, set the Properties to:
OpenEXR, RGB, 'half' or 'full' Color Depth (I've only tested 'half'), Codec to something lossless; I've only tested 'None' or 'ZIP' since
get_pixel() from an Image won't work if it is compressed, but it probably needs more testing.
Set the Color Management to Override, and set Linear. Non-Color will NOT import into Godot.
Render and the Save File node will automatically save the depth file.
Godot:
If necessary, reimport the saved file as an Import type.
My personal project is casting a panoramic image onto a sphere, allowing the camera to look around. The depthmap saved is a lower-resolution image with the same aspect ratio as the main image. You can lower or increase the resolution as you need, it will always work.
Here are some sample codes to get the depth under the cursor of a panoramic image. These just try to show the basics; there are other things under the hood I don't need to share.
func LoadDM(): #loads the generated depthmap
DIMG = g.GetPreload["DP0101"] as Image #DP0101 is a PreLoaded resource.
DIMG.lock()
DepthMapW = DIMG.get_width() #these are used elsewhere
DepthMapH = DIMG.get_height()
func GetImgPixelDepth():
# make sure the mouse is within the display window
var xin = g.mouse_x < g.Hrez-1 and g.mouse_x >= 0
var yin = g.mouse_y < g.Vrez-1 and g.mouse_y >= 0
if xin and yin:
# g.mouseaz and the other are from a routine elsewhere that detects the azimuth and elevation of the mouse cursor projected
onto the panoramic background
var pixX = int(g.mouseaz*DepthMapW)
var pixY = int(g.mouseel*DepthMapH)
var depth = DIMG.get_pixel(pixX,pixY)
return depth
else:
return -1.0