Skip to content

Commit 3f692e5

Browse files
authored
jme3-examples: TestAmbient - test code optimization
1 parent 2fa2101 commit 3f692e5

File tree

1 file changed

+60
-40
lines changed

1 file changed

+60
-40
lines changed
Lines changed: 60 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2009-2021 jMonkeyEngine
2+
* Copyright (c) 2009-2025 jMonkeyEngine
33
* All rights reserved.
44
*
55
* Redistribution and use in source and binary forms, with or without
@@ -39,50 +39,70 @@
3939
import com.jme3.math.ColorRGBA;
4040
import com.jme3.math.Vector3f;
4141
import com.jme3.scene.Geometry;
42-
import com.jme3.scene.shape.Box;
42+
import com.jme3.scene.Mesh;
43+
import com.jme3.scene.debug.Grid;
44+
import com.jme3.scene.shape.Sphere;
4345

4446
public class TestAmbient extends SimpleApplication {
4547

46-
public static void main(String[] args) {
47-
TestAmbient test = new TestAmbient();
48-
test.start();
49-
}
48+
public static void main(String[] args) {
49+
TestAmbient test = new TestAmbient();
50+
test.start();
51+
}
5052

51-
@Override
52-
public void simpleInitApp() {
53-
float[] eax = new float[]{15, 38.0f, 0.300f, -1000, -3300, 0,
54-
1.49f, 0.54f, 1.00f, -2560, 0.162f, 0.00f, 0.00f,
55-
0.00f, -229, 0.088f, 0.00f, 0.00f, 0.00f, 0.125f, 1.000f,
56-
0.250f, 0.000f, -5.0f, 5000.0f, 250.0f, 0.00f, 0x3f};
57-
Environment env = new Environment(eax);
58-
audioRenderer.setEnvironment(env);
53+
private final float[] eax = {
54+
15, 38.0f, 0.300f, -1000, -3300, 0,
55+
1.49f, 0.54f, 1.00f, -2560, 0.162f, 0.00f, 0.00f,
56+
0.00f, -229, 0.088f, 0.00f, 0.00f, 0.00f, 0.125f, 1.000f,
57+
0.250f, 0.000f, -5.0f, 5000.0f, 250.0f, 0.00f, 0x3f
58+
};
5959

60-
AudioNode waves = new AudioNode(assetManager,
61-
"Sound/Environment/Ocean Waves.ogg", DataType.Buffer);
62-
waves.setPositional(true);
63-
waves.setLocalTranslation(new Vector3f(0, 0,0));
64-
waves.setMaxDistance(100);
65-
waves.setRefDistance(5);
60+
@Override
61+
public void simpleInitApp() {
62+
configureCamera();
6663

67-
AudioNode nature = new AudioNode(assetManager,
68-
"Sound/Environment/Nature.ogg", DataType.Stream);
69-
nature.setPositional(false);
70-
nature.setVolume(3);
71-
72-
waves.playInstance();
73-
nature.play();
74-
75-
// just a blue box to mark the spot
76-
Box box1 = new Box(.5f, .5f, .5f);
77-
Geometry player = new Geometry("Player", box1);
78-
Material mat1 = new Material(assetManager,
79-
"Common/MatDefs/Misc/Unshaded.j3md");
80-
mat1.setColor("Color", ColorRGBA.Blue);
81-
player.setMaterial(mat1);
82-
rootNode.attachChild(player);
83-
}
64+
Environment env = new Environment(eax);
65+
audioRenderer.setEnvironment(env);
8466

85-
@Override
86-
public void simpleUpdate(float tpf) {
87-
}
67+
AudioNode waves = new AudioNode(assetManager,
68+
"Sound/Environment/Ocean Waves.ogg", DataType.Buffer);
69+
waves.setPositional(true);
70+
waves.setLooping(true);
71+
waves.setReverbEnabled(true);
72+
rootNode.attachChild(waves);
73+
74+
AudioNode nature = new AudioNode(assetManager,
75+
"Sound/Environment/Nature.ogg", DataType.Stream);
76+
nature.setPositional(false);
77+
nature.setLooping(true);
78+
nature.setVolume(3);
79+
rootNode.attachChild(nature);
80+
81+
waves.play();
82+
nature.play();
83+
84+
// just a blue sphere to mark the spot
85+
Geometry marker = makeShape("Marker", new Sphere(16, 16, 1f), ColorRGBA.Blue);
86+
waves.attachChild(marker);
87+
88+
Geometry grid = makeShape("DebugGrid", new Grid(21, 21, 2), ColorRGBA.Gray);
89+
grid.center().move(0, 0, 0);
90+
rootNode.attachChild(grid);
91+
}
92+
93+
private void configureCamera() {
94+
flyCam.setMoveSpeed(25f);
95+
flyCam.setDragToRotate(true);
96+
97+
cam.setLocation(Vector3f.UNIT_XYZ.mult(5f));
98+
cam.lookAt(Vector3f.ZERO, Vector3f.UNIT_Y);
99+
}
100+
101+
private Geometry makeShape(String name, Mesh mesh, ColorRGBA color) {
102+
Geometry geo = new Geometry(name, mesh);
103+
Material mat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
104+
mat.setColor("Color", color);
105+
geo.setMaterial(mat);
106+
return geo;
107+
}
88108
}

0 commit comments

Comments
 (0)