1 package jme3test.post; 2 3 import com.jme3.app.SimpleApplication; 4 import com.jme3.collision.CollisionResult; 5 import com.jme3.collision.CollisionResults; 6 import com.jme3.input.controls.ActionListener; 7 import com.jme3.input.controls.AnalogListener; 8 import com.jme3.input.controls.KeyTrigger; 9 import com.jme3.light.DirectionalLight; 10 import com.jme3.material.Material; 11 import com.jme3.math.*; 12 import com.jme3.post.FilterPostProcessor; 13 import com.jme3.post.filters.DepthOfFieldFilter; 14 import com.jme3.renderer.Camera; 15 import com.jme3.renderer.queue.RenderQueue.ShadowMode; 16 import com.jme3.scene.Node; 17 import com.jme3.scene.Spatial; 18 import com.jme3.terrain.geomipmap.TerrainQuad; 19 import com.jme3.terrain.heightmap.AbstractHeightMap; 20 import com.jme3.terrain.heightmap.ImageBasedHeightMap; 21 import com.jme3.texture.Texture; 22 import com.jme3.texture.Texture.WrapMode; 23 import com.jme3.util.SkyFactory; 24 import java.util.ArrayList; 25 import java.util.List; 26 27 /** 28 * test 29 * @author Nehon 30 */ 31 public class TestDepthOfField extends SimpleApplication { 32 33 private FilterPostProcessor fpp; 34 private Vector3f lightDir = new Vector3f(-4.9236743f, -1.27054665f, 5.896916f); 35 TerrainQuad terrain; 36 Material matRock; 37 DepthOfFieldFilter dofFilter; 38 main(String[] args)39 public static void main(String[] args) { 40 TestDepthOfField app = new TestDepthOfField(); 41 app.start(); 42 } 43 44 @Override simpleInitApp()45 public void simpleInitApp() { 46 47 48 Node mainScene = new Node("Main Scene"); 49 rootNode.attachChild(mainScene); 50 51 createTerrain(mainScene); 52 DirectionalLight sun = new DirectionalLight(); 53 sun.setDirection(lightDir); 54 sun.setColor(ColorRGBA.White.clone().multLocal(1.7f)); 55 mainScene.addLight(sun); 56 57 DirectionalLight l = new DirectionalLight(); 58 l.setDirection(Vector3f.UNIT_Y.mult(-1)); 59 l.setColor(ColorRGBA.White.clone().multLocal(0.3f)); 60 mainScene.addLight(l); 61 62 flyCam.setMoveSpeed(50); 63 cam.setFrustumFar(3000); 64 cam.setLocation(new Vector3f(-700, 100, 300)); 65 cam.setRotation(new Quaternion().fromAngles(new float[]{FastMath.PI * 0.06f, FastMath.PI * 0.65f, 0})); 66 67 68 Spatial sky = SkyFactory.createSky(assetManager, "Scenes/Beach/FullskiesSunset0068.dds", false); 69 sky.setLocalScale(350); 70 mainScene.attachChild(sky); 71 72 73 74 fpp = new FilterPostProcessor(assetManager); 75 // fpp.setNumSamples(4); 76 77 dofFilter = new DepthOfFieldFilter(); 78 dofFilter.setFocusDistance(0); 79 dofFilter.setFocusRange(50); 80 dofFilter.setBlurScale(1.4f); 81 fpp.addFilter(dofFilter); 82 viewPort.addProcessor(fpp); 83 84 inputManager.addListener(new ActionListener() { 85 86 public void onAction(String name, boolean isPressed, float tpf) { 87 if (isPressed) { 88 if (name.equals("toggle")) { 89 dofFilter.setEnabled(!dofFilter.isEnabled()); 90 } 91 92 93 } 94 } 95 }, "toggle"); 96 inputManager.addListener(new AnalogListener() { 97 98 public void onAnalog(String name, float value, float tpf) { 99 if (name.equals("blurScaleUp")) { 100 dofFilter.setBlurScale(dofFilter.getBlurScale() + 0.01f); 101 System.out.println("blurScale : " + dofFilter.getBlurScale()); 102 } 103 if (name.equals("blurScaleDown")) { 104 dofFilter.setBlurScale(dofFilter.getBlurScale() - 0.01f); 105 System.out.println("blurScale : " + dofFilter.getBlurScale()); 106 } 107 if (name.equals("focusRangeUp")) { 108 dofFilter.setFocusRange(dofFilter.getFocusRange() + 1f); 109 System.out.println("focusRange : " + dofFilter.getFocusRange()); 110 } 111 if (name.equals("focusRangeDown")) { 112 dofFilter.setFocusRange(dofFilter.getFocusRange() - 1f); 113 System.out.println("focusRange : " + dofFilter.getFocusRange()); 114 } 115 if (name.equals("focusDistanceUp")) { 116 dofFilter.setFocusDistance(dofFilter.getFocusDistance() + 1f); 117 System.out.println("focusDistance : " + dofFilter.getFocusDistance()); 118 } 119 if (name.equals("focusDistanceDown")) { 120 dofFilter.setFocusDistance(dofFilter.getFocusDistance() - 1f); 121 System.out.println("focusDistance : " + dofFilter.getFocusDistance()); 122 } 123 124 } 125 }, "blurScaleUp", "blurScaleDown", "focusRangeUp", "focusRangeDown", "focusDistanceUp", "focusDistanceDown"); 126 127 128 inputManager.addMapping("toggle", new KeyTrigger(keyInput.KEY_SPACE)); 129 inputManager.addMapping("blurScaleUp", new KeyTrigger(keyInput.KEY_U)); 130 inputManager.addMapping("blurScaleDown", new KeyTrigger(keyInput.KEY_J)); 131 inputManager.addMapping("focusRangeUp", new KeyTrigger(keyInput.KEY_I)); 132 inputManager.addMapping("focusRangeDown", new KeyTrigger(keyInput.KEY_K)); 133 inputManager.addMapping("focusDistanceUp", new KeyTrigger(keyInput.KEY_O)); 134 inputManager.addMapping("focusDistanceDown", new KeyTrigger(keyInput.KEY_L)); 135 136 } 137 createTerrain(Node rootNode)138 private void createTerrain(Node rootNode) { 139 matRock = new Material(assetManager, "Common/MatDefs/Terrain/TerrainLighting.j3md"); 140 matRock.setBoolean("useTriPlanarMapping", false); 141 matRock.setBoolean("WardIso", true); 142 matRock.setTexture("AlphaMap", assetManager.loadTexture("Textures/Terrain/splat/alphamap.png")); 143 Texture heightMapImage = assetManager.loadTexture("Textures/Terrain/splat/mountains512.png"); 144 Texture grass = assetManager.loadTexture("Textures/Terrain/splat/grass.jpg"); 145 grass.setWrap(WrapMode.Repeat); 146 matRock.setTexture("DiffuseMap", grass); 147 matRock.setFloat("DiffuseMap_0_scale", 64); 148 Texture dirt = assetManager.loadTexture("Textures/Terrain/splat/dirt.jpg"); 149 dirt.setWrap(WrapMode.Repeat); 150 matRock.setTexture("DiffuseMap_1", dirt); 151 matRock.setFloat("DiffuseMap_1_scale", 16); 152 Texture rock = assetManager.loadTexture("Textures/Terrain/splat/road.jpg"); 153 rock.setWrap(WrapMode.Repeat); 154 matRock.setTexture("DiffuseMap_2", rock); 155 matRock.setFloat("DiffuseMap_2_scale", 128); 156 Texture normalMap0 = assetManager.loadTexture("Textures/Terrain/splat/grass_normal.jpg"); 157 normalMap0.setWrap(WrapMode.Repeat); 158 Texture normalMap1 = assetManager.loadTexture("Textures/Terrain/splat/dirt_normal.png"); 159 normalMap1.setWrap(WrapMode.Repeat); 160 Texture normalMap2 = assetManager.loadTexture("Textures/Terrain/splat/road_normal.png"); 161 normalMap2.setWrap(WrapMode.Repeat); 162 matRock.setTexture("NormalMap", normalMap0); 163 matRock.setTexture("NormalMap_1", normalMap2); 164 matRock.setTexture("NormalMap_2", normalMap2); 165 166 AbstractHeightMap heightmap = null; 167 try { 168 heightmap = new ImageBasedHeightMap(heightMapImage.getImage(), 0.25f); 169 heightmap.load(); 170 } catch (Exception e) { 171 e.printStackTrace(); 172 } 173 terrain = new TerrainQuad("terrain", 65, 513, heightmap.getHeightMap()); 174 List<Camera> cameras = new ArrayList<Camera>(); 175 cameras.add(getCamera()); 176 terrain.setMaterial(matRock); 177 terrain.setLocalScale(new Vector3f(5, 5, 5)); 178 terrain.setLocalTranslation(new Vector3f(0, -30, 0)); 179 terrain.setLocked(false); // unlock it so we can edit the height 180 181 terrain.setShadowMode(ShadowMode.Receive); 182 rootNode.attachChild(terrain); 183 184 } 185 186 @Override simpleUpdate(float tpf)187 public void simpleUpdate(float tpf) { 188 Vector3f origin = cam.getWorldCoordinates(new Vector2f(settings.getWidth() / 2, settings.getHeight() / 2), 0.0f); 189 Vector3f direction = cam.getWorldCoordinates(new Vector2f(settings.getWidth() / 2, settings.getHeight() / 2), 0.3f); 190 direction.subtractLocal(origin).normalizeLocal(); 191 Ray ray = new Ray(origin, direction); 192 CollisionResults results = new CollisionResults(); 193 int numCollisions = terrain.collideWith(ray, results); 194 if (numCollisions > 0) { 195 CollisionResult hit = results.getClosestCollision(); 196 fpsText.setText(""+hit.getDistance()); 197 dofFilter.setFocusDistance(hit.getDistance()/10.0f); 198 } 199 } 200 } 201