Răsfoiți Sursa

have meshbatch calculate correct bounds for instanced primitive

Nicolas Cannasse 4 ani în urmă
părinte
comite
ac850da731
2 a modificat fișierele cu 17 adăugiri și 13 ștergeri
  1. 14 12
      h3d/prim/Instanced.hx
  2. 3 1
      h3d/scene/MeshBatch.hx

+ 14 - 12
h3d/prim/Instanced.hx

@@ -3,13 +3,14 @@ package h3d.prim;
 class Instanced extends MeshPrimitive {
 
 	public var commands : h3d.impl.InstanceBuffer;
-	public var offset : h3d.col.Sphere;
+	public var bounds : h3d.col.Bounds;
 	var baseBounds : h3d.col.Bounds;
 	var tmpBounds : h3d.col.Bounds;
 	var primitive : MeshPrimitive;
 
 	public function new() {
-		offset = new h3d.col.Sphere();
+		bounds = new h3d.col.Bounds();
+		bounds.addPos(0,0,0); // if not init
 		tmpBounds = new h3d.col.Bounds();
 	}
 
@@ -32,6 +33,16 @@ class Instanced extends MeshPrimitive {
 		}
 	}
 
+	public function initBounds() {
+		bounds.empty();
+	}
+
+	public inline function addInstanceBounds( absPos : h3d.Matrix ) {
+		tmpBounds.load(baseBounds);
+		tmpBounds.transform(absPos);
+		bounds.add(tmpBounds);
+	}
+
 	override function dispose() {
 		// Not owning any resources
 	}
@@ -49,16 +60,7 @@ class Instanced extends MeshPrimitive {
 	}
 
 	override function getBounds():h3d.col.Bounds {
-		tmpBounds.load(baseBounds);
-		var r = offset.r;
-		tmpBounds.offset(offset.x, offset.y, offset.z);
-		tmpBounds.xMin -= r;
-		tmpBounds.yMin -= r;
-		tmpBounds.zMin -= r;
-		tmpBounds.xMax += r;
-		tmpBounds.yMax += r;
-		tmpBounds.zMax += r;
-		return tmpBounds;
+		return bounds;
 	}
 
 	// make public

+ 3 - 1
h3d/scene/MeshBatch.hx

@@ -134,6 +134,7 @@ class MeshBatch extends MultiMaterial {
 
 	public function begin( emitCountTip = -1, resizeDown = false ) {
 		instanceCount = 0;
+		instanced.initBounds();
 		if( shadersChanged ) {
 			initShadersMapping();
 			shadersChanged = false;
@@ -236,7 +237,8 @@ class MeshBatch extends MultiMaterial {
 	}
 
 	public function emitInstance() {
-		syncPos();
+		if( worldPosition == null ) syncPos();
+		instanced.addInstanceBounds(worldPosition == null ? absPos : worldPosition);
 		var p = dataPasses;
 		while( p != null ) {
 			syncData(p);