@niivue/niivue 0.24.2 → 0.26.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.
@@ -130,12 +130,13 @@ void main() {
130
130
  while (samplePos.a <= len) {
131
131
  vec4 colorSample = texture(volume, samplePos.xyz);
132
132
  samplePos += deltaDir; //advance ray position
133
- if (colorSample.a < 0.01) continue;
134
- if (firstHit.a > lenNoClip)
135
- firstHit = samplePos;
136
- backNearest = min(backNearest, samplePos.a);
137
- if (colorSample.a > colAcc.a) //ties generate errors for TT_desai_dd_mpm
138
- colAcc = vec4(colorSample.rgb, colorSample.a+0.00001);
133
+ if (colorSample.a >= 0.01) {
134
+ if (firstHit.a > lenNoClip)
135
+ firstHit = samplePos;
136
+ backNearest = min(backNearest, samplePos.a);
137
+ if (colorSample.a > colAcc.a) //ties generate errors for TT_desai_dd_mpm
138
+ colAcc = vec4(colorSample.rgb, colorSample.a+0.00001);
139
+ }
139
140
  }
140
141
  if (firstHit.a < len)
141
142
  gl_FragDepth = frac2ndc(firstHit.xyz);
@@ -171,15 +172,16 @@ void main() {
171
172
  while (samplePos.a <= len) {
172
173
  vec4 colorSample = texture(overlay, samplePos.xyz);
173
174
  samplePos += deltaDir; //advance ray position
174
- if (colorSample.a < 0.01) continue;
175
- if (overFirstHit.a > len)
176
- overFirstHit = samplePos;
177
- colorSample.a = 1.0-pow((1.0 - colorSample.a), opacityCorrection);
178
- colorSample.rgb *= colorSample.a;
179
- colAcc= (1.0 - colAcc.a) * colorSample + colAcc;
180
- overFarthest = samplePos.a;
181
- if ( colAcc.a > earlyTermination )
182
- break;
175
+ if (colorSample.a >= 0.01) {
176
+ if (overFirstHit.a > len)
177
+ overFirstHit = samplePos;
178
+ colorSample.a = 1.0-pow((1.0 - colorSample.a), opacityCorrection);
179
+ colorSample.rgb *= colorSample.a;
180
+ colAcc= (1.0 - colAcc.a) * colorSample + colAcc;
181
+ overFarthest = samplePos.a;
182
+ if ( colAcc.a > earlyTermination )
183
+ break;
184
+ }
183
185
  }
184
186
  if (overFirstHit.a < firstHit.a)
185
187
  //if (overFirstHit.a < len)
@@ -231,7 +233,19 @@ float frac2ndc(vec3 frac) {
231
233
 
232
234
  }
233
235
  void main() {
236
+ if (fColor.x > 2.0) {
237
+ fColor = vec4(1.0, 0.0, 0.0, 0.5);
238
+ return;
239
+ }
234
240
  fColor = vec4(0.0,0.0,0.0,0.0);
241
+ vec4 clipPlaneColorX = clipPlaneColor;
242
+ //if (clipPlaneColor.a < 0.0)
243
+ // clipPlaneColorX.a = - 1.0;
244
+ bool isColorPlaneInVolume = false;
245
+ if (clipPlaneColorX.a < 0.0) {
246
+ isColorPlaneInVolume = true;
247
+ clipPlaneColorX.a = 0.0;
248
+ }
235
249
  //fColor = vec4(vColor.rgb, 1.0); return;
236
250
  vec3 start = vColor;
237
251
  gl_FragDepth = 0.0;
@@ -258,19 +272,23 @@ void main() {
258
272
  vec4 deltaDirFast = vec4(dir.xyz * stepSizeFast, stepSizeFast);
259
273
  while (samplePos.a <= len) {
260
274
  float val = texture(volume, samplePos.xyz).a;
261
- if (val > 0.01) break;
275
+ if (val > 0.01) {
276
+ break;
277
+ }
262
278
  samplePos += deltaDirFast; //advance ray position
263
279
  }
264
280
  if ((samplePos.a >= len) && ((overlays < 1.0) || (backgroundMasksOverlays > 0))) {
265
- if (isClip)
266
- fColor += clipPlaneColor;
281
+ if (isClip){
282
+ fColor += clipPlaneColorX;
283
+ }
267
284
  return;
268
285
  }
269
286
  fColor = vec4(1.0, 1.0, 1.0, 1.0);
270
287
  //gl_FragDepth = frac2ndc(samplePos.xyz); //crude due to fast pass resolution
271
288
  samplePos -= deltaDirFast;
272
- if (samplePos.a < 0.0)
289
+ if (samplePos.a < 0.0){
273
290
  vec4 samplePos = vec4(start.xyz, 0.0); //ray position
291
+ }
274
292
  //end: fast pass
275
293
  vec4 colAcc = vec4(0.0,0.0,0.0,0.0);
276
294
  vec4 firstHit = vec4(0.0,0.0,0.0,2.0 * lenNoClip);
@@ -281,23 +299,29 @@ void main() {
281
299
  while (samplePos.a <= len) {
282
300
  vec4 colorSample = texture(volume, samplePos.xyz);
283
301
  samplePos += deltaDir; //advance ray position
284
- if (colorSample.a < 0.01) continue;
285
- if (firstHit.a > lenNoClip)
286
- firstHit = samplePos;
287
- backNearest = min(backNearest, samplePos.a);
288
- colorSample.a = 1.0-pow((1.0 - colorSample.a), opacityCorrection);
289
- colorSample.rgb *= colorSample.a;
290
- colAcc= (1.0 - colAcc.a) * colorSample + colAcc;
291
- if ( colAcc.a > earlyTermination )
292
- break;
302
+ if (colorSample.a >= 0.01) {
303
+ if (firstHit.a > lenNoClip)
304
+ firstHit = samplePos;
305
+ backNearest = min(backNearest, samplePos.a);
306
+ colorSample.a = 1.0-pow((1.0 - colorSample.a), opacityCorrection);
307
+ colorSample.rgb *= colorSample.a;
308
+ colAcc= (1.0 - colAcc.a) * colorSample + colAcc;
309
+ if ( colAcc.a > earlyTermination )
310
+ break;
311
+ }
293
312
  }
294
- if (firstHit.a < len)
313
+ if (firstHit.a < len){
295
314
  gl_FragDepth = frac2ndc(firstHit.xyz);
315
+ }
296
316
  colAcc.a = (colAcc.a / earlyTermination) * backOpacity;
297
317
  fColor = colAcc;
298
- if (isClip) //CR
299
- fColor.rgb = mix(fColor.rgb, clipPlaneColor.rgb, clipPlaneColor.a * 0.15);
300
- if (overlays < 1.0) return;
318
+ //if (isClip) //CR
319
+ if ((isColorPlaneInVolume) && (clipPos.a != samplePos.a) && (abs(firstHit.a - clipPos.a) < deltaDir.a))
320
+ fColor.rgb = mix(fColor.rgb, clipPlaneColorX.rgb, abs(clipPlaneColor.a));
321
+ //fColor.rgb = mix(fColor.rgb, clipPlaneColorX.rgb, clipPlaneColorX.a * 0.65);
322
+ if (overlays < 1.0) {
323
+ return;
324
+ }
301
325
  //overlay pass
302
326
  len = lenNoClip;
303
327
  samplePos = vec4(start.xyz, 0.0); //ray position
@@ -306,48 +330,56 @@ void main() {
306
330
  deltaDirFast = vec4(dir.xyz * stepSizeFast, stepSizeFast);
307
331
  while (samplePos.a <= len) {
308
332
  float val = texture(overlay, samplePos.xyz).a;
309
- if (val > 0.01) break;
333
+ if (val > 0.01) {
334
+ break;
335
+ }
310
336
  samplePos += deltaDirFast; //advance ray position
311
337
  }
312
338
  if (samplePos.a >= len) {
313
- if (isClip && (fColor.a == 0.0))
314
- fColor += clipPlaneColor;
339
+ if (isClip && (fColor.a == 0.0)) {
340
+ fColor += clipPlaneColorX;
341
+ }
315
342
  return;
316
343
  }
317
344
  samplePos -= deltaDirFast;
318
- if (samplePos.a < 0.0)
345
+ if (samplePos.a < 0.0){
319
346
  vec4 samplePos = vec4(start.xyz, 0.0); //ray position
347
+ }
320
348
  //end: fast pass
321
349
  float overFarthest = len;
322
350
  colAcc = vec4(0.0, 0.0, 0.0, 0.0);
323
351
  samplePos += deltaDir * ran; //jitter ray
324
352
  vec4 overFirstHit = vec4(0.0,0.0,0.0,2.0 * len);
325
- if (backgroundMasksOverlays > 0)
353
+ if (backgroundMasksOverlays > 0){
326
354
  samplePos = firstHit;
355
+ }
327
356
  while (samplePos.a <= len) {
328
357
  vec4 colorSample = texture(overlay, samplePos.xyz);
329
358
  samplePos += deltaDir; //advance ray position
330
- if (colorSample.a < 0.01) continue;
331
- if (overFirstHit.a > len)
332
- overFirstHit = samplePos;
333
- colorSample.a = 1.0-pow((1.0 - colorSample.a), opacityCorrection);
334
- colorSample.rgb *= colorSample.a;
335
- colAcc= (1.0 - colAcc.a) * colorSample + colAcc;
336
- overFarthest = samplePos.a;
337
- if ( colAcc.a > earlyTermination )
338
- break;
359
+ if (colorSample.a >= 0.01) {
360
+ if (overFirstHit.a > len)
361
+ overFirstHit = samplePos;
362
+ colorSample.a = 1.0-pow((1.0 - colorSample.a), opacityCorrection);
363
+ colorSample.rgb *= colorSample.a;
364
+ colAcc= (1.0 - colAcc.a) * colorSample + colAcc;
365
+ overFarthest = samplePos.a;
366
+ if ( colAcc.a > earlyTermination )
367
+ break;
368
+ }
339
369
  }
340
370
  if (samplePos.a >= len) {
341
- if (isClip && (fColor.a == 0.0))
342
- fColor += clipPlaneColor;
371
+ if (isClip && (fColor.a == 0.0)){
372
+ fColor += clipPlaneColorX;
373
+ }
343
374
  return;
344
375
  }
345
376
  //if (overFirstHit.a < len)
346
- gl_FragDepth = frac2ndc(overFirstHit.xyz);
377
+ gl_FragDepth = frac2ndc(overFirstHit.xyz);
347
378
  float overMix = colAcc.a;
348
379
  float overlayDepth = 0.3;
349
- if (fColor.a <= 0.0)
380
+ if (fColor.a <= 0.0){
350
381
  overMix = 1.0;
382
+ }
351
383
  else if (((overFarthest) > backNearest)) {
352
384
  float dx = (overFarthest - backNearest)/1.73;
353
385
  dx = fColor.a * pow(dx, overlayDepth);
@@ -363,15 +395,22 @@ layout(location=0) in vec3 pos;
363
395
  uniform int axCorSag;
364
396
  uniform float slice;
365
397
  uniform vec2 canvasWidthHeight;
398
+ uniform vec3 panXYscale;
366
399
  uniform vec4 leftTopWidthHeight;
367
400
  out vec3 texPos;
368
401
  void main(void) {
369
402
  //convert pixel x,y space 1..canvasWidth,1..canvasHeight to WebGL 1..-1,-1..1
370
403
  vec2 frac;
371
- frac.x = (leftTopWidthHeight.x + (pos.x * leftTopWidthHeight.z)) / canvasWidthHeight.x; //0..1
372
- frac.y = 1.0 - ((leftTopWidthHeight.y + ((1.0 - pos.y) * leftTopWidthHeight.w)) / canvasWidthHeight.y); //1..0
404
+ vec3 vpos = pos;
405
+ frac.x = (leftTopWidthHeight.x + (vpos.x * leftTopWidthHeight.z)) / canvasWidthHeight.x; //0..1
406
+ frac.y = 1.0 - ((leftTopWidthHeight.y + ((1.0 - vpos.y) * leftTopWidthHeight.w)) / canvasWidthHeight.y); //1..0
407
+ //frac.x = pos.x; //0..1
408
+ //frac.y = pos.y; //1..0
409
+
373
410
  frac = (frac * 2.0) - 1.0;
374
411
  gl_Position = vec4(frac, 0.0, 1.0);
412
+ gl_Position.x += panXYscale.x;
413
+ gl_Position.y += panXYscale.y;
375
414
  if (axCorSag == 1)
376
415
  texPos = vec3(pos.x, slice, pos.y);
377
416
  else if (axCorSag == 2)
@@ -422,7 +461,70 @@ void main() {
422
461
  color.a = aout;
423
462
  }`;
424
463
 
425
- export var fragLineShader = `#version 300 es
464
+ export var vertSliceMMShader = `#version 300 es
465
+ #line 4
466
+ layout(location=0) in vec3 pos;
467
+ uniform int axCorSag;
468
+ uniform mat4 mvpMtx;
469
+ uniform mat4 frac2mm;
470
+ uniform float slice;
471
+ out vec3 texPos;
472
+ void main(void) {
473
+ texPos = vec3(pos.x, pos.y, slice);
474
+ if (axCorSag > 1)
475
+ texPos = vec3(slice, pos.x, pos.y);
476
+ else if (axCorSag > 0)
477
+ texPos = vec3(pos.x, slice, pos.y);
478
+ vec4 mm = frac2mm * vec4(texPos, 1.0);
479
+ //vec4 mm = vec4(texPos, 1.0) * frac2mm;
480
+ gl_Position = mvpMtx * mm;
481
+ //gl_Position = mm;
482
+ }`;
483
+
484
+ export var fragSliceMMShader = `#version 300 es
485
+ #line 228
486
+ precision highp int;
487
+ precision highp float;
488
+ uniform highp sampler3D volume, overlay;
489
+ uniform int backgroundMasksOverlays;
490
+ uniform float overlays;
491
+ uniform float opacity;
492
+ uniform float drawOpacity;
493
+ uniform highp sampler3D drawing;
494
+ in vec3 texPos;
495
+ out vec4 color;
496
+ void main() {
497
+ //color = vec4(1.0, 0.0, 1.0, 1.0);return;
498
+ vec4 background = texture(volume, texPos);
499
+ color = vec4(background.rgb, opacity);
500
+ vec4 ocolor = vec4(0.0);
501
+ if (overlays > 0.0) {
502
+ ocolor = texture(overlay, texPos);
503
+ }
504
+ float draw = texture(drawing, texPos).r;
505
+ if (draw > 0.0) {
506
+ vec3 dcolor = vec3(0.0, 0.0, 0.0);
507
+ if (draw >= (4.0/255.0))
508
+ dcolor.rgb = vec3(draw,0.0,draw);
509
+ else if (draw >= (3.0/255.0))
510
+ dcolor.b = 1.0;
511
+ else if (draw >= (2.0/255.0))
512
+ dcolor.g = 1.0;
513
+ else
514
+ dcolor.r = 1.0;
515
+ color.rgb = mix(color.rgb, dcolor, drawOpacity);
516
+ color.a = max(drawOpacity, color.a);
517
+ }
518
+ if ((backgroundMasksOverlays > 0) && (background.a == 0.0))
519
+ return;
520
+ //float aout = ocolor.a + (1.0 - ocolor.a) * color.a;
521
+ //if (aout <= 0.0) return;
522
+ //color.rgb = ((ocolor.rgb * ocolor.a) + (color.rgb * color.a * (1.0 - ocolor.a))) / aout;
523
+ color.rgb = mix(color.rgb, ocolor.rgb, ocolor.a);
524
+ color.a = 1.0;
525
+ }`;
526
+
527
+ export var fragRectShader = `#version 300 es
426
528
  #line 189
427
529
  precision highp int;
428
530
  precision highp float;
@@ -453,10 +555,13 @@ export var fragColorbarShader = `#version 300 es
453
555
  precision highp int;
454
556
  precision highp float;
455
557
  uniform highp sampler2D colormap;
558
+ uniform float layer;
456
559
  in vec2 vColor;
457
560
  out vec4 color;
458
561
  void main() {
459
- color = vec4(texture(colormap, vColor).rgb, 1.0);
562
+ float nlayer = float(textureSize(colormap, 0).y);
563
+ float fmap = (0.5 + layer) / nlayer;
564
+ color = vec4(texture(colormap, vec2(vColor.x, fmap)).rgb, 1.0);
460
565
  }`;
461
566
 
462
567
  export var vertGraphShader = `#version 300 es
@@ -499,7 +604,7 @@ void main(void) {
499
604
  gl_Position = vec4(frac, 0.0, 1.0);
500
605
  }`;
501
606
 
502
- export var vertLineShader = `#version 300 es
607
+ export var vertRectShader = `#version 300 es
503
608
  #line 229
504
609
  layout(location=0) in vec3 pos;
505
610
  uniform vec2 canvasWidthHeight;
@@ -513,6 +618,21 @@ void main(void) {
513
618
  gl_Position = vec4(frac, 0.0, 1.0);
514
619
  }`;
515
620
 
621
+ export var vertLineShader = `#version 300 es
622
+ #line 229
623
+ layout(location=0) in vec3 pos;
624
+ uniform vec2 canvasWidthHeight;
625
+ uniform float thickness;
626
+ uniform vec4 startXYendXY;
627
+ void main(void) {
628
+ vec2 posXY = mix(startXYendXY.xy, startXYendXY.zw, pos.x);
629
+ vec2 dir = normalize(startXYendXY.xy - startXYendXY.zw);
630
+ posXY += vec2(-dir.y, dir.x) * thickness * (pos.y - 0.5);
631
+ posXY.x = (posXY.x) / canvasWidthHeight.x; //0..1
632
+ posXY.y = 1.0 - (posXY.y / canvasWidthHeight.y); //1..0
633
+ gl_Position = vec4((posXY * 2.0) - 1.0, 0.0, 1.0);
634
+ }`;
635
+
516
636
  export var vertBmpShader = `#version 300 es
517
637
  #line 229
518
638
  layout(location=0) in vec3 pos;
@@ -608,7 +728,7 @@ in vec2 TexCoord;
608
728
  out vec4 FragColor;
609
729
  uniform float coordZ;
610
730
  uniform float layer;
611
- uniform float numLayers;
731
+ //uniform float numLayers;
612
732
  uniform highp sampler2D colormap;
613
733
  uniform lowp sampler3D blend3D;
614
734
  uniform float opacity;
@@ -638,7 +758,9 @@ void main(void) {
638
758
  }
639
759
  idx = ((idx - uint(1)) % uint(100))+uint(1);
640
760
  float fx = (float(idx)+0.5) / 256.0;
641
- float y = (2.0 * layer + 1.0)/(2.0 * numLayers);
761
+ float nlayer = float(textureSize(colormap, 0).y) * 0.5; //0.5 as both each layer has positive and negative color slot
762
+ float y = (2.0 * layer + 1.0)/(4.0 * nlayer);
763
+ //float y = (2.0 * layer + 1.0)/(4.0 * numLayers);
642
764
  FragColor = texture(colormap, vec2(fx, y)).rgba;
643
765
  FragColor.a *= opacity;
644
766
  if (layer < 2.0) return;
@@ -658,28 +780,43 @@ in vec2 TexCoord;
658
780
  out vec4 FragColor;
659
781
  uniform float coordZ;
660
782
  uniform float layer;
661
- uniform float numLayers;
783
+ //uniform float numLayers;
662
784
  uniform float scl_slope;
663
785
  uniform float scl_inter;
664
786
  uniform float cal_max;
665
787
  uniform float cal_min;
666
788
  uniform highp sampler2D colormap;
667
789
  uniform lowp sampler3D blend3D;
790
+ uniform int modulation;
791
+ uniform highp sampler3D modulationVol;
668
792
  uniform float opacity;
669
793
  uniform mat4 mtx;
670
794
  void main(void) {
671
795
  vec4 vx = vec4(TexCoord.xy, coordZ, 1.0) * mtx;
672
796
  float f = (scl_slope * float(texture(intensityVol, vx.xyz).r)) + scl_inter;
797
+ bool isNegative = (f < 0.0);
673
798
  float r = max(0.00001, abs(cal_max - cal_min));
674
799
  float mn = min(cal_min, cal_max);
675
- f = mix(0.0, 1.0, (f - mn) / r);
676
- //float y = 1.0 / numLayers;
677
- //y = ((layer + 0.5) * y);
800
+ float txl = mix(0.0, 1.0, (f - mn) / r);
678
801
  //https://stackoverflow.com/questions/5879403/opengl-texture-coordinates-in-pixel-space
679
- float y = (2.0 * layer + 1.0)/(2.0 * numLayers);
680
- FragColor = texture(colormap, vec2(f, y)).rgba;
802
+ float nlayer = float(textureSize(colormap, 0).y) * 0.5; //0.5 as both each layer has positive and negative color slot
803
+ float y = (2.0 * layer + 1.0)/(4.0 * nlayer);
804
+ FragColor = texture(colormap, vec2(txl, y)).rgba;
805
+ if (isNegative) {
806
+ y = (2.0 * layer + nlayer + nlayer + 1.0)/(4.0 * nlayer);
807
+ //select texels at positions 0 and 1 of lookup table:
808
+ vec4 v0 = texture(colormap, vec2(0.5/256.0, y));
809
+ vec4 v1 = texture(colormap, vec2(1.5/256.0, y));
810
+ txl = mix(0.0, 1.0, (- f - mn) / r);
811
+ //detect bogus color: negative color slot not used than
812
+ // v0 = 1,1,1,0 and v1 = 0,0,0,1
813
+ if ((v0.r != 1.0) || (v0.a != 0.0) || (v1.r != 0.0) || (v1.a != 1.0))
814
+ FragColor = texture(colormap, vec2(txl, y));
815
+ }
681
816
  if (layer > 0.7)
682
817
  FragColor.a = step(0.00001, FragColor.a);
818
+ if (modulation > 0)
819
+ FragColor.rgb *= texture(modulationVol, vx.xyz).r;
683
820
  FragColor.a *= opacity;
684
821
  if (layer < 1.0) return;
685
822
  vec2 texXY = TexCoord.xy*0.5 +vec2(0.5,0.5);
@@ -698,7 +835,7 @@ in vec2 TexCoord;
698
835
  out vec4 FragColor;
699
836
  uniform float coordZ;
700
837
  uniform float layer;
701
- uniform float numLayers;
838
+ //uniform float numLayers;
702
839
  uniform float scl_slope;
703
840
  uniform float scl_inter;
704
841
  uniform float cal_max;
@@ -708,10 +845,14 @@ uniform lowp sampler3D blend3D;
708
845
  uniform float opacity;
709
846
  uniform mat4 mtx;
710
847
  uniform bool hasAlpha;
848
+ uniform int modulation;
849
+ uniform highp sampler3D modulationVol;
711
850
  void main(void) {
712
851
  vec4 vx = vec4(TexCoord.xy, coordZ, 1.0) * mtx;
713
852
  uvec4 aColor = texture(intensityVol, vx.xyz);
714
853
  FragColor = vec4(float(aColor.r) / 255.0, float(aColor.g) / 255.0, float(aColor.b) / 255.0, float(aColor.a) / 255.0);
854
+ if (modulation > 0)
855
+ FragColor.rgb *= texture(modulationVol, vx.xyz).r;
715
856
  if (!hasAlpha)
716
857
  FragColor.a = (FragColor.r * 0.21 + FragColor.g * 0.72 + FragColor.b * 0.07);
717
858
  FragColor.a *= opacity;
@@ -760,15 +901,13 @@ export var fragGrowCutShader = `#version 300 es
760
901
  layout(location = 0) out int label;
761
902
  layout(location = 1) out int strength;
762
903
  in vec2 TexCoord;
763
- //out vec4 FragColor;
904
+ uniform int finalPass;
764
905
  uniform float coordZ;
765
906
  uniform lowp sampler3D in3D;
766
907
  uniform highp isampler3D inputTexture0; // background
767
908
  uniform highp isampler3D inputTexture1; // label
768
909
  uniform highp isampler3D inputTexture2; // strength
769
910
  void main(void) {
770
- //int MAX_STRENGTH = int(255);
771
- int MAX_STRENGTH = int(10000);
772
911
  vec3 interpolatedTextureCoordinate = vec3(TexCoord.xy, coordZ);
773
912
  ivec3 size = textureSize(inputTexture0, 0);
774
913
  ivec3 texelIndex = ivec3(floor(interpolatedTextureCoordinate * vec3(size)));
@@ -792,6 +931,29 @@ void main(void) {
792
931
  }
793
932
  }
794
933
  }
934
+ if (finalPass < 1)
935
+ return;
936
+ int ok = 1;
937
+ ivec4 labelCount = ivec4(0,0,0,0);
938
+ for (int k = -1; k <= 1; k++)
939
+ for (int j = -1; j <= 1; j++)
940
+ for (int i = -1; i <= 1; i++) {
941
+ ivec3 neighborIndex = texelIndex + ivec3(i,j,k);
942
+ int ilabel = texelFetch(inputTexture1, neighborIndex, 0).r;
943
+ if ((ilabel < 0) || (ilabel > 3))
944
+ ok = 0;
945
+ else
946
+ labelCount[ilabel]++;
947
+ }
948
+ if (ok != 1) {
949
+ return;
950
+ }
951
+ int maxIdx = 0;
952
+ for (int i = 1; i < 4; i++) {
953
+ if (labelCount[i] > labelCount[maxIdx])
954
+ maxIdx = i;
955
+ }
956
+ label = maxIdx;
795
957
  }`; //inputTexture0
796
958
 
797
959
  export var vertSurfaceShader = `#version 300 es
@@ -849,6 +1011,25 @@ void main(void) {
849
1011
  vClr = clr;
850
1012
  }`;
851
1013
 
1014
+ //report depth for fragment
1015
+ // https://github.com/rii-mango/Papaya/blob/782a19341af77a510d674c777b6da46afb8c65f1/src/js/viewer/screensurface.js#L89
1016
+ export var fragMeshDepthShader = `#version 300 es
1017
+ precision highp int;
1018
+ precision highp float;
1019
+ uniform float opacity;
1020
+ out vec4 color;
1021
+ vec4 packFloatToVec4i(const float value) {
1022
+ //this Papaya function uses BSD 3-clause license Copyright (c) 2012-2015, RII-UTHSCSA
1023
+ const vec4 bitSh = vec4(256.0*256.0*256.0, 256.0*256.0, 256.0, 1.0);
1024
+ const vec4 bitMsk = vec4(0.0, 1.0/256.0, 1.0/256.0, 1.0/256.0);
1025
+ vec4 res = fract(value * bitSh);
1026
+ res -= res.xxyz * bitMsk;
1027
+ return res;
1028
+ }
1029
+ void main() {
1030
+ color = packFloatToVec4i(gl_FragCoord.z);
1031
+ }`;
1032
+
852
1033
  //ToonShader https://prideout.net/blog/old/blog/index.html@tag=toon-shader.html
853
1034
  export var fragMeshToonShader = `#version 300 es
854
1035
  precision highp int;