Is it possible to make an OSL toon shader in witch something like IOR/glass effect can be added. For example, if there is an animation where something is put inside a glass, the glass to be noticeable as a glass?


All of the cycles material nodes available in blender have OSL script versions that are used when OSL is enabled. These scripts can be a good place to begin when you want to start your own OSL script. You can view them online at developer.blender.org or download a copy of blender's source code to view locally.

For your example let's start with the toon shader. The main part is the call to diffuse_toon(). For IOR the glass shader by default calls microfacet_beckmann_refraction() with some other calculations. Merging these two together you could start with an OSL script as -

#include "stdosl.h"
#include "node_fresnel.h"

shader toon_with_IOR(
    color ToonColor = color(0.0,0.0,0.8),
    float Size = 0.5,
    float Smooth = 0.0,
    color GlassColor = 0.8,
    float IOR = 1.52,
    float IOR_Roughness = 0.0,
    normal Normal = N,
    output closure color BSDF = 0)
    float f = max(IOR, 1e-5);
    float eta = backfacing() ? 1.0 / f : f;
    float cosi = dot(I, Normal);
    float Fr = fresnel_dielectric_cos(cosi, eta);

    closure color ToonPart = ToonColor * diffuse_toon(Normal, Size, Smooth);
    closure color GlassPart = GlassColor * (Fr * microfacet_beckmann(Normal, IOR_Roughness) +
            (1.0 - Fr) * microfacet_beckmann_refraction(Normal, IOR_Roughness, eta));

    BSDF = ToonPart + GlassPart;

Instead of adding the two results you could mix them together, you would then add another float input called Fac and change the last line to BSDF = (1.0 - Fac) * ToonPart + Fac * GlassPart; - that's from the mix closure node.

At this stage you can easily test the result by comparing it to the nodes we got the code from. Then you can go further and customise the result to get the result you want.

| improve this answer | |

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.