The shader presented in this article will create a unique snowflake for each value of its

`Seed`

. Using the object info node we may obtain a unique random number for each object that we may use for this purpose as we will see when we examine the node setup. ## Snowflakes

The code below defines the following utility functions;- hex
- this function will produce the x and y coordinates of a point as it is rotated through the six segments of a hexagon
- in_hexagon
- this function will return a non zero value if a point lies within a hexagon
- pattern
- this function does the real work. It returns non zero if a point is positioned within a number of hexagons that are randomly positioned along the x-axis. It also draws a connecting rod that extends as far as the farthest hexagon.

`pattern()`

. Note that because we expect uv-coordinates for a simple square (I.e in de range [0,1], we transform these coordinates so that the center is at (0.5, 0.5) [line 68]. #define SLOPE 1/sqrt(3) #define SIDE sqrt(.75) #define D60c cos(radians(60)) #define D60s sin(radians(60)) void hex(float x, float y, output float hx[6], output float hy[6]){ hx[0]=x; hy[0]=y; hx[1]=x*D60c-y*D60s; hy[1]=y*D60c+x*D60s; hx[2]=x*D60c+y*D60s;; hy[2]=y*D60c-x*D60s; hx[3]=-hx[0]; hy[3]=-hy[0]; hx[4]=-hx[1]; hy[4]=-hy[1]; hx[5]=-hx[2]; hy[5]=-hy[2]; } int in_hexagon( float px, float py, float cx, float cy, float r, output float d ){ d=hypot(px-cx,py-cy); if(d>r){ return 0; } float hx[6],hy[6]; hex(px-cx, py-cy, hx, hy); for(int h=0; h < 6; h++){ if((abs(hy[h]) < SLOPE*hx[h]) && (hx[h]< r*SIDE)){ d=abs(hx[h]); return 1; } } return 0; } #define CELL noise("cell",seed++) float pattern(float x, float y, int Seed, int Kernels){ int seed=Seed; int n=(int)(1+Kernels*CELL); float hx=0, maxx=0; for(int f=0; f < n; f++){ float hy=0; float r=0.2*CELL; float d; if(in_hexagon(x,y, hx,hy,r, d)){ return d; } hx=SIDE*CELL; if(hx>maxx){maxx=hx;} } if(x < maxx && abs(y) < 0.01){ return 1; } } shader snowflake( point Pos=P, int Seed=0, int Kernels=15, output float Fac=0 ){ float hx[6],hy[6]; hex(2*(Pos[0]-0.5), 2*(Pos[1]-0.5), hx, hy); for(int h=0; h<6 br="" fac="=0;" h=""> if(abs(hy[h]) < SLOPE*hx[h]){ Fac=pattern(hx[h],hy[h],Seed, Kernels); } } }

## Example node setup

The node setup used to shade the particles in the image at the start of this article looks like this:The random value from the object info node lies in the range [0,1], so we multiply it by a large value to get a unique integer.The output of the shader node is checked to see if it is non-zero. if so we use a group of shaders to mimic ice, otherwise a transparent shader. The

`Fac`

output may vary smoothly so we use a color ramp node with constant interpolation the create stepped values that we can use to drive an ice-like bump pattern. The `Kernels`

input determines how many random hexagons are within each snow flake, so this essentially controles how dense the flakes look. ## Code availability

You can copy the code from the listing above but you may also dowload it from GitHub.
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.

## No comments:

## Post a comment