Kitware / vtk-js

Visualization Toolkit for the Web
https://kitware.github.io/vtk-js/
BSD 3-Clause "New" or "Revised" License
1.23k stars 371 forks source link

[Bug] Opacity problem on stl file when backface culling is enabled #3069

Open TimDeBauwReLu opened 4 months ago

TimDeBauwReLu commented 4 months ago

Bug description

I have a problem where the opacity of stl objects behaves inconsistently when backface culling is enabled. I have two meshes of a mandible, which both are rendered correctly when opacity=1 (see pictures below). I also checked that the backfaces of both meshes are on the inside.

Screenshot 2024-05-13 at 12 58 00 Screenshot 2024-05-13 at 12 58 11

When changing the opacity to be less than 1, you can see that the color of the bone is different on both objects (see pictures below). When zooming out far, both objects are again rendered correctly. Do you know what can cause this?

Screenshot 2024-05-13 at 12 57 22 Screenshot 2024-05-13 at 12 57 41

Steps to reproduce

const reader = vtkSTLReader.newInstance()
reader.setUrl('Mandible_broken.stl', { binary: true })

const mapper = vtkMapper.newInstance();
mapper.setInputConnection(reader.getOutputPort());

const actor = vtkActor.newInstance();
actor.getProperty().setBackfaceCulling(true);
actor.getProperty().setOpacity(0.5);
actor.setMapper(mapper);

const renderer = fullScreenRenderer.getRenderer();
const renderWindow = fullScreenRenderer.getRenderWindow();
renderer.addActor(actor);
 renderWindow.render();

Detailed Behavior

No response

Expected Behavior

Both objects are rendered correctly when opacity is less than 1 and backface culling is enabled

Environment

finetjul commented 4 months ago

What picture is incorrect ? do you have backface culling enabled on the incorrect picture ?

TimDeBauwReLu commented 4 months ago

Backface culling is enabled on all pictures, the last picture (larger mandible with the full jawbone) is the incorrect one

TimDeBauwReLu commented 4 months ago

I forgot to mention that this problem does not appear when using camera.setParallelProjection(true);

finetjul commented 4 months ago

What's the difference between the 3rd picture and the last picture ? Only the mesh is different ?

TimDeBauwReLu commented 4 months ago

The color of the bone is too dark, and you can see a big contrast between the bone and the teeth sockets (dark red vs bright red). It is supposed to be like this I believe:

Screenshot 2024-05-13 at 14 15 38
finetjul commented 4 months ago

Gotcha.

It would be great if you could attach that STL file to the issue. The shader code should be investigated...

TimDeBauwReLu commented 4 months ago

Here are the files used to reproduce it:

Archive.zip

finetjul commented 4 months ago

You might want to check the magnitude of your normals (maybe it is different between the teeth triangles and the jaw triangles). Alternatively, you might also want to test without diffuse light coefficient, only ambient, and see if the problem is light related. Finally, you might want to consider the OrderIndependentTranslucentPass code...

TimDeBauwReLu commented 4 months ago

@finetjul It looks like it has something to do with the diffuse light coefficient. When I put it to zero, it looks like this.

Screenshot 2024-05-15 at 17 04 18

I guess that depending on the distance from the camera to the object, the intensity of the diffuse component changes? Do you have an idea where to investigate this issue?

finetjul commented 4 months ago

And this blackness does not happen if it is opaque? Do you have ambient lighting?

TimDeBauwReLu commented 4 months ago

Yes, it is caused because I do not have ambient lighting I believe. The blackness also occurs when the mesh is opaque