You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Plotly绘制含Y=0平面的3D三角形网格不显示问题求助

解决Plotly 3D网格Y=0平面天线部分不渲染的问题

核心原因排查

问题大概率是Plotly的背面剔除机制或单个小Mesh3d对象的渲染优化导致:

  • Plotly Mesh3d 默认仅渲染顶点按逆时针顺序定义的"正面",Y=0平面的三角形若法线方向与视角相反,会被自动剔除。
  • 每个三角形单独创建Mesh3d对象,可能触发渲染引擎的优化逻辑,导致平面部分被忽略。

解决方案

1. 强制渲染正反两面

给所有Mesh3d添加side="both"参数,关闭背面剔除:

import numpy as np
import plotly.graph_objects as go

triangleIn3D = ... # 存储所有RWG三角形坐标的numpy矩阵

dat = []

for coord in range(triangleIn3D.shape[0]):    
    dat.append(go.Mesh3d(
        x=triangleIn3D[coord,:3, 0], 
        y=triangleIn3D[coord,:3, 1], 
        z=triangleIn3D[coord,:3, 2], 
        color='lightpink',
        contour_width=16, 
        contour_color="#101010", 
        contour_show=True,
        side="both"  # 关键参数:强制渲染正反两面
    ))
    dat.append(go.Mesh3d(
        x=triangleIn3D[coord,3:, 0], 
        y=triangleIn3D[coord,3:, 1], 
        z=triangleIn3D[coord,3:, 2], 
        color='lightpink', 
        opacity=0.50,
        side="both"
    ))

fig = go.Figure(data=dat)
fig.show()

2. 合并Mesh3d对象(更稳定)

将所有三角形合并为单个Mesh3d,用顶点索引定义三角形,避免大量小对象的渲染异常:

import numpy as np
import plotly.graph_objects as go

triangleIn3D = ... # 存储所有RWG三角形坐标的numpy矩阵

# 收集所有顶点和三角形索引
vertices = []
tri_indices = []
current_idx = 0

for coord in range(triangleIn3D.shape[0]):
    # 处理第一个三角形
    tri1 = triangleIn3D[coord, :3, :]
    vertices.extend(tri1.tolist())
    tri_indices.extend([current_idx, current_idx+1, current_idx+2])
    current_idx += 3
    
    # 处理第二个三角形
    tri2 = triangleIn3D[coord, 3:, :]
    vertices.extend(tri2.tolist())
    tri_indices.extend([current_idx, current_idx+1, current_idx+2])
    current_idx += 3

# 转换为numpy数组适配Plotly格式
vertices = np.array(vertices)
tri_indices = np.array(tri_indices).reshape(-1, 3)

# 创建单个Mesh3d对象
fig = go.Figure(data=[go.Mesh3d(
    x=vertices[:, 0],
    y=vertices[:, 1],
    z=vertices[:, 2],
    i=tri_indices[:, 0],
    j=tri_indices[:, 1],
    k=tri_indices[:, 2],
    color='lightpink',
    contour_width=16,
    contour_color="#101010",
    contour_show=True,
    opacity=0.50,
    side="both"
)])
fig.show()

3. 调整顶点顺序(备选)

如果上述方法无效,尝试反转Y=0平面三角形的顶点顺序,修正法线方向:

import numpy as np
import plotly.graph_objects as go

triangleIn3D = ... # 存储所有RWG三角形坐标的numpy矩阵

dat = []

for coord in range(triangleIn3D.shape[0]):    
    # 检查第一个三角形是否在Y=0平面
    if np.all(triangleIn3D[coord,:3,1] == 0):
        x1 = triangleIn3D[coord,:3,0][::-1]
        y1 = triangleIn3D[coord,:3,1][::-1]
        z1 = triangleIn3D[coord,:3,2][::-1]
    else:
        x1 = triangleIn3D[coord,:3,0]
        y1 = triangleIn3D[coord,:3,1]
        z1 = triangleIn3D[coord,:3,2]
    
    dat.append(go.Mesh3d(
        x=x1, y=y1, z=z1,
        color='lightpink',contour_width=16, 
        contour_color="#101010", contour_show=True,
        side="both"
    ))
    
    # 检查第二个三角形是否在Y=0平面
    if np.all(triangleIn3D[coord,3:,1] == 0):
        x2 = triangleIn3D[coord,3:,0][::-1]
        y2 = triangleIn3D[coord,3:,1][::-1]
        z2 = triangleIn3D[coord,3:,2][::-1]
    else:
        x2 = triangleIn3D[coord,3:,0]
        y2 = triangleIn3D[coord,3:,1]
        z2 = triangleIn3D[coord,3:,2]
    
    dat.append(go.Mesh3d(
        x=x2, y=y2, z=z2,
        color='lightpink', opacity=0.50,
        side="both"
    ))

fig = go.Figure(data=dat)
fig.show()

内容的提问来源于stack exchange,提问作者Tfloow

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.29 19:16:24