File: demo_scene.py

package info (click to toggle)
open3d 0.19.0-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 83,496 kB
  • sloc: cpp: 206,543; python: 27,254; ansic: 8,356; javascript: 1,883; sh: 1,527; makefile: 259; xml: 69
file content (152 lines) | stat: -rw-r--r-- 6,679 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# ----------------------------------------------------------------------------
# -                        Open3D: www.open3d.org                            -
# ----------------------------------------------------------------------------
# Copyright (c) 2018-2024 www.open3d.org
# SPDX-License-Identifier: MIT
# ----------------------------------------------------------------------------
"""Demo scene demonstrating models, built-in shapes, and materials"""

import numpy as np
import open3d as o3d
import open3d.visualization as vis


def create_scene():
    """Creates the geometry and materials for the demo scene and returns a
    dictionary suitable for draw call
    """
    # Create some shapes for our scene
    a_cube = o3d.geometry.TriangleMesh.create_box(2,
                                                  4,
                                                  4,
                                                  create_uv_map=True,
                                                  map_texture_to_each_face=True)
    a_cube.compute_triangle_normals()
    a_cube.translate((-5, 0, -2))
    a_cube = o3d.t.geometry.TriangleMesh.from_legacy(a_cube)

    a_sphere = o3d.geometry.TriangleMesh.create_sphere(2.5,
                                                       resolution=40,
                                                       create_uv_map=True)
    a_sphere.compute_vertex_normals()
    rotate_90 = o3d.geometry.get_rotation_matrix_from_xyz((-np.pi / 2, 0, 0))
    a_sphere.rotate(rotate_90)
    a_sphere.translate((5, 2.4, 0))
    a_sphere = o3d.t.geometry.TriangleMesh.from_legacy(a_sphere)

    a_cylinder = o3d.geometry.TriangleMesh.create_cylinder(
        1.0, 4.0, 30, 4, True)
    a_cylinder.compute_triangle_normals()
    a_cylinder.rotate(rotate_90)
    a_cylinder.translate((10, 2, 0))
    a_cylinder = o3d.t.geometry.TriangleMesh.from_legacy(a_cylinder)

    a_ico = o3d.geometry.TriangleMesh.create_icosahedron(1.25,
                                                         create_uv_map=True)
    a_ico.compute_triangle_normals()
    a_ico.translate((-10, 2, 0))
    a_ico = o3d.t.geometry.TriangleMesh.from_legacy(a_ico)

    # Load an OBJ model for our scene
    helmet_data = o3d.data.FlightHelmetModel()
    helmet = o3d.io.read_triangle_model(helmet_data.path)
    helmet_parts = o3d.t.geometry.TriangleMesh.from_triangle_mesh_model(helmet)

    # Create a ground plane
    ground_plane = o3d.geometry.TriangleMesh.create_box(
        50.0, 0.1, 50.0, create_uv_map=True, map_texture_to_each_face=True)
    ground_plane.compute_triangle_normals()
    rotate_180 = o3d.geometry.get_rotation_matrix_from_xyz((-np.pi, 0, 0))
    ground_plane.rotate(rotate_180)
    ground_plane.translate((-25.0, -0.1, -25.0))
    ground_plane.paint_uniform_color((1, 1, 1))
    ground_plane = o3d.t.geometry.TriangleMesh.from_legacy(ground_plane)

    # Material to make ground plane more interesting - a rough piece of glass
    ground_plane.material = vis.Material("defaultLitSSR")
    ground_plane.material.scalar_properties['roughness'] = 0.15
    ground_plane.material.scalar_properties['reflectance'] = 0.72
    ground_plane.material.scalar_properties['transmission'] = 0.6
    ground_plane.material.scalar_properties['thickness'] = 0.3
    ground_plane.material.scalar_properties['absorption_distance'] = 0.1
    ground_plane.material.vector_properties['absorption_color'] = np.array(
        [0.82, 0.98, 0.972, 1.0])
    painted_plaster_texture_data = o3d.data.PaintedPlasterTexture()
    ground_plane.material.texture_maps['albedo'] = o3d.t.io.read_image(
        painted_plaster_texture_data.albedo_texture_path)
    ground_plane.material.texture_maps['normal'] = o3d.t.io.read_image(
        painted_plaster_texture_data.normal_texture_path)
    ground_plane.material.texture_maps['roughness'] = o3d.t.io.read_image(
        painted_plaster_texture_data.roughness_texture_path)

    # Load textures and create materials for each of our demo items
    wood_floor_texture_data = o3d.data.WoodFloorTexture()
    a_cube.material = vis.Material('defaultLit')
    a_cube.material.texture_maps['albedo'] = o3d.t.io.read_image(
        wood_floor_texture_data.albedo_texture_path)
    a_cube.material.texture_maps['normal'] = o3d.t.io.read_image(
        wood_floor_texture_data.normal_texture_path)
    a_cube.material.texture_maps['roughness'] = o3d.t.io.read_image(
        wood_floor_texture_data.roughness_texture_path)

    tiles_texture_data = o3d.data.TilesTexture()
    a_sphere.material = vis.Material('defaultLit')
    a_sphere.material.texture_maps['albedo'] = o3d.t.io.read_image(
        tiles_texture_data.albedo_texture_path)
    a_sphere.material.texture_maps['normal'] = o3d.t.io.read_image(
        tiles_texture_data.normal_texture_path)
    a_sphere.material.texture_maps['roughness'] = o3d.t.io.read_image(
        tiles_texture_data.roughness_texture_path)

    terrazzo_texture_data = o3d.data.TerrazzoTexture()
    a_ico.material = vis.Material('defaultLit')
    a_ico.material.texture_maps['albedo'] = o3d.t.io.read_image(
        terrazzo_texture_data.albedo_texture_path)
    a_ico.material.texture_maps['normal'] = o3d.t.io.read_image(
        terrazzo_texture_data.normal_texture_path)
    a_ico.material.texture_maps['roughness'] = o3d.t.io.read_image(
        terrazzo_texture_data.roughness_texture_path)

    metal_texture_data = o3d.data.MetalTexture()
    a_cylinder.material = vis.Material('defaultLit')
    a_cylinder.material.texture_maps['albedo'] = o3d.t.io.read_image(
        metal_texture_data.albedo_texture_path)
    a_cylinder.material.texture_maps['normal'] = o3d.t.io.read_image(
        metal_texture_data.normal_texture_path)
    a_cylinder.material.texture_maps['roughness'] = o3d.t.io.read_image(
        metal_texture_data.roughness_texture_path)
    a_cylinder.material.texture_maps['metallic'] = o3d.t.io.read_image(
        metal_texture_data.metallic_texture_path)

    geoms = [{
        "name": "plane",
        "geometry": ground_plane
    }, {
        "name": "cube",
        "geometry": a_cube
    }, {
        "name": "cylinder",
        "geometry": a_cylinder
    }, {
        "name": "ico",
        "geometry": a_ico
    }, {
        "name": "sphere",
        "geometry": a_sphere
    }]
    # Load the helmet
    for name, tmesh in helmet_parts.items():
        geoms.append({
            "name": name,
            "geometry": tmesh.scale(10.0, (0.0, 0.0, 0.0))
        })
    return geoms


if __name__ == "__main__":
    geoms = create_scene()
    vis.draw(geoms,
             bg_color=(0.8, 0.9, 0.9, 1.0),
             show_ui=True,
             width=1920,
             height=1080)