Skip to content

Commit dba0f7f

Browse files
committed
Update Normals/USD/BSP examples and docs
1 parent 9edfad4 commit dba0f7f

6 files changed

Lines changed: 680 additions & 600 deletions

File tree

Elements/extensions/Normals_USDimporter_BSP/README.md

Lines changed: 32 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3,49 +3,57 @@ Authors:
33
Kapetanakis Ioannis - csd4641
44

55
Tests are written for pytest
6-
-> To run all the tests, type "pytest Tests"
6+
-> To run all the tests, type: pytest Tests
77

8-
<For Proper Normals Task:>
9-
Implemented correct flat/smooth normal handling by detecting shared vs unique vertex indexing and converting the vertex/index buffers accordingly. Custom helper functions exist to determine whether vertices are shared or unique by analyzing index usage. This correction fixes smooth-shading artifacts caused by incorrect vertex-sharing assumptions and ensures the appropriate flat or smooth shading path is selected.
108

11-
Usage:
12-
python cow_example.py --shading {smooth|flat} [-colored]
13-
python sphere.py --shading {smooth|flat} [-colored]
14-
15-
Options:
16-
--shading Shading mode to use.
17-
smooth : smooth (per-vertex) normals
18-
flat : flat (per-face) normals
19-
20-
Flags:
21-
-colored Enable color visualization by using normals as colors (optional)
9+
<Proper Normals Task:>
10+
Implemented correct flat/smooth normal handling by detecting shared vs unique vertex indexing and converting the vertex/index buffers accordingly. Helper utilities determine whether vertices are shared or unique by analyzing index usage. This fixes smooth-shading artifacts caused by incorrect vertex-sharing assumptions and ensures the appropriate flat or smooth shading path is selected.
2211

23-
Example:
24-
python cow_example.py --shading flat -colored
12+
Usage:
13+
python cow_example.py
14+
python sphere_example.py
2515

16+
Runtime options (ImGui):
17+
- Shading: Smooth / Flat
18+
- Normals as color: toggles normal-visualization by mapping normals to RGB
2619

27-
<For USD Importer Task:>
20+
<USD Importer Task:>
2821
The LoadScene_Blender method imports a USD scene exported from Blender and converts it into an Elements scene representation. It traverses the USD stage hierarchy, creates corresponding entities for each UsdGeom.Xform, and reconstructs parent–child relationships based on USD paths.
2922

3023
For each UsdGeom.Mesh, the method extracts geometry data including vertex positions, face topology, normals, and material color information. It supports both smooth and flat shading by handling different normal interpolation modes (vertex and faceVarying). Polygonal faces are triangulated appropriately, with corner-space triangulation used for flat shading.
3124

3225
The method uploads vertex attributes and indices to the GPU and performs coordinate system conversion from Blender’s Z-up convention to the engine’s Y-up convention.
3326

3427
Usage:
35-
python usd_import_example.py [-colored]
36-
37-
Flags:
38-
-colored Enable color visualization by using normals as colors (optional)
28+
python usd_import_example.py
3929

30+
Runtime options (ImGui):
31+
- Load USD (colors)
32+
- Load USD (normals as color)
4033

41-
<For BSP Task:>
34+
<BSP Task:>
4235
The implemented method builds an axis-aligned Binary Space Partitioning (BSP) tree for triangle meshes.
4336

37+
Each triangle is implicitly assigned an ID based on its position in the index buffer (every 3 consecutive indices correspond to one triangle). The search(id) function references triangles using this index-based ID.
38+
4439
At each node, the splitting axis is selected based on the largest spatial extent of the triangles, while the split position is chosen as the median of triangle centroids along that axis to avoid unbalanced partitions.
4540

4641
Triangles are classified as fully on one side of the split plane or intersecting it; intersecting triangles are propagated to all child leaf nodes to preserve spatial correctness.
4742

48-
During search, the BSP tree is traversed by testing each triangle against the split planes. Depending on whether the triangle lies on one side of the plane or intersects it, the traversal proceeds to the appropriate child nodes.
43+
During search, the BSP tree is traversed by testing each triangle against the split planes. The traversal path and tree structure are printed to the terminal.
4944

5045
Usage:
51-
python bsp_example.py
46+
python bsp_example.py
47+
48+
What to try:
49+
- Move the camera to view triangle placement.
50+
- Use Triangle id = 0..4 and press Search to observe different traversal paths.
51+
- Press Print tree to inspect the BSP structure by depth.
52+
53+
In each case, the result is printed to the terminal.
54+
55+
56+
Runtime options (ImGui):
57+
- Triangle id (input)
58+
- Search (prints traversal path to the terminal)
59+
- Print tree (prints the BSP structure by depth to the terminal)

Elements/extensions/Normals_USDimporter_BSP/UsdImporter.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
The USDImporter file, holds functionality for importing and exporting Elements scenes as .usd files
88
99
"""
10-
from pxr import Usd, UsdGeom
1110

1211
from pxr import Usd, UsdGeom, UsdShade
1312
import numpy as np

Elements/extensions/Normals_USDimporter_BSP/bsp_example.py

Lines changed: 36 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import numpy as np
2+
import imgui
23

34
import Elements.pyECSS.math_utilities as util
45
from Elements.pyECSS.Entity import Entity
@@ -20,14 +21,14 @@
2021

2122

2223
example_description = \
23-
"This is a scene with triangles (instead of cubes), a terrain and axes. \n\
24-
The triangles and axes are rendered with a simple shader. \n\
25-
that allow camera movement too, via the Elements GUI. \n\n\
26-
An ECSS Graph shows the Entities and Components of the \n\
27-
scene, in read only way, i.e., you cannot manipulate \n\
28-
any information via the ECSS Graph GUI. \n\n\
29-
You can move the camera through the Elements GUI \n\
30-
or the mouse. Hit ESC OR Close the window to quit."
24+
"This example demonstrates building and querying a BSP tree over a small set of triangles.\n\
25+
Five triangles are placed in the scene along with a terrain grid and world axes.\n\
26+
\n\
27+
Use the 'BSP Controls' window to select a Triangle id and press 'Search' to print the\n\
28+
BSP traversal path to the terminal. Press 'Print tree' to print the BSP structure by depth.\n\
29+
\n\
30+
You can move the camera via the Elements GUI or the mouse. Hit ESC or close the window to quit."
31+
3132

3233
winWidth = 1024
3334
winHeight = 768
@@ -271,23 +272,42 @@
271272
BSP.build()
272273

273274
print()
274-
print("---- BSP Tree ----")
275-
BSP.print_by_depth()
275+
# print("---- BSP Tree ----")
276+
# BSP.print_by_depth()
276277

277-
print()
278-
print("---- Search path traversal (for a non-intersected triangle) ----")
279-
BSP.search(1)
278+
# print()
279+
# print("---- Search path traversal (for a non-intersected triangle) ----")
280+
# BSP.search(1)
280281

281-
print()
282-
print("---- Search path traversal (for a intersected triangle) ----")
282+
# print()
283+
# print("---- Search path traversal (for a intersected triangle) ----")
283284

284-
BSP.search(0)
285+
# BSP.search(0)
285286

286287
model_terrain = terrain.getChild(0).trs
287288
model_axes = axes_trans.trs
288289

290+
search_id = 0
291+
print_tree = False
292+
289293
while running:
290294
running = scene.render()
295+
imgui.begin("BSP Controls")
296+
297+
changed, search_id = imgui.input_int("Triangle id", search_id)
298+
299+
if imgui.button("Search"):
300+
print("\n---- Search path traversal ----")
301+
BSP.search(int(search_id))
302+
303+
imgui.same_line()
304+
305+
if imgui.button("Print tree"):
306+
print("\n---- BSP Tree ----")
307+
BSP.print_by_depth()
308+
309+
imgui.end()
310+
291311
displayGUI_text(example_description)
292312
scene.world.traverse_visit(renderUpdate, scene.world.root)
293313
scene.world.traverse_visit_pre_camera(camUpdate, orthoCam)

0 commit comments

Comments
 (0)