简体   繁体   English

如何在 python Open3d 中向网格添加纹理?

[英]How to add texture to a mesh in python Open3d?

I am working with a triangle meshes with python Open3d and I want to add a texture mapping to my mesh (I didn't find it in the documentation), this is an example code with simple cube mesh:我正在使用带有 python Open3d 的三角形网格,我想向我的网格添加纹理映射(我在文档中没有找到它),这是一个带有简单立方体网格的示例代码:

import numpy as np
import open3d as o3d

vert=[[0,0,0],[0,1,0],[1,1,0],[1,0,0],
   [0,0,1],[0,1,1],[1,1,1],[1,0,1]]

faces=[[0, 1, 2], [0, 2, 3], [6, 5, 4],
 [7, 6, 4], [5, 1, 0], [0, 4, 5], [3, 2, 6],
 [6, 7, 3], [0, 3, 7], [0, 7, 4], [1, 5, 6],
 [1, 6, 2]]

m=o3d.geometry.TriangleMesh(o3d.open3d_pybind.utility.Vector3dVector(vert),
                            o3d.open3d_pybind.utility.Vector3iVector(faces))

m.compute_vertex_normals()
o3d.visualization.draw_geometries([m])

I can see the cube: cube mesh我可以看到立方体:立方体网格

Now I try to add texture:现在我尝试添加纹理:

text=cv2.imread('~/Downloads/cupe_uv.png')
plt.imshow(text)

this is the texture image: texture image of a cube这是纹理图像:立方体的纹理图像

DX,DY=0.5/2,0.66/2
v_uv=[[DX,DY],[DX,2*DY],[2*DX,2*DY],[2*DX,DY],
      [0,DX],[DX,1],[3*DX,2*DY],[3*DX,DY]]

v_uv=np.asarray(v_uv)
v_uv=np.concatenate((v_uv,v_uv,v_uv),axis=0)
m.triangle_uvs = o3d.open3d_pybind.utility.Vector2dVector(v_uv)

m.textures=[o3d.geometry.Image(text)]

o3d.visualization.draw_geometries([m])

I know I didn't set the uv coordinates to display all the colors of the cube (but some colors should be there...).我知道我没有设置 uv 坐标来显示立方体的所有 colors(但应该有一些 colors ......)。 Any way the mesh is still with out texture (same as in the beginning).无论如何,网格仍然没有纹理(与开始时相同)。

mesh.triangle_uvs is an array of shape (3 * num_triangles, 2) , not (3 * num_vertices, 2) . mesh.triangle_uvs是一个形状数组(3 * num_triangles, 2) ,而不是(3 * num_vertices, 2)

Try this:尝试这个:

v_uv = np.random.rand(len(faces) * 3, 2)
m.triangle_uvs = o3d.open3d_pybind.utility.Vector2dVector(v_uv)

By the way, it seems that your Open3D version is quite old.顺便说一句,您的Open3D版本似乎很旧。
Open3d 0.10.0 is already out and a lot of new features added. Open3d 0.10.0已经发布并添加了许多新功能。
You might wanna try the new version:)您可能想尝试新版本:)

like Jing Zhao said in https://stackoverflow.com/a/63005705/15099601就像赵晶在https 中所说的那样://stackoverflow.com/a/63005705/15099601

v_uv = np.random.rand(len(faces) * 3, 2) m.triangle_uvs =
o3d.open3d_pybind.utility.Vector2dVector(v_uv)

will work.将工作。 For Open3d Versions 0.10.0 the material_ids do not seem to get set.对于 Open3d 版本 0.10.0,material_ids 似乎没有设置。

m.triangle_material_ids = o3d.utility.IntVector([0]*len(faces))

fixes that, so that the textured cube can be visualized without crashing修复了这个问题,这样纹理立方体可以在不崩溃的情况下可视化

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM