zincjs 1.16.2 → 1.16.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,287 @@
1
+ const THREE = require('three');
2
+
3
+ const glslVersion = null;
4
+
5
+ const fs =
6
+ `
7
+ precision highp float;
8
+ precision highp sampler2DArray;
9
+ precision highp sampler2D;
10
+
11
+ uniform vec3 u_size;
12
+ uniform int u_renderstyle;
13
+ uniform float u_renderthreshold;
14
+ uniform vec2 u_clim;
15
+
16
+ uniform sampler2DArray u_data;
17
+ uniform sampler2D u_cmdata;
18
+
19
+ varying vec3 v_position;
20
+ varying vec4 v_nearpos;
21
+ varying vec4 v_farpos;
22
+
23
+ // The maximum distance through our rendering volume is sqrt(3).
24
+ const int MAX_STEPS = 887; // 887 for 512^3, 1774 for 1024^3
25
+ const int REFINEMENT_STEPS = 4;
26
+ const float relative_step_size = 1.0;
27
+ const vec4 ambient_color = vec4(0.2, 0.4, 0.2, 1.0);
28
+ const vec4 diffuse_color = vec4(0.8, 0.2, 0.2, 1.0);
29
+ const vec4 specular_color = vec4(1.0, 1.0, 1.0, 1.0);
30
+ const float shininess = 40.0;
31
+
32
+ void cast_mip(vec3 start_loc, vec3 step, int nsteps, vec3 view_ray);
33
+ void cast_iso(vec3 start_loc, vec3 step, int nsteps, vec3 view_ray);
34
+
35
+ vec3 sample1(vec3 texcoords);
36
+ vec4 apply_colormap(float val);
37
+ vec4 add_lighting(float val, vec3 loc, vec3 step, vec3 view_ray);
38
+
39
+
40
+ void main() {
41
+ // Normalize clipping plane info
42
+ vec3 farpos = v_farpos.xyz / v_farpos.w;
43
+ vec3 nearpos = v_nearpos.xyz / v_nearpos.w;
44
+
45
+ // Calculate unit vector pointing in the view direction through this fragment.
46
+ vec3 view_ray = normalize(nearpos.xyz - farpos.xyz);
47
+
48
+ // Compute the (negative) distance to the front surface or near clipping plane.
49
+ // v_position is the back face of the cuboid, so the initial distance calculated in the dot
50
+ // product below is the distance from near clip plane to the back of the cuboid
51
+ float distance = dot(nearpos - v_position, view_ray);
52
+ distance = max(distance, min((-0.5 - v_position.x) / view_ray.x,
53
+ (u_size.x - 0.5 - v_position.x) / view_ray.x));
54
+ distance = max(distance, min((-0.5 - v_position.y) / view_ray.y,
55
+ (u_size.y - 0.5 - v_position.y) / view_ray.y));
56
+ distance = max(distance, min((-0.5 - v_position.z) / view_ray.z,
57
+ (u_size.z - 0.5 - v_position.z) / view_ray.z));
58
+
59
+ // Now we have the starting position on the front surface
60
+ vec3 front = v_position + view_ray * distance;
61
+
62
+ // Decide how many steps to take
63
+ int nsteps = int(-distance / relative_step_size + 0.5);
64
+ if ( nsteps < 1 )
65
+ discard;
66
+
67
+ // Get starting location and step vector in texture coordinates
68
+ vec3 step = ((v_position - front) / u_size) / float(nsteps);
69
+ vec3 start_loc = front / u_size;
70
+
71
+ // For testing: show the number of steps. This helps to establish
72
+ // whether the rays are correctly oriented
73
+ //gl_FragColor = vec4(0.0, float(nsteps) / 1.0 / u_size.x, 1.0, 1.0);
74
+ //return;
75
+
76
+ if (u_renderstyle == 0)
77
+ cast_mip(start_loc, step, nsteps, view_ray);
78
+ else if (u_renderstyle == 1)
79
+ cast_iso(start_loc, step, nsteps, view_ray);
80
+
81
+ if (gl_FragColor.a < 0.05)
82
+ discard;
83
+ }
84
+
85
+
86
+ vec3 sample1(vec3 texcoords) {
87
+ /* Sample float value from a 3D texture. Assumes intensity data. */
88
+ return texture(u_data, texcoords.xyz).rgb;
89
+ }
90
+
91
+
92
+ vec4 apply_colormap(float val) {
93
+ val = (val - u_clim[0]) / (u_clim[1] - u_clim[0]);
94
+ return texture2D(u_cmdata, vec2(val, 0.5));
95
+ }
96
+
97
+
98
+ void cast_mip(vec3 start_loc, vec3 step, int nsteps, vec3 view_ray) {
99
+
100
+ float max_val = -1e6;
101
+ int max_i = 100;
102
+ vec3 loc = start_loc;
103
+
104
+ // Enter the raycasting loop. In WebGL 1 the loop index cannot be compared with
105
+ // non-constant expression. So we use a hard-coded max, and an additional condition
106
+ // inside the loop.
107
+ for (int iter=0; iter<MAX_STEPS; iter++) {
108
+ if (iter >= nsteps)
109
+ break;
110
+ // Sample from the 3D texture
111
+ vec3 val = sample1(loc);
112
+ float avg = (val.x + val.y + val.z) / 3.0;
113
+ // Apply MIP operation
114
+ if (val > max_val) {
115
+ max_val = val;
116
+ max_i = iter;
117
+ }
118
+ // Advance location deeper into the volume
119
+ loc += step;
120
+ }
121
+
122
+ // Refine location, gives crispier images
123
+ vec3 iloc = start_loc + step * (float(max_i) - 0.5);
124
+ vec3 istep = step / float(REFINEMENT_STEPS);
125
+ for (int i=0; i<REFINEMENT_STEPS; i++) {
126
+ max_val = max(max_val, sample1(iloc));
127
+ iloc += istep;
128
+ }
129
+
130
+ // Resolve final color
131
+ gl_FragColor = apply_colormap(max_val);
132
+ }
133
+
134
+
135
+ void cast_iso(vec3 start_loc, vec3 step, int nsteps, vec3 view_ray) {
136
+
137
+ gl_FragColor = vec4(0.0); // init transparent
138
+ vec4 color3 = vec4(0.0); // final color
139
+ vec3 dstep = 1.5 / u_size; // step to sample derivative
140
+ vec3 loc = start_loc;
141
+
142
+ float low_threshold = u_renderthreshold - 0.02 * (u_clim[1] - u_clim[0]);
143
+
144
+ // Enter the raycasting loop. In WebGL 1 the loop index cannot be compared with
145
+ // non-constant expression. So we use a hard-coded max, and an additional condition
146
+ // inside the loop.
147
+ for (int iter=0; iter<MAX_STEPS; iter++) {
148
+ if (iter >= nsteps)
149
+ break;
150
+
151
+ // Sample from the 3D texture
152
+ float val = sample1(loc);
153
+
154
+ if (val > low_threshold) {
155
+ // Take the last interval in smaller steps
156
+ vec3 iloc = loc - 0.5 * step;
157
+ vec3 istep = step / float(REFINEMENT_STEPS);
158
+ for (int i=0; i<REFINEMENT_STEPS; i++) {
159
+ val = sample1(iloc);
160
+ if (val > u_renderthreshold) {
161
+ gl_FragColor = add_lighting(val, iloc, dstep, view_ray);
162
+ return;
163
+ }
164
+ iloc += istep;
165
+ }
166
+ }
167
+
168
+ // Advance location deeper into the volume
169
+ loc += step;
170
+ }
171
+ }
172
+
173
+
174
+ vec4 add_lighting(float val, vec3 loc, vec3 step, vec3 view_ray)
175
+ {
176
+ // Calculate color by incorporating lighting
177
+
178
+ // View direction
179
+ vec3 V = normalize(view_ray);
180
+
181
+ // calculate normal vector from gradient
182
+ vec3 N;
183
+ float val1, val2;
184
+ val1 = sample1(loc + vec3(-step[0], 0.0, 0.0));
185
+ val2 = sample1(loc + vec3(+step[0], 0.0, 0.0));
186
+ N[0] = val1 - val2;
187
+ val = max(max(val1, val2), val);
188
+ val1 = sample1(loc + vec3(0.0, -step[1], 0.0));
189
+ val2 = sample1(loc + vec3(0.0, +step[1], 0.0));
190
+ N[1] = val1 - val2;
191
+ val = max(max(val1, val2), val);
192
+ val1 = sample1(loc + vec3(0.0, 0.0, -step[2]));
193
+ val2 = sample1(loc + vec3(0.0, 0.0, +step[2]));
194
+ N[2] = val1 - val2;
195
+ val = max(max(val1, val2), val);
196
+
197
+ float gm = length(N); // gradient magnitude
198
+ N = normalize(N);
199
+
200
+ // Flip normal so it points towards viewer
201
+ float Nselect = float(dot(N, V) > 0.0);
202
+ N = (2.0 * Nselect - 1.0) * N; // == Nselect * N - (1.0-Nselect)*N;
203
+
204
+ // Init colors
205
+ vec4 ambient_color = vec4(0.0, 0.0, 0.0, 0.0);
206
+ vec4 diffuse_color = vec4(0.0, 0.0, 0.0, 0.0);
207
+ vec4 specular_color = vec4(0.0, 0.0, 0.0, 0.0);
208
+
209
+ // note: could allow multiple lights
210
+ for (int i=0; i<1; i++)
211
+ {
212
+ // Get light direction (make sure to prevent zero devision)
213
+ vec3 L = normalize(view_ray); //lightDirs[i];
214
+ float lightEnabled = float( length(L) > 0.0 );
215
+ L = normalize(L + (1.0 - lightEnabled));
216
+
217
+ // Calculate lighting properties
218
+ float lambertTerm = clamp(dot(N, L), 0.0, 1.0);
219
+ vec3 H = normalize(L+V); // Halfway vector
220
+ float specularTerm = pow(max(dot(H, N), 0.0), shininess);
221
+
222
+ // Calculate mask
223
+ float mask1 = lightEnabled;
224
+
225
+ // Calculate colors
226
+ ambient_color += mask1 * ambient_color; // * gl_LightSource[i].ambient;
227
+ diffuse_color += mask1 * lambertTerm;
228
+ specular_color += mask1 * specularTerm * specular_color;
229
+ }
230
+
231
+ // Calculate final color by componing different components
232
+ vec4 final_color;
233
+ vec4 color = apply_colormap(val);
234
+ final_color = color * (ambient_color + diffuse_color) + specular_color;
235
+ final_color.a = color.a;
236
+ return final_color;
237
+ }
238
+ `;
239
+
240
+ const vs =
241
+ `
242
+ varying vec4 v_nearpos;
243
+ varying vec4 v_farpos;
244
+ varying vec3 v_position;
245
+
246
+ void main() {
247
+ // Prepare transforms to map to "camera view". See also:
248
+ // https://threejs.org/docs/#api/renderers/webgl/WebGLProgram
249
+ mat4 viewtransformf = modelViewMatrix;
250
+ mat4 viewtransformi = inverse(modelViewMatrix);
251
+
252
+ // Project local vertex coordinate to camera position. Then do a step
253
+ // backward (in cam coords) to the near clipping plane, and project back. Do
254
+ // the same for the far clipping plane. This gives us all the information we
255
+ // need to calculate the ray and truncate it to the viewing cone.
256
+ vec4 position4 = vec4(position, 1.0);
257
+ vec4 pos_in_cam = viewtransformf * position4;
258
+
259
+ // Intersection of ray and near clipping plane (z = -1 in clip coords)
260
+ pos_in_cam.z = -pos_in_cam.w;
261
+ v_nearpos = viewtransformi * pos_in_cam;
262
+
263
+ // Intersection of ray and far clipping plane (z = +1 in clip coords)
264
+ pos_in_cam.z = pos_in_cam.w;
265
+ v_farpos = viewtransformi * pos_in_cam;
266
+
267
+ // Set varyings and output pos
268
+ v_position = position;
269
+ gl_Position = projectionMatrix * viewMatrix * modelMatrix * position4;
270
+ }
271
+ `;
272
+
273
+ const getUniforms = function() {
274
+ return {
275
+ u_size: { value: new THREE.Vector3( 1, 1, 1 ) },
276
+ u_renderstyle: { value: 0 },
277
+ u_renderthreshold: { value: 0.5 },
278
+ u_clim: { value: new THREE.Vector2( 1, 1 ) },
279
+ u_data: { value: null },
280
+ u_cmdata: { value: null },
281
+ }
282
+ };
283
+
284
+ exports.fs = fs;
285
+ exports.vs = vs;
286
+ exports.glslVersion = glslVersion;
287
+ exports.getUniforms = getUniforms;