Browse Source

partial particles upload after sorting

ncannasse 9 years ago
parent
commit
d8dbd44fd5
2 changed files with 24 additions and 5 deletions
  1. 1 1
      h3d/Buffer.hx
  2. 23 4
      h3d/parts/GpuParticles.hx

+ 1 - 1
h3d/Buffer.hx

@@ -84,7 +84,7 @@ class Buffer {
 
 	public function uploadVector( buf : hxd.FloatBuffer, bufPos : Int, vertices : Int, startVertice = 0 ) {
 		var cur = this;
-		while( cur != null && startVertice > cur.vertices ) {
+		while( cur != null && startVertice >= cur.vertices ) {
 			startVertice -= cur.vertices;
 			cur = cur.next;
 		}

+ 23 - 4
h3d/parts/GpuParticles.hx

@@ -43,6 +43,8 @@ enum GpuEmitMode {
 
 private class GpuPart {
 
+	public var index : Int;
+
 	public var x : Float;
 	public var y : Float;
 	public var z : Float;
@@ -96,7 +98,6 @@ class GpuPartGroup {
 		return FIELDS;
 	}
 
-
 	var needRebuild = true;
 	var pshader = new h3d.shader.GpuParticle();
 	var partIndex = 0;
@@ -230,6 +231,8 @@ class GpuPartGroup {
 class GpuParticles extends h3d.scene.MultiMaterial {
 
 	static inline var VERSION = 1;
+	static inline var STRIDE = 14;
+
 	var groups : Array<GpuPartGroup>;
 	var bounds : h3d.col.Bounds;
 	var primitiveBuffer : hxd.FloatBuffer;
@@ -424,6 +427,7 @@ class GpuParticles extends h3d.scene.MultiMaterial {
 					v.transform3x3(absPos);
 
 
+					pt.index = i;
 					pt.sx = p.x;
 					pt.sy = p.y;
 					pt.sz = p.z;
@@ -463,7 +467,7 @@ class GpuParticles extends h3d.scene.MultiMaterial {
 				}
 			}
 		}
-		primitive = new h3d.prim.RawPrimitive( { vbuf : vbuf, stride : 14, quads : true, bounds:bounds }, true);
+		primitive = new h3d.prim.RawPrimitive( { vbuf : vbuf, stride : STRIDE, quads : true, bounds:bounds }, true);
 		primitive.buffer.flags.set(RawFormat);
 		primitiveBuffer = vbuf;
 		if( currentTime > duration )
@@ -499,13 +503,23 @@ class GpuParticles extends h3d.scene.MultiMaterial {
 
 		g.particles = haxe.ds.ListSort.sortSingleLinked(g.particles, function(p1:GpuPart, p2:GpuPart) return p1.w < p2.w ? 1 : -1);
 
-		var startIndex = g.partIndex * primitive.buffer.buffer.stride * 4;
+		var startIndex = g.partIndex * STRIDE * 4;
 		var index = startIndex;
 		var vbuf = primitiveBuffer;
 		var p = g.particles;
 		var uvs = PUVS;
+		var pidx = 0;
+		var firstPart = g.nparts;
+		var lastPart = -1;
 		while( p != null ) {
 
+			if( p.index == pidx ) {
+				pidx++;
+				index += STRIDE * 4;
+				p = p.next;
+				continue;
+			}
+
 			inline function add(v) vbuf[index++] = v;
 			for( u in uvs ) {
 				add(p.sx);
@@ -527,9 +541,14 @@ class GpuParticles extends h3d.scene.MultiMaterial {
 				add(p.deltaY);
 			}
 
+			if( pidx < firstPart ) firstPart = pidx;
+			if( pidx > lastPart ) lastPart = pidx;
+
+			p.index = pidx++;
 			p = p.next;
 		}
-		primitive.buffer.uploadVector(vbuf, startIndex, g.nparts * 4, g.partIndex * 4);
+		if( firstPart <= lastPart )
+			primitive.buffer.uploadVector(vbuf, (g.partIndex + firstPart) * STRIDE * 4, (lastPart - firstPart + 1) * 4, (g.partIndex + firstPart) * 4);
 	}
 
 	override function emit( ctx : h3d.scene.RenderContext ) {