Friday 13 September 2013

Putting all together to generate a procedural tile pattern

Taking all that we have learned from mod( ), odd( ), even( ), whichTile( ) and so on, we can try building a procedural pattern such as this:


















The codes for generating this tile pattern is:

#include "D:\SweeKim\rsl\src\include\zin\zinMacro.h"

surface zinTest()
{
float pulseU, pulseV;
color ColA = (0,0,1), ColB = (0,1,0);
float pulseFreq = 5;

float uu = zinRepeat(u,pulseFreq);
float vv = zinRepeat(v,pulseFreq);

float col = zinWhichTile(u,pulseFreq);
float row = zinWhichTile(v,pulseFreq);

pulseU = (step(0.4,uu) - step(0.6,uu));
pulseV = (step(0.4,vv) - step(0.6,vv));

if(zinOdd(row))
{
if(zinEven(col))
{
ColA = (0.6,0.2,0.7); //purple
ColB = (0.3,0.5,0.7); //light blue
}
}
else
{
if(zinOdd(col))
{
ColA = (0.6,0.2,0.7); //purple
ColB = (0.3,0.5,0.7); //light blue
}
}

Oi = Os;
Ci = mix(ColA,ColB,pulseU * pulseV);
}

First of all, I have injected a #include line to read in the zinMacro.h header file. This header has the macros we need in the following codes, such as zinRepeat, zinWhichTile, zinOdd and zinEven.

float pulseU, pulseV;
- Created two float variables. The intention is to store the horizontal and vertical stripe pattern.

















_________________________________________________________________________________

color ColA = (0,0,1), ColB = (0,1,0);
- Created two color variables. Initialize them to blue and green. These two colors will be served as base colors.


_________________________________________________________________________________
float pulseFreq = 5;
- Created a float variable that decides how many times the pattern repeated. In this case, 5.


_________________________________________________________________________________
float uu = zinRepeat(u,pulseFreq);
float vv = zinRepeat(v,pulseFreq);
- Created another two float variables. The two variables will be used to store the result of repeating the pattern in u and v directions.
- zinRepeat function is defined as following in the zinMacro header file:
#define zinRepeat(x,freq)    (mod((x) * (freq), 1.0))

please refer to the previous post to understand how zinRepeat works.


















_________________________________________________________________________________

float col = zinWhichTile(u,pulseFreq);
float row = zinWhichTile(v,pulseFreq);
- The zinWhichTile function simply return the tile number. If the pulseFreq is 5, the returning value is 0,1,2,3,4. Tow float variables are created to store the value.
- The definition of zinWhichTile is:
 #define zinWhichTile(x,freq) (floor((x) * (freq)))

For more details explanation of how it works please refer to the previous post.

















_________________________________________________________________________________

pulseU = (step(0.4,uu) - step(0.6,uu));
pulseV = (step(0.4,vv) - step(0.6,vv));
- The pulseU and pulseV variables is making a stripe pattern starting from 0.4 to 0.6 from the range 0 to 1. With uu and vv previous set to repeat 5 times, the final result will look like this:
- Please note that even though the images presented here are in color, they are in fact in float values, meaning grayscale images.

















_________________________________________________________________________________

if(zinOdd(row))
{
     if(zinEven(col))
     {
          ColA = (0.6,0.2,0.7); //purple
          ColB = (0.3,0.5,0.7); //light blue
     }
}
else
{
     if(zinOdd(col))
     {
          ColA = (0.6,0.2,0.7); //purple
          ColB = (0.3,0.5,0.7); //light blue
     }
}

- This is the part where we start coloring the tile in different colors based on conditions. We must remember there is already a base color created at the very begining:
color ColA = (0,0,1), ColB = (0,1,0);

Then, check on each row to see if it is in odd number row, using zinOdd function. If so, go onto next condition, if it is in even column, using zinEven function. If both conditions are met, color it accordingly.

Else, if the very first condition returns false, which the result is in even row, go on to next condition to check if it is in odd number column. If so, color it accordingly.

- What we are trying to do here is, if the checking conditions are met, then we use a different color, else just use the default base color.


_________________________________________________________________________________

Ci = mix(ColA,ColB,pulseU * pulseV);
- Final part would be putting all these information together using mix function.





No comments:

Post a Comment