Better premultiplied alpha support.

This commit is contained in:
NathanSweet 2013-08-04 17:51:37 +02:00
parent d0ebe5515e
commit 99bafd2971
2 changed files with 30 additions and 11 deletions

View File

@ -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;
}
}

View File

@ -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;