Python下Open3D实现纹理贴图渲染

利用open3d的python库来实现三维模型的纹理贴图渲染。

渲染原理类似OpenGL,需要指定顶点信息,三角网格顶点的索引,以及UV贴图坐标。

需要提前安装open3d库:

pip install open3d

实现代码如下:

import os
import cv2
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 = [[3, 0, 1], [1, 2, 3],
         [1, 5, 6], [6, 2, 1], 
         [5, 4, 7], [7, 6, 5], 
         [4, 0, 3], [3, 7, 4], 
         [6, 7, 3], [3, 2, 6], 
         [1, 0, 4], [4, 5, 1]]

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

m.compute_vertex_normals()

text=cv2.imread('./cupe_uv.png')

DX,DY=0.5/2,0.66/2 # UV坐标

one = [[3*DX,1*DY], [3*DX,2*DY], [4*DX,2*DY], [4*DX,2*DY], [4*DX,1*DY], [3*DX,1*DY]]
two = [[2*DX,1*DY], [2*DX,2*DY], [3*DX,2*DY], [3*DX,2*DY], [3*DX,1*DY], [2*DX,1*DY]]
three=[[1*DX,1*DY], [1*DX,2*DY], [2*DX,2*DY], [2*DX,2*DY], [2*DX,1*DY], [1*DX,1*DY]]
four= [[0*DX,1*DY], [0*DX,2*DY], [1*DX,2*DY], [1*DX,2*DY], [1*DX,1*DY], [0*DX,1*DY]]
five= [[1*DX,0*DY], [1*DX,1*DY], [2*DX,1*DY], [2*DX,1*DY], [2*DX,0*DY], [1*DX,0*DY]]
six = [[1*DX,2*DY], [1*DX,3*DY], [2*DX,3*DY], [2*DX,3*DY], [2*DX,2*DY], [1*DX,2*DY]]

v_uv=np.concatenate((one,two,three,four,five,six),axis=0)
print(v_uv.shape) # (48, 2)

m.triangle_uvs = o3d.open3d.utility.Vector2dVector(v_uv)
m.triangle_material_ids = o3d.utility.IntVector([0]*len(faces))
m.textures = [o3d.geometry.Image(text)]

o3d.visualization.draw_geometries([m])

 代码中使用到的纹理贴图cupe_uv.png下载网址:https://i.stack.imgur.com/QwIKM.png。

纹理贴图:

Python下Open3D实现纹理贴图渲染_第1张图片

 

渲染效果是一个三维的骰子,拖动鼠标可以手动旋转:

Python下Open3D实现纹理贴图渲染_第2张图片

参考:How to add texture to a mesh in python Open3d? - Stack Overflow

你可能感兴趣的:(python,python,open3d,纹理贴图)