Browse Source

change getBoundsRec to addBoundsRec, allow relativeTo transform

Nicolas Cannasse 2 years ago
parent
commit
87ca03e492

+ 1 - 1
h3d/col/SkinCollider.hx

@@ -59,7 +59,7 @@ class SkinCollider extends Collider {
 		lastBoundsFrame = obj.lastFrame;
 		obj.syncJoints();
 		currentBounds.empty();
-		obj.getBoundsRec(currentBounds);
+		obj.addBoundsRec(currentBounds,null);
 	}
 
 	function applyTransform() {

+ 6 - 4
h3d/parts/GpuParticles.hx

@@ -501,9 +501,11 @@ class GpuParticles extends h3d.scene.MultiMaterial {
 			if( p != null ) p.dispose();
 	}
 
-	override function getBoundsRec(b:h3d.col.Bounds) {
-		if( flags.has(FIgnoreBounds) )
-			return super.getBoundsRec(b);
+	override function addBoundsRec(b:h3d.col.Bounds, relativeTo) {
+		if( flags.has(FIgnoreBounds) ) {
+			super.addBoundsRec(b, relativeTo);
+			return;
+		}
 		for( g in groups )
 			if( g.needRebuild ) {
 				var s = getScene();
@@ -516,7 +518,7 @@ class GpuParticles extends h3d.scene.MultiMaterial {
 				}
 				break;
 			}
-		return super.getBoundsRec(b);
+		super.addBoundsRec(b, relativeTo);
 	}
 
 	public dynamic function onEnd() {

+ 0 - 4
h3d/scene/Graphics.hx

@@ -55,10 +55,6 @@ class Graphics extends Mesh {
 		material.mainPass.culling = None;
 	}
 
-	override function getBoundsRec(b:h3d.col.Bounds):h3d.col.Bounds {
-		return super.getBoundsRec(b);
-	}
-
 	override function onRemove() {
 		super.onRemove();
 		bprim.clear();

+ 12 - 7
h3d/scene/Mesh.hx

@@ -37,14 +37,19 @@ class Mesh extends Object {
 		return [material];
 	}
 
-	override function getBoundsRec( b : h3d.col.Bounds ) {
-		b = super.getBoundsRec(b);
+
+	static var tmpMat = new h3d.Matrix();
+	override function addBoundsRec( b : h3d.col.Bounds, relativeTo : h3d.Matrix ) {
+		super.addBoundsRec(b, relativeTo);
 		if( primitive == null || flags.has(FIgnoreBounds) )
-			return b;
-		var tmp = primitive.getBounds().clone();
-		tmp.transform(absPos);
-		b.add(tmp);
-		return b;
+			return;
+		var bounds = primitive.getBounds();
+		if( relativeTo == null ) {
+			b.addTransform(bounds,absPos);
+		} else {
+			tmpMat.multiply(absPos, relativeTo);
+			b.addTransform(bounds,tmpMat);
+		}
 	}
 
 	override function clone( ?o : Object ) : Object {

+ 9 - 5
h3d/scene/MeshBatch.hx

@@ -266,15 +266,19 @@ class MeshBatch extends MultiMaterial {
 		needUpload = true;
 	}
 
-	override function getBoundsRec( b : h3d.col.Bounds ) {
+	override function addBoundsRec( b : h3d.col.Bounds, relativeTo: h3d.Matrix ) {
 		var old = primitive;
 		primitive = null;
-		b = super.getBoundsRec(b);
+		super.addBoundsRec(b, relativeTo);
 		primitive = old;
 		if( primitive == null || flags.has(FIgnoreBounds) )
-			return b;
-		b.add(primitive.getBounds());
-		return b;
+			return;
+		// already transformed in absolute
+		var bounds = primitive.getBounds();
+		if( relativeTo == null )
+			b.add(bounds);
+		else
+			b.addTransform(bounds, relativeTo);
 	}
 
 	public function emitInstance() {

+ 9 - 8
h3d/scene/Object.hx

@@ -386,17 +386,19 @@ class Object {
 	}
 
 	/**
-		Return the bounds of this object and all its children, in absolute global coordinates.
+		Return the bounds of this object and all its children, in absolute global coordinates or relative to the
+		object being used as parameter.
 	**/
-	@:final public function getBounds( ?b : h3d.col.Bounds ) {
+	@:final public function getBounds( ?b : h3d.col.Bounds, ?relativeTo : Object ) {
 		if( b == null )
 			b = new h3d.col.Bounds();
-		if( parent != null )
+		if( parent != null && parent != relativeTo )
 			parent.syncPos();
-		return getBoundsRec(b);
+		addBoundsRec(b, relativeTo == null ? null : relativeTo.getInvPos());
+		return b;
 	}
 
-	function getBoundsRec( b : h3d.col.Bounds ) {
+	function addBoundsRec( b : h3d.col.Bounds, relativeTo : h3d.Matrix ) {
 		if( posChanged ) {
 			for( c in children )
 				c.posChanged = true;
@@ -404,8 +406,7 @@ class Object {
 			calcAbsPos();
 		}
 		for( c in children )
-			c.getBoundsRec(b);
-		return b;
+			c.addBoundsRec(b, relativeTo);
 	}
 
 	/**
@@ -785,7 +786,7 @@ class Object {
 		#if sceneprof h3d.impl.SceneProf.mark(this); #end
 
 		if( !visible || (culled && inheritCulled && !ctx.computingStatic) )
-			return;		
+			return;
 
 		// fallback in case the object was added during a sync() event and we somehow didn't update it
 		if( posChanged ) {

+ 3 - 4
h3d/scene/Skin.hx

@@ -94,14 +94,14 @@ class Skin extends MultiMaterial {
 		return s;
 	}
 
-	override function getBoundsRec( b : h3d.col.Bounds ) {
+	override function addBoundsRec( b : h3d.col.Bounds, relativeTo : h3d.Matrix ) {
 		// ignore primitive bounds !
 		var old = primitive;
 		primitive = null;
-		b = super.getBoundsRec(b);
+		super.addBoundsRec(b, relativeTo);
 		primitive = old;
 		if( flags.has(FIgnoreBounds) )
-			return b;
+			return;
 		syncJoints();
 		if( skinData.vertexWeights == null )
 			cast(primitive, h3d.prim.HMDModel).loadSkin(skinData);
@@ -119,7 +119,6 @@ class Skin extends MultiMaterial {
 				b.addSpherePos(pt.x, pt.y, pt.z, j.offsetRay * scale);
 			}
 		}
-		return b;
 	}
 
 	public function getCurrentSkeletonBounds() {