Open3Dでmeshにテクスチャをはる
- meshを作成する(読み込む)
- テクスチャの画像ファイルを読み込む
- マッピングを設定する
です。
import open3d as o3d
import numpy as np
# 適当にメッシュを作成する! 別に read_triangle_mesh で読み込んでもいいです
mesh = o3d.geometry.TriangleMesh()
mesh.vertices = o3d.utility.Vector3dVector([
[0, 0, 0], [1, 0, 0], [1, 1, 0], [0, 1, 0],
[0, 0, 1], [1, 0, 1], [1, 1, 1], [0, 1, 1]
])
mesh.triangles = o3d.utility.Vector3iVector([
[0, 1, 2], [2, 3, 0], [4, 5, 6], [6, 7, 4], [0, 1, 5], [5, 4, 0],
[1, 2, 6], [6, 5, 1], [2, 3, 7], [7, 6, 2], [3, 0, 4], [4, 7, 3]
])
mesh.compute_vertex_normals()
##########################################
# テクスチャファイルを読み込む
texture = o3d.io.read_image("texture.png")
mesh.textures = [texture]
# マッピングを設定する
triangle_uvs = np.random.rand(len(mesh.triangles) * 3, 2)
mesh.triangle_uvs = o3d.utility.Vector2dVector(triangle_uvs)
mesh.triangle_material_ids = o3d.utility.IntVector([0]*len(mesh.triangles))
##########################################
# 描画する
o3d.visualization.draw_geometries([mesh])
使った画像はこういう感じ。
実行するとこう。
ぜんぜんマッピングの位置合わせなどはしていないので適当に貼られていますが、、、
とりあえず無事にはることが出来ます。