Open Shading Language (OSL) is a small but rich language for programmable shading in advanced renderers and other applications, ideal for describing materials, lights, displacement, and pattern generation.
OSL was originally developed by Sony Pictures Imageworks for use in its in- house renderer used for feature film animation and visual effects, released as open source so it could be used by other visual effects and animation studios and rendering software vendors. Now it's the de facto standard shading language for VFX and animated features, used across the industry in many commercial and studio- proprietary renderers. Because of this, the work on OSL received an Academy Award for Technical Achievement in 2017 ..., see the GitHub project.
Today I'll show you how to use this option with Blender 3D to create a glass-like material for an object and render it with Blender 3D.
Open the Blender 3D application and set the render to Cycles, CPU and check Open Shading Language, see the next image:
Add an object, add a new material remove the Principled BSDF node
Create a file named Glass_001.osl select this file in the material area for the object.
You can see in the image above how I set the material with a Script node.
Add this source code in the file and render the scene:
#include "stdosl.h"
#define IOR_THRESHOLD 1.000001
float FresnelDielectric(vector i, normal n, float eta)
{
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;
}
surface Glass(
color diffuse_col = 0.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 = FresnelDielectric(I, N, eta);
bsdf = diffuse_col * (fr * reflection(N) + (1.0 - fr) * refraction(N, eta));
}