[libgdx] Option to reset blend function.

This commit is contained in:
NathanSweet 2018-04-15 18:08:02 +02:00
parent 643a1cb2aa
commit 4e5e82502b
2 changed files with 39 additions and 5 deletions

View File

@ -30,18 +30,20 @@
package com.esotericsoftware.spine.utils;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.g2d.Batch;
import com.badlogic.gdx.scenes.scene2d.Actor;
import com.esotericsoftware.spine.AnimationState;
import com.esotericsoftware.spine.Skeleton;
import com.esotericsoftware.spine.SkeletonRenderer;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.g2d.Batch;
import com.badlogic.gdx.scenes.scene2d.Actor;
/** A scene2d actor that draws a skeleton. */
public class SkeletonActor extends Actor {
private SkeletonRenderer renderer;
private Skeleton skeleton;
AnimationState state;
private boolean resetBlendFunction = true;
/** Creates an uninitialized SkeletonActor. The renderer, skeleton, and animation state must be set before use. */
public SkeletonActor () {
@ -61,6 +63,9 @@ public class SkeletonActor extends Actor {
}
public void draw (Batch batch, float parentAlpha) {
int blendSrc = batch.getBlendSrcFunc(), blendDst = batch.getBlendDstFunc();
int blendSrcAlpha = batch.getBlendSrcFuncAlpha(), blendDstAlpha = batch.getBlendDstFuncAlpha();
Color color = skeleton.getColor();
float oldAlpha = color.a;
skeleton.getColor().a *= parentAlpha;
@ -68,6 +73,8 @@ public class SkeletonActor extends Actor {
skeleton.setPosition(getX(), getY());
renderer.draw(batch, skeleton);
if (resetBlendFunction) batch.setBlendFunctionSeparate(blendSrc, blendDst, blendSrcAlpha, blendDstAlpha);
color.a = oldAlpha;
}
@ -94,4 +101,14 @@ public class SkeletonActor extends Actor {
public void setAnimationState (AnimationState state) {
this.state = state;
}
public boolean getResetBlendFunction () {
return resetBlendFunction;
}
/** If false, the blend function will be left as whatever {@link SkeletonRenderer#draw(Batch, Skeleton)} set. This can reduce
* batch flushes in some cases, but means other rendering may need to first set the blend function. Default is true. */
public void setResetBlendFunction (boolean resetBlendFunction) {
this.resetBlendFunction = resetBlendFunction;
}
}

View File

@ -30,18 +30,20 @@
package com.esotericsoftware.spine.utils;
import com.badlogic.gdx.graphics.g2d.Batch;
import com.badlogic.gdx.scenes.scene2d.utils.BaseDrawable;
import com.esotericsoftware.spine.AnimationState;
import com.esotericsoftware.spine.Skeleton;
import com.esotericsoftware.spine.SkeletonRenderer;
import com.badlogic.gdx.graphics.g2d.Batch;
import com.badlogic.gdx.scenes.scene2d.utils.BaseDrawable;
/** A scene2d drawable that draws a skeleton. The animation state and skeleton must be updated each frame, or
* {@link #update(float)} called each frame. */
public class SkeletonDrawable extends BaseDrawable {
private SkeletonRenderer renderer;
private Skeleton skeleton;
AnimationState state;
private boolean resetBlendFunction = true;
/** Creates an uninitialized SkeletonDrawable. The renderer, skeleton, and animation state must be set before use. */
public SkeletonDrawable () {
@ -59,9 +61,14 @@ public class SkeletonDrawable extends BaseDrawable {
}
public void draw (Batch batch, float x, float y, float width, float height) {
int blendSrc = batch.getBlendSrcFunc(), blendDst = batch.getBlendDstFunc();
int blendSrcAlpha = batch.getBlendSrcFuncAlpha(), blendDstAlpha = batch.getBlendDstFuncAlpha();
skeleton.setPosition(x, y);
skeleton.updateWorldTransform();
renderer.draw(batch, skeleton);
if (resetBlendFunction) batch.setBlendFunctionSeparate(blendSrc, blendDst, blendSrcAlpha, blendDstAlpha);
}
public SkeletonRenderer getRenderer () {
@ -87,4 +94,14 @@ public class SkeletonDrawable extends BaseDrawable {
public void setAnimationState (AnimationState state) {
this.state = state;
}
public boolean getResetBlendFunction () {
return resetBlendFunction;
}
/** If false, the blend function will be left as whatever {@link SkeletonRenderer#draw(Batch, Skeleton)} set. This can reduce
* batch flushes in some cases, but means other rendering may need to first set the blend function. Default is true. */
public void setResetBlendFunction (boolean resetBlendFunction) {
this.resetBlendFunction = resetBlendFunction;
}
}