forked from jMonkeyEngine/jmonkeyengine
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTestScene.java
More file actions
1233 lines (1125 loc) · 38.7 KB
/
TestScene.java
File metadata and controls
1233 lines (1125 loc) · 38.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) 2024 jMonkeyEngine
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* * Neither the name of 'jMonkeyEngine' nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package jme3test;
import com.jme3.app.SimpleApplication;
import com.jme3.asset.AssetManager;
import com.jme3.bullet.BulletAppState;
import com.jme3.bullet.PhysicsSpace;
import com.jme3.bullet.collision.PhysicsCollisionObject;
import com.jme3.bullet.control.RigidBodyControl;
import com.jme3.environment.EnvironmentProbeControl;
import com.jme3.input.FlyByCamera;
import com.jme3.light.AmbientLight;
import com.jme3.light.DirectionalLight;
import com.jme3.light.LightProbe;
import com.jme3.material.Material;
import com.jme3.math.ColorRGBA;
import com.jme3.math.FastMath;
import com.jme3.math.Vector3f;
import com.jme3.post.FilterPostProcessor;
import com.jme3.post.filters.BloomFilter;
import com.jme3.post.filters.BloomFilter.GlowMode;
import com.jme3.post.ssao.SSAOFilter;
import com.jme3.renderer.Camera;
import com.jme3.renderer.Limits;
import com.jme3.renderer.Renderer;
import com.jme3.renderer.ViewPort;
import com.jme3.renderer.queue.RenderQueue;
import com.jme3.scene.Geometry;
import com.jme3.scene.Node;
import com.jme3.scene.SceneGraphIterator;
import com.jme3.scene.Spatial;
import com.jme3.scene.plugins.gltf.GltfModelKey;
import com.jme3.scene.shape.RectangleMesh;
import com.jme3.shadow.AbstractShadowFilter;
import com.jme3.shadow.AbstractShadowRenderer;
import com.jme3.shadow.CompareMode;
import com.jme3.shadow.DirectionalLightShadowFilter;
import com.jme3.shadow.DirectionalLightShadowRenderer;
import com.jme3.texture.Texture;
import com.jme3.util.SkyFactory;
import java.util.LinkedList;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
* Loads a configurable PBR test scene.
*
* @author codex
*/
public class TestScene extends Node {
/**
* Shadow modes used in the scene.
*/
public static enum Shadows {
/**
* No shadows.
*/
None,
/**
* Use an {@link AbstractShadowRenderer} to render shadows.
*/
Renderer,
/**
* Use an {@link AbstractShadowFilter} to render shadows.
*/
Filter;
}
/**
* Bloom modes used in the scene.
*/
public static enum Bloom {
/**
* No bloom.
*/
None(null),
/**
* Use {@link SoftBloomFilter} to add a glow effect to the entire scene.
* <p>
* This setting is currently unsupported.
*/
SoftScene(null),
/**
* Use {@link BloomFilter} to add a glow effect to the entire scene.
* @see GlowMode#Scene
*/
Scene(GlowMode.Scene),
/**
* Use {@link BloomFilter} to add a glow effect to the scene and specific objects.
* @see GlowMode#SceneAndObjects
*/
SceneAndObjects(GlowMode.SceneAndObjects),
/**
* Use {@link BloomFilter} to add a glow effect to specific objects.
* @see GlowMode#Objects
*/
Objects(GlowMode.Objects);
private final GlowMode equivalent;
private Bloom(GlowMode equivalent) {
this.equivalent = equivalent;
}
/**
* Gets the {@link GlowMode} equivalent, or null if none exists.
*
* @return GlowMode equivalent
*/
public GlowMode getEquivalent() {
return equivalent;
}
/**
* Returns true if glow is made using {@link SoftBloomFilter}.
*
* @return
*/
public boolean usesSoftBloom() {
return equivalent == null;
}
}
/**
* Represents a probe created at runtime by the GPU.
*/
public static final String HARDWARE_PROBE = "HARDWARE_LIGHT_PROBE";
/**
* Represents a subscene meant for testing character movement.
*/
public static final String CHARACTER_SUBSCENE = "Scenes/TestScene/character.gltf";
/**
* Represents a subscene meant for testing physics.
*/
public static final String PHYSICS_SUBSCENE = "Scenes/TestScene/physics.gltf";
/**
* Represents a single sky texture used to create a skybox.
*/
public static final String BRIGHT_SKY = "Textures/Sky/Bright/BrightSky.dds";
/**
* Represents 6 sky textures used to create a skybox.
* <p>
* The "multi:" prefix indicates that 6 textures will be used. On load,
* the "$" is replaced by "west", "east", "north", "south", "up", and "down"
* for each of 6 texture loads to create a texture for each orthogonal direction.
*/
public static final String LAGOON_SKY = "multi:Textures/Sky/Lagoon/lagoon_$.jpg";
/**
* Represents grey grid texture.
*/
public static final String GRAY_TEXTURE = "Scenes/TestScene/grid-grey.png";
/**
* Represents grey grid texture.
*/
public static final String BLUE_TEXTURE = "Scenes/TestScene/grid-blue.png";
/**
* Represents grey grid texture.
*/
public static final String RED_TEXTURE = "Scenes/TestScene/grid-red.png";
/**
* Represents grey grid texture.
*/
public static final String GREEN_TEXTURE = "Scenes/TestScene/grid-green.png";
/**
* Represents grey grid texture.
*/
public static final String YELLOW_TEXTURE = "Scenes/TestScene/grid-yellow.png";
/**
* Represents grey grid texture.
*/
public static final String PURPLE_TEXTURE = "Scenes/TestScene/grid-purple.png";
/**
* Represents the size of each tile.
* <p>
* The Y axis parameter is set to zero, as it is not used.
*/
public static final Vector3f TILE_SIZE = new Vector3f(20, 0, 20);
/**
* Default background color for the scene.
* <p>
* Apply this color to the viewport by calling {@link #configureBackgroundColor()}.
*/
public static final ColorRGBA SCENE_BACKGROUND_COLOR = new ColorRGBA(.6f, .7f, 1f, 1f);
private static final String BASE_SCENE = "Scenes/TestScene/base-scene.gltf";
private static final String MULTI_TEXTURE = "multi:";
private static final Logger logger = Logger.getLogger(TestScene.class.getName());
private final AssetManager assetManager;
private final ViewPort viewPort;
private final LinkedList<RigidBodyControl> rigidBodyList = new LinkedList<>();
private boolean loaded = false;
private int width = 3;
private int height = 3;
private boolean sceneGeometry = true;
private boolean walls = true;
private boolean lights = true;
private String subSceneAsset;
private boolean filters = true;
private FilterPostProcessor fpp;
private PhysicsSpace space;
private boolean boundaries = true;
private int shadowRes = 2048;
private int sunSplits = 4;
private Shadows shadows = Shadows.Renderer;
private boolean occlusion = true;
private Bloom bloom = Bloom.None;
private String lightProbeAsset = HARDWARE_PROBE;
private int hardwareProbeRes = 256;
private String skyAsset = null;
private Node subScene;
private Spatial sky;
private DirectionalLight sun;
private DirectionalLight atmosphere;
private AmbientLight ambient;
private DirectionalLightShadowRenderer shadowRenderer;
private DirectionalLightShadowFilter shadowFilter;
private SSAOFilter ssaoFilter;
private BloomFilter bloomFilter;
//private SoftBloomFilter softBloomFilter;
private EnvironmentProbeControl hardwareProbe;
private LightProbe lightProbe;
/**
* Creates a test scene with the specified viewport.
*
* @param assetManager
* @param viewPort
* @see #TestScene(com.jme3.asset.AssetManager, com.jme3.renderer.ViewPort, java.lang.String)
*/
public TestScene(AssetManager assetManager, ViewPort viewPort) {
this(assetManager, viewPort, null);
}
/**
* Creates a test scene with the specified viewport and subscene.
* <p>
* All effects are added to the specified viewport.
*
* @param assetManager
* @param viewPort
* @param subScene
*/
public TestScene(AssetManager assetManager, ViewPort viewPort, String subScene) {
this.assetManager = assetManager;
this.viewPort = viewPort;
this.subSceneAsset = subScene;
}
/**
* Launches a dedicated test application for this class.
*
* @param args application arguments
*/
public static void main(String[] args) {
TestApp.start(args);
}
/**
* Loads the scene.
*
* @return this instance
*/
public TestScene load() {
if (loaded) {
return this;
}
try {
setShadowMode(RenderQueue.ShadowMode.CastAndReceive);
// scene
if (sceneGeometry) {
if (subSceneAsset != null) {
logger.info("Loading subscene.");
loadSubScene(subSceneAsset);
}
logger.info("Loading main scene.");
EnvironmentProbeControl.tagGlobal(loadBaseScene());
if (boundaries) {
logger.info("Loading boundary meshes.");
loadBoundaries();
}
}
if (skyAsset != null) {
loadSky();
}
// lights
if (lights) {
logger.info("Loading lights (3).");
sun = new DirectionalLight();
sun.setDirection(new Vector3f(.5f, -1, .3f));
sun.setColor(ColorRGBA.White);
addLight(sun);
atmosphere = new DirectionalLight();
atmosphere.setDirection(new Vector3f(-1, -1, 1));
atmosphere.setColor(ColorRGBA.White.mult(.2f));
addLight(atmosphere);
ambient = new AmbientLight();
ambient.setColor(ColorRGBA.White.mult(.5f));
addLight(ambient);
}
// filters
if (filters) {
boolean makeFpp = (fpp == null);
if (makeFpp) {
logger.info("No FilterPostProcessor specified. Creating new instance.");
fpp = new FilterPostProcessor(assetManager);
}
if (lights) {
if (shadows == Shadows.Renderer) {
logger.info("Loading shadow renderer.");
shadowRenderer = new DirectionalLightShadowRenderer(assetManager, shadowRes, sunSplits);
shadowRenderer.setLight(sun);
shadowRenderer.setRenderBackFacesShadows(false);
shadowRenderer.setShadowCompareMode(CompareMode.Hardware);
viewPort.addProcessor(shadowRenderer);
} else if (shadows == Shadows.Filter) {
logger.info("Loading shadow filter.");
shadowFilter = new DirectionalLightShadowFilter(assetManager, shadowRes, sunSplits);
shadowFilter.setLight(sun);
shadowFilter.setRenderBackFacesShadows(false);
shadowFilter.setShadowCompareMode(CompareMode.Hardware);
fpp.addFilter(shadowFilter);
}
}
if (occlusion) {
logger.info("Loading screenspace ambient occlusion filter.");
ssaoFilter = new SSAOFilter();
ssaoFilter.setIntensity(1.5f);
fpp.addFilter(ssaoFilter);
}
if (bloom != Bloom.None) {
if (!bloom.usesSoftBloom()) {
logger.log(Level.INFO, "Loading bloom filter. GlowMode={0}", bloom.getEquivalent().toString());
bloomFilter = new BloomFilter(bloom.getEquivalent());
fpp.addFilter(bloomFilter);
} else {
//softBloomFilter = new SoftBloomFilter();
//fpp.addFilter(softBloomFilter);
logger.warning("SoftScene bloom is currently unsupported.");
}
}
if (makeFpp) {
if (!fpp.getFilterList().isEmpty()) {
viewPort.addProcessor(fpp);
} else {
logger.info("No filters created. Deleting new FilterPostProcessor.");
fpp = null;
}
}
}
// light probes
if (lightProbeAsset != null) {
if (lightProbeAsset.equals(HARDWARE_PROBE)) {
logger.info("Loading hardware light probe.");
hardwareProbe = new EnvironmentProbeControl(assetManager, hardwareProbeRes);
addControl(hardwareProbe);
} else {
logger.info("Loading light probe from asset.");
try {
Spatial m = assetManager.loadModel(lightProbeAsset);
lightProbe = (LightProbe)m.getLocalLightList().get(0);
addLight(lightProbe);
} catch (Exception ex) {
logger.log(Level.SEVERE, "Failed to load light probe from \""+lightProbeAsset+"\"", ex);
}
}
}
// remove spatials
LinkedList<Spatial> remove = new LinkedList<>();
for (Spatial s : new SceneGraphIterator(this)) {
if (s.getUserData("Remove") != null) {
remove.add(s);
}
}
for (Spatial s : remove) {
s.removeFromParent();
}
if (!remove.isEmpty()) {
logger.log(Level.INFO, "{0} spatials removed from scene by userdata.", remove.size());
}
// physics
if (space != null) {
for (SceneGraphIterator it = new SceneGraphIterator(this); it.hasNext();) {
Spatial s = it.next();
Double mass = s.getUserData("Mass");
if (mass != null && mass >= 0) {
addRigidBody(s, mass.floatValue());
it.ignoreChildren();
}
}
logger.log(Level.INFO, "{0} rigid bodies created.", rigidBodyList.size());
}
} catch (Exception ex) {
logger.log(Level.SEVERE, "An exception occured while loading test scene", ex);
}
loaded = true;
return this;
}
/**
* Dumps all resources for this scene.
* <p>
* After dumping, {@link #load()} can be called again.
*/
public void dump() {
clearRigidBodies();
detachAllChildren();
subScene = null;
if (sun != null) {
removeLight(sun);
sun = null;
}
if (atmosphere != null) {
removeLight(atmosphere);
atmosphere = null;
}
if (ambient != null) {
removeLight(ambient);
ambient = null;
}
if (hardwareProbe != null) {
removeLight(hardwareProbe);
removeControl(hardwareProbe);
hardwareProbe = null;
}
if (lightProbe != null) {
removeLight(lightProbe);
lightProbe = null;
}
if (fpp != null) {
if (shadowFilter != null) {
this.fpp.removeFilter(shadowFilter);
shadowFilter = null;
}
if (ssaoFilter != null) {
this.fpp.removeFilter(ssaoFilter);
ssaoFilter = null;
}
if (bloomFilter != null) {
this.fpp.removeFilter(bloomFilter);
bloomFilter = null;
}
// if (softBloomFilter != null) {
// this.fpp.removeFilter(softBloomFilter);
// softBloomFilter = null;
// }
}
if (shadowRenderer != null) {
viewPort.removeProcessor(shadowRenderer);
shadowRenderer = null;
}
loaded = false;
}
/**
* Configures the viewport background to match the scene.
*/
public void configureBackgroundColor() {
viewPort.setBackgroundColor(SCENE_BACKGROUND_COLOR);
}
/**
* Configures {@link FlyByCamera} to the scene.
*
* @param cam
*/
public void configureFlyCamSpeed(FlyByCamera cam) {
//cam.get.lookAt(new Vector3f(0, 2, 0), Vector3f.UNIT_Y);
//cam.setLocation(new Vector3f(-10, 10, -10));
cam.setMoveSpeed(15);
}
/**
* Configures the {@link Camera} to the scene.
*
* @param cam
*/
public void configureCameraPosition(Camera cam) {
cam.setLocation(new Vector3f(-10, 10, -10));
cam.lookAtDirection(new Vector3f(1, -1, 1), Vector3f.UNIT_Y);
}
/**
* Configures the default texture anisotropy level to match the scene.
* <p>
* Anisotropy makes textures viewed at an oblique angle sharper.
*
* @param renderer
*/
public void configureDefaultAnisotropy(Renderer renderer) {
renderer.setDefaultAnisotropicFilter(Math.min(8, renderer.getLimits().get(Limits.TextureAnisotropy)));
}
private Node loadBaseScene() {
GltfModelKey key = new GltfModelKey(BASE_SCENE);
Node base = (Node)assetManager.loadModel(key);
Node terrainSrc = (Node)base.getChild("Terrain");
base.detachChild(terrainSrc);
Vector3f offset = new Vector3f(-0.5f*(width-1)*TILE_SIZE.x, 0, -0.5f*(height-1)*TILE_SIZE.z);
for (int i = 0; i < width; i++) {
for (int j = 0; j < height; j++) {
Spatial tile = null;
// note: coordinates are reversed: N -> S, E -> W
if (walls && width > 1 && height > 1) {
if (i == 0 && j == 0) {
// south east
tile = createTerrainTile(terrainSrc, "TerrainNW");
} else if (i == width-1 && j == 0) {
// south west
tile = createTerrainTile(terrainSrc, "TerrainNE");
} else if (i == width-1 && j == height-1) {
// north west
tile = createTerrainTile(terrainSrc, "TerrainSE");
} else if (i == 0 && j == height-1) {
// north east
tile = createTerrainTile(terrainSrc, "TerrainSW");
} else if (i == 0) {
// east
tile = createTerrainTile(terrainSrc, "TerrainW");
} else if (i == width-1) {
// west
tile = createTerrainTile(terrainSrc, "TerrainE");
} else if (j == 0) {
// south
tile = createTerrainTile(terrainSrc, "TerrainN");
} else if (j == height-1) {
// north
tile = createTerrainTile(terrainSrc, "TerrainS");
}
}
if (tile == null) {
// center
tile = createTerrainTile(terrainSrc, "TerrainC");
}
tile.setLocalTranslation(TILE_SIZE.x*i+offset.x, offset.y, TILE_SIZE.z*j+offset.z);
if (!boundaries) {
tile.setUserData("Mass", 0.0);
}
base.attachChild(tile);
}
}
attachChild(base);
return base;
}
private Spatial createTerrainTile(Node terrainSrc, String name) {
int v = getInt(terrainSrc, name, -1);
if (v > 0) {
v = FastMath.rand.nextInt(v);
return terrainSrc.getChild(name+v).clone();
}
return terrainSrc.getChild(name).clone();
}
private Node loadBoundaries() {
float y = 40;
float x = TILE_SIZE.x*width*0.5f;
float z = TILE_SIZE.z*height*0.5f;
Node n = new Node("Boundaries");
Geometry[] bounds = {
new Geometry("NorthBounds", new RectangleMesh(new Vector3f(x, 0, 0), new Vector3f(-x, 0, 0), new Vector3f(x, y*2, 0))),
new Geometry("EastBounds", new RectangleMesh(new Vector3f(0, 0, z), new Vector3f(0, 0, -z), new Vector3f(0, y*2, z))),
new Geometry("SouthBounds", new RectangleMesh(new Vector3f(-x, 0, 0), new Vector3f(x, 0, 0), new Vector3f(-x, y*2, 0))),
new Geometry("WestBounds", new RectangleMesh(new Vector3f(0, 0, -z), new Vector3f(0, 0, z), new Vector3f(0, y*2, -z))),
new Geometry("UpperBounds", new RectangleMesh(new Vector3f(-x, y, -z), new Vector3f(x, y, -z), new Vector3f(-x, y, z))),
new Geometry("LowerBounds", new RectangleMesh(new Vector3f(x, 0, -z), new Vector3f(-x, 0, -z), new Vector3f(x, 0, z))),
};
bounds[0].setLocalTranslation(0, 0, z);
bounds[1].setLocalTranslation(-x, 0, 0);
bounds[2].setLocalTranslation(0, 0, -z);
bounds[3].setLocalTranslation(x, 0, 0);
bounds[4].setLocalTranslation(0, y, 0);
Material mat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
mat.setColor("Color", ColorRGBA.BlackNoAlpha);
for (Geometry g : bounds) {
g.setMaterial(mat);
g.setCullHint(Spatial.CullHint.Always);
g.setUserData("Mass", 0.0);
n.attachChild(g);
}
attachChild(n);
return n;
}
private void loadSubScene(String assetPath) {
try {
subScene = (Node)assetManager.loadModel(assetPath);
subScene.setName("SubScene");
width = Math.max(width, getInt(subScene, "Width", 1));
height = Math.max(height, getInt(subScene, "Height", 1));
attachChild(subScene);
} catch (Exception ex) {
logger.log(Level.SEVERE, "Failed to load subscene from \"{0}\"", assetPath);
}
}
private void loadSky() {
try {
if (!skyAsset.startsWith(MULTI_TEXTURE)) {
logger.info("Loading sky from texture.");
sky = SkyFactory.createSky(assetManager, skyAsset, SkyFactory.EnvMapType.CubeMap);
} else {
logger.info("Loading sky from multiple textures.");
String a = skyAsset.substring(MULTI_TEXTURE.length());
Texture west = assetManager.loadTexture(a.replace("$", "west"));
Texture east = assetManager.loadTexture(a.replace("$", "east"));
Texture north = assetManager.loadTexture(a.replace("$", "north"));
Texture south = assetManager.loadTexture(a.replace("$", "south"));
Texture up = assetManager.loadTexture(a.replace("$", "up"));
Texture down = assetManager.loadTexture(a.replace("$", "down"));
sky = SkyFactory.createSky(assetManager, west, east, north, south, up, down);
}
EnvironmentProbeControl.tagGlobal(sky);
attachChild(sky);
} catch (Exception ex) {
logger.log(Level.SEVERE, "Failed to load sky from \"{0}\"", skyAsset);
}
}
private void addRigidBody(Spatial model, float mass) {
RigidBodyControl rigidBody = new RigidBodyControl(mass);
model.addControl(rigidBody);
space.add(rigidBody);
rigidBodyList.add(rigidBody);
}
private void clearRigidBodies() {
for (PhysicsCollisionObject object : rigidBodyList) {
space.remove(object);
}
rigidBodyList.clear();
}
private static int getInt(Spatial spatial, String name, int defaultValue) {
Double value = spatial.getUserData(name);
if (value != null) {
return value.intValue();
} else {
return defaultValue;
}
}
/**
* Sets the width (X axis) of the scene in tiles.
* <p>
* A single tile is 20 units in width. If the subscene requires a width greater
* than what is set, the width will be changed.
* <p>
* default=3
*
* @param width number of tiles in width
*/
public void setWidth(int width) {
this.width = width;
}
/**
* Sets the height (Z axis) of the scene in tiles.
* <p>
* A single tile is 20 units in height. If the subscene requires a height greater
* than what is set, the height will be changed.
* <p>
* default=3
*
* @param height number of tiles in height
*/
public void setHeight(int height) {
this.height = height;
}
/**
* Sets the width and height of the scene in tiles.
*
* @param width
* @param height
* @see #setWidth(int)
* @see #setHeight(int)
*/
public void setMapSize(int width, int height) {
setWidth(width);
setHeight(height);
}
/**
* Enables visible scene geometry.
* <p>
* If not enabled, no scene geometry will be loaded, except the skybox.
* <p>
* default=true
*
* @param sceneGeometry
*/
public void setEnableSceneGeometry(boolean sceneGeometry) {
this.sceneGeometry = sceneGeometry;
}
/**
* Enables visible walls along the border of the scene.
* <p>
* If disabled, the scene will be the equivalent of a plane.
* <p>
* default=true
*
* @param walls
*/
public void setEnableWalls(boolean walls) {
this.walls = walls;
}
/**
* Sets the subscene asset path.
* <p>
* The subscene is loaded on top of the main scene. This allows TestScene
* to be used for a variety of applications and tests.
* <p>
* The subscene can specify the minimum width and height (in tiles) of the scene
* by assigning userdata ("Width" and "Height" keys, respectively) to the subscene
* root. This can be done in Blender by assigning custom properties to the scene
* (properties -> scene -> custom properties).
* <p>
* Any spatial in the subscene that contains a "Remove" userdata entry will not
* be included in the overall scene. Any spatial containing a double userdata entry
* under "Mass" will be given a {@link RigidBodyControl} of the specified mass.
* <p>
* Any spatial in the subscene containing a "tag.env" (according to
* {@link EnvironmentProbeControl}) userdata entry that is equal to {@code true}
* will be included in environment map creation. These spatials should be static.
* <p>
* default=null (no subscene)
*
* @param subSceneAsset
* @see #CHARACTER_SUBSCENE
* @see #PHYSICS_SUBSCENE
*/
public void setSubScene(String subSceneAsset) {
this.subSceneAsset = subSceneAsset;
}
/**
* Enables scene lights.
* <p>
* default=true
*
* @param lights
*/
public void setEnableLights(boolean lights) {
this.lights = lights;
}
/**
* Enables post-processing filters.
* <p>
* If disabled, a {@link FilterPostProcessor} will not be created. This setting
* also enables/disables shadows.
* <p>
* default=true
*
* @param filters
*/
public void setEnableFilters(boolean filters) {
this.filters = filters;
}
/**
* Sets the {@link FilterPostProcessor} to use for filter effects.
* <p>
* The internal FilterPostProcessor cannot be set after load.
* <p>
* If null, a new FilterPostProcessor will be created on load if any filters are used.
*
* @param fpp
*/
public void setFilterPostProcessor(FilterPostProcessor fpp) {
if (!loaded) {
this.fpp = fpp;
}
}
/**
* Sets the physics space.
* <p>
* If the physics space is not null, any spatials containing a "Mass" userdata
* entry will become physical.
* <p>
* If the internal physics space is not null and null is passed, then all
* {@link RigidBodyControl} (represented by {@link #getRigidBodyList()})
* objects currently in the scene will be removed from the physics space.
* <p>
* default=null
*
* @param space
*/
public void setPhysicsSpace(PhysicsSpace space) {
if (this.space != null && space == null && loaded) {
clearRigidBodies();
}
this.space = space;
}
/**
* Enables boundaries created from 6 quads around the edge of the scene.
* <p>
* The resulting quads are always culled. If the physics space is not null,
* the resulting quads will become physical and the visible terrain will
* not become physical.
* <p>
* default=true
*
* @param boundaries
*/
public void setEnableBoundaries(boolean boundaries) {
this.boundaries = boundaries;
}
/**
* Sets the resolution of shadow maps.
* <p>
* default=2048
*
* @param shadowRes
*/
public void setShadowResolution(int shadowRes) {
this.shadowRes = shadowRes;
}
/**
* Sets the number of splits used for sunlight shadows.
* <p>
* default=4
*
* @param splits
*/
public void setSunShadowSplits(int splits) {
this.sunSplits = splits;
}
/**
* Sets the shadow mode.
*
* @param shadows
*/
public void setShadows(Shadows shadows) {
this.shadows = shadows;
}
/**
* Enables {@link SSAOFilter} for ambient occlusion effects.
*
* @param occlusion
*/
public void setEnableOcclusion(boolean occlusion) {
this.occlusion = occlusion;
}
/**
* Sets the bloom mode.
* <p>
* <em>Note: {@link Bloom#SoftScene} is currently unsupported.</em>
* <p>
* default={@link Bloom#None}
*
* @param bloom
*/
public void setBloom(Bloom bloom) {
this.bloom = bloom;
}
/**
* Sets the light probe asset path.
* <p>
* The light probe are expected to be the only light added to the main
* node of the j3o scene.
* <p>
* If the light probe asset path is set to {@link #HARDWARE_PROBE}, then
* {@link EnvironmentProbeControl} will be used to generate a light probe.
* <p>
* default={@link #HARDWARE_PROBE}
*
* @param lightProbeAsset
*/
public void setLightProbe(String lightProbeAsset) {
this.lightProbeAsset = lightProbeAsset;
}
/**
* Sets the resolution of the probe generated with {@link EnvironmentProbeControl}.
* <p>
* This is only applicable if the value set by {@link #setLightProbe(java.lang.String)}
* is equal to {@link #HARDWARE_PROBE}.
* <p>
* default=256
*
* @param probeRes
*/
public void setHardwareProbeResolution(int probeRes) {
hardwareProbeRes = probeRes;
}
/**
* Sets the skybox asset path.
* <p>
* If the asset path begins with "multi:", then the path is assumed to reference 6
* textures. Any "$" symbols detected in the asset path will be replaced with
* west, east, north, south, up, and down (for each orthogonal direction).
* <p>
* default=null
*
* @param skyAsset
* @see #BRIGHT_SKY
* @see #LAGOON_SKY for an example of multiple textures
*/
public void setSky(String skyAsset) {
this.skyAsset = skyAsset;
}
/**
* Gets the width (X axis) of the scene in tiles.
*
* @return
* @see #setWidth(int)
*/
public int getWidth() {
return width;
}
/**
* Gets the height (Z axis) of the scene in tiles.
*
* @return
* @see #setHeight(int)
*/
public int getHeight() {
return height;
}
/**
* Get the width (X axis) of the scene.
*