diff --git a/jme3-core/src/main/java/com/jme3/scene/Geometry.java b/jme3-core/src/main/java/com/jme3/scene/Geometry.java index f2f33501a..f403e9c62 100644 --- a/jme3-core/src/main/java/com/jme3/scene/Geometry.java +++ b/jme3-core/src/main/java/com/jme3/scene/Geometry.java @@ -458,7 +458,7 @@ public class Geometry extends Spatial { } @Override - public void depthFirstTraversal(SceneGraphVisitor visitor) { + public void depthFirstTraversal(SceneGraphVisitor visitor, DFSMode mode) { visitor.visit(this); } diff --git a/jme3-core/src/main/java/com/jme3/scene/Node.java b/jme3-core/src/main/java/com/jme3/scene/Node.java index 6089eda49..7806addd0 100644 --- a/jme3-core/src/main/java/com/jme3/scene/Node.java +++ b/jme3-core/src/main/java/com/jme3/scene/Node.java @@ -770,11 +770,18 @@ public class Node extends Spatial { } } @Override - public void depthFirstTraversal(SceneGraphVisitor visitor) { - for (Spatial child : children.getArray()) { - child.depthFirstTraversal(visitor); + public void depthFirstTraversal(SceneGraphVisitor visitor, DFSMode mode) { + if (mode == DFSMode.POST_ORDER) { + for (Spatial child : children.getArray()) { + child.depthFirstTraversal(visitor); + } + visitor.visit(this); + } else { //pre order + visitor.visit(this); + for (Spatial child : children.getArray()) { + child.depthFirstTraversal(visitor); + } } - visitor.visit(this); } @Override protected void breadthFirstTraversal(SceneGraphVisitor visitor, Queue queue) { diff --git a/jme3-core/src/main/java/com/jme3/scene/Spatial.java b/jme3-core/src/main/java/com/jme3/scene/Spatial.java index 452ccce47..d0369ca3c 100644 --- a/jme3-core/src/main/java/com/jme3/scene/Spatial.java +++ b/jme3-core/src/main/java/com/jme3/scene/Spatial.java @@ -1798,10 +1798,35 @@ public abstract class Spatial implements Savable, Cloneable, Collidable, Cloneab } /** - * Visit each scene graph element ordered by DFS + * Visit each scene graph element ordered by DFS with the default post order mode. * @param visitor + * @see #depthFirstTraversal(com.jme3.scene.SceneGraphVisitor, com.jme3.scene.Spatial.DFSMode) */ - public abstract void depthFirstTraversal(SceneGraphVisitor visitor); + public void depthFirstTraversal(SceneGraphVisitor visitor) { + depthFirstTraversal(visitor, DFSMode.POST_ORDER); + } + + /** + * Specifies the mode of the depth first search. + */ + public static enum DFSMode { + /** + * Pre order: the current spatial is visited first, then its children. + */ + PRE_ORDER, + /** + * Post order: the children are visited first, then the parent. + */ + POST_ORDER; + } + + /** + * Visit each scene graph element ordered by DFS. + * There are two modes: pre order and post order. + * @param visitor + * @param mode the traversal mode: pre order or post order + */ + public abstract void depthFirstTraversal(SceneGraphVisitor visitor, DFSMode mode); /** * Visit each scene graph element ordered by BFS