I'm experimenting with NPR materials in Cycles, in particular, half-toning and hatching. In order to make things work, I often have to bake the output of a Cycles shader to a texture map and, in a second session/pass, multiply the colors in that map with a halftone screen, put the result through a threshold, etc., and feed the result back into say, an emission shader, to render the result.

Would it be possible to write an OSL shader that would accept a closure color as input (like the Cycles 'Mix Shader' node) so I can use the output of, say, a Diffuse BSDF node as the input for further calculations? (so as not have to bake it first?) I've seen OSL scripts which contain the grammar for this, but I've never seen it used.

At least, for example, it would be nice to be able to combine the outputs of Cycles closures in other ways than the factored mix provided by the 'Mix Shader' node. Maybe multiply, subtract, and all those other modes.

Being an OSL baby, I'm afraid I might be getting the shading logic all wrong- I don't understand why I haven't seen this done in any of the OSL scripts I've seen - even if it worked, would it be an abuse of the rendering system?


1 Answer 1


When OSL is enabled in Blender all shaders are compiled using OSL. Therefore the Add Shader and Mix Shader are very good examples of what you are looking for.

The next script is the implementation of the Mix Shader node used in Blender 1.

shader node_mix_closure(
    float Fac = 0.5,
    closure color Closure1 = 0,
    closure color Closure2 = 0,
    output closure color Closure = 0)
    float t = clamp(Fac, 0.0, 1.0);
    Closure = (1.0 - t) * Closure1 + t * Closure2;

BTW I don't see it as abuse of the render system but still be aware that OSL will not allow you to do many operations on a closure

At present, the only type of closure supported by Open Shading Language is the closure color, and the only allowed operations are those that let you form a linear combination of closure color’s. Additional closure types and operations are reserved for future use.

  • - closure color: unary negation
  • color * closure color: component-wise scaling
  • closure color * color: component-wise scaling
  • float * closure color: scaling
  • closure color * float: scaling
  • closure color + closure color: component-wise addition

Other operations are not supported. and that said there is just a little room left of things to do, what cannot be done by a set of Add Shader and Mix Shader

  • $\begingroup$ ..and thanks for the link: now I know where to have a root around. $\endgroup$
    – Robin Betts
    Mar 30, 2018 at 9:30

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .