TestManyLightsSingle.java 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. /*
  2. * Copyright (c) 2009-2021 jMonkeyEngine
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are
  7. * met:
  8. *
  9. * * Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. *
  12. * * Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in the
  14. * documentation and/or other materials provided with the distribution.
  15. *
  16. * * Neither the name of 'jMonkeyEngine' nor the names of its contributors
  17. * may be used to endorse or promote products derived from this software
  18. * without specific prior written permission.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
  22. * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  23. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  24. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  25. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  26. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  27. * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  28. * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  29. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  30. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. */
  32. package jme3test.light;
  33. import com.jme3.app.BasicProfilerState;
  34. import com.jme3.app.SimpleApplication;
  35. import com.jme3.font.BitmapText;
  36. import com.jme3.input.KeyInput;
  37. import com.jme3.input.controls.ActionListener;
  38. import com.jme3.input.controls.KeyTrigger;
  39. import com.jme3.light.AmbientLight;
  40. import com.jme3.light.DirectionalLight;
  41. import com.jme3.light.Light;
  42. import com.jme3.light.LightList;
  43. import com.jme3.light.PointLight;
  44. import com.jme3.light.SpotLight;
  45. import com.jme3.material.Material;
  46. import com.jme3.material.TechniqueDef;
  47. import com.jme3.math.ColorRGBA;
  48. import com.jme3.math.FastMath;
  49. import com.jme3.math.Quaternion;
  50. import com.jme3.math.Vector3f;
  51. import com.jme3.renderer.RenderManager;
  52. import com.jme3.renderer.ViewPort;
  53. import com.jme3.scene.Geometry;
  54. import com.jme3.scene.Node;
  55. import com.jme3.scene.Spatial;
  56. import com.jme3.scene.control.AbstractControl;
  57. import com.jme3.scene.shape.Box;
  58. import com.jme3.util.MaterialDebugAppState;
  59. public class TestManyLightsSingle extends SimpleApplication {
  60. public static void main(String[] args) {
  61. TestManyLightsSingle app = new TestManyLightsSingle();
  62. app.start();
  63. }
  64. /**
  65. * Switch mode with space bar at run time
  66. */
  67. private TechniqueDef.LightMode lm = TechniqueDef.LightMode.SinglePass;
  68. @Override
  69. public void simpleInitApp() {
  70. renderManager.setPreferredLightMode(lm);
  71. renderManager.setSinglePassLightBatchSize(6);
  72. flyCam.setMoveSpeed(10);
  73. Node scene = (Node) assetManager.loadModel("Scenes/ManyLights/Main.scene");
  74. rootNode.attachChild(scene);
  75. Node n = (Node) rootNode.getChild(0);
  76. final LightList lightList = n.getWorldLightList();
  77. final Geometry g = (Geometry) n.getChild("Grid-geom-1");
  78. g.getMaterial().setColor("Ambient", new ColorRGBA(0.2f, 0.2f, 0.2f, 1f));
  79. /* A colored lit cube. Needs light source! */
  80. Box boxMesh = new Box(1f, 1f, 1f);
  81. final Geometry boxGeo = new Geometry("Colored Box", boxMesh);
  82. Material boxMat = g.getMaterial().clone();
  83. boxMat.clearParam("DiffuseMap");
  84. boxMat.setBoolean("UseMaterialColors", true);
  85. boxMat.setColor("Ambient", new ColorRGBA(0.2f, 0.2f, 0.2f, 1f));
  86. boxMat.setColor("Diffuse", ColorRGBA.Blue);
  87. boxGeo.setMaterial(boxMat);
  88. final Node cubeNodes = new Node();
  89. n.attachChild(cubeNodes);
  90. int nb = 0;
  91. for (Light light : lightList) {
  92. nb++;
  93. PointLight p = (PointLight) light;
  94. if (nb > 60) {
  95. n.removeLight(light);
  96. } else {
  97. int rand = FastMath.nextRandomInt(0, 3);
  98. switch (rand) {
  99. case 0:
  100. light.setColor(ColorRGBA.Red);
  101. break;
  102. case 1:
  103. light.setColor(ColorRGBA.Yellow);
  104. break;
  105. case 2:
  106. light.setColor(ColorRGBA.Green);
  107. break;
  108. case 3:
  109. light.setColor(ColorRGBA.Orange);
  110. break;
  111. }
  112. }
  113. Geometry b = boxGeo.clone(false);
  114. cubeNodes.attachChild(b);
  115. b.setLocalTranslation(p.getPosition().x, 2, p.getPosition().z);
  116. }
  117. // cam.setLocation(new Vector3f(3.1893547f, 17.977385f, 30.8378f));
  118. // cam.setRotation(new Quaternion(0.14317635f, 0.82302624f, -0.23777823f, 0.49557027f));
  119. cam.setLocation(new Vector3f(-1.8901939f, 29.34097f, 73.07533f));
  120. cam.setRotation(new Quaternion(0.0021000702f, 0.971012f, -0.23886925f, 0.008527749f));
  121. BasicProfilerState profiler = new BasicProfilerState(true);
  122. profiler.setGraphScale(1000f);
  123. // getStateManager().attach(profiler);
  124. // guiNode.setCullHint(CullHint.Always);
  125. flyCam.setDragToRotate(true);
  126. flyCam.setMoveSpeed(50);
  127. final MaterialDebugAppState debug = new MaterialDebugAppState();
  128. stateManager.attach(debug);
  129. inputManager.addListener(new ActionListener() {
  130. @Override
  131. public void onAction(String name, boolean isPressed, float tpf) {
  132. if (name.equals("toggle") && isPressed) {
  133. if (lm == TechniqueDef.LightMode.SinglePass) {
  134. lm = TechniqueDef.LightMode.MultiPass;
  135. helloText.setText("(Multi pass)");
  136. } else {
  137. lm = TechniqueDef.LightMode.SinglePass;
  138. helloText.setText("(Single pass) nb lights per batch : " + renderManager.getSinglePassLightBatchSize());
  139. }
  140. renderManager.setPreferredLightMode(lm);
  141. reloadScene(g, boxGeo, cubeNodes);
  142. }
  143. if (name.equals("lightsUp") && isPressed) {
  144. renderManager.setSinglePassLightBatchSize(renderManager.getSinglePassLightBatchSize() + 1);
  145. helloText.setText("(Single pass) nb lights per batch : " + renderManager.getSinglePassLightBatchSize());
  146. }
  147. if (name.equals("lightsDown") && isPressed) {
  148. renderManager.setSinglePassLightBatchSize(renderManager.getSinglePassLightBatchSize() - 1);
  149. helloText.setText("(Single pass) nb lights per batch : " + renderManager.getSinglePassLightBatchSize());
  150. }
  151. if (name.equals("toggleOnOff") && isPressed) {
  152. for (final Light light : lightList) {
  153. if (light instanceof AmbientLight) {
  154. continue;
  155. }
  156. light.setEnabled(!light.isEnabled());
  157. }
  158. }
  159. }
  160. }, "toggle", "lightsUp", "lightsDown", "toggleOnOff");
  161. inputManager.addMapping("toggle", new KeyTrigger(KeyInput.KEY_SPACE));
  162. inputManager.addMapping("lightsUp", new KeyTrigger(KeyInput.KEY_UP));
  163. inputManager.addMapping("lightsDown", new KeyTrigger(KeyInput.KEY_DOWN));
  164. inputManager.addMapping("toggleOnOff", new KeyTrigger(KeyInput.KEY_L));
  165. SpotLight spot = new SpotLight();
  166. spot.setDirection(new Vector3f(-1f, -1f, -1f).normalizeLocal());
  167. spot.setColor(ColorRGBA.Blue.mult(5));
  168. spot.setSpotOuterAngle(FastMath.DEG_TO_RAD * 20);
  169. spot.setSpotInnerAngle(FastMath.DEG_TO_RAD * 5);
  170. spot.setPosition(new Vector3f(10, 10, 20));
  171. rootNode.addLight(spot);
  172. DirectionalLight dl = new DirectionalLight();
  173. dl.setDirection(new Vector3f(-1, -1, 1));
  174. rootNode.addLight(dl);
  175. AmbientLight al = new AmbientLight();
  176. al.setColor(new ColorRGBA(0.2f, 0.2f, 0.2f, 1f));
  177. rootNode.addLight(al);
  178. /*
  179. * Write text on the screen (HUD)
  180. */
  181. guiNode.detachAllChildren();
  182. guiFont = assetManager.loadFont("Interface/Fonts/Default.fnt");
  183. helloText = new BitmapText(guiFont);
  184. helloText.setSize(guiFont.getCharSet().getRenderedSize());
  185. helloText.setText("(Single pass) nb lights per batch : " + renderManager.getSinglePassLightBatchSize());
  186. helloText.setLocalTranslation(300, helloText.getLineHeight(), 0);
  187. guiNode.attachChild(helloText);
  188. }
  189. protected void reloadScene(Geometry g, Geometry boxGeo, Node cubeNodes) {
  190. MaterialDebugAppState debug = stateManager.getState(MaterialDebugAppState.class);
  191. Material m = debug.reloadMaterial(g.getMaterial());
  192. if (m != null) {
  193. g.setMaterial(m);
  194. }
  195. m = debug.reloadMaterial(boxGeo.getMaterial());
  196. if (m != null) {
  197. cubeNodes.setMaterial(m);
  198. }
  199. }
  200. private BitmapText helloText;
  201. @Override
  202. public void simpleUpdate(float tpf) {
  203. // if (nbFrames == 4000) {
  204. // startTime = System.nanoTime();
  205. // }
  206. // if (nbFrames > 4000) {
  207. // time = System.nanoTime();
  208. // float average = ((float) time - (float) startTime) / ((float) nbFrames - 4000f);
  209. // helloText.setText("Average = " + average);
  210. // }
  211. // nbFrames++;
  212. }
  213. class MoveControl extends AbstractControl {
  214. final private float direction;
  215. final private Vector3f origPos = new Vector3f();
  216. public MoveControl(float direction) {
  217. this.direction = direction;
  218. }
  219. @Override
  220. public void setSpatial(Spatial spatial) {
  221. super.setSpatial(spatial); //To change body of generated methods, choose Tools | Templates.
  222. origPos.set(spatial.getLocalTranslation());
  223. }
  224. private float time = 0;
  225. @Override
  226. protected void controlUpdate(float tpf) {
  227. time += tpf;
  228. spatial.setLocalTranslation(origPos.x + FastMath.cos(time) * direction, origPos.y, origPos.z + FastMath.sin(time) * direction);
  229. }
  230. @Override
  231. protected void controlRender(RenderManager rm, ViewPort vp) {
  232. }
  233. }
  234. }