A OSL leaf shape shader for Cycles

In this first part of a series I present a simple shader that can be used to generate different leaf shapes. In later articles we will add shaders that produce the veins of the leaf.

Simple leaf shape with cubic splines

In the example image we have used the IvyGen addon to generate a single limb of some climbing plant species and used the shader from this article to produce the leaf shapes The node setup for this specific material is dicussed later on. (the stone texture is from cgtextures.com and was converted to a normal map and a displacement map with Shadermap CL. The backplate and environment lighting are from the Topanga Forest B collection by Blochi as found on Sibl archive)

The leaf shape shader is essentially creates one side of a leaf from a cubic spline and mirrors that to the other side to create a symmetrical shape. The spline that is used has four controls.

It starts on the left in the direction of P1 and ends on the right coming from the direction of P2. The lenght and angles of the vectors going to P1 and P2 are inputs to the shader. Some of the shapes you can create are shown below.
Note that it is also possible to define a shape that crosses itself in which case the behavior of the shader is undefined.

The annotated code for the shader is shown below. It includes a small library of functions that will be reused by shaders that will appear on this blog in the near future and this include has its separate page where you can download it and find installation instructions.

#include "equations.h"

shader leaf(
point Pos = P,
float Angle1 = 70,
float L1 = 1,
float Angle2 = 70,
float L2 = 1,
output float Leaf = 0

// calculate the four control point of the cubic spline
float x1,y1,x2,y2;
point P0 = point(0 , 0 ,0);
point P1 = point(x1 , y1,0)*L1;
point P2 = point(1-x2*L2, y2*L2,0);
point P3 = point(1 , 0 ,0);

// to determin the y value(s) of the spline at the x position we
// are located, we want to solve spline(t) - x = 0
// we therefore gather all factors and solve the cubic equation
float tfactor[4] = { P0[0]-Pos[0],
P0[0]+3*P1[0]-3*P2[0]+P3[0] };
float t[3];
int nrealroots;
cubic(tfactor, t, nrealroots);

// at this point, the array t holds up to 3 real roots
// remove any real root that is not in range [0,1]
int i=0;
while(i < nrealroots){
if ((t[i] < 0) || (t[i] > 1)) {
int j=i;
while(j < (nrealroots-1)){

// note that a cubic funtion can have 3 real roots,
// but in this case we ignore such very warped curves
// TODO: w. 3 real roots w could set leaf = 1, if y < y0 OR y between y1,y2
// TODO: seration, possible by determining the closest
// distance (if inside leaf) to the spline and
// determining if w are within some periodic funtion f(t)

// we generate the shape mirrored about the x-axis
float y = Pos[1];
if(y<0) y = -y;

if(nrealroots > 0){
point Sy0 = cubicspline(t[0],P0,P1,P2,P3);
if(nrealroots > 1){
// if we have 2 roots we calculate and order the y values
// and check whether the current y values is between them
point Sy1 = cubicspline(t[1],P0,P1,P2,P3);
if ( Sy1[1] < Sy0[1] ){
if( (y > Sy1[1]) && (y < Sy0[1]) ) Leaf = 1;
if( (y > Sy0[1]) && (y < Sy1[1]) ) Leaf = 1;
// with a single value we check if we are below the y value
if( y < Sy0[1] ) Leaf = 1;

Example node setup

The node setup used to create the sample shapes look like this:

As you can see there is nothing fancy going on here. The leaves are simple squares with a reset uv-map and a mapping node is used to position the leave onto this square (in this case rotated 90 degrees). The output of the shader is then used to drive a mix shader which shows some green material where there is a leaf and a fully transparent material where there isn't.

Combining things with the Ivy Generator

The sample image at the beginning was create with the IvyGen addon. The leaves it produces are simple square faces that are not connected but nevertheless consist of a single mesh object. Because we want to orient each individual leaf a bit different we need a random number for each leaf. We therefore have to separate each face into its own object.

The workflow to achieve that then becomes:

  1. Create your ivy
  2. Assign the material shown in the noodle below to the leaf object
  3. Goto edit mode
  4. Select all
  5. Select Mesh->Vertices->Separate->By loose parts
  6. Go back to object mode.
Each leaf now is its own object we our leaf material attacthed.

Example node setup II

The noodle consists of three distinct parts:
  • The green part adds a small random rotation around the z axis to each uv map. This will be different for each individual leaf. The actual rotation is done by another OSL shader (given below) because the vector mapping node cannot be driven by inputs.
  • The red part maps the rotated uv to the correct position before handing it to our leaf shader. This mapping is necessary because the squares created by IvyGen are sort of centered on the vines and we want our leaves to protrude from the vined instead of being pierced by them.
  • The blue part is just a simple mottled green shader with some gloss.

The rotation of the uv-map arond the z-axis is performed by an OSL shader because the vector mapping node has no inputs to control the rotation and it would take a lot of vector math nodes to achieve what we want whereas thanks to OSL this rotation is a no-brainer:

shader rotate_z(
point Pos = P,
float Angle = 0,
output point Pout = P
Pout = rotate(Pos,radians(Angle),point(0,0,0),point(0,0,1));

Next steps

In a next article I will show how to add veins to the leaf shapes.

1 comment:

  1. Hmmm all my typing gone because of Wordpress and its sad caching mechanism. :-/ Might cause other comments to go awry.

    Anyway: short version... well done. Look forward to using this on Sapling.