-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathdigits.yaml
104 lines (96 loc) · 6.01 KB
/
digits.yaml
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
styles:
shapes-digits:
doc:
author:
name: P_Malin
url: https://www.shadertoy.com/view/4sBSWW
twitter: P_Malin
version: 0.0.1
tangram-version: 0.0.7
licence: License Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.
description: |
Collection of functions to draw number digits.
shaders:
defines:
CHAR_BLANK: 12.0
CHAR_MINUS: 11.0
CHAR_DECIMAL_POINT: 10.
blocks:
global: |
#ifdef TANGRAM_FRAGMENT_SHADER
float SampleDigit (const in float fDigit, const in vec2 vUV) {
if(vUV.x < 0.0) return 0.0;
if(vUV.y < 0.0) return 0.0;
if(vUV.x >= 1.0) return 0.0;
if(vUV.y >= 1.0) return 0.0;
// In this version, each digit is made up of a 4x5 array of bits
float fDigitBinary = 0.0;
if(fDigit < 0.5) { // 0
fDigitBinary = 7.0 + 5.0 * 16.0 + 5.0 * 256.0 + 5.0 * 4096.0 + 7.0 * 65536.0;
} else if(fDigit < 1.5) { // 1
fDigitBinary = 2.0 + 2.0 * 16.0 + 2.0 * 256.0 + 2.0 * 4096.0 + 2.0 * 65536.0;
} else if(fDigit < 2.5) { // 2
fDigitBinary = 7.0 + 1.0 * 16.0 + 7.0 * 256.0 + 4.0 * 4096.0 + 7.0 * 65536.0;
} else if(fDigit < 3.5) { // 3
fDigitBinary = 7.0 + 4.0 * 16.0 + 7.0 * 256.0 + 4.0 * 4096.0 + 7.0 * 65536.0;
} else if(fDigit < 4.5) { // 4
fDigitBinary = 4.0 + 7.0 * 16.0 + 5.0 * 256.0 + 1.0 * 4096.0 + 1.0 * 65536.0;
} else if(fDigit < 5.5) { // 5
fDigitBinary = 7.0 + 4.0 * 16.0 + 7.0 * 256.0 + 1.0 * 4096.0 + 7.0 * 65536.0;
} else if(fDigit < 6.5) { // 6
fDigitBinary = 7.0 + 5.0 * 16.0 + 7.0 * 256.0 + 1.0 * 4096.0 + 7.0 * 65536.0;
} else if(fDigit < 7.5) { // 7
fDigitBinary = 4.0 + 4.0 * 16.0 + 4.0 * 256.0 + 4.0 * 4096.0 + 7.0 * 65536.0;
} else if(fDigit < 8.5) { // 8
fDigitBinary = 7.0 + 5.0 * 16.0 + 7.0 * 256.0 + 5.0 * 4096.0 + 7.0 * 65536.0;
} else if(fDigit < 9.5) { // 9
fDigitBinary = 7.0 + 4.0 * 16.0 + 7.0 * 256.0 + 5.0 * 4096.0 + 7.0 * 65536.0;
} else if(fDigit < 10.5) { // '.'
fDigitBinary = 2.0 + 0.0 * 16.0 + 0.0 * 256.0 + 0.0 * 4096.0 + 0.0 * 65536.0;
} else if(fDigit < 11.5) { // '-'
fDigitBinary = 0.0 + 0.0 * 16.0 + 7.0 * 256.0 + 0.0 * 4096.0 + 0.0 * 65536.0;
}
vec2 vPixel = floor(vUV * vec2(4.0, 5.0));
float fIndex = vPixel.x + (vPixel.y * 4.0);
return mod(floor(fDigitBinary / pow(2.0, fIndex)), 2.0);
}
float PrintValue (const in vec2 vStringCharCoords, const in float fValue, const in float fMaxDigits, const in float fDecimalPlaces) {
float fAbsValue = abs(fValue);
float fStringCharIndex = floor(vStringCharCoords.x);
float fLog10Value = log2(fAbsValue) / log2(10.0);
float fBiggestDigitIndex = max(floor(fLog10Value), 0.0);
// This is the character we are going to display for this pixel
float fDigitCharacter = CHAR_BLANK;
float fDigitIndex = fMaxDigits - fStringCharIndex;
if(fDigitIndex > (-fDecimalPlaces - 1.5)) {
if(fDigitIndex > fBiggestDigitIndex) {
if(fValue < 0.0) {
if(fDigitIndex < (fBiggestDigitIndex+1.5)) {
fDigitCharacter = CHAR_MINUS;
}
}
} else {
if(fDigitIndex == -1.0) {
if(fDecimalPlaces > 0.0) {
fDigitCharacter = CHAR_DECIMAL_POINT;
}
} else {
if(fDigitIndex < 0.0) {
// move along one to account for .
fDigitIndex += 1.0;
}
float fDigitValue = (fAbsValue / (pow(10.0, fDigitIndex)));
// This is inaccurate - I think because I treat each digit independently
// The value 2.0 gets printed as 2.09 :/
//fDigitCharacter = mod(floor(fDigitValue), 10.0);
fDigitCharacter = mod(floor(0.0001+fDigitValue), 10.0); // fix from iq
}
}
}
vec2 vCharPos = vec2(fract(vStringCharCoords.x), vStringCharCoords.y);
return SampleDigit(fDigitCharacter, vCharPos);
}
float PrintValue (in vec2 fragCoord, const in vec2 vPixelCoords, const in vec2 vFontSize, const in float fValue, const in float fMaxDigits, const in float fDecimalPlaces) {
return PrintValue((fragCoord.xy - vPixelCoords) / vFontSize, fValue, fMaxDigits, fDecimalPlaces);
}
#endif