Voronoi playtime, some Cycles OSL secrets

This article now has a follow up that shows how to implement different distance metrics.
Members over at BlenderArtists wondered what it would take to get the diversity of voronoi textures back in Cycles which are present in Blender Internal. Well, the functionality is available but hidden in an OSL include file, so it takes a very simple OSL script node to get at it.
The example below is a Voronoi Crackle textures (basically the distance to the second closest point minus the distance to the closest point):

The node setup for this image is shown below.

Code

The code is quite simple. It utilizes the node_texture.h include distributed with Blender. Its outputs are the distances to the four closests points.
#include "node_texture.h"

shader Crackle(
  point p = P,
  output float Fac1 = 0,
  output float Fac2 = 0,
  output float Fac3 = 0,
  output float Fac4 = 0
){
  float da[4];
  point pa[4];

  voronoi(p, "Distance Squared", 0, da, pa);
  
  Fac1 = da[0];  
  Fac2 = da[1];  
  Fac3 = da[2];  
  Fac4 = da[3];  
}
Mind you, there is a lot more in the include (it is in the scripts\addons\cycles\shader directory of you Blender distribution), it even sports a voronoi_Cr function which does more or less the same as the node setup below, but this node is a bit more versatile for your experimentation :-) The only drawback is that we cannot define anything in the make up of the node ohter than its input and output sockers, so it is not possibly to define a nice drop down with for example distance metrics in OSL. (We could use an integer socket to switch, but that feels clunky).

Example node setup

The image at the start of this post was created with the following node setup:




No comments:

Post a Comment