Geometry Fading Question

I would like to have an effect where a Textured Geometry fades to invisible over a period of a couple of seconds when it collides with another object. I found some old posts about FadeInOut and a FadeInOutController, but I don’t think that exists anymore (at least I couldn’t find it).



I actually have a similar fade out effect on a BitmapText, but that was just adjusting the alpha value on the text color over a period of time using a control attached to the BitmapText. For my Geometry, I’m using textures and I don’t think adjusting the alpha portion of the color would do anything.



Would I have to use a shader to fade out a texture based geometry by adjusting the alpha value in the fragment shader? If yes, is there one already done? If no, can anyone explain how I might accomplish such an effect?

I think i’ve done it before, by making an alpha map cover the whole texture and just adjust the alpha value. Was a while ago tho :stuck_out_tongue:

https://wiki.jmonkeyengine.org/legacy/doku.php/jme3:intermediate:how_to_use_materials?s[]=transparent#optional_transparent



If you want to do the shader route, perhaps @thetoucher can help

1 Like

I would much rather not do a shader purely because I have never done one before and quite frankly, they scare me :). The alphamap method seems like it is what I was looking for. Thanks for the feedback. Maybe I’ll do the alphamap first and then try to do a shader version to “get in the game” so to speak.



thanks again.

Um… that is the shader route :slight_smile:



If you are not going to adjust this manually (i.e. the time it takes to fade will never change), I would set up the shader and pass in the global uniforms Time and TPF. Have the shader do the fade so your not constantly changing the uniform for something that is… well… constant :wink:





It’s my understanding that global (world) params are handled differently than user defined… thought I could be wrong… usually am.

1 Like

Just change the alpha of the color… it can work the same as BitmapText.



Are you using Lighting.j3md or Unshaded.j3md?

1 Like

I using Unshaded.j3md. I’m on Android and Lighting drags down the performance quite a bit on Android.



After thinking about that (using Unshaded), I realized that I was modifying the DiffuseColor instead of Color. Also, I was not setting the blend mode to alpha. I now have it working with the following.



Thanks for your help!



[java]

public class BlockFadeControl extends AbstractControl {



float curAlpha = 1.0f; //Where 1.0f is 100% opaque and 0.0f is 100% transparent

float curTime = 0.0f;

float maxTime = 5.0f;

float delayTime = 3.0f;



public BlockFadeControl() {

super();

enabled = false;

}



@Override

protected void controlUpdate(float tpf) {

curTime += tpf;

if (curTime < delayTime) {

return;

}



ColorRGBA Color = null;

MatParam tmpMatParam = ((Geometry)spatial).getMaterial().getParam(“Color”); // for Unshaded

// MatParam tmpMatParam = ((Geometry)spatial).getMaterial().getParam(“Diffuse”); // for Lighting



if (tmpMatParam == null) {

Logger.getLogger(BlockFadeControl.class.getName()).log(Level.INFO, “Color param is null”);

return;

} else {

Color = ((ColorRGBA)tmpMatParam.getValue());

}



if (curTime <= maxTime) {

// Logger.getLogger(BlockFadeControl.class.getName()).log(

// Level.INFO, “controlUpdate curTime: {0}, maxTime: {1}, alpha: {2}”,

// new Object[]{curTime, maxTime, curAlpha});



curAlpha = ((maxTime-delayTime)-(curTime-delayTime)) / (maxTime-delayTime);

if (curAlpha > 1) curAlpha = 1f;

if (curAlpha < 0) curAlpha = 0f;



// Logger.getLogger(BlockFadeControl.class.getName()).log(

// Level.INFO, “controlUpdate new alpha: {0}”, curAlpha);



if (Color != null) {

Color.a = curAlpha;

spatial.setQueueBucket(Bucket.Transparent);

((Geometry)spatial).getMaterial().getAdditionalRenderState().setBlendMode(BlendMode.Alpha);

((Geometry)spatial).getMaterial().setColor(“Color”, Color);

}



} else {



if (Color != null) {

Color.a = 0;

((Geometry)spatial).getMaterial().setColor(“Color”, Color);

}



if (spatial.getControl(RigidBodyControl.class) instanceof RigidBodyControl) {

RigidBodyControl phy = spatial.getControl(RigidBodyControl.class);

if (phy != null) {

phy.getPhysicsSpace().removeAll(spatial);

spatial.removeFromParent();

}

}



setEnabled(false);

spatial.removeControl(this);

}

}





@Override

protected void controlRender(RenderManager rm, ViewPort vp) {

}



public Control cloneForSpatial(Spatial sptl) {

BlockFadeControl control = new BlockFadeControl();

control.setEnabled(false);

return control;

}



}



[/java]

3 Likes