125 lines
3.8 KiB
Python
125 lines
3.8 KiB
Python
import open3d as o3d
|
|
from open3d.visualization.gui import Application
|
|
from open3d.visualization import O3DVisualizer
|
|
import sys, os
|
|
|
|
|
|
# 假设 obj_file_path 是你的 OBJ 文件路径,image_save_path 是保存图片的路径
|
|
filePath = "G://obj_fix/print"
|
|
pid = "8160"
|
|
obj_file_path = f"{filePath}/{pid}/{pid}.obj"
|
|
image_save_path = f"{filePath}/{pid}/{pid}_test.png"
|
|
|
|
|
|
|
|
# import open3d as o3d
|
|
|
|
# 加载OBJ文件
|
|
mesh = o3d.io.read_triangle_mesh(obj_file_path)
|
|
|
|
# 创建一个OffscreenRenderer对象
|
|
renderer = o3d.visualization.rendering.OffscreenRenderer()
|
|
|
|
# 创建一个Open3DScene对象
|
|
scene = o3d.visualization.rendering.Open3DScene()
|
|
|
|
# 将几何体添加到场景中
|
|
scene.add_geometry(mesh)
|
|
|
|
# 创建一个材质并应用到几何体上
|
|
material = o3d.visualization.rendering.MaterialRecord()
|
|
material.base_color = [1.0, 1.0, 1.0] # 白色材质
|
|
mesh.materials.append(material)
|
|
|
|
# 创建一个灯光对象
|
|
light = o3d.visualization.rendering.Light()
|
|
light.type = "directional" # 方向光
|
|
light.direction = [0.0, -1.0, 0.0] # 光照方向
|
|
light.color = [1.0, 1.0, 1.0] # 白色光线
|
|
|
|
# 将灯光添加到场景中
|
|
scene.add_light(light)
|
|
|
|
# 设置渲染参数
|
|
renderer.set_scene(scene)
|
|
|
|
# 渲染场景
|
|
image = renderer.render()
|
|
|
|
# 显示渲染结果
|
|
o3d.geometry.image.axial_slice(image, 2, (128, 128, 128))
|
|
|
|
# # 计算顶点法线,这对于渲染是非常重要的
|
|
# mesh.compute_vertex_normals()
|
|
|
|
# # 可选:可以对网格进行进一步处理,例如滤波或下采样
|
|
# # mesh = o3d.geometry.filter_triangle_mesh(mesh, "default")
|
|
|
|
# # 创建一个Open3D的可视化对象
|
|
# vis = o3d.visualization.Visualizer()
|
|
# vis.create_window()
|
|
|
|
# # 将网格添加到可视化对象中
|
|
# vis.add_geometry(mesh)
|
|
|
|
# # 创建一个环境光源
|
|
# light = get_light_source("default")
|
|
# light.set_intensity([1.0, 1.0, 1.0]) # 设置光照强度
|
|
|
|
# # 将灯光添加到可视化对象中
|
|
# vis.add_light(light)
|
|
|
|
# # 调整灯光位置和方向
|
|
# light.set_position([0.0, 0.0, 0.0])
|
|
# light.set_direction([0.0, 0.0, 1.0])
|
|
# screenshot = vis.capture_screen()
|
|
# o3d.io.write_image("screenshot.png", screenshot)
|
|
|
|
# # 渲染并显示窗口
|
|
# vis.run()
|
|
|
|
|
|
# # 加载一个点云或者网格
|
|
# mesh = o3d.io.read_triangle_mesh(obj_file_path)
|
|
|
|
# # 创建 O3DVisualizer 实例
|
|
# vis = o3d.visualization.O3DVisualizer("O3D Visualizer Example", 1024, 768)
|
|
|
|
# # 添加几何体到可视化窗口
|
|
# vis.add_geometry("Mesh", mesh)
|
|
|
|
# # 显示可视化窗口
|
|
# Application.instance.add_window(vis)
|
|
# Application.instance.run()
|
|
|
|
# # Derive the object path set the model, material, and shader
|
|
# model_dir = sys.argv[1]
|
|
# model_name = os.path.join(model_dir, os.path.basename(model_dir) + ".obj")
|
|
# model = o3d.io.read_triangle_mesh(obj_file_path)
|
|
# material = o3d.visualization.rendering.Material()
|
|
# material.shader = "defaultLit"
|
|
|
|
# # Derive the texture paths
|
|
# albedo_name = os.path.join(model_dir, "albedo.png")
|
|
# normal_name = os.path.join(model_dir, "normal.png")
|
|
# ao_name = os.path.join(model_dir, "ao.png")
|
|
# metallic_name = os.path.join(model_dir, "metallic.png")
|
|
# roughness_name = os.path.join(model_dir, "roughness.png")
|
|
|
|
# # Check if the textures are available and loads the texture. For example, if metallic exists then load metallic texture
|
|
# if os.path.exists(albedo_name):
|
|
# material.albedo_img = o3d.io.read_image(albedo_name)
|
|
# if os.path.exists(normal_name):
|
|
# material.normal_img = o3d.io.read_image(normal_name)
|
|
# if os.path.exists(ao_name):
|
|
# material.ao_img = o3d.io.read_image(ao_name)
|
|
# if os.path.exists(metallic_name):
|
|
# material.base_metallic = 1.0
|
|
# material.metallic_img = o3d.io.read_image(metallic_name)
|
|
# if os.path.exists(roughness_name):
|
|
# material.roughness_img = o3d.io.read_image(roughness_name)
|
|
|
|
# # Draw an object named cube using the available model and texture
|
|
# o3d.visualization.draw([{"name": "cube", "geometry": model, "material": material}])
|
|
|