TestLightingFog.java 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. package jme3test.light;
  2. import com.jme3.app.SimpleApplication;
  3. import com.jme3.input.KeyInput;
  4. import com.jme3.input.controls.ActionListener;
  5. import com.jme3.input.controls.KeyTrigger;
  6. import com.jme3.light.DirectionalLight;
  7. import com.jme3.material.Material;
  8. import com.jme3.material.Materials;
  9. import com.jme3.math.ColorRGBA;
  10. import com.jme3.math.FastMath;
  11. import com.jme3.math.Vector2f;
  12. import com.jme3.math.Vector3f;
  13. import com.jme3.scene.Geometry;
  14. import com.jme3.scene.shape.Sphere;
  15. public class TestLightingFog extends SimpleApplication implements ActionListener {
  16. private Material material;
  17. private Vector2f linear = new Vector2f(25, 120);
  18. private float exp = 0.015f;
  19. private float expsq = 0.02f;
  20. public static void main(String[] args) {
  21. TestLightingFog testLightingFog = new TestLightingFog();
  22. testLightingFog.start();
  23. }
  24. @Override
  25. public void simpleInitApp() {
  26. ColorRGBA skyColor = new ColorRGBA(0.5f, 0.6f, 0.7f, 0.0f);
  27. flyCam.setMoveSpeed(20);
  28. viewPort.setBackgroundColor(skyColor.mult(0.9f));
  29. DirectionalLight directionalLight = new DirectionalLight(new Vector3f(-1, -1, -1).normalizeLocal());
  30. rootNode.addLight(directionalLight);
  31. material = new Material(assetManager, Materials.LIGHTING);
  32. material.setBoolean("UseFog", true);
  33. material.setColor("FogColor", skyColor);
  34. material.setVector2("LinearFog", linear);
  35. int distance = -3;
  36. for (int i = 0; i < 100; i++) {
  37. Geometry geometry = new Geometry("Sphere", new Sphere(32, 32, 2));
  38. geometry.setMaterial(material);
  39. geometry.setLocalTranslation((FastMath.nextRandomFloat() - 0.5f) * 45, 0, i * distance);
  40. rootNode.attachChild(geometry);
  41. }
  42. inputManager.addMapping("Linear", new KeyTrigger(KeyInput.KEY_1));
  43. inputManager.addMapping("Exponential", new KeyTrigger(KeyInput.KEY_2));
  44. inputManager.addMapping("ExponentialSquared", new KeyTrigger(KeyInput.KEY_3));
  45. inputManager.addListener(this, "Linear", "Exponential", "ExponentialSquared");
  46. }
  47. @Override
  48. public void onAction(String name, boolean isPressed, float tpf) {
  49. if (name.equals("Linear") && !isPressed) {
  50. material.setVector2("LinearFog", linear);
  51. material.clearParam("ExpFog");
  52. material.clearParam("ExpSqFog");
  53. }
  54. else if (name.equals("Exponential") && !isPressed) {
  55. material.clearParam("LinearFog");
  56. material.setFloat("ExpFog", exp);
  57. material.clearParam("ExpSqFog");
  58. }
  59. else if (name.equals("ExponentialSquared") && !isPressed) {
  60. material.clearParam("LinearFog");
  61. material.clearParam("ExpFog");
  62. material.setFloat("ExpSqFog", expsq);
  63. }
  64. }
  65. }