Particle Color/Size Easing

Hey guys,



Very simple contribution but enhances the look for particles. You simply implement the ‘Easer’ interface to handle the change in color and size of particles.



It can be used like so:



[java]

// QuadIn easing port from ‘Robert Penner's Easing Functions

particleemitter.setColorEaser( new Easer() {

@Override

public float Ease(float from, float to, float b) {

return (to-from)*(b/=2)*b + from;

}

})

[/java]



Here is the patch:



[patch]Index: core/com/jme3/effect/Easer.java

===================================================================

— core/com/jme3/effect/Easer.java (revision 0)

+++ core/com/jme3/effect/Easer.java (revision 0)

@@ -0,0 +1,15 @@

+package com.jme3.effect;

+

+public interface Easer {

+

  • public float Ease( float from, float to, float b );

    +
  • public static Easer DefaultEaser = new Easer() {

    +
  •   @Override<br />
    
  •   public float Ease(float from, float to, float b)<br />
    
  •   {<br />
    
  •   	return (to - from) * b + from;<br />
    
  •   }<br />
    
  • };

    +}

    No newline at end of file

    Index: core/com/jme3/effect/ParticleEmitter.java

    ===================================================================

    — core/com/jme3/effect/ParticleEmitter.java (revision 7820)

    +++ core/com/jme3/effect/ParticleEmitter.java (working copy)

    @@ -54,6 +54,7 @@

    import com.jme3.scene.Spatial;

    import com.jme3.scene.control.Control;

    import com.jme3.util.TempVars;

    +

    import java.io.IOException;



    /**

    @@ -106,6 +107,9 @@

    //variable that helps with computations

    private transient Vector3f temp = new Vector3f();


  • private Easer colorEaser = Easer.DefaultEaser;
  • private Easer sizeEaser = Easer.DefaultEaser;

    +

    private class ParticleEmitterControl implements Control {



    public Control cloneForSpatial(Spatial spatial) {

    @@ -202,6 +206,26 @@

    }



    /**
  • * Set the {@link Easer} to ease the color of the particle.<br />
    
  • *<br />
    
  • * @param easer the {@link Easer} to use<br />
    
  • */<br />
    
  • public void setColorEaser( Easer easer )
  • {
  •   this.colorEaser = easer;<br />
    
  • }

    +
  • /**
  • * Set the {@link Easer} to ease the size of the particle.<br />
    
  • *<br />
    
  • * @param easer the {@link Easer} to use<br />
    
  • */<br />
    
  • public void setSizeEaser( Easer easer )
  • {
  •   this.sizeEaser = easer;<br />
    
  • }

    +
  • /**
  • Set the {@link ParticleInfluencer} to influence this particle emitter.

    *
  • @param particleInfluencer the {@link ParticleInfluencer} to influence

    @@ -936,8 +960,16 @@



    // affecting color, size and angle

    float b = (p.startlife - p.life) / p.startlife;
  •        p.color.interpolate(startColor, endColor, b);<br />
    
  •        p.size = FastMath.interpolateLinear(b, startSize, endSize);<br />
    
  •        //p.color.interpolate(startColor, endColor, b);<br />
    
  •        //p.size = FastMath.interpolateLinear(b, startSize, endSize);<br />
    

+

  •        p.color.r = this.colorEaser.Ease( startColor.r, endColor.r, b );<br />
    
  •        p.color.g = this.colorEaser.Ease( startColor.g, endColor.g, b );<br />
    
  •        p.color.b = this.colorEaser.Ease( startColor.b, endColor.b, b );<br />
    
  •        p.color.a = this.colorEaser.Ease( startColor.a, endColor.a, b );<br />
    

+

  •        p.size = this.sizeEaser.Ease( startSize, endSize, b );<br />
    

+

p.angle += p.rotateSpeed * tpf;



// Computing bounding volume

[/patch]

1 Like