Page 1 of 3

[ISL] Rendering depth maps

Posted: Thu Jul 02, 2009 8:51 pm
by fused
Update: cahnged the shader to use the 11% faster lenght()

ZomB asked me yesterday, if it was somehow possbile to get C4D to render a depth map that fits the Indigo viewport (the C4D viewport does not 1:1 match with the Indigo viewport). This is of course close to impossible.

But then i had the idea to make Indigo render the depth map:

By using an ISL shader it should be possible to calculate the distance between the surface point to be shaded and the camera. The shader is used as an emission shader in a diffuse material with bure black albedo. The pure black albedo is to prevent the objects to illuminate each other as rays will be absorbed as soon as they hit the pure black albedo.

So much for the idea, heres how i did it.

This material has to be applied to all meshes. The shader calculates the distance between the camera position(hardcoded) and the surface point:

Code: Select all

    <material>
        <name>depthMap shader</name>
        <diffuse>
            <random_triangle_colours>false</random_triangle_colours>
            <albedo>
                <constant>
                    <rgb>
                        <rgb>0.000000 0.000000 0.000000</rgb>
                        <gamma>2.200000</gamma>
                    </rgb>
                </constant>
            </albedo>
            <base_emission>
                <constant>
                    <rgb>
                        <rgb>1000.000000 1000.000000 1000.000000</rgb>
                        <gamma>1.000000</gamma>
                    </rgb>
                </constant>
            </base_emission>
            <layer>1</layer>
            <emission>
                <shader>
                    <shader>
                        <![CDATA[def calcDistance(vec3 a, vec3 b) real: length(a - b)

def eval(vec3 pos) vec3: vec3(calcDistance(vec3(0.17073, -4.39744, -0.77524), pos))]]>
</shader>
                </shader>
            </emission>
        </diffuse>
    </material>
The emission should not be scaled on each mesh, so that every object emits the correct relative ammount of light.

That works pretty well already, but i does give us an inverted depth map (usually white represents close and black far). This not that bad since we can just invert the image afterwards, but after that definiton the background would be close (since its black). We need to add a background material.

Code: Select all

    <background_settings>
        <background_material>
            <material>
                <name>background_material</name>
                <diffuse>
                    <base_emission>
                        <constant>
                            <rgb>
                                <rgb>1000.000000 1000.000000 1000.000000</rgb>
                                <gamma>1.000000</gamma>
                            </rgb>
                        </constant>
                    </base_emission>
                    <layer>0</layer>
                </diffuse>
            </material>
        </background_material>
    </background_settings>
Both should be in seperate layers, because you would want to scale the background layer so its pure white.

Heres a quick test, i converted the image to greyscale in gimp and then inverted it. It rendered reasonably fast.
depth.png
indigo output (to greyscale)
depth.png (126.47 KiB) Viewed 9809 times
depth_inv.png
inverted with gimp
depth_inv.png (125.36 KiB) Viewed 9808 times

Re: [ISL] Rendering depth maps

Posted: Thu Jul 02, 2009 10:04 pm
by Pibuz
Hey fused!
This is definitely useful!!!!!
I'd very like to have this shader integrated within Indigo!

Congratulations for your work!

Re: [ISL] Rendering depth maps

Posted: Thu Jul 02, 2009 11:26 pm
by zeitmeister
Super!!!

Just make it accessable as an render-option, like "render alpha", "render clay": "render depth".
Rendering this depthmap at the same time when rendering the original image would be a killer, too.

Re: [ISL] Rendering depth maps

Posted: Thu Jul 02, 2009 11:39 pm
by pixie
zeitmeister wrote:Super!!!

Just make it accessable as an render-option, like "render alpha", "render clay": "render depth".
Rendering this depthmap at the same time when rendering the original image would be a killer, too.
If someone is able to do it is Fused!!
Go Fused, go!! Fused for president! :D

Re: [ISL] Rendering depth maps

Posted: Fri Jul 03, 2009 12:02 am
by Zom-B
Here is my test with this shader:

Re: [ISL] Rendering depth maps

Posted: Fri Jul 03, 2009 12:51 am
by suvakas
Very nice scene ZomB !
Is it yours? Can you show us more?

Re: [ISL] Rendering depth maps

Posted: Fri Jul 03, 2009 12:53 am
by lycium
that's a really cool idea fused :D it should definitely become part of the rendering core, allowing for advanced compositing. depth of field, antialiasing and now also motion blur complicate matters a bit however...


ps. ZomB, omg, that render of yours is beauuutiful!

Re: [ISL] Rendering depth maps

Posted: Fri Jul 03, 2009 2:13 am
by CTZn
Wow fused ! I agree, it's fitting nicely beside the alpha render.

edit: there is another one that can be usefull as a shading parameter, it's the camera facing ratio, or angle between the camera ray and the normal at surface (fresnel effects, ghost/xray shaders).

Re: [ISL] Rendering depth maps

Posted: Fri Jul 03, 2009 7:42 am
by fused
thank you all for your comments :)

hope this turns out to be useful for someone...

Re: [ISL] Rendering depth maps

Posted: Fri Jul 03, 2009 7:46 am
by Stur
Excellent idea Fused.

Re: [ISL] Rendering depth maps

Posted: Fri Jul 03, 2009 12:32 pm
by OnoSendai
Fused, you are a crazy genius!

Re: [ISL] Rendering depth maps

Posted: Fri Jul 03, 2009 2:06 pm
by Zom-B
yes, abusing your shadercode for funny stuff :)

Now tell us Ono how can we speed this up?? setting Indigo render Depth to 1 and other Advanced tweaks... ?!
How ist the chance for some updated LLVM JIT backend, and will it be availible for 64builds too???

Re: [ISL] Rendering depth maps

Posted: Fri Jul 03, 2009 2:09 pm
by OnoSendai
The 64 bit build will use the LLVM JITing backend, when LLVM supports 64 bit JITing without crashing and burning :)

Re: [ISL] Rendering depth maps

Posted: Fri Jul 03, 2009 2:10 pm
by benn
Awesome fused - well done :)

Re: [ISL] Rendering depth maps

Posted: Fri Jul 03, 2009 4:18 pm
by lycium
another way to possibly speed it up (a lot) is to replace those two pow() calls with a multiplication.