Repository: napari/napari

Issue:
In 3D, iso-categorical rendering of labels doesn't show labels correctly
## 🐛 Bug

The background of labels (correspond to value 0) should be transparent. In "translucent" rendering, it works fine. But in "iso categorical" rendering, it seems that background is replaced with some opaque colors.
Rendering used to be fine in `0.4.16`.

## To Reproduce

Steps to reproduce the behavior:

1. Run following code
   ```python
   import napari
   import numpy as np
   viewer = napari.Viewer()
   labels = np.zeros((40,40,40), dtype=np.int16)
   labels[10:30, 10:30, 10:30] = 1
   layer = viewer.add_labels(labels)
   layer.color_mode = "direct"
   layer.color = {1: [1,0,0,1]}
   ```

2. View in 3D mode.

<!-- If you have a code sample, error messages, stack traces, please provide it here as well -->

## Expected behavior

Background should be transparent.

## Environment

 - Please copy and paste the information at napari info option in help menubar here:

```
napari: 0.4.17rc4
Platform: Windows-10-10.0.22621-SP0
Python: 3.9.7 | packaged by conda-forge | (default, Sep 29 2021, 19:20:16) [MSC v.1916 64 bit (AMD64)]
Qt: 5.15.2
PyQt5: 5.15.6
NumPy: 1.23.2
SciPy: 1.9.1
Dask: 2022.9.0
VisPy: 0.11.0
magicgui: 0.5.1
superqt: 0.3.2
in-n-out: 0.1.5
app-model: 0.0.9
npe2: 0.6.1

OpenGL:
- GL version: 4.6.0 - Build 27.20.100.9621
- MAX_TEXTURE_SIZE: 16384

Screens:
- screen 1: resolution 1368x912, scale 2.0
- screen 2: resolution 1920x1080, scale 1.0

```

 - Any other relevant information:

## Additional context

<!-- Add any other context about the problem here. -->



Discussion:
Here's what it looks like to me anyways:
Live:
<img width="1214" alt="image" src="https://user-images.githubusercontent.com/76622105/195382098-bee98217-43fe-44c3-b832-9d868f7a46b1.png">
RC4 (same with `main`):
<img width="1214" alt="image" src="https://user-images.githubusercontent.com/76622105/195382465-835d19e0-2d40-4b9b-86c6-8b8e7212d6f7.png">

Git bisect indicates this commit:
https://github.com/napari/napari/commit/d4b28364a2d1cc832b1eba06cc33ab5849514f62
CC: @perlman 

`layer.color_mode = "auto"` also makes the cube visible again.

Edit: this dup issue has a video that more clearly shows the issue:
https://github.com/napari/napari/issues/5218#issue-1409162268
Importantly, the video shows two different labels, **both** disappearing in 3D mode.
Here better video to show the problem using labels that touches the border. Its cover case is linked by @psobolewskiPhD in the previous comment 

https://user-images.githubusercontent.com/3826210/195834147-f855e3c1-f0ac-425a-8859-5e7c225f4473.mp4


The error was introduced in #4935
> The error was introduced in #4935

Yup, I also did a git bisect—what an awesome tool! my favorite git thing I think—posted above 😉 

>Git bisect indicates this commit:
https://github.com/napari/napari/commit/d4b28364a2d1cc832b1eba06cc33ab5849514f62


Resolving patch:
diff --git a/napari/_vispy/visuals/volume.py b/napari/_vispy/visuals/volume.py
--- a/napari/_vispy/visuals/volume.py
+++ b/napari/_vispy/visuals/volume.py
@@ -127,10 +127,10 @@
             vec3 iloc = loc - step;
             for (int i=0; i<10; i++) {
                 color = $get_data(iloc);
-                if (floatNotEqual(color.g, categorical_bg_value) ) {
-                    // when the non-background value is reached
+                color = applyColormap(color.g);
+                if (floatNotEqual(color.a, 0) ) {
+                    // when the value mapped to non-transparent color is reached
                     // calculate the color (apply lighting effects)
-                    color = applyColormap(color.g);
                     color = calculateCategoricalColor(color, iloc, dstep);
                     gl_FragColor = color;
 


Test patch: