我正在尝试将两个网格合并到一个 3D 模型中。一种网格是墙壁,另一种是地面。我想要墙壁下方的地面,但当我尝试合并它们时,地面总是在墙壁上方
任何人都可以帮我将地面网格(mesh1)定位在墙壁网格(mesh)下方
from PIL import Image
import meshlib.mrmeshpy as mr
# Load and prepare raster images
Image.open('walls_mask.png').convert('RGBA').save('walls_mask.png')
Image.open('ground.png').convert('RGBA').save('ground.png')
# Load distance maps from images
dm = mr.loadDistanceMapFromImage("walls_mask.png", 0)
dm1 = mr.loadDistanceMapFromImage("ground.png", 0)
# Find the boundary contours between black and white
polyline2 = mr.distanceMapTo2DIsoPolyline(dm, isoValue=127)
polyline21 = mr.distanceMapTo2DIsoPolyline(dm1, isoValue=127)
# Triangulate the contours
mesh = mr.triangulateContours(polyline2.contours2(mr.HolesVertIds()))
mesh1 = mr.triangulateContours(polyline21.contours2(mr.HolesVertIds()))
# Subdivide the meshes to add detail
mr.subdivideMesh(mesh)
mr.subdivideMesh(mesh1)
# Add bases to the planar meshes
mr.addBaseToPlanarMesh(mesh, zOffset=150)
mr.addBaseToPlanarMesh(mesh1, zOffset=10)
# Merge the two meshes
merged_mesh = mr.mergeMeshes([mesh1,mesh])
# Save the merged mesh as an OBJ or STL file
mr.saveMesh(merged_mesh, mr.Path("Merged_mesh.stl"))
发生这种情况是因为
addBaseToPlanarMesh
实际上在 -Z 方向上添加了基数(独立于 zOffset
符号),因此您将 ground
向下挤压到 10,将 walls
向下挤压到 150。
要修复此问题,您可以在合并之前变换网格:
mesh.transform(mr.AffineXf3f.translation(mr.Vector3f(0,0,150))) # shift walls 150 up
mesh1.transform(mr.AffineXf3f.translation(mr.Vector3f(0,0,10))) # shift ground 10 up
附注看起来 meshlib 文档中有一个错误:
// given a planar mesh with boundary on input located in plane XY, packs and extends it along Z on zOffset to make a volumetric closed mesh
// zOffset should be > 0
MRSYMBOLMESH_API void addBaseToPlanarMesh( Mesh& mesh, float zOffset = 1.0f );
但实际上它总是沿着-Z轴延伸。
很快就会修复。