浏览代码

Rework HierarchicalWorld canSubdivide. Adding lockAt and unlockAt.

clementlandrin 1 年之前
父节点
当前提交
0285365571
共有 1 个文件被更改,包括 33 次插入6 次删除
  1. 33 6
      h3d/scene/HierarchicalWorld.hx

+ 33 - 6
h3d/scene/HierarchicalWorld.hx

@@ -82,8 +82,12 @@ class HierarchicalWorld extends Object {
 			data.onCreate(this);
 	}
 
+	final function isLeaf() {
+		return data.depth == data.maxDepth;
+	}
+
 	function canSubdivide() {
-		return data.depth < data.maxDepth;
+		return true;
 	}
 
 	function createNode(parent, data) {
@@ -91,7 +95,7 @@ class HierarchicalWorld extends Object {
 	}
 
 	function subdivide() {
-		if ( subdivided )
+		if ( subdivided || isLeaf() )
 			return;
 		subdivided = true;
 		var childSize = data.size >> 1;
@@ -136,9 +140,11 @@ class HierarchicalWorld extends Object {
 		}
 
 		culled = !bounds.inFrustum(ctx.camera.frustum);
-		if ( canSubdivide() ) {
+		if ( !isLeaf() ) {
 			if ( FULL || calcDist(ctx) < data.size * 2.0 ) {
-				subdivide();
+				if ( canSubdivide() ) {
+					subdivide();
+				}
 			} else if ( !locked ) {
 				removeSubdivisions();
 			}
@@ -167,8 +173,7 @@ class HierarchicalWorld extends Object {
 			return;
 		if ( lock )
 			locked = true;
-		if ( canSubdivide() )
-			subdivide();
+		subdivide();
 		for ( c in children ) {
 			var node = Std.downcast(c, HierarchicalWorld);
 			if ( node == null )
@@ -177,6 +182,18 @@ class HierarchicalWorld extends Object {
 		}
 	}
 
+	public function lockAt(x : Float, y : Float) {
+		if ( !bounds.contains(new h3d.col.Point(x, y, 0.0)) )
+			return;
+		locked = true;
+		for ( c in children ) {
+			var node = Std.downcast(c, HierarchicalWorld);
+			if ( node == null )
+				continue;
+			node.lockAt(x, y);
+		}
+	}
+
 	public function unlockAt(x : Float, y : Float) {
 		if ( !bounds.contains(new h3d.col.Point(x, y, 0.0)) )
 			return;
@@ -189,6 +206,16 @@ class HierarchicalWorld extends Object {
 		}
 	}
 
+	public function unlockAll() {
+		locked = false;
+		for ( c in children ) {
+			var node = Std.downcast(c, HierarchicalWorld);
+			if ( node == null )
+				continue;
+			node.unlockAll();
+		}
+	}
+
 	public function getRoot() : h3d.scene.HierarchicalWorld {
 		var root : h3d.scene.Object = this;
 		while ( Std.isOfType(root.parent, HierarchicalWorld) )