Pages

miercuri, 4 octombrie 2017

Blender 3D - Open Shading Language tutorial - part 001 .

Using this tutorial about Open Shading Language, you can try a glass shader. The source code show a simple use for Fresnel equations. The backfacing() and raytype() are two functions that provide the shader some information about the state of the renderer and the scene at the time of evaluation. I used just backfacing(), because is a raw example of this Fresnel equations. As you can see the I, N, eta inputs from Fresnel_Dielectric() is used to get a result variable fr. The all result of this shader is bsdf like a equation. See the next code:
#define IOR_THRESHOLD 1.000000001

float Fresnel_Dielectric(vector i, normal n, float eta)
{
//see https://en.wikipedia.org/wiki/Fresnel_equations
    float c = fabs(dot(i, n));
    float g = eta * eta - 1 + c * c;
    float result = 1.0;
    
    if (g > 0) {
        g = sqrt(g);
        float a = (g - c) / (g + c);
        float b = (c * (g + c) - 1) / (c * (g + c) + 1);
        result = 0.5 * a * a * (1 + b * b);
    }
    
    return result;
}

shader glass(
    color diffuse_col = 1.8, 
    float ior = 1.45, 
    output closure color bsdf = 0)
{
    float real_ior = max(ior, IOR_THRESHOLD);
    float eta = backfacing()? 1.0 / real_ior : real_ior;
    float fr = Fresnel_Dielectric(I, N, eta);
    
    bsdf = diffuse_col * (fr * reflection(N) + (1.0 - fr) * refraction(N, eta));
}