浏览代码

added CachedBitmap alphaMap + tile scroll

ncannasse 12 年之前
父节点
当前提交
05034184f7
共有 3 个文件被更改,包括 34 次插入4 次删除
  1. 14 3
      h2d/CachedBitmap.hx
  2. 10 0
      h2d/Tile.hx
  3. 10 1
      h2d/Tools.hx

+ 14 - 3
h2d/CachedBitmap.hx

@@ -9,6 +9,7 @@ class CachedBitmap extends Sprite {
 	public var colorMatrix : Null<h3d.Matrix>;
 	public var colorAdd : Null<h3d.Color>;
 	public var skew : Float;
+	public var alphaMap : Tile;
 	
 	var renderDone : Bool;
 	var realWidth : Int;
@@ -49,7 +50,7 @@ class CachedBitmap extends Sprite {
 	}
 
 	override function draw( engine : h3d.Engine ) {
-		if( colorMatrix == null && colorAdd == null && skew == 0. ) {
+		if( colorMatrix == null && colorAdd == null && skew == 0. && alphaMap == null ) {
 			Tools.drawTile(engine, this, tile, new h3d.Color(1, 1, 1, 1), blendMode);
 			return;
 		}
@@ -72,8 +73,8 @@ class CachedBitmap extends Sprite {
 		tmp.z = absY + tile.dx * matB + tile.dy * matD;
 		b.shader.mat2 = tmp;
 		var tmp = core.tmpUVScale;
-		tmp.x = tile.u2 - tile.u;
-		tmp.y = tile.v2 - tile.v;
+		tmp.x = tile.u2;
+		tmp.y = tile.v2;
 		b.shader.uvScale = tmp;
 		b.shader.mcolor = colorMatrix == null ? h3d.Matrix.I() : colorMatrix;
 		var tmp = core.tmpColor;
@@ -92,6 +93,16 @@ class CachedBitmap extends Sprite {
 		b.shader.skew = skew;
 		
 		b.shader.tex = tile.getTexture();
+		
+		if( alphaMap != null ) {
+			b.shader.hasAlphaMap = true;
+			b.shader.alphaMap = alphaMap.getTexture();
+			b.shader.alphaUV = new h3d.Vector(alphaMap.u, alphaMap.v, (alphaMap.u2 - alphaMap.u) / tile.u2, (alphaMap.v2 - alphaMap.v) / tile.v2);
+		} else {
+			b.shader.hasAlphaMap = false;
+			b.shader.alphaMap = null;
+		}
+			
 		b.render(engine);
 	}
 	

+ 10 - 0
h2d/Tile.hx

@@ -54,6 +54,16 @@ class Tile {
 		return new Tile(innerTex, this.x + x, this.y + y, w, h, dx, dy);
 	}
 	
+	public function scrollDiscrete( dx : Float, dy : Float ) {
+		var tex = innerTex;
+		u += dx / tex.width;
+		v -= dy / tex.height;
+		u2 += dx / tex.width;
+		v2 -= dy / tex.height;
+		x = Std.int(u * tex.width);
+		v = Std.int(v * tex.height);
+	}
+	
 	public function dispose() {
 		if( innerTex != null ) innerTex.dispose();
 		innerTex = null;

+ 10 - 1
h2d/Tools.hx

@@ -38,8 +38,17 @@ private class CachedBitmapShader extends h3d.Shader {
 			out = tmp;
 			tuv = pos * uvScale;
 		}
+		
+		var hasAlphaMap : Bool;
+		var alphaMap : Texture;
+		var alphaUV : Float4;
+		
 		function fragment( tex : Texture, mcolor : M44, acolor : Float4 ) {
-			out = tex.get(tuv, nearest) * mcolor + acolor;
+			var c = tex.get(tuv, nearest);
+			if( hasAlphaMap ) c.a *= alphaMap.get(tuv * alphaUV.zw + alphaUV.xy).r;
+			if( mcolor != null ) c *= mcolor;
+			if( acolor != null ) c += acolor;
+			out = c;
 		}
 	}
 }