Browse Source

Better premultiplied alpha support.

NathanSweet 12 năm trước cách đây
mục cha
commit
99bafd2971

+ 14 - 5
spine-libgdx/src/com/esotericsoftware/spine/SkeletonRenderer.java

@@ -9,16 +9,22 @@ import com.badlogic.gdx.graphics.g2d.SpriteBatch;
 import com.badlogic.gdx.utils.Array;
 
 public class SkeletonRenderer {
+	private boolean premultipliedAlpha;
+
 	public void draw (SpriteBatch batch, Skeleton skeleton) {
-		Array<Slot> drawOrder = skeleton.drawOrder;
+		boolean premultipliedAlpha = this.premultipliedAlpha;
+		int srcFunc = premultipliedAlpha ? GL11.GL_ONE : GL11.GL_SRC_ALPHA;
+		batch.setBlendFunction(srcFunc, GL11.GL_ONE_MINUS_SRC_ALPHA);
+
 		boolean additive = false;
-		int srcFunc = batch.getBlendSrcFunc();
+
+		Array<Slot> drawOrder = skeleton.drawOrder;
 		for (int i = 0, n = drawOrder.size; i < n; i++) {
 			Slot slot = drawOrder.get(i);
 			Attachment attachment = slot.attachment;
 			if (attachment instanceof RegionAttachment) {
 				RegionAttachment regionAttachment = (RegionAttachment)attachment;
-				regionAttachment.updateVertices(slot);
+				regionAttachment.updateVertices(slot, premultipliedAlpha);
 				float[] vertices = regionAttachment.getVertices();
 				if (slot.data.getAdditiveBlending() != additive) {
 					additive = !additive;
@@ -27,9 +33,12 @@ public class SkeletonRenderer {
 					else
 						batch.setBlendFunction(srcFunc, GL11.GL_ONE_MINUS_SRC_ALPHA);
 				}
-				batch.draw(regionAttachment.getRegion().getTexture(), vertices, 0, vertices.length);
+				batch.draw(regionAttachment.getRegion().getTexture(), vertices, 0, 20);
 			}
 		}
-		if (additive) batch.setBlendFunction(srcFunc, GL11.GL_ONE_MINUS_SRC_ALPHA);
+	}
+
+	public void setPremultipliedAlpha (boolean premultipliedAlpha) {
+		this.premultipliedAlpha = premultipliedAlpha;
 	}
 }

+ 16 - 6
spine-libgdx/src/com/esotericsoftware/spine/attachments/RegionAttachment.java

@@ -130,15 +130,25 @@ public class RegionAttachment extends Attachment {
 		return region;
 	}
 
-	public void updateVertices (Slot slot) {
+	public void updateVertices (Slot slot, boolean premultipliedAlpha) {
 		Skeleton skeleton = slot.getSkeleton();
 		Color skeletonColor = skeleton.getColor();
 		Color slotColor = slot.getColor();
-		float color = NumberUtils.intToFloatColor( //
-			((int)(255 * skeletonColor.a * slotColor.a) << 24) //
-				| ((int)(255 * skeletonColor.b * slotColor.b) << 16) //
-				| ((int)(255 * skeletonColor.g * slotColor.g) << 8) //
-				| ((int)(255 * skeletonColor.r * slotColor.r)));
+		float color;
+		if (premultipliedAlpha) {
+			float a = 255 * skeletonColor.a * slotColor.a;
+			color = NumberUtils.intToFloatColor( //
+				((int)a << 24) //
+					| ((int)(a * skeletonColor.b * slotColor.b) << 16) //
+					| ((int)(a * skeletonColor.g * slotColor.g) << 8) //
+					| ((int)(a * skeletonColor.r * slotColor.r)));
+		} else {
+			color = NumberUtils.intToFloatColor( //
+				((int)(255 * skeletonColor.a * slotColor.a) << 24) //
+					| ((int)(255 * skeletonColor.b * slotColor.b) << 16) //
+					| ((int)(255 * skeletonColor.g * slotColor.g) << 8) //
+					| ((int)(255 * skeletonColor.r * slotColor.r)));
+		}
 		float[] vertices = this.vertices;
 		vertices[C1] = color;
 		vertices[C2] = color;