Do you remember the beautiful underwater effects that we recently discussed, with waves and sunbeams? You can increase the immersion of an underwater scene by dampening all sounds while the camera is under water.
Make a copy of
WaterTerrainSky.java
and name the classUnderWaterAudio.java
. Remember to also refactor the first line of themain()
method toUnderWaterAudioapp = new UnderWaterAudio();
.Keep all code of the water-terrain-sky simulation.
Add the following class fields for the audio node and the environmental effect:
private AudioNode wavesAudio; private LowPassFilter underWaterAudioFilter = new LowPassFilter(0.5f, 0.1f); private LowPassFilter aboveWaterAudioFilter = new LowPassFilter(1f, 1f);
Create a Boolean class field to track whether the player is above or under the water surface, so you can toggle the audio filter on and off:
private boolean wasUnderwater = false;
Keep the code in the
simpleInitApp()
method, and add the following to initialize the sound...