-
Notifications
You must be signed in to change notification settings - Fork 4
/
shader_source.go
236 lines (210 loc) · 6.48 KB
/
shader_source.go
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
package main
const vsSource = `#version 300 es
layout (location = 0) in vec4 aVertexPosition;
layout (location = 1) in uint aVertexLabel;
layout (location = 2) in uint aSelectMask;
uniform mat4 uModelViewMatrix;
uniform mat4 uProjectionMatrix;
uniform mat4 uSelectMatrix;
uniform mat4 uCropMatrix;
uniform float uZMin;
uniform float uZRange;
uniform float uPointSizeBase;
uniform int uUseSelectMask;
uniform uint uMinLabel;
uniform uint uMaxLabel;
vec4 viewPosition;
vec4 selectPosition;
vec4 cropPosition;
lowp float c;
lowp float cSelected;
out lowp vec4 vColor;
vec4 label2color(int label) {
int idx = label % 20;
vec3 c;
if (idx == 0) c = vec3(128.0, 128.0, 128.0);
else if (idx == 1) c = vec3(60.0, 180.0, 75.0);
else if (idx == 2) c = vec3(230.0, 25.0, 75.0);
else if (idx == 3) c = vec3(255.0, 225.0, 25.0);
else if (idx == 4) c = vec3(67.0, 99.0, 216.0);
else if (idx == 5) c = vec3(245.0, 130.0, 49.0);
else if (idx == 6) c = vec3(145.0, 30.0, 180.0);
else if (idx == 7) c = vec3(70.0, 240.0, 240.0);
else if (idx == 8) c = vec3(240.0, 50.0, 230.0);
else if (idx == 9) c = vec3(188.0, 246.0, 12.0);
else if (idx == 10) c = vec3(250.0, 190.0, 190.0);
else if (idx == 11) c = vec3(0.0, 128.0, 128.0);
else if (idx == 12) c = vec3(230.0, 190.0, 255.0);
else if (idx == 13) c = vec3(154.0, 99.0, 36.0);
else if (idx == 14) c = vec3(255.0, 250.0, 200.0);
else if (idx == 15) c = vec3(128.0, 0.0, 0.0);
else if (idx == 16) c = vec3(170.0, 255.0, 195.0);
else if (idx == 17) c = vec3(128.0, 128.0, 0.0);
else if (idx == 18) c = vec3(255.0, 216.0, 177.0);
else if (idx == 19) c = vec3(0.0, 0.0, 117.0);
else c = vec3(128.0, 128.0, 128.0);
c = mix(c, vec3(255.0, 255.0, 255.0), cSelected);
c /= 255.0;
return vec4(c, 1.0);
}
void main(void) {
cropPosition = uCropMatrix * aVertexPosition;
if (any(lessThan(vec3(cropPosition), vec3(0, 0, 0))) ||
any(lessThan(vec3(1.0, 1.0, 1.0), vec3(cropPosition)))) {
gl_Position = vec4(-1, -1, 0, 0);
gl_PointSize = 0.0;
return;
}
viewPosition = uModelViewMatrix * aVertexPosition;
gl_Position = uProjectionMatrix * viewPosition;
if (uProjectionMatrix[3][3] == 0.0) {
// Perspective mode
gl_PointSize = clamp(uPointSizeBase / length(viewPosition), 1.0, uPointSizeBase);
} else {
// Orthographic mode
gl_PointSize = uPointSizeBase / 20.0;
}
if (uUseSelectMask == 0) {
selectPosition = uSelectMatrix * aVertexPosition;
if (uSelectMatrix[3][3] == 1.0 &&
all(lessThanEqual(vec3(0, 0, 0), vec3(selectPosition))) &&
all(lessThanEqual(vec3(selectPosition), vec3(1.0, 1.0, 1.0)))) {
cSelected = 0.5;
} else {
cSelected = 0.0;
}
} else {
if ((aSelectMask & 0x10u) != 0u) {
cSelected = 0.5;
} else {
cSelected = 0.0;
}
}
if (aVertexLabel >= uMinLabel && aVertexLabel <= uMaxLabel) {
vColor = label2color(int(aVertexLabel));
} else {
c = (aVertexPosition[2] - uZMin) / uZRange;
vColor = vec4(c, cSelected, 1.0 - c, 1.0);
}
}
`
const vsSubSource = `#version 300 es
layout (location = 0) in vec4 aVertexPosition;
uniform mat4 uModelViewMatrix;
uniform mat4 uProjectionMatrix;
uniform float uPointSizeBase;
vec4 viewPosition;
out lowp vec4 vColor;
void main(void) {
viewPosition = uModelViewMatrix * aVertexPosition;
gl_Position = uProjectionMatrix * viewPosition;
if (uProjectionMatrix[3][3] == 0.0) {
// Perspective mode
gl_PointSize = clamp(uPointSizeBase / length(viewPosition), 1.0, uPointSizeBase);
} else {
// Orthographic mode
gl_PointSize = uPointSizeBase / 20.0;
}
vColor = vec4(0.8, 0.8, 0.8, 0.5);
}
`
const vsSelectSource = `#version 300 es
layout (location = 0) in vec4 aVertexPosition;
uniform mat4 uModelViewMatrix;
uniform mat4 uProjectionMatrix;
uniform float uPointSizeBase;
vec4 viewPosition;
out lowp vec4 vColor;
void main(void) {
viewPosition = uModelViewMatrix * aVertexPosition;
gl_Position = uProjectionMatrix * viewPosition;
if (uProjectionMatrix[3][3] == 0.0) {
// Perspective mode
gl_PointSize = clamp(1.5 * uPointSizeBase / length(viewPosition), 6.0, uPointSizeBase);
} else {
// Orthographic mode
gl_PointSize = 3.0 * uPointSizeBase / 20.0;
}
vColor = vec4(1.0, 1.0, 1.0, 0.8);
}
`
const fsSource = `#version 300 es
in lowp vec4 vColor;
out lowp vec4 outColor;
void main(void) {
outColor = vColor;
}
`
const vsMapSource = `#version 300 es
layout (location = 0) in vec4 aVertexPosition;
layout (location = 1) in vec2 aTextureCoord;
uniform mat4 uModelViewMatrix;
uniform mat4 uProjectionMatrix;
out highp vec2 vTextureCoord;
void main(void) {
gl_Position = uProjectionMatrix * uModelViewMatrix * aVertexPosition;
vTextureCoord = aTextureCoord;
}
`
const fsMapSource = `#version 300 es
in highp vec2 vTextureCoord;
uniform sampler2D uSampler;
uniform lowp float uAlpha;
out lowp vec4 vColor;
void main(void) {
vColor = texture(uSampler, vec2(vTextureCoord.s, vTextureCoord.t));
vColor[3] = uAlpha;
}
`
const csComputeSelectSource = `#version 300 es
layout (location = 0) in vec4 aVertexPosition;
layout (location = 2) in uint aSelectMask;
uniform mat4 uModelViewMatrix;
uniform mat4 uProjectionMatrix;
uniform mat4 uSelectMatrix;
uniform mat4 uCropMatrix;
uniform vec3 uOrigin;
uniform vec3 uDir;
lowp vec4 selectPosition;
lowp vec4 cropPosition;
lowp vec3 rel;
lowp float dotDir;
lowp float distSq;
lowp float dSq;
vec4 screenPosition;
flat out uint oResult;
void main(void) {
oResult = aSelectMask & 0x10u; // keep previous segment select result
cropPosition = uCropMatrix * aVertexPosition;
if (any(lessThan(vec3(cropPosition), vec3(0, 0, 0))) ||
any(lessThan(vec3(1.0, 1.0, 1.0), vec3(cropPosition)))) {
oResult |= 1u;
} else {
screenPosition = uProjectionMatrix * uModelViewMatrix * aVertexPosition;
if (any(lessThan(vec3(screenPosition), vec3(0, 0, 0))) ||
any(lessThan(vec3(1.0, 1.0, 1.0), vec3(screenPosition)))) {
oResult |= 8u;
}
}
selectPosition = uSelectMatrix * aVertexPosition;
if (uSelectMatrix[3][3] == 1.0 &&
all(lessThanEqual(vec3(0, 0, 0), vec3(selectPosition))) &&
all(lessThanEqual(vec3(selectPosition), vec3(1.0, 1.0, 1.0)))) {
oResult |= 2u;
}
// Check distance from mouse cursor
rel = vec3(uOrigin) - vec3(aVertexPosition);
dotDir = dot(rel, vec3(uDir));
if (dotDir < 0.0) {
distSq = dot(rel, rel);
dSq = distSq - dotDir * dotDir;
if (dSq < 0.1 * 0.1 && distSq > 1.0) {
oResult |= 4u;
}
}
}
`
const fsComputeSelectSource = `#version 300 es
void main(void) {
}
`