-
Notifications
You must be signed in to change notification settings - Fork 57
/
Copy pathChHalftone.osl
127 lines (107 loc) · 2.76 KB
/
ChHalftone.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
/*
* ChHalftone.osl by Charlie (c)2012
* from https://github.com/sambler/osl-shaders
*
* original script from -
* http://blenderartists.org/forum/showthread.php?270332-OSL-Goodness/page9
*
*/
/**
Pattern selects the dot type
Axis is a switch to set the Axis to the centre of the pattern area. (Shifts the texture)
Scale and Frequency do the same thing but it is useful to have the two inputs so one can be driven.
Size is equivalent to the width of the halftone dot.
SizeMultiplier adds additional control, so one can be driven.
Halftone returns the halftone.
Texture returns the texture used to generate the halftone.
X & Y return the texture coordinates.
*/
shader halftone_texture(
int Pattern = 0,
int Axis = 0,
int PingPong = 0,
point Point = P,
float Size = 0.5,
float SizeMultiplier = M_SQRT2,
float Frequency = 16,
float Scale = 16,
output float Y = 0,
output float X = 0,
output float Texture = 0,
output float Halftone = 0
)
{
void centre_axis(){
/* generate texture origin based on 0,0 -1 to 1 */
X= X*2-1;
Y= Y*2-1;
}
float ping_pong(float in){
/* reverses coordinates every other pattern */
int i= 0;
i= (int)ceil(in);
if (in > 1.0) {
if ( i % 2 == 0 ) {
in= mod(in,1);
in= 1-in;
}
else {
in= mod(in,1);
}
}
return in;
}
X= Point[0] * Scale * Frequency;
Y= Point[1] * Scale * Frequency;
if (PingPong == 1){
X= ping_pong(X);
Y= ping_pong(Y);
} else if (PingPong == 2){
X= ping_pong(X);
} else if (PingPong == 3){
Y= ping_pong(Y);
}
X= mod(X,1);
Y= mod(Y,1);
if (Axis == 0){
centre_axis();
}
if (Pattern == 0)
{
Texture= X*X+Y*Y; /* circle */
}
else if (Pattern == 1)
{
Texture= max(abs(X),abs(Y)); /* square */
}
else if (Pattern == 2)
{
Texture= (abs(X)+abs(Y))/2; /* diamond */
}
else if (Pattern == 3)
{
Texture= (atan2(abs(Y),X) / (2*M_PI))*2; /* cone */
}
else if (Pattern == 4)
{
Texture= (X+Y)/2; /* triangle */
}
else if (Pattern == 5)
{
Texture= (X+1)/2; /* linear x */
}
else if (Pattern == 6)
{
Texture= (Y+1)/2; /* linear y */
}
else if (Pattern == 7)
{
Texture= atan(abs(X))+atan(abs(Y));; /* cross */
}
else {
Texture= (atan2(Y,X) / (2*M_PI) + 0.5); /* test */
}
/* compare R to the dot size */
if (Texture > Size*SizeMultiplier) Halftone= 0;
else Halftone= 1;
}