OSL - Open Shading Language
In this section you can find some OSL scripts I have written. OSL in Blender is used only with Cycles render and allows for a more versatile usage of the materials. To use them you must use cycles and paste the code into a script node.
- Laser Sheet
- Random Color
- Grid
- Laser Sheet
- Random Color
- Grid
Laser Sheet
This script was created to simulate a laser sheet illuminating the objects in a scene. You can find an example of a node setup and practical application here.
/*
* DESCRIPTION:
* Simulates the emmission of a laser sheet with origin in point (laserx, lasery, laserz)
* and with a normal (norx, nory, norz)
* The output variable, laserout, defines the strength of the laser, which should be
* connected to an emission shader, which is then added to the base material of the object.
* The input parameters Thickness and Intensity allow the ajustment of the laser sheet.
* High Intensity values might increase specle points
* AUTHOR: written by Renato Sousa,
* http://renatogsousa.weebly.com/
* email: renatogsousa AT gmail DOT com
*
* last modified 27 December 2012
*/
#include "stdosl.h"
shader pattern (
float Thickness = 0.1,
float Intensity=1,
float laserx=0.0,
float lasery=0.0,
float laserz=0.0,
float norx=0,
float nory=0,
float norz=1,
output float laserout=0)
{
point Pos = P;
int light=0;
/* ***** plane definition ***** */
vector lasernormal=normalize(vector(norx,nory,norz));
float plane=lasernormal[0]*(laserx-Pos[0])+lasernormal[1]*(lasery-Pos[1])+lasernormal[2]*(laserz-Pos[2]);
/* ***** light direction search: from point P to laser origin ***** */
vector laserrelativepos=vector(laserx-Pos[0],lasery-Pos[1],laserz-Pos[2]);
/* ***** limit the search distance to the distance between object and laser ***** */
float maxdist=length(laserrelativepos);
laserrelativepos=normalize(laserrelativepos);
/* ***** trace if there is any object between point P and laser origin ***** */
int shadow=trace(Pos, laserrelativepos,"maxdist",maxdist);
light=(1-shadow);
/* **** laser light is maximum in the plane, limited by Intensity **** */
laserout=abs(Thickness / (plane))*light;
laserout=min(laserout,Intensity);
}
* DESCRIPTION:
* Simulates the emmission of a laser sheet with origin in point (laserx, lasery, laserz)
* and with a normal (norx, nory, norz)
* The output variable, laserout, defines the strength of the laser, which should be
* connected to an emission shader, which is then added to the base material of the object.
* The input parameters Thickness and Intensity allow the ajustment of the laser sheet.
* High Intensity values might increase specle points
* AUTHOR: written by Renato Sousa,
* http://renatogsousa.weebly.com/
* email: renatogsousa AT gmail DOT com
*
* last modified 27 December 2012
*/
#include "stdosl.h"
shader pattern (
float Thickness = 0.1,
float Intensity=1,
float laserx=0.0,
float lasery=0.0,
float laserz=0.0,
float norx=0,
float nory=0,
float norz=1,
output float laserout=0)
{
point Pos = P;
int light=0;
/* ***** plane definition ***** */
vector lasernormal=normalize(vector(norx,nory,norz));
float plane=lasernormal[0]*(laserx-Pos[0])+lasernormal[1]*(lasery-Pos[1])+lasernormal[2]*(laserz-Pos[2]);
/* ***** light direction search: from point P to laser origin ***** */
vector laserrelativepos=vector(laserx-Pos[0],lasery-Pos[1],laserz-Pos[2]);
/* ***** limit the search distance to the distance between object and laser ***** */
float maxdist=length(laserrelativepos);
laserrelativepos=normalize(laserrelativepos);
/* ***** trace if there is any object between point P and laser origin ***** */
int shadow=trace(Pos, laserrelativepos,"maxdist",maxdist);
light=(1-shadow);
/* **** laser light is maximum in the plane, limited by Intensity **** */
laserout=abs(Thickness / (plane))*light;
laserout=min(laserout,Intensity);
}
Random Color
In this example, a random color node is created for every object, which can be the input for other nodes. Try to input it into a diffuse shader for example and duplicate the object to see what happens.
#include "stdosl.h"
shader RandomColor(
output float R=0.8,
output float G=0.8,
output float B=0.8,
output color RandomColor=0)
{
float Random=0;
getattribute("object:random", Random);
R=trunc(Random*100.0)/100.0;
G=trunc((Random*100.0-trunc(Random*100.0))*100.0)/100.0;
B=trunc((Random*10000.0-trunc(Random*10000.0))*100.0)/100.0;
RandomColor=color(R,G,B);
}
#include "stdosl.h"
shader RandomColor(
output float R=0.8,
output float G=0.8,
output float B=0.8,
output color RandomColor=0)
{
float Random=0;
getattribute("object:random", Random);
R=trunc(Random*100.0)/100.0;
G=trunc((Random*100.0-trunc(Random*100.0))*100.0)/100.0;
B=trunc((Random*10000.0-trunc(Random*10000.0))*100.0)/100.0;
RandomColor=color(R,G,B);
}
Grid
Here's a simple OSL script to simulate a grid, and the corresponding node setup. You can change the size of the grid with the freq. parameter, change its apparent thickness with Mag and displace it with deltax and deltay.
#include "stdosl.h"
shader grelha(
point Pos=P,
float freq=10,
float Mag=1,
float deltax=0,
float deltay=0,
output float Fac=0,
output float solido=0
)
{
Fac=max(Mag*((sin(freq*(Pos[0]+deltax)))),Mag*((sin(freq*(Pos[1]+deltay)))));
solido=Fac>0;
}
#include "stdosl.h"
shader grelha(
point Pos=P,
float freq=10,
float Mag=1,
float deltax=0,
float deltay=0,
output float Fac=0,
output float solido=0
)
{
Fac=max(Mag*((sin(freq*(Pos[0]+deltax)))),Mag*((sin(freq*(Pos[1]+deltay)))));
solido=Fac>0;
}