forked from jMonkeyEngine/jmonkeyengine
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSpatialUtils.java
More file actions
86 lines (80 loc) · 2.72 KB
/
SpatialUtils.java
File metadata and controls
86 lines (80 loc) · 2.72 KB
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
package jme3test.app;
import com.jme3.scene.Geometry;
import com.jme3.scene.Node;
import com.jme3.scene.Spatial;
/**
* Utility class for working with spatial hierarchies.
* Provides helper methods for searching and manipulating spatial scene graphs.
*/
public class SpatialUtils {
/**
* Recursively finds the first Geometry in a spatial hierarchy.
* This is useful for handling both simple flat structures (Ogre models) and
* complex nested node trees (glTF models).
*
* @param spatial The root spatial to search from
* @return The first Geometry found, or null if none exists
*/
public static Geometry findFirstGeometry(Spatial spatial) {
if (spatial instanceof Geometry) {
return (Geometry) spatial;
}
if (spatial instanceof Node) {
Node node = (Node) spatial;
for (Spatial child : node.getChildren()) {
Geometry geom = findFirstGeometry(child);
if (geom != null) {
return geom;
}
}
}
return null;
}
/**
* Recursively finds a Geometry by name in a spatial hierarchy.
* Searches depth-first through the entire spatial tree.
*
* @param spatial The root spatial to search from
* @param name The name of the geometry to find
* @return The Geometry with the matching name, or null if not found
*/
public static Geometry findGeometryByName(Spatial spatial, String name) {
if (spatial.getName() != null && spatial.getName().equals(name) && spatial instanceof Geometry) {
return (Geometry) spatial;
}
if (spatial instanceof Node) {
Node node = (Node) spatial;
for (Spatial child : node.getChildren()) {
Geometry geom = findGeometryByName(child, name);
if (geom != null) {
return geom;
}
}
}
return null;
}
/**
* Counts the total number of Geometries in a spatial hierarchy.
* Useful for debugging and understanding scene structure.
*
* @param spatial The root spatial to count from
* @return The total number of geometries in the hierarchy
*/
public static int countGeometries(Spatial spatial) {
if (spatial instanceof Geometry) {
return 1;
}
if (spatial instanceof Node) {
int count = 0;
Node node = (Node) spatial;
for (Spatial child : node.getChildren()) {
count += countGeometries(child);
}
return count;
}
return 0;
}
private SpatialUtils() {
// Utility class, no instantiation
}
}