It is my understanding that Cycles is a ray/path tracing render engine, and that it works by firing out "sightlines" into the scene to render, which interact with materials they hit in a defined manner to produce the color of a given pixel.

I know there are materials such as glass, which refract the ray so it travels at a slightly different angle, mirrors, which reflect the ray into another part of the scene, and diffusive materials which scatter several rays in different directions. Additionally, there are "special" materials like the holdout which simply force the given pixel to be transparent.

Given this information, I would assume that it is possible for there to be materials or shaders which instruct an incoming ray to behave in specially defined ways, such as reflecting directly back at the camera, or translating a set distance through the scene.

This got me wondering if it is possible for a custom shader to define such actions? I have seen OSL shaders which give materials a "toony" look, and other such styles that would not be possible using the standard nodes, but I haven't been able to find any information regarding what a custom shader can and can't do, and how it would be done.

Is OSL the only way to define custom shaders? Can a plugin be written such that it responds when any given ray hits it, or are there only ways to specify general rules for handling a ray?

  • $\begingroup$ There is a node for tight path. Using the many different options from it you can combine different shaders to do wonderful things. Watch Bartek Skorupa's video youtube.com/watch?v=MTFxyNzI5EE $\endgroup$
    – user1853
    Jul 27 '16 at 20:11
  • 1
    $\begingroup$ Perhaps the OSL readme is relevant. As I understand it, OSL basically works like the graphical nodes, in the sense that you define "closures", which I think are basically functions which return a probability distribution of possible reflected/transmitted rays given an incoming ray. I've never actually used OSL, so I may be way off $\endgroup$
    – gandalf3
    Jul 27 '16 at 20:13
  • $\begingroup$ Never used OSL myself either, I don't think you can control ray direction directly like that, but from what I've seen OSL allows you to either write custom procedural textures, like say an honeycomb hexagon pattern, or a rounded square tiles pattern, but also surface properties definition, like shininess, bumpiness, or refraction, possibly even volumetric properties like density and light dispersion $\endgroup$ Jul 27 '16 at 23:55
  • $\begingroup$ So if I wanted to make some sort of material that directly affected ray direction that would require a modification to the cycles engine directly? $\endgroup$ Jul 28 '16 at 1:01

Using an OSL script is the closest you can come to modifying cycles without modifying the cycles source code and building your own version. Using an OSL script we can create nodes that perform any calculations we may want to do. These nodes can be incorporated into a node tree the same as any built-in node that exists in blender. You can look at how existing nodes are implemented using OSL. The main consideration with OSL scripts is that there is no GPU rendering support.

An OSL script can be as simple as a math node with a numeric input that does a calculation and outputs a numeric value, or at the extreme an OSL script can be used to create a ray tracing engine that runs within cycles. François Gastaldo created an OSL script that re-creates the old 1986 juggler animation on the surface of an object, he later also created a full path tracing engine - these were made for educational purposes as François is a teacher but serve as good examples of not being limited by how complex an OSL script can be.

If you look at tables 6.1 and 6.2 on page 39 of the OSL specification you will see the data available from the render engine that can be used within an OSL script. Most of the data is read only, we can only alter data such as the location of the point that the ray has hit, the surface normal and the closure that describes the colour used to get the final image. This means we can't directly alter the ray as it passes through the scene or how it interacts with other objects but we can use an OSL script to distort the surface of an object, that should make it possible to connect the camera vector from a Texture Coordinates node to a vector input for your OSL script and use that to distort the surface normal so that it faces the camera.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.