浏览代码

added Object.drawToTextures with MRT support

Nicolas Cannasse 5 年之前
父节点
当前提交
57a11f1669
共有 3 个文件被更改,包括 31 次插入7 次删除
  1. 15 1
      h2d/Object.hx
  2. 8 0
      h2d/RenderContext.hx
  3. 8 6
      h2d/Scene.hx

+ 15 - 1
h2d/Object.hx

@@ -443,7 +443,21 @@ class Object #if (domkit && !domkit_heaps) implements domkit.Model<h2d.Object> #
 		var s = getScene();
 		var needDispose = s == null;
 		if( s == null ) s = new h2d.Scene();
-		@:privateAccess s.drawImplTo(this, t);
+		@:privateAccess s.drawImplTo(this, [t]);
+		if( needDispose ) {
+			s.dispose();
+			onRemove();
+		}
+	}
+
+	/**
+		Draw the object and all its children into the given Textures
+	**/
+	public function drawToTextures( texs : Array<h3d.mat.Texture>, outputs : Array<hxsl.Output> ) {
+		var s = getScene();
+		var needDispose = s == null;
+		if( s == null ) s = new h2d.Scene();
+		@:privateAccess s.drawImplTo(this, texs, outputs);
 		if( needDispose ) {
 			s.dispose();
 			onRemove();

+ 8 - 0
h2d/RenderContext.hx

@@ -182,6 +182,14 @@ class RenderContext extends h3d.impl.RenderContext {
 		if( hasRenderZone ) clearRenderZone();
 	}
 
+	public function pushTargets( texs : Array<h3d.mat.Texture> ) {
+		pushTarget(texs[0]);
+		if( texs.length > 1 ) {
+			engine.popTarget();
+			engine.pushTargets(texs);
+		}
+	}
+
 	public function popTarget( restore = true ) {
 		flush();
 		if( targetsStackIndex <= 0 ) throw "Too many popTarget()";

+ 8 - 6
h2d/Scene.hx

@@ -643,20 +643,22 @@ class Scene extends Layers implements h3d.IDrawable implements hxd.SceneEvents.I
 		ctx.elapsedTime = v;
 	}
 
-	function drawImplTo( s : Object, t : h3d.mat.Texture ) {
-
-		if( !t.flags.has(Target) ) throw "Can only draw to texture created with Target flag";
-		var needClear = !t.flags.has(WasCleared);
+	function drawImplTo( s : Object, texs : Array<h3d.mat.Texture>, ?outputs : Array<hxsl.Output> ) {
+		for( t in texs )
+			if( !t.flags.has(Target) )
+				throw "Can only draw to texture created with Target flag";
 		ctx.engine = h3d.Engine.getCurrent();
 		var oldBG = ctx.engine.backgroundColor;
 		ctx.engine.backgroundColor = null; // prevent clear bg
 		ctx.engine.begin();
 		ctx.globalAlpha = alpha;
 		ctx.begin();
-		ctx.pushTarget(t);
-		if( needClear )
+		ctx.pushTargets(texs);
+		if( outputs != null ) @:privateAccess ctx.manager.setOutput(outputs);
+		if( texs.length == 1 && !texs[0].flags.has(WasCleared) )
 			ctx.engine.clear(0);
 		s.drawRec(ctx);
+		if( outputs != null ) @:privateAccess ctx.manager.setOutput();
 		ctx.popTarget();
 		ctx.engine.backgroundColor = oldBG;
 	}