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:

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 or blender build bot to get a version supporting this feature.
Make sure it is at least the revision rev 46200

SVN Patch:

Useful link:
Cycles Light Paths manual



  1. This is brilliant! I hope that you bug the Blender devs to get this included. I would love to be using this almost every day :)

  2. Is already in Trunk... Well done.

  3. Me encanta este desarrollo muy buen trabajo.


  4. This is an awesome little feature, well done and congrats to get your first patch included THIS quickly.

    On a sidenote, there seems to be a bug with your blog for showing images.
    If I click on an image to enlarge it, the site will jump to the top of the page and the images will end up being shown behind the header. Furthermore, they aren't enlarged at all. Some even get shown smaller than they are when directly included into the page.

    I'm using this version of Firefox, if that helps:

    Build-Identifikator: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:12.0) Gecko/20100101 Firefox/12.0

  5. great feature!

  6. Thanks guys!

    Hi @kram1032, i think it is how blogger works, as far as i know i can control the layout/color content, but i am not sure if i can control when you exit the image viewing mode where to jump or do that at all, also the images are actually small, only the wider gets shrink in the post, so those are the only worth to zoom.

  7. Excellent. This is very useful. This should hold me until we get real sss. Although this is useful beyond only sss.

  8. Agustin, you are doing some really good work on Cycles.

    Thank you very much.

    I notice you don't keep your blog very up to date with your developments, is there anywhere else where I can keep up to date with the work you are doing on Cycles.

    I am very excited about your mist and render time bevel. I think it is excellent to have a faster mist with volume light. I am still to try it out.

    Why don't you give us BSSRDF in Cycles, lol.

    Thanks again Agustin!!

  9. Hello! Is there any way you can share Mist Patch + Quick Volume Shadows with us? I'd really appreciate it!

  10. Hi. First off, thanks for this great patch. It's definitely a step in the right direction.

    Now, as for the demonstration, while it works as expected at object sizes in the demo, it doesn't work well for very small object in real-life scale, such as a glass of wine.

    Suzane scaled to 0.05 needs 2718 (1,000x) to 27180 (10,000x) in the power node in order to work. Now I have objects that are 5cm across in 1:1 scale, so this node gorup has no noticeable effect even when multiplying the power base by 1 million. Do you have any suggestions on how to deal with super-small objects (other than scaling them up, obviously).

  11. In addition to my last comment, I've done some further testing, and the appearance is OK with 1,000,000x2.718 in power node, but only if normals are facing outwards. That is not the case in liquid-inside-glass-container scenario, where I also have flipped normals (or negative IOR). How can I adjust this node group for that scenario?

  12. WAPEXP the best internet marketing company
    providing all solutions regarding web designing, development and search engine optimization with maximum of the quality of the work and time management and cost effective solutions for your business. All these quality services may take your business to another height on internet.

  13. Belive me this is one of the best renders i have seen from Blender....Artists

    Inks ...inka Blog

  14. This post has shared the information about the unique creature. I really like this post and I used to see these types of creatures on TV. Thanks for updating the information like this. uk best essays

  15. Wow, that is fantastic! Beautiful render!

    I also really liked your cycles island you did awhile back. I have learned so much by looking at the .blend file you shared.

    If you don't mind, I was wondering if I could find out where the sky texture from the cycles island scene is from initially. Did you make it yourself? Are there more like it where that came from?

    Anyway, thank you for sharing your expertise with the Blender community.

  16. I am really enjoying reading your well written articles. I think you spend numerous effort and time updating your blog. I have bookmarked it and I am taking a look ahead to reading new articles. Please keep up the good articles!credit card online

  17. This post has distributed the data about the exclusive creäture. I actually like this post and I utilised to see these kinds of animals on TV. Thanks for updating the data like this.

    For more visit-

  18. Easton Bell Electrical are your Melbourne based electricians, and it is our priority that
    we provide you with a friendly and courteous service while being professional and well presented.
    We pride ourselves on being on time and discussing with you what work suits you best. And best of all, We Clean Up!Caulfield Electrician

  19. For example to create glass absorption/filtering based on the distance traveled by ...

  20. As I promised I'll try to code new features for Blender, to warm up with ...

  21. This post has imparted the data about the remarkable animal. I truly like this post and I used to see these sorts of animals on TV. Much obliged concerning upgrading the data like this. Click Here

  22. Now you can design by our online service of company logo designer. You watch closely you can see we are not even computing a single scattering event.

  23. Nice post you have done here. I am really very happy to read this. This is a very informative topic that you are chosen. keep it upCustomer Contact Page

  24. Students have magic in their hands as their hardwork pays them in shape of the best outcomes of their sculptures which they design and give them their inner thoughts through which they can talk tot he world and can tell the world the solution of assignment writer what wrong is happening in our society.

  25. Thanks a lot for letting me a chance to visit your any pointers. Your article about web design is really impressed me very much.Communicator Plugin

  26. This comment has been removed by the author.

  27. Buy Electronic Components in Online for affordable prices at Component Buy. Get 25% discount on Semiconductor micro controller relays Connectors and many more

  28. Outer glass I really like that information. Now need to all over the world. So I like to your suggest webpage..Thank you

  29. Thanks for sharing such informative article. Know about Know about English to Tamil from techfizy.

  30. very usefull blog It was useful.thanks for sharing

  31. Thank you for useful information. It is exactly what I needed. I managed to get here everything that I need for my article.

    light booth measurements

  32. This is the great information. Thanks for providing.