Hide WaterFilter on trigger

Hello all,



It it possible to hide a water surface (made with WaterFilter like in the example TestPostWater)?



What I’m looking for is to setup a input key to toggle between show and hide the water.

The input part is simple, now I don’t know what command to write on the listener.

Do the inverse of what you do to add the water, like adding the post processor.

This is what I do to add the water:

[java] public void initWater(Vector3f lightDir) {

filterWater = new FilterPostProcessor(assetManager);

final WaterFilter water = new WaterFilter(rootNode, lightDir);

water.setWaterHeight(WATER_HEIGHT);

// Color constantes

ColorRGBA shallowWaterColor = new ColorRGBA(0.0078f, 0.3176f, 0.5f, 1.0f);

ColorRGBA deepWaterColor = new ColorRGBA(0.0039f, 0.00196f, 0.145f, 1.0f);

Vector3f colorExtinction = new Vector3f(5.0f, 20.0f, 30.0f);

// set underwater color

water.setDeepWaterColor(deepWaterColor);

water.setWaterColor(shallowWaterColor);

water.setColorExtinction(colorExtinction); // refraction color extintion

water.setWaterTransparency(0.1f);

// set wave stuff

water.setUseFoam(false);

water.setUseRipples(true);

water.setMaxAmplitude(0.3f);

water.setWaveScale(0.008f);

water.setSpeed(0.7f);

water.setShoreHardness(0.8f); // changed from 0.6 to 0.8 so water integrates better with terrain

water.setRefractionConstant(0.01f); // changed from 0.2 to 0.01 underwater sky woobles more

water.setShininess(0.3f);

water.setSunScale(1.0f);

filterWater.addFilter(water);

viewPort.addProcessor(filterWater);

}[/java]



This is what I tried:

[java]public void toggleWater() {

if (showWater) {

viewPort.removeProcessor(filterWater);

}

else {

viewPort.addProcessor(filterWater);

}

showWater = !showWater;

}[/java]



But I got:

Jun 20, 2012 4:30:14 PM com.jme3.app.Application handleError
SEVERE: Uncaught exception thrown in Thread[LWJGL Renderer Thread,6,main]
java.lang.NullPointerException
at com.jme3.renderer.RenderManager.renderViewPort(RenderManager.java:1097)
at com.jme3.renderer.RenderManager.render(RenderManager.java:1168)
at com.jme3.app.SimpleApplication.update(SimpleApplication.java:254)
at com.jme3.system.lwjgl.LwjglAbstractDisplay.runLoop(LwjglAbstractDisplay.java:149)
at com.jme3.system.lwjgl.LwjglCanvas.runLoop(LwjglCanvas.java:227)
at com.jme3.system.lwjgl.LwjglAbstractDisplay.run(LwjglAbstractDisplay.java:223)
at java.lang.Thread.run(Thread.java:662)


If i do it the other way round (try to add it first on first press) I get this:
Jun 20, 2012 4:31:29 PM com.jme3.app.Application handleError
SEVERE: Uncaught exception thrown in Thread[LWJGL Renderer Thread,6,main]
java.lang.NullPointerException
at com.jme3.renderer.ViewPort.removeProcessor(ViewPort.java:144)
at pt.up.fe.dceg.neptus.plugins.r3d.jme3.TerrainDefinitions.toggleWater(TerrainDefinitions.java:386)
at pt.up.fe.dceg.neptus.plugins.r3d.jme3.ShowDataState$1.onAction(ShowDataState.java:227)
at com.jme3.input.InputManager.invokeActions(InputManager.java:168)
at com.jme3.input.InputManager.onKeyEventQueued(InputManager.java:425)
at com.jme3.input.InputManager.processQueue(InputManager.java:799)
at com.jme3.input.InputManager.update(InputManager.java:851)
at com.jme3.app.Application.update(Application.java:598)
at com.jme3.app.SimpleApplication.update(SimpleApplication.java:233)
at com.jme3.system.lwjgl.LwjglAbstractDisplay.runLoop(LwjglAbstractDisplay.java:149)
at com.jme3.system.lwjgl.LwjglCanvas.runLoop(LwjglCanvas.java:227)
at com.jme3.system.lwjgl.LwjglAbstractDisplay.run(LwjglAbstractDisplay.java:223)
at java.lang.Thread.run(Thread.java:662)


I think this is what you suggested but I'm not sure, can you confirm?

water.setEnabled(false);

1 Like

your filterWater variable is null

This might be one of those cases where some if( null ) throw new IllegalArgumentException code might be warranted. If I get time soon then I will add it.

Done. In the next update there will be a slightly better error message in this case.

1 Like

Thanks!



@nehon that was the method I was missing.



@pspeed thanks for changing this.

Note: Just to clarify, there was still a problem with your code the way you had it, you’d just get a better error message than NullPointerException now.