Sfoglia il codice sorgente

Added CPU lod selection for MeshBatch.

TothBenoit 8 mesi fa
parent
commit
ddf7b0e11e
4 ha cambiato i file con 25 aggiunte e 7 eliminazioni
  1. 2 2
      h3d/prim/HMDModel.hx
  2. 13 0
      h3d/prim/Instanced.hx
  3. 2 1
      h3d/prim/Primitive.hx
  4. 8 4
      h3d/scene/MeshBatch.hx

+ 2 - 2
h3d/prim/HMDModel.hx

@@ -50,8 +50,8 @@ class HMDModel extends MeshPrimitive {
 		curMaterial = material + lod * data.indexCounts.length;
 	}
 
-	override function getMaterialIndexes(material:Int):{count:Int, start:Int} {
-		return { start : indexesTriPos[material]*3, count : data.indexCounts[material] };
+	override function getMaterialIndexes(material:Int, lod:Int=0):{count:Int, start:Int} {
+		return { start : indexesTriPos[material + lod * data.indexCounts.length]*3, count : lods[lod].indexCounts[material] };
 	}
 
 	public function getDataBuffers(fmt, ?defaults,?material) {

+ 13 - 0
h3d/prim/Instanced.hx

@@ -56,6 +56,19 @@ class Instanced extends Primitive {
 		return bounds;
 	}
 
+	override function screenRatioToLod( screenRatio : Float ) {
+		return primitive.screenRatioToLod(screenRatio);
+	}
+
+	public function setCommand( material : Int, lod : Int, count : Int ) {
+		if ( lod > primitive.lodCount() - 1) {
+			commands.setCommand(0, 0, 0);
+			return;
+		}
+		var infos = primitive.getMaterialIndexes(material, lod);
+		commands.setCommand(count, infos.count, infos.start);
+	}
+
 	override function render( engine : h3d.Engine ) {
 		if( primitive.buffer == null || primitive.buffer.isDisposed() )
 			primitive.alloc(engine);

+ 2 - 1
h3d/prim/Primitive.hx

@@ -94,7 +94,8 @@ class Primitive {
 	/**
 		Returns the number and offset of indexes for the specified material
 	**/
-	public function getMaterialIndexes( material : Int ) : { count : Int, start : Int } {
+	public function getMaterialIndexes( material : Int, lod : Int = 0 ) : { count : Int, start : Int } {
+		if ( lod != 0 ) return { start : 0, count : 0 };
 		return { start : 0, count : indexes == null ? triCount() * 3 : indexes.count };
 	}
 

+ 8 - 4
h3d/scene/MeshBatch.hx

@@ -1,7 +1,4 @@
 package h3d.scene;
-
-import hxsl.ShaderList;
-
 class BatchData {
 
 	public var paramsCount : Int;
@@ -206,6 +203,8 @@ class MeshBatch extends MultiMaterial {
 	var dataPasses : BatchData;
 	var needUpload = false;
 
+	public var lodDistance : Float;
+
 	public var meshBatchFlags(default, null) : haxe.EnumFlags<MeshBatchFlag>;
 	var enableLOD(get, never) : Bool;
 	function get_enableLOD() return meshBatchFlags.has( EnableLod );
@@ -892,6 +891,10 @@ class MeshBatch extends MultiMaterial {
 		needUpload = false;
 	}
 
+	override function calcScreenRatio(ctx:RenderContext) {
+		curScreenRatio = @:privateAccess instanced.primitive.getBounds().dimension() / ( 2.0 * hxd.Math.max(lodDistance, 0.0001) );
+	}
+
 	override function draw(ctx:RenderContext) {
 		var p = dataPasses;
 		while( true ) {
@@ -903,7 +906,7 @@ class MeshBatch extends MultiMaterial {
 					p.shader.Batch_Buffer = p.buffers[bufferIndex];
 				if( p.instanceBuffers == null ) {
 					var count = hxd.Math.imin( instanceCount - p.maxInstance * bufferIndex, p.maxInstance );
-					instanced.commands.setCommand(count,p.indexCount,p.indexStart);
+					instanced.setCommand(p.matIndex, instanced.screenRatioToLod(curScreenRatio), count);
 					if ( p.commandBuffers != null && p.commandBuffers.length > 0 ) {
 						@:privateAccess instanced.commands.data = p.commandBuffers[bufferIndex].vbuf;
 						@:privateAccess instanced.commands.countBuffer = p.countBuffers[bufferIndex].vbuf;
@@ -923,6 +926,7 @@ class MeshBatch extends MultiMaterial {
 
 	override function emit(ctx:RenderContext) {
 		if( instanceCount == 0 ) return;
+		calcScreenRatio(ctx);
 		var p = dataPasses;
 		while( p != null ) {
 			var pass = p.pass;