OSDN Git Service

Update terrain.
[mikumikustudio/MikuMikuStudio.git] / engine / src / test / jme3test / terrain / TerrainTestAndroid.java
diff --git a/engine/src/test/jme3test/terrain/TerrainTestAndroid.java b/engine/src/test/jme3test/terrain/TerrainTestAndroid.java
new file mode 100644 (file)
index 0000000..d5b74b2
--- /dev/null
@@ -0,0 +1,200 @@
+/*\r
+ * Copyright (c) 2009-2012 jMonkeyEngine\r
+ * All rights reserved.\r
+ *\r
+ * Redistribution and use in source and binary forms, with or without\r
+ * modification, are permitted provided that the following conditions are\r
+ * met:\r
+ *\r
+ * * Redistributions of source code must retain the above copyright\r
+ *   notice, this list of conditions and the following disclaimer.\r
+ *\r
+ * * Redistributions in binary form must reproduce the above copyright\r
+ *   notice, this list of conditions and the following disclaimer in the\r
+ *   documentation and/or other materials provided with the distribution.\r
+ *\r
+ * * Neither the name of 'jMonkeyEngine' nor the names of its contributors\r
+ *   may be used to endorse or promote products derived from this software\r
+ *   without specific prior written permission.\r
+ *\r
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS\r
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED\r
+ * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR\r
+ * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR\r
+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,\r
+ * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,\r
+ * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR\r
+ * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF\r
+ * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING\r
+ * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r
+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r
+ */\r
+package jme3test.terrain;\r
+\r
+import com.jme3.app.SimpleApplication;\r
+import com.jme3.font.BitmapText;\r
+import com.jme3.input.KeyInput;\r
+import com.jme3.input.controls.ActionListener;\r
+import com.jme3.input.controls.KeyTrigger;\r
+import com.jme3.light.DirectionalLight;\r
+import com.jme3.light.PointLight;\r
+import com.jme3.material.Material;\r
+import com.jme3.math.ColorRGBA;\r
+import com.jme3.math.Vector3f;\r
+import com.jme3.scene.Geometry;\r
+import com.jme3.terrain.geomipmap.TerrainLodControl;\r
+import com.jme3.terrain.geomipmap.TerrainQuad;\r
+import com.jme3.terrain.geomipmap.lodcalc.DistanceLodCalculator;\r
+import com.jme3.terrain.heightmap.AbstractHeightMap;\r
+import com.jme3.terrain.heightmap.ImageBasedHeightMap;\r
+import com.jme3.texture.Texture;\r
+import com.jme3.texture.Texture.WrapMode;\r
+\r
+/**\r
+ * Demonstrates how to use terrain on Android.\r
+ * The only difference is it uses a much smaller heightmap so it won't use up\r
+ * all of the android device's memory.\r
+ *\r
+ * @author bowens\r
+ */\r
+public class TerrainTestAndroid extends SimpleApplication {\r
+\r
+    private TerrainQuad terrain;\r
+    Material matRock;\r
+    Material matWire;\r
+    boolean wireframe = false;\r
+    boolean triPlanar = false;\r
+    protected BitmapText hintText;\r
+    PointLight pl;\r
+    Geometry lightMdl;\r
+    private float grassScale = 64;\r
+    private float dirtScale = 16;\r
+    private float rockScale = 128;\r
+\r
+    public static void main(String[] args) {\r
+        TerrainTestAndroid app = new TerrainTestAndroid();\r
+        app.start();\r
+    }\r
+\r
+    @Override\r
+    public void initialize() {\r
+        super.initialize();\r
+\r
+        loadHintText();\r
+    }\r
+\r
+    @Override\r
+    public void simpleInitApp() {\r
+        setupKeys();\r
+\r
+        // First, we load up our textures and the heightmap texture for the terrain\r
+\r
+        // TERRAIN TEXTURE material\r
+        matRock = new Material(assetManager, "Common/MatDefs/Terrain/Terrain.j3md");\r
+        matRock.setBoolean("useTriPlanarMapping", false);\r
+\r
+        // ALPHA map (for splat textures)\r
+        matRock.setTexture("Alpha", assetManager.loadTexture("Textures/Terrain/splat/alphamap.png"));\r
+\r
+        // HEIGHTMAP image (for the terrain heightmap)\r
+        Texture heightMapImage = assetManager.loadTexture("Textures/Terrain/splat/mountains128.png");\r
+\r
+        // GRASS texture\r
+        Texture grass = assetManager.loadTexture("Textures/Terrain/splat/grass.jpg");\r
+        grass.setWrap(WrapMode.Repeat);\r
+        matRock.setTexture("Tex1", grass);\r
+        matRock.setFloat("Tex1Scale", grassScale);\r
+\r
+        // DIRT texture\r
+        Texture dirt = assetManager.loadTexture("Textures/Terrain/splat/dirt.jpg");\r
+        dirt.setWrap(WrapMode.Repeat);\r
+        matRock.setTexture("Tex2", dirt);\r
+        matRock.setFloat("Tex2Scale", dirtScale);\r
+\r
+        // ROCK texture\r
+        Texture rock = assetManager.loadTexture("Textures/Terrain/splat/road.jpg");\r
+        rock.setWrap(WrapMode.Repeat);\r
+        matRock.setTexture("Tex3", rock);\r
+        matRock.setFloat("Tex3Scale", rockScale);\r
+\r
+        // WIREFRAME material\r
+        matWire = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");\r
+        matWire.getAdditionalRenderState().setWireframe(true);\r
+        matWire.setColor("Color", ColorRGBA.Green);\r
+\r
+        // CREATE HEIGHTMAP\r
+        AbstractHeightMap heightmap = null;\r
+        try {\r
+            heightmap = new ImageBasedHeightMap(heightMapImage.getImage(), 1f);\r
+            heightmap.load();\r
+\r
+        } catch (Exception e) {\r
+            e.printStackTrace();\r
+        }\r
+\r
+        /*\r
+         * Here we create the actual terrain. The tiles will be 33x33, and the total size of the\r
+         * terrain will be 129x129. It uses the heightmap we created to generate the height values.\r
+         */\r
+        terrain = new TerrainQuad("terrain", 33, 129, heightmap.getHeightMap());\r
+        TerrainLodControl control = new TerrainLodControl(terrain, getCamera());\r
+        control.setLodCalculator( new DistanceLodCalculator(33, 2.7f) ); // patch size, and a multiplier\r
+        terrain.addControl(control);\r
+        terrain.setMaterial(matRock);\r
+        terrain.setLocalTranslation(0, -100, 0);\r
+        terrain.setLocalScale(8f, 0.5f, 8f);\r
+        rootNode.attachChild(terrain);\r
+\r
+        DirectionalLight light = new DirectionalLight();\r
+        light.setDirection((new Vector3f(-0.5f, -1f, -0.5f)).normalize());\r
+        rootNode.addLight(light);\r
+\r
+        cam.setLocation(new Vector3f(0, 10, -10));\r
+        cam.lookAtDirection(new Vector3f(0, -1.5f, -1).normalizeLocal(), Vector3f.UNIT_Y);\r
+    }\r
+\r
+    public void loadHintText() {\r
+        hintText = new BitmapText(guiFont, false);\r
+        hintText.setSize(guiFont.getCharSet().getRenderedSize());\r
+        hintText.setLocalTranslation(0, getCamera().getHeight(), 0);\r
+        hintText.setText("Hit T to switch to wireframe,  P to switch to tri-planar texturing");\r
+        guiNode.attachChild(hintText);\r
+    }\r
+\r
+    private void setupKeys() {\r
+        flyCam.setMoveSpeed(50);\r
+        inputManager.addMapping("wireframe", new KeyTrigger(KeyInput.KEY_T));\r
+        inputManager.addListener(actionListener, "wireframe");\r
+        inputManager.addMapping("triPlanar", new KeyTrigger(KeyInput.KEY_P));\r
+        inputManager.addListener(actionListener, "triPlanar");\r
+    }\r
+    private ActionListener actionListener = new ActionListener() {\r
+\r
+        public void onAction(String name, boolean pressed, float tpf) {\r
+            if (name.equals("wireframe") && !pressed) {\r
+                wireframe = !wireframe;\r
+                if (!wireframe) {\r
+                    terrain.setMaterial(matWire);\r
+                } else {\r
+                    terrain.setMaterial(matRock);\r
+                }\r
+            } else if (name.equals("triPlanar") && !pressed) {\r
+                triPlanar = !triPlanar;\r
+                if (triPlanar) {\r
+                    matRock.setBoolean("useTriPlanarMapping", true);\r
+                    // planar textures don't use the mesh's texture coordinates but real world coordinates,\r
+                    // so we need to convert these texture coordinate scales into real world scales so it looks\r
+                    // the same when we switch to/from tr-planar mode\r
+                    matRock.setFloat("Tex1Scale", 1f / (float) (512f / grassScale));\r
+                    matRock.setFloat("Tex2Scale", 1f / (float) (512f / dirtScale));\r
+                    matRock.setFloat("Tex3Scale", 1f / (float) (512f / rockScale));\r
+                } else {\r
+                    matRock.setBoolean("useTriPlanarMapping", false);\r
+                    matRock.setFloat("Tex1Scale", grassScale);\r
+                    matRock.setFloat("Tex2Scale", dirtScale);\r
+                    matRock.setFloat("Tex3Scale", rockScale);\r
+                }\r
+            }\r
+        }\r
+    };\r
+}\r