Open3Dでテクスチャマッピング

import open3d as o3d
import numpy as np
import cv2
from copy import deepcopy


def main():

img = cv2.imread('lenna.bmp')
img = deepcopy(img[:, :, ::-1])

vert = [[-1, 1, 0], [-1, -1, 0], [1, 1, 0], [1, -1, 0]]
faces = [[0, 1, 2], [1, 3, 2]]
uvs = np.array([[0, 0], [0, 1], [1, 0], [0, 1], [1, 1], [1, 0]])

obj = o3d.geometry.TriangleMesh(o3d.utility.Vector3dVector(vert),
o3d.utility.Vector3iVector(faces))
obj.triangle_uvs = o3d.utility.Vector2dVector(uvs)
obj.textures = [o3d.geometry.Image(img)]
obj.triangle_material_ids = o3d.utility.IntVector([0] * len(faces))

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


if __name__ == '__main__':
main()

 

n_rows = 6
vertices = []
for y in range(n_rows):
for x in range(n_rows):
vertices.append([x - 2.5, 2.5 - y, 0])

uv_unit = 0.2
faces = []
uvs = []
for y in range(n_rows - 1):
for x in range(n_rows - 1):
faces.extend([[y * 6 + x, (y + 1) * 6 + x, y * 6 + x + 1],
[y * 6 + x + 1, (y + 1) * 6 + x, (y + 1) * 6 + x + 1]])
uvs.extend([[x * uv_unit, y * uv_unit], [x * uv_unit, (y + 1) * uv_unit],
[(x + 1) * uv_unit, y * uv_unit], [(x + 1) * uv_unit, y * uv_unit],
[x * uv_unit, (y + 1) * uv_unit], [(x + 1) * uv_unit, (y + 1) * uv_unit]])