159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta/*
259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * Copyright (c) 2009-2010 jMonkeyEngine
359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * All rights reserved.
459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta *
559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * Redistribution and use in source and binary forms, with or without
659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * modification, are permitted provided that the following conditions are
759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * met:
859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta *
959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * * Redistributions of source code must retain the above copyright
1059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta *   notice, this list of conditions and the following disclaimer.
1159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta *
1259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * * Redistributions in binary form must reproduce the above copyright
1359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta *   notice, this list of conditions and the following disclaimer in the
1459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta *   documentation and/or other materials provided with the distribution.
1559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta *
1659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * * Neither the name of 'jMonkeyEngine' nor the names of its contributors
1759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta *   may be used to endorse or promote products derived from this software
1859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta *   without specific prior written permission.
1959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta *
2059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
2159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
2259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
2359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
2459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
2559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
2659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
2759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
2859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
2959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
3059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
3159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta */
3259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta
3359b2e6871c65f58fdad78cd7229c292f6a177578Scott Bartapackage jme3test.water;
3459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta
3559b2e6871c65f58fdad78cd7229c292f6a177578Scott Bartaimport com.jme3.app.SimpleApplication;
3659b2e6871c65f58fdad78cd7229c292f6a177578Scott Bartaimport com.jme3.asset.plugins.HttpZipLocator;
3759b2e6871c65f58fdad78cd7229c292f6a177578Scott Bartaimport com.jme3.asset.plugins.ZipLocator;
3859b2e6871c65f58fdad78cd7229c292f6a177578Scott Bartaimport com.jme3.light.DirectionalLight;
3959b2e6871c65f58fdad78cd7229c292f6a177578Scott Bartaimport com.jme3.material.Material;
4059b2e6871c65f58fdad78cd7229c292f6a177578Scott Bartaimport com.jme3.math.*;
4159b2e6871c65f58fdad78cd7229c292f6a177578Scott Bartaimport com.jme3.renderer.queue.RenderQueue.ShadowMode;
4259b2e6871c65f58fdad78cd7229c292f6a177578Scott Bartaimport com.jme3.scene.Geometry;
4359b2e6871c65f58fdad78cd7229c292f6a177578Scott Bartaimport com.jme3.scene.Node;
4459b2e6871c65f58fdad78cd7229c292f6a177578Scott Bartaimport com.jme3.scene.Spatial;
4559b2e6871c65f58fdad78cd7229c292f6a177578Scott Bartaimport com.jme3.scene.shape.Quad;
4659b2e6871c65f58fdad78cd7229c292f6a177578Scott Bartaimport com.jme3.scene.shape.Sphere;
4759b2e6871c65f58fdad78cd7229c292f6a177578Scott Bartaimport com.jme3.util.SkyFactory;
4859b2e6871c65f58fdad78cd7229c292f6a177578Scott Bartaimport com.jme3.water.SimpleWaterProcessor;
4959b2e6871c65f58fdad78cd7229c292f6a177578Scott Bartaimport java.io.File;
5059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta
5159b2e6871c65f58fdad78cd7229c292f6a177578Scott Bartapublic class TestSceneWater extends SimpleApplication {
5259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta
5359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    // set default for applets
5459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    private static boolean useHttp = true;
5559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta
5659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    public static void main(String[] args) {
5759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta
5859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        TestSceneWater app = new TestSceneWater();
5959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        app.start();
6059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    }
6159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta
6259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    public void simpleInitApp() {
6359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        this.flyCam.setMoveSpeed(10);
6459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        Node mainScene=new Node();
6559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        cam.setLocation(new Vector3f(-27.0f, 1.0f, 75.0f));
6659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        cam.setRotation(new Quaternion(0.03f, 0.9f, 0f, 0.4f));
6759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta
6859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        // load sky
6959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        mainScene.attachChild(SkyFactory.createSky(assetManager, "Textures/Sky/Bright/BrightSky.dds", false));
7059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta
7159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta
7259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        File file = new File("wildhouse.zip");
7359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        if (file.exists()) {
7459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta            useHttp = false;
7559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        }
7659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        // create the geometry and attach it
7759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        // load the level from zip or http zip
7859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        if (useHttp) {
7959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta            assetManager.registerLocator("http://jmonkeyengine.googlecode.com/files/wildhouse.zip", HttpZipLocator.class.getName());
8059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        } else {
8159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta            assetManager.registerLocator("wildhouse.zip", ZipLocator.class.getName());
8259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        }
8359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        Spatial scene = assetManager.loadModel("main.scene");
8459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta
8559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        DirectionalLight sun = new DirectionalLight();
8659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        Vector3f lightDir=new Vector3f(-0.37352666f, -0.50444174f, -0.7784704f);
8759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        sun.setDirection(lightDir);
8859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        sun.setColor(ColorRGBA.White.clone().multLocal(2));
8959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        scene.addLight(sun);
9059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta
9159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        Material mat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
9259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        mat.setTexture("ColorMap", assetManager.loadTexture("Interface/Logo/Monkey.jpg"));
9359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta           //add lightPos Geometry
9459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        Sphere lite=new Sphere(8, 8, 3.0f);
9559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        Geometry lightSphere=new Geometry("lightsphere", lite);
9659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        lightSphere.setMaterial(mat);
9759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        Vector3f lightPos=lightDir.multLocal(-400);
9859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        lightSphere.setLocalTranslation(lightPos);
9959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        rootNode.attachChild(lightSphere);
10059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta
10159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta
10259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        SimpleWaterProcessor waterProcessor = new SimpleWaterProcessor(assetManager);
10359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        waterProcessor.setReflectionScene(mainScene);
10459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        waterProcessor.setDebug(false);
10559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        waterProcessor.setLightPosition(lightPos);
10659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        waterProcessor.setRefractionClippingOffset(1.0f);
10759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta
10859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta
10959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        //setting the water plane
11059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        Vector3f waterLocation=new Vector3f(0,-20,0);
11159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        waterProcessor.setPlane(new Plane(Vector3f.UNIT_Y, waterLocation.dot(Vector3f.UNIT_Y)));
11259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        WaterUI waterUi=new WaterUI(inputManager, waterProcessor);
11359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        waterProcessor.setWaterColor(ColorRGBA.Brown);
11459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        waterProcessor.setDebug(true);
11559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        //lower render size for higher performance
11659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta//        waterProcessor.setRenderSize(128,128);
11759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        //raise depth to see through water
11859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta//        waterProcessor.setWaterDepth(20);
11959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        //lower the distortion scale if the waves appear too strong
12059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta//        waterProcessor.setDistortionScale(0.1f);
12159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        //lower the speed of the waves if they are too fast
12259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta//        waterProcessor.setWaveSpeed(0.01f);
12359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta
12459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        Quad quad = new Quad(400,400);
12559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta
12659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        //the texture coordinates define the general size of the waves
12759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        quad.scaleTextureCoordinates(new Vector2f(6f,6f));
12859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta
12959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        Geometry water=new Geometry("water", quad);
13059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        water.setShadowMode(ShadowMode.Receive);
13159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        water.setLocalRotation(new Quaternion().fromAngleAxis(-FastMath.HALF_PI, Vector3f.UNIT_X));
13259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        water.setMaterial(waterProcessor.getMaterial());
13359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        water.setLocalTranslation(-200, -20, 250);
13459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta
13559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        rootNode.attachChild(water);
13659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta
13759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        viewPort.addProcessor(waterProcessor);
13859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta
13959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        mainScene.attachChild(scene);
14059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        rootNode.attachChild(mainScene);
14159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    }
14259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta}
143