Strips OSL Shader: subdivide a uv map into strips and slots

In an ongoing effort to search for methods that can be used to generate bark textures I devised this simple OSL shader: it takes a uv-map (for example one that is wrapped around a cylinder representing a branch ans subdivides this in to strips. These strips are then subdivided into slots and each of these slots is given its own uv map and a random value.

This simple setup makes it possible for example to combine two tileable images in a manner that resembles bark an knots or cracks in that bark. The example below shows the result with two simple black and white images, mapped to a plane a a cylinder:

The underlying slots look like this:

Example node setup and code availability

The example node setup used to create both of the images above looks like this (click to enlarge):

The shader is availble on GitHub along with two sample textures: 1, 2.
The code should contain enough comments to get you going, but just tweaking the input parameters based on the example noodle should get you a long way.

If you would like to know more about programming OSL you might be interested in my book "Open Shading Language for Blender". More on the availability of this book and a sample can be found on this page.

3D printing an H0 scale stone bridge: the result

I sent my bridge design to the printers and I am quite pleased with the result as it came back from Shapeways, especially the detailing. Even though it's done in Strong and Flexible (as opposed to Frosted detail) the stonework shows up nicely. I will be painting it the coming weeks but as that has nothing much to do with Blender I'll probably will document that on my model train blog.

3D printing an H0 scale stone bridge: detailing

Added all the brickwork and some randomness along with an ornament on the cap stone:

3D printing an H0 scale stone bridge

I put the basket arch addon discussed earlier to good use for a personal project, a H0 scale stone bridge to be used on my model train layout.

On my layout I wanted to have something different than the many stock models availabel from the big brands so I decided to design and print them myself. The actual printing will be done by Shapeways and currently I am working on finalizing the design of the model. The rough version is shown below:

It is already good to print (which I verified first with the 3D printing tools and then by uploading it to Shapeways) but it needs some tweaking, mainly in the form of randomizing the stones. Maybe I'll add some kind of ornament to the cap stone.

Chaos mosaic with OSL

Chaos mosaic is a technique tht can be used to reduce the repetative appearance of tilebale images. An example is shown below. The upper right plane shows visible repetition artifacts that are not apparent in the lower left plane.

(I used a 700x700 tileable texture from (Gravel0170_3_S.jpg) repeated four times in each direction on the upper right plane, and the same image with the chaosmosaic shader on the lower left plane)

Shader code

The shader code to implement this is very short:

shader chaosmosaic(
  point Pos=P,
  string Image="",
  int M=10,
  output color Color=0
  float x,y;

  float xi=floor(x*M);
  float yi=floor(y*M);
  // mapping here
  float xp=noise("cell",Pos,xi*M+yi);
  float yp=noise("cell",Pos,xi*M+yi+M*M);
  float xoffset = mod(mod(x,1.0/M)+xp,1);
  float yoffset = mod(mod(y,1.0/M)+yp,1);
  Color = texture(Image,xoffset,yoffset);
In the code above M is the number of squares in each direction in our uv map. We use it to calculate two indices (xi, and yi) that are subsequently used pick a random position in the texture (xp,yp). The pixel from the texture is then selected according to the relative offset in the small square inside the uv map (xoffset, yoffset).

The original algorithm also uses a blending function between adjacent tiles form the textures but that will blur the image. In this case we didn't implement that because from far enough away the small tile edges aren't noticable but the large scale repetion has gone away so with very little code we already have a better result.

Example node setup

The node setup used for the example image (the plane on the lower left) looks like this:


If you would like to know more about programming OSL you might be interested in my book "Open Shading Language for Blender". More on the availability of this book and a sample can be found on this page.

Free e-books at Packt

Packt (the publisher of my books on blender scripting and python web development) is giving away free e-books for Christmas.

I was a bit late in noticing it but so far they have given away some pretty interesting titles so it might be a good idea to check it out for more to come...