Skip to content

Commit

Permalink
Fix flickering particle opacity when fading.
Browse files Browse the repository at this point in the history
Remove distinction between Particle color alpha and opacity.
  • Loading branch information
nightm4re94 committed Oct 19, 2020
1 parent 156f832 commit 1ca3997
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ public abstract class Particle implements ITimeToLive {

private Collision collisionType;
private Color color;
private float colorAlpha = 1;
private float deltaHeight;
private float deltaWidth;
/**
Expand Down Expand Up @@ -59,8 +58,6 @@ public abstract class Particle implements ITimeToLive {
private RenderType customRenderType;
private boolean useCustomRenderType;

private float opacity;

private boolean fade;

private boolean fadeOnCollision;
Expand All @@ -84,7 +81,6 @@ public Particle(final float width, final float height) {
this.setWidth(width);
this.setHeight(height);
this.collisionType = Collision.NONE;
this.opacity = 1;
this.fade = true;
this.setStopOnCollision(true);
this.setContinuousCollision(false);
Expand Down Expand Up @@ -114,10 +110,6 @@ public Color getColor() {
return this.color;
}

public float getColorAlpha() {
return this.colorAlpha;
}

public float getDeltaHeight() {
return this.deltaHeight;
}
Expand Down Expand Up @@ -162,6 +154,13 @@ public boolean isAntiAliased() {
return this.antiAliasing;
}

public float getOpacity() {
if (this.isFading() && this.getTimeToLive() > 0) {
return MathUtilities.clamp(this.getColor().getAlpha() / 255f - (float) this.getAliveTime() / this.getTimeToLive(), 0, 1);
}
return 1;
}

/**
* Gets the location relative to the specified effect location.
*
Expand Down Expand Up @@ -239,24 +238,10 @@ public void setStopOnCollision(boolean stopOnCollision) {
public Particle setColor(final Color color) {
if (color != null) {
this.color = color;
this.setColorAlpha((float) this.color.getAlpha() / 255);
}
return this;
}

/**
* Sets the color alpha. A float value between 0 and 1 is expected.
*
* @param colorAlpha
* the new color alpha
*
* @return This {@link Particle} instance to chain further setter calls.
*/
public Particle setColorAlpha(final float colorAlpha) {
this.colorAlpha = MathUtilities.clamp(colorAlpha, 0, 1);
return this;
}

public Particle setDeltaHeight(final float deltaHeight) {
this.deltaHeight = deltaHeight;
return this;
Expand Down Expand Up @@ -399,11 +384,6 @@ public void update(final Point2D emitterOrigin, final float updateRatio) {
if (this.timeToLiveReached()) {
return;
}
final int alpha = (int) (this.getOpacity() * 255);

if (this.color != null) {
this.setColor(new Color(this.getColor().getRed(), this.getColor().getGreen(), this.getColor().getBlue(), MathUtilities.clamp(alpha, 0, 255)));
}

if (this.colliding) {
return;
Expand Down Expand Up @@ -493,14 +473,6 @@ protected float getAbsoluteY(Point2D emitterOrigin) {
return (float) (emitterOrigin.getY() + this.getY() - this.getHeight() / 2.0);
}

protected float getOpacity() {
if (this.isFading() && this.getTimeToLive() > 0) {
this.opacity = MathUtilities.clamp(1 - this.getColorAlpha() - (float) this.getAliveTime() / this.getTimeToLive(), 0, 1);
return this.opacity;
}
return 1;
}

public boolean usesCustomRenderType() {
return this.useCustomRenderType;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package de.gurkenlabs.litiengine.graphics.emitters.particles;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Shape;
import java.awt.geom.Point2D;
Expand All @@ -23,7 +24,8 @@ public Rectangle2D getBoundingBox(Point2D origin) {

@Override
public void render(final Graphics2D g, final Point2D emitterOrigin) {
g.setColor(this.getColor());
g.setColor(new Color(this.getColor().getRed() / 255f, this.getColor().getGreen() / 255f, this.getColor().getBlue() / 255f, this.getOpacity()));

if (this.isOutlineOnly() || this instanceof LineParticle) {
Game.graphics().renderOutline(g, this.getShape(emitterOrigin), new BasicStroke(1.0f / Game.graphics().getBaseRenderScale()), this.isAntiAliased());
} else {
Expand Down

0 comments on commit 1ca3997

Please sign in to comment.