forked from varkenvarken/osl-shaders
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hexagon2.osl
98 lines (78 loc) · 2.56 KB
/
hexagon2.osl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#define A 0.86602540378443864676372317075294 // sqrt(3)/2
#define A2 (2*A)
#define A4 (4*A)
#define SY (1/A)
#define SWAP(t,a,b) t=a; a=b; b=t
shader hexagons(
color Diffuse_Color1 = color(0.2, 0.8, 0.2),
color Diffuse_Color2 = color(0.8, 0.2, 0.2),
color Diffuse_Color3 = color(0.2, 0.2, 0.8),
float Width = 0.05,
vector Coordinates = 0,
output color Color = 0,
output int Index = 1,
output float Center = 0,
output float Edge = 0,
output int InEdge = 0
){
// calculate the colors
color colors[3] = {Diffuse_Color1,
Diffuse_Color2,
Diffuse_Color3};
// we warp the grid so that two adjacent equilateral triangles
// are mapped to two triangles that fit in a square
float syc = Coordinates[1] * SY;
float sxc = Coordinates[0] + 0.5 * syc;
int ind[18] = {1,1,3,3,3,1, 2,2,2,3,3,3, 1,2,2,2,1,1};
int iy = int(mod(syc,3.0));
int ix = int(mod(sxc,3.0));
ix = iy * 6 + ix * 2 + ( mod(sxc,1.0) > mod(syc,1.0) );
Index = ind[ix];
Color = colors[Index-1];
// calculate the distance to the center of the hexagon
float sx = mod(Coordinates[0],3);
float sy = mod(Coordinates[1]+0.75,A4);
// map everthing to a single quadrant
if ( sx > 1.5 ) sx = 3 - sx;
if ( sy > A2 ) sy = A4 - sy;
// distance to nearest edge
float x = mod(Coordinates[0]/3,1.0);
float y = mod(Coordinates[1]/3,A2);
#define N 18
vector hc[N] = {
vector( 0, -A2/3 ,0),
vector( 0, 0 ,0),
vector( 0, A2/3 ,0),
vector( 0,2*A2/3 ,0),
vector( 0, A2 ,0),
vector( 0,4*A2/3 ,0),
vector(0.5, -A2/3+A2/6,0),
vector(0.5, 0+A2/6,0),
vector(0.5, A2/3+A2/6,0),
vector(0.5,2*A2/3+A2/6,0),
vector(0.5, A2 +A2/6,0),
vector(0.5,4*A2/3+A2/6,0),
vector(1.0, -A2/3 ,0),
vector(1.0, 0 ,0),
vector(1.0, A2/3 ,0),
vector(1.0,2*A2/3 ,0),
vector(1.0, A2 ,0),
vector(1.0,4*A2/3 ,0)
};
float d[N], t;
for(int i=0; i < N; i++){
float dx = x - hc[i][0];
float dy = y - hc[i][1];
d[i] = hypot(dx, dy);
}
for(int j= N-1; j >= 0; j--){
for(int i= 0; i < j; i++){
if(d[i] > d[i+1]){
SWAP(t, d[i], d[i+1]);
}
}
}
Center = d[0];
Edge = d[1] - d[0];
InEdge = Edge < Width;
}