-
Notifications
You must be signed in to change notification settings - Fork 64
/
Digits.osl
166 lines (139 loc) · 3.84 KB
/
Digits.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
// A shader that emulates a 7-segment numerical display
// Digits.osl by Zap Andersson
// Modified: 2019-11-25
// Copyright 2019 Autodesk Inc, All rights reserved. This file is licensed under Apache 2.0 license
// https://github.com/ADN-DevTech/3dsMax-OSL-Shaders/blob/master/LICENSE.txt
int inBit(vector pt, point start, point end)
{
if (pt[0] > start[0] && pt[0] < end[0] &&
pt[1] > start[1] && pt[1] < end[1])
return 1;
return 0;
}
int inDigit(vector in, int digit, int dp, int ms)
{
vector pt = in;
pt -= point(0.5, 0.2, 0.0);
pt *= 2.5;
pt += point(0.5, 0.2, 0.0);
if (ms && inBit(pt, point(-0.9, 0.9, 0.0), point(-0.2, 1.1, 0.0)))
return 1;
if (pt[0] < -0.1 || pt[1] < -0.1 ||
pt[0] > 1.3 || pt[1] > 2.1)
return -1;
if (dp && inBit(pt, point(1.15, -0.1, 0.0), point(1.3, 0.1, 0)))
{
return 1;
}
if (inBit(pt, point(0.1,-0.1,0.0), point(0.9,0.1,0.0)))
{
if (digit == 2 || digit == 3 || digit == 5 || digit == 6 || digit == 8 || digit == 9 || digit == 0)
return 1;
else
return 0;
}
if (inBit(pt, point(0.1,0.9,0.0), point(0.9,1.1,0.0)))
{
if (digit == 2 || digit == 3 || digit == 4 || digit == 5 || digit == 6 || digit == 8 || digit == 9)
return 1;
else
return 0;
}
if (inBit(pt, point(0.1,1.9,0.0), point(0.9,2.1,0.0)))
{
if (digit == 2 || digit == 3 || digit == 5 || digit == 6 || digit == 7 || digit == 8 || digit == 9 || digit == 0)
return 1;
else
return 0;
}
if (inBit(pt, point(-0.1,0.1,0.0), point(0.1,0.9,0.0)))
{
if (digit == 2 || digit == 6 || digit == 8 || digit == 0)
return 1;
else
return 0;
}
if (inBit(pt, point(-0.1,1.1,0.0), point(0.1,1.9,0.0)))
{
if (digit == 4 || digit == 5 || digit == 6 || digit == 8 || digit == 9 || digit == 0)
return 1;
else
return 0;
}
if (inBit(pt, point(0.9,0.1,0.0), point(1.1,0.9,0.0)))
{
if (digit == 1 || digit == 3 || digit == 4 || digit == 5 || digit==6 || digit == 7 || digit == 8 || digit == 9 || digit == 0)
return 1;
else
return 0;
}
if (inBit(pt, point(0.9,1.1,0.0), point(1.1,1.9,0.0)))
{
if (digit == 1 || digit == 2 || digit == 3 || digit == 4 || digit == 7 || digit == 8 || digit == 9 || digit == 0)
return 1;
else
return 0;
}
return -1;
}
shader Digits
[[ string help="A shader that emulates a 7-Segment display of numbers.<br/>Useful for debugging and showing numbers in SME",
string category = "Textures"
]]
(
vector UVW = vector(u,v,0),
float Number = 42,
int Digits = 2 [[ int min=2, int max=8 ]],
int Decimals = 0 [[ int min=0, int max=6 ]],
float Scale = 1.0,
color BGColor = 0.0,
color OnColor = color(1,0,0),
color OffColor = color(0.1,0.0,0.0),
int UseOffColor = 1
[[ string widget="checkBox", string label = "Use OffColor" ]],
output color Out = BGColor,
output float Fac = 0.0,
output float NumberOut = Number [[ string label = "Number" ]]
)
{
int mult[9] = { 1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000 };
point px = UVW / Scale;
int number = int(Number * mult[Decimals]);
int minus = 0;
if (number < 0)
{
number = -number;
minus = 1;
}
int digits = Digits;
// Figure out how many digits we need
int leftOfDecimalPoint = (int(floor(log(fabs(Number), 10.0)))) + 1;
// ...but at least one
if (leftOfDecimalPoint <= 0)
leftOfDecimalPoint = 1;
int maxdigi = leftOfDecimalPoint + Decimals;
if (maxdigi > digits)
digits = maxdigi;
if (px[0] > 0 && px[0] < 1 && px[1] > 0 && px[1] < 1)
{
px -= point(0.9, 0.5, 0.0);
px *= (digits * 0.8);
px += point(0.9, 0.5, 0.0);
for (int i = 0; i < digits; i++)
{
int x = inDigit(px, int(number) % 10, Decimals == i, minus && i == digits-1);
if (x == 0 && UseOffColor)
{
Out = OffColor;
Fac = 1.0;
}
if (x == 1)
{
Out = OnColor;
Fac = 1.0;
}
number /= 10;
px += point(0.6,0.0,0.0);
}
}
}