Growing, fading sphere

Hey there,



I want to create a growing fading Sphere. I have a solution how to create a growing Sphere with keyframes but I have no clue how make it fading …

I didn't find a similar problem in the tutorials or in the forum. My idea was to set the material of the starting and ending sphere, but i only managed to set the material of the morphing sphere. Anyone has an idea?



my code:


        ...
        TriMesh growingSphere = loadGrowingSphere();
        this.attachChild(growingSphere);
        ...
       
    }
   
    private TriMesh loadGrowingSphere(){

         /** Create a ZBuffer to display pixels closest to the camera above farther ones.  */
        ZBufferState buf = DisplaySystem.getDisplaySystem("LWJGL").getRenderer().createZBufferState();
        buf.setEnabled(true);
        buf.setFunction(ZBufferState.TestFunction.LessThanOrEqualTo);

        // StartSphere
        TriMesh startSphere=new Sphere("beginning explosion",position,15,15,0.5f);

        // Null colors,normals,textures because they aren't being updated
        startSphere.setColorBuffer(null);
        startSphere.setNormalBuffer(null);
       
        //startSphere.setRenderState(this.getTextureForSphere("gras.jpg"));
        //startSphere.setRenderState(buf);
        //startSphere.updateRenderState();
        startSphere.setTextureCoords((ArrayList<TexCoords>)null);

       
        // Die Endexplosion
        TriMesh finalSphere=new Sphere("ending explosion",position,15,15,radius);
        finalSphere.setColorBuffer(null);
        finalSphere.setNormalBuffer(null);

        //finalSphere.setRenderState(this.getTextureForSphere("gras.jpg"));
        //finalSphere.setRenderState(buf);
        //finalSphere.updateRenderState();
        finalSphere.setTextureCoords((ArrayList<TexCoords>)null);

        TriMesh renderedObject=new Sphere("Rendered Object",position,15,15,radius);
        this.attachChild(renderedObject);
//
        // Create my KeyframeController
        KeyframeController kc=new KeyframeController();

        // Assign the object I'll be changing
        kc.setMorphingMesh(renderedObject);

        // Assign for a time, what my renderedObject will look like
        kc.setKeyframe(0,startSphere);
        kc.setKeyframe(lifeTimeOfAnimation,finalSphere);
        kc.setRepeatType(Controller.RT_CLAMP);

        //setTexture     
        renderedObject.setRenderState(getTextureForSphere("gras.jpg"));
        renderedObject.setRenderState(buf);
        renderedObject.updateRenderState();

        // Add the controller to my object
        renderedObject.addController(kc);
        return renderedObject;
    }

    private TextureState getTextureForSphere(String name){
        // Point to the monkey image
        URL monkeyLoc = Explosion.class.getClassLoader().getResource(
            "data/textures/"+name);

        // Get my TextureState
        TextureState ts = DisplaySystem.getDisplaySystem("LWJGL").getRenderer().createTextureState();

        // Get my Texture
        Texture t = TextureManager.loadTexture(monkeyLoc,
                        Texture.MinificationFilter.BilinearNearestMipMap,
                        Texture.MagnificationFilter.Bilinear);

        // Set a wrap for my texture so it repeats
        t.setWrap(Texture.WrapMode.Repeat);

        // Set the texture to the TextureState
        ts.setTexture(t);

        return ts;

    }
   




Thanks for your anwers!!

I was never that familiar with Keyframe controllers, but I imagine if you created your own controller extending the controller class, that you could likely do keyframes there and manage some custom code to change the material of the object during it's morph.



You have the right idea though, you need to adjust the alpha of the object's material. Keep in mind the object needs to be in the transparent render queue!



I wrote this a while back, so I don't know how much it will help, but it's a controller that handles the fading in-and-out of an object with no texture, using vertex colors.


public class Controller_PulsingTransparency extends Controller {

   private static final long serialVersionUID = 1L;

   private float minAlpha;
   private float maxAlpha;
   private float currAlpha;
   private boolean isVanishing;
   private float changeStep;
   
   private MaterialState ms;
   
   Controller_PulsingTransparency(float minT, float maxT, float startT, float changeAmt,
         boolean isVanishing, MaterialState ms) {
      minAlpha = minT;
      maxAlpha = maxT;
      currAlpha = startT;
      this.isVanishing = isVanishing;
      this.ms = ms;
      changeStep = changeAmt;
   }
   
   @Override
   public void update(float time) {
      if(isVanishing) {
         currAlpha -= changeStep * time;
      }
      else {
         currAlpha += changeStep * time;
      }
      if(ms.getDiffuse().a >= maxAlpha) {
         ms.setDiffuse(new ColorRGBA(ms.getDiffuse().r, ms.getDiffuse().g, ms.getDiffuse().b, maxAlpha));
         isVanishing = true;
      }
      else if(ms.getDiffuse().a <= minAlpha) {
         ms.setDiffuse(new ColorRGBA(ms.getDiffuse().r, ms.getDiffuse().g, ms.getDiffuse().b, minAlpha));
         isVanishing = false;
      }
      System.out.println(currAlpha);
      ms.setDiffuse(new ColorRGBA(ms.getDiffuse().r, ms.getDiffuse().g, ms.getDiffuse().b, currAlpha));
   }

}