浏览代码

added filter.blendMode

ncannasse 6 年之前
父节点
当前提交
1cc054ae5a
共有 3 个文件被更改,包括 27 次插入1 次删除
  1. 7 0
      h2d/Drawable.hx
  2. 12 1
      h2d/Object.hx
  3. 8 0
      h2d/filter/Filter.hx

+ 7 - 0
h2d/Drawable.hx

@@ -190,6 +190,13 @@ class Drawable extends Object {
 		return false;
 	}
 
+	override function emitFilterTile( ctx, tile ) {
+		var oldBlend = blendMode;
+		if( filter.blendMode != null ) blendMode = filter.blendMode;
+		emitTile(ctx, tile);
+		if( filter.blendMode != null ) blendMode = oldBlend;
+	}
+
 	override function emitTile( ctx : RenderContext, tile : Tile ) {
 		if( tile == null )
 			tile = new Tile(null, 0, 0, 5, 5);

+ 12 - 1
h2d/Object.hx

@@ -726,11 +726,22 @@ class Object {
 			return;
 
 		ctx.globalAlpha = oldAlpha * alpha;
-		emitTile(ctx, finalTile);
+		emitFilterTile(ctx, finalTile);
 		ctx.globalAlpha = oldAlpha;
 		ctx.flush();
 	}
 
+	function emitFilterTile( ctx, tile ) {
+		if( filter.blendMode != null ) {
+			if( nullDrawable == null )
+				nullDrawable = @:privateAccess new h2d.Drawable(null);
+			nullDrawable.blendMode = filter.blendMode;
+		}
+		emitTile(ctx, tile);
+		if( filter.blendMode != null )
+			nullDrawable.blendMode = Alpha;
+	}
+
 	function drawRec( ctx : RenderContext ) {
 		if( !visible ) return;
 		// fallback in case the object was added during a sync() event and we somehow didn't update it

+ 8 - 0
h2d/filter/Filter.hx

@@ -1,11 +1,19 @@
 package h2d.filter;
 
+/**
+	The base filter class, you can extend it in order to define your own filters, although ShaderFilter will be the most straightforward way to define simple custom filter.
+**/
 class Filter {
 
 	public var autoBounds = true;
 	public var boundsExtend : Float = 0.;
 	public var smooth = false;
 
+	/**
+		When set, will use this blend mode when merging the filtered output to screen.
+	**/
+	public var blendMode : Null<h2d.BlendMode>;
+
 	function new() {
 	}