How can I have depth of game objects in different color scales?

Hi
I’m using a C# script and a shader to get depth images from Unity. The code and shader I have can provide depth images in two different scales; Gray and Colorful Scale.
Now I am trying to get depth images in which the depth of the whole view is on grayscale except for some specific game objects. I want their depth to be on color scale. Can anybody help me with this situation?

The reason to use greyscale vs “color scale” depends on the image format being used. If you’re using an ARGB32 render texture, use the “color scale”, which is packing a single 32 bit float into all 4 color channels. The “greyscale” one assumes you’re rendering to an RFloat render texture which is a single 32 bit float. There’s not really a reason to use both ever.

Thanks for your answer. Actually I need such depth images for training A.I models. So I have to have both grayscales along with other color scales in the same image. Is there any method for that?

I guess my question was not clear. Maybe with this picture I can explain better.

In this picture on the left, middle and right, what I have are the main view, color depth, and gray-scale depth view respectively.
Now I need to have a single view in which some objects are in the format of the middle view and the rest are in the format of the right view.

Presumably the color and greyscale depth are generated by using it as a replacement shader? Or is the shader reading from the depth texture?

If it’s a replacement shader pass, you’d need to make a new replacement shader that includes both the colored and greyscale passes, with custom “RenderType” tags for the colored passes. Then on the objects you want to show as colored you’d need to call mat.SetOverrideTag("RenderType"="OpaqueColoredDepth"); to change those materials to use those custom tags that use that pass.

If the shaders are just directly reading the _CameraDepthTexture then … there’s nothing you can do because that texture doesn’t know about separate objects. It’s a just a texture of depth information. You’ll need to render some sort of mask to combine the two depth colorings.

1 Like

So the shader is directly reading the _CameraDepthTexture. I have uploaded the shader and the C# code. Can you help me with writing the sub shader you have in mind?

5198570–516788–DepthGrayScale.shader (1.1 KB)
5198570–516791–DepthCameraScript.cs (740 Bytes)

What you want to add is going to require about 10x as much code as this currently does, mostly in c#, with a little in the shader (reading from a texture to switch between greyscale & colored).

Here’s a project that does outlines on objects. This may seem unrelated, but part of what it’s doing is marking objects to be rendered into another texture that gets used later to generate and mask the outlines. You can try going through this to try to understand what you need to do.

Feel free to come back and ask more specific questions on parts of that code, or your own code you can’t figure out.

The project seemed too complicated for me. So here is how I did it
I create two cameras and make one of them the child of the other one the parent. Here is the configuration of the cameras;

Parent camera:
Clear flag= Don’t Clear
Culling Mask= Only the specif objects that I want to render its depth in another color scale
Depth = 1

Children camera:
Clear flag= Whatever
Culling Mask= Everything
Depth = 0

Then I hang the “Gray.cs” code to the children-camera. I also create a material and hang the shader “Red” to it. Then I hang this material to the specific objects that I want to render its depth in another color scale
Then it OK!

5212442–519044–Gray.cs (818 Bytes)
5212442–519047–Gray.shader (1.13 KB)
5212442–519050–Red.shader (881 Bytes)

1 Like