From 99bafd29716ffda62eb3efc8e3b343070afb9d4f Mon Sep 17 00:00:00 2001 From: NathanSweet Date: Sun, 4 Aug 2013 17:51:37 +0200 Subject: [PATCH] Better premultiplied alpha support. --- .../spine/SkeletonRenderer.java | 19 +++++++++++----- .../spine/attachments/RegionAttachment.java | 22 ++++++++++++++----- 2 files changed, 30 insertions(+), 11 deletions(-) diff --git a/spine-libgdx/src/com/esotericsoftware/spine/SkeletonRenderer.java b/spine-libgdx/src/com/esotericsoftware/spine/SkeletonRenderer.java index 187ad31a5..d3210dd6e 100644 --- a/spine-libgdx/src/com/esotericsoftware/spine/SkeletonRenderer.java +++ b/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 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 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; } } diff --git a/spine-libgdx/src/com/esotericsoftware/spine/attachments/RegionAttachment.java b/spine-libgdx/src/com/esotericsoftware/spine/attachments/RegionAttachment.java index 0eee4ddb0..0c3c8e4ac 100644 --- a/spine-libgdx/src/com/esotericsoftware/spine/attachments/RegionAttachment.java +++ b/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;