Răsfoiți Sursa

allow texture capture

ncannasse 10 ani în urmă
părinte
comite
66bc6552d8
6 a modificat fișierele cu 39 adăugiri și 21 ștergeri
  1. 0 4
      h3d/Engine.hx
  2. 1 1
      h3d/impl/Driver.hx
  3. 3 3
      h3d/impl/LogDriver.hx
  4. 2 2
      h3d/impl/ScnDriver.hx
  5. 2 11
      h3d/impl/Stage3dDriver.hx
  6. 31 0
      h3d/mat/Texture.hx

+ 0 - 4
h3d/Engine.hx

@@ -79,10 +79,6 @@ class Engine {
 		return driver.getDriverName(details);
 	}
 
-	public function setCapture( bmp : hxd.BitmapData, callb : Void -> Void ) {
-		driver.setCapture(bmp,callb);
-	}
-
 	public function selectShader( shader : hxsl.RuntimeShader ) {
 		if( driver.selectShader(shader) )
 			shaderSwitches++;

+ 1 - 1
h3d/impl/Driver.hx

@@ -88,7 +88,7 @@ class Driver {
 	public function clear( ?color : h3d.Vector, ?depth : Float, ?stencil : Int ) {
 	}
 
-	public function setCapture( bmp : hxd.BitmapData, callb : Void -> Void ) {
+	public function captureRenderBuffer( bmp : hxd.BitmapData ) {
 	}
 
 	public function reset() {

+ 3 - 3
h3d/impl/LogDriver.hx

@@ -44,9 +44,9 @@ class LogDriver extends Driver {
 		d.clear(color, depth, stencil);
 	}
 
-	override function setCapture( bmp : hxd.BitmapData, callb : Void -> Void ) {
-		log('SetCapture ${bmp.width}x${bmp.height}');
-		d.setCapture(bmp, callb);
+	override function captureRenderBuffer( bmp : hxd.BitmapData ) {
+		log('CaptureRenderBuffer ${bmp.width}x${bmp.height}');
+		d.captureRenderBuffer(bmp);
 	}
 
 	override function reset() {

+ 2 - 2
h3d/impl/ScnDriver.hx

@@ -90,8 +90,8 @@ class ScnDriver extends Driver {
 		d.clear(color, depth, stencil);
 	}
 
-	override function setCapture( bmp : hxd.BitmapData, callb : Void -> Void ) {
-		d.setCapture(bmp, callb);
+	override function captureRenderBuffer( bmp : hxd.BitmapData ) {
+		d.captureRenderBuffer(bmp);
 	}
 
 	override function reset() {

+ 2 - 11
h3d/impl/Stage3dDriver.hx

@@ -69,7 +69,6 @@ class Stage3dDriver extends Driver {
 	var width : Int;
 	var height : Int;
 	var enableDraw : Bool;
-	var capture : { bmp : hxd.BitmapData, callb : Void -> Void };
 	var frame : Int;
 	var programs : Map<Int, CompiledShader>;
 	var isStandardMode : Bool;
@@ -186,8 +185,8 @@ class Stage3dDriver extends Driver {
 		ctx.clear( color == null ? 0 : color.r, color == null ? 0 : color.g, color == null ? 0 : color.b, color == null ? 1 : color.a, depth == null ? 1 : depth, stencil == null ? 0 : stencil, mask);
 	}
 
-	override function setCapture( bmp : hxd.BitmapData, onCapture : Void -> Void ) {
-		capture = { bmp : bmp, callb : onCapture };
+	override function captureRenderBuffer( bmp : hxd.BitmapData ) {
+		ctx.drawToBitmapData(bmp.toNative());
 	}
 
 	override function dispose() {
@@ -201,14 +200,6 @@ class Stage3dDriver extends Driver {
 	}
 
 	override function present() {
-		if( capture != null ) {
-			ctx.drawToBitmapData(capture.bmp.toNative());
-			ctx.present();
-			var callb = capture.callb;
-			capture = null;
-			callb();
-			return;
-		}
 		ctx.present();
 	}
 

+ 31 - 0
h3d/mat/Texture.hx

@@ -175,6 +175,37 @@ class Texture {
 		}
 	}
 
+	/**
+		Downloads the current texture data from the GPU. On some platforms, color might be premultiplied by Alpha.
+		Beware, this is a very slow operation that shouldn't be done during rendering.
+	**/
+	public function captureBitmap() {
+		var e = h3d.Engine.getCurrent();
+		var oldW = e.width, oldH = e.height;
+		var oldF = filter, oldM = mipMap, oldWrap = wrap;
+		if( e.width < width || e.height < height )
+			e.resize(width, height);
+		e.driver.clear(new h3d.Vector(0, 0, 0, 0),1,0);
+		var s2d = new h2d.Scene();
+		new h2d.Bitmap(h2d.Tile.fromTexture(this), s2d);
+
+		mipMap = None;
+
+		s2d.render(e);
+
+		filter = oldF;
+		mipMap = oldM;
+		wrap = oldWrap;
+
+		var bmp = new hxd.BitmapData(width, height);
+		e.driver.captureRenderBuffer(bmp);
+		if( e.width != oldW || e.height != oldH )
+			e.resize(oldW, oldH);
+		e.driver.clear(new h3d.Vector(0, 0, 0, 0));
+		s2d.dispose();
+		return bmp;
+	}
+
 	public static function fromBitmap( bmp : hxd.BitmapData, ?allocPos : h3d.impl.AllocPos ) {
 		var t = new Texture(bmp.width, bmp.height, allocPos);
 		t.uploadBitmap(bmp);