Upcoming Blender features

I will be coding new features for Blender, stay tuned...

Wednesday, 2 May 2012

Blender-Cycles Ray Length node output

As I promised I'll try to code new features for Blender, to warm up with the source code base, lets start with some small features first, like this one:


Ray length output for LightPathNode:




-What?
For every ray that hits a surface, Cycles executes shader graph of the surface material hit point. The ray length output will correspond with the ray indicated in the other outputs (is xxxx ray). So in short, it is the length of the ray being shaded. 


-For what?
For example to create glass absorption/filtering based on the distance traveled by the transmission ray. Currently in Cycles with glass BSDF shader when you select the glass color, you are actually setting the way and the amount of the light is filtered by the glass, if you select bright green it means the tiny Impurity  particles in the glass are more likely to reflect the green color and absorb/transmit the red and blue colors,  the more darker the color the more light it is absorbed, the more bright the color the more light is transmitted (escapes) from the object:


As you can see in the above picture, the absorbed light Rays gets lost inside the material, and the reflected/transmitted rays can escape, the more reflected/transmitted rays the more transparent is the material, the more absorbed Rays the more darker is the surface.


What was missing in Cycles was the ability to reproduce the absorption based on the distance that light rays travel inside the surface, as you may guess the final absorption effect will be based on the density of those tiny impurity particles and how wide is the object. 


Now, the ray distance output actually gives you the distance of the light traveled, thus you can create a cool node setup to recreate, for example the absorption of the glass based on how wide the object it is:
Cycles tests:


As you can see, the more thick the objects parts, the more light is transmitted and not absorbed.






This is the node setup using the ray length output, what it does is with the first two math nodes convert the distance to a simpler exponential fallof/attenuation magnitude (the output distance is in blender units, so the exponential fallof maps it between 0 and 1 in an exponential way). then we add this value to the is ray camera, why? because we want to exclude this filtering effect from the camera ray so when Is Cam Ray is 1 it choose the white color, showing to the camera ray a clear glass, this is important because we only want to apply this attenuation only for the transmission and the other rays (again, except the camera)


The Backfacing parameter from Geometry node will tell not take into account the transmission rays that are outside of the object, so it doesn't get attenuated by the shader( this problem was discover by the BlenderArtist niabot, thanks! )


...But If you don't have time to understand that node setup, no problem!  
lets bundle them in a simple node group which you can consider an upgrade of the current glass BSDF:


Absorption parameter goes from 0 to -inf ( minus infinity ) the more negative the number the more absorption.




...But this doesn't stop here, now because you can know how far ray light traveled, you can create/fake many other materials, for example to recreate a pseudo sub-surface scattering effect using the length of the translucent ray:








This is an proximate ice material, if you watch closely you can see we are not even computing a single scattering event here, but with making the translucent shader taking into account the distance of the transmission rays, we can make more translucent the thicker parts and more opaque the wider parts of the objects


The cool thing about this very fake sss and translucent deep based material it is very fast shader setup to render and may be okay for several renders you want to do.


More test renders: (Click for enlarge)
















.blend file used for these test renders: 
 .Blend file (Fixed and updated)


Windows builds (I don't compile with Cuda but i'll try soon):
Win x64
Win x32
UPDATE: This patch(functionality) was committed to trunk! so you can go to graphicall.org or blender build bot to get a version supporting this feature.
Make sure it is at least the revision rev 46200


SVN Patch:
Cycles_LightPathLength_0.9.patch




Useful link:
Cycles Light Paths manual

Cheers!
Agus