plotly.py icon indicating copy to clipboard operation
plotly.py copied to clipboard

change default lighting attributes to prevent mesh3d rendering bug

Open robert-lieck opened this issue 8 months ago • 2 comments

Mesh3D seems to have a shading bug, not sure how to describe it more precisely, but the images should help. This is the minimal example produced with the code below:

Image

import plotly.graph_objects as go
import numpy as np

vertices = np.array([[-1.0, -1.0, 0.0], [-1.0, 1.0, 0.0], [1.0, -1.0, 0.0], [1.0, 1.0, 0.0], [-1.0, 0.0, 1.0], [0.0, 0.0, 0.0], [0.0, -1.0, -1.0], [1.0, 0.0, 1.0], [0.0, 1.0, -1.0], [-1.0, -0.5, 0.75], [-0.5, -0.5, 0.0], [-0.5, -1.0, -0.75], [-0.5, 0.5, 0.0], [-0.5, 0.0, 0.25], [-1.0, 0.5, 0.75], [0.5, -1.0, -0.75], [0.0, -0.5, -0.25], [0.5, -0.5, 0.0], [0.0, 0.5, -0.25], [-0.5, 1.0, -0.75], [1.0, -0.5, 0.75], [0.5, 0.0, 0.25], [0.5, 1.0, -0.75], [0.5, 0.5, 0.0], [1.0, 0.5, 0.75]])
faces = np.array([[0, 9, 11], [4, 10, 9], [6, 11, 10], [9, 10, 11], [1, 12, 14], [5, 13, 12], [4, 14, 13], [12, 13, 14], [2, 15, 17], [6, 16, 15], [5, 17, 16], [15, 16, 17], [4, 13, 10], [5, 16, 13], [6, 10, 16], [13, 16, 10], [1, 12, 19], [5, 18, 12], [8, 19, 18], [12, 18, 19], [2, 20, 17], [7, 21, 20], [5, 17, 21], [20, 21, 17], [3, 22, 24], [8, 23, 22], [7, 24, 23], [22, 23, 24], [5, 21, 18], [7, 23, 21], [8, 18, 23], [21, 23, 18]])

x, y, z = vertices.T
i, j, k = faces.T

fig = go.Figure(data=[go.Mesh3d(x=x, y=y, z=z,
                                i=i, j=j, k=k,
                                flatshading=False)])
fig.show()

This is not related to the saddle point per se and it is also not just an interpolation issue, as similar problems occur with flatshading=True on a parabola:

Image

Interestingly, when I tried to get a minimal example with flatshading by isolating the problematic triangles around the centre of the parabola, the problem disappeared. So it seems to be related to some kind of scaling issue (like min/max slope or similar). Not sure whether this is actually plotly.py or rather plotly.js.

Any ideas?

robert-lieck avatar May 13 '25 21:05 robert-lieck

To remove the artifacts insert two lighting attributes in the Mesh3d definition:

fig = go.Figure(go.Mesh3d(x=x, y=y, z=z, i=i, j=j, k=k, flatshading=True, color="lightblue",   
                 lighting=dict(vertexnormalsepsilon=0,
                                      facenormalsepsilon= 0)))

empet avatar May 15 '25 15:05 empet

Excellent, thank you! If a value of zero resolves these problems, I would argue this should be the default.

robert-lieck avatar May 16 '25 08:05 robert-lieck