159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta/*
259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * Copyright (c) 2009-2012 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 Bartapackage com.jme3.post.filters;
3359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta
3459b2e6871c65f58fdad78cd7229c292f6a177578Scott Bartaimport com.jme3.asset.AssetManager;
3559b2e6871c65f58fdad78cd7229c292f6a177578Scott Bartaimport com.jme3.export.InputCapsule;
3659b2e6871c65f58fdad78cd7229c292f6a177578Scott Bartaimport com.jme3.export.JmeExporter;
3759b2e6871c65f58fdad78cd7229c292f6a177578Scott Bartaimport com.jme3.export.JmeImporter;
3859b2e6871c65f58fdad78cd7229c292f6a177578Scott Bartaimport com.jme3.export.OutputCapsule;
3959b2e6871c65f58fdad78cd7229c292f6a177578Scott Bartaimport com.jme3.material.Material;
4059b2e6871c65f58fdad78cd7229c292f6a177578Scott Bartaimport com.jme3.math.ColorRGBA;
4159b2e6871c65f58fdad78cd7229c292f6a177578Scott Bartaimport com.jme3.post.Filter;
4259b2e6871c65f58fdad78cd7229c292f6a177578Scott Bartaimport com.jme3.renderer.RenderManager;
4359b2e6871c65f58fdad78cd7229c292f6a177578Scott Bartaimport com.jme3.renderer.ViewPort;
4459b2e6871c65f58fdad78cd7229c292f6a177578Scott Bartaimport java.io.IOException;
4559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta
4659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta/**
4759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * A filter to render a fog effect
4859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * @author Rémy Bouquet aka Nehon
4959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta */
5059b2e6871c65f58fdad78cd7229c292f6a177578Scott Bartapublic class FogFilter extends Filter {
5159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta
5259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    private ColorRGBA fogColor = ColorRGBA.White.clone();
5359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    private float fogDensity = 0.7f;
5459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    private float fogDistance = 1000;
5559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta
5659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    /**
5759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta     * Creates a FogFilter
5859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta     */
5959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    public FogFilter() {
6059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        super("FogFilter");
6159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    }
6259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta
6359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    /**
6459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta     * Create a fog filter
6559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta     * @param fogColor the color of the fog (default is white)
6659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta     * @param fogDensity the density of the fog (default is 0.7)
6759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta     * @param fogDistance the distance of the fog (default is 1000)
6859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta     */
6959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    public FogFilter(ColorRGBA fogColor, float fogDensity, float fogDistance) {
7059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        this();
7159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        this.fogColor = fogColor;
7259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        this.fogDensity = fogDensity;
7359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        this.fogDistance = fogDistance;
7459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    }
7559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta
7659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    @Override
7759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    protected boolean isRequiresDepthTexture() {
7859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        return true;
7959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    }
8059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta
8159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    @Override
8259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    protected void initFilter(AssetManager manager, RenderManager renderManager, ViewPort vp, int w, int h) {
8359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        material = new Material(manager, "Common/MatDefs/Post/Fog.j3md");
8459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        material.setColor("FogColor", fogColor);
8559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        material.setFloat("FogDensity", fogDensity);
8659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        material.setFloat("FogDistance", fogDistance);
8759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    }
8859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta
8959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    @Override
9059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    protected Material getMaterial() {
9159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta
9259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        return material;
9359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    }
9459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta
9559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta
9659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    /**
9759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta     * returns the fog color
9859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta     * @return
9959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta     */
10059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    public ColorRGBA getFogColor() {
10159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        return fogColor;
10259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    }
10359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta
10459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    /**
10559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta     * Sets the color of the fog
10659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta     * @param fogColor
10759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta     */
10859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    public void setFogColor(ColorRGBA fogColor) {
10959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        if (material != null) {
11059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta            material.setColor("FogColor", fogColor);
11159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        }
11259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        this.fogColor = fogColor;
11359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    }
11459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta
11559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    /**
11659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta     * returns the fog density
11759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta     * @return
11859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta     */
11959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    public float getFogDensity() {
12059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        return fogDensity;
12159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    }
12259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta
12359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    /**
12459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta     * Sets the density of the fog, a high value gives a thick fog
12559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta     * @param fogDensity
12659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta     */
12759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    public void setFogDensity(float fogDensity) {
12859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        if (material != null) {
12959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta            material.setFloat("FogDensity", fogDensity);
13059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        }
13159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        this.fogDensity = fogDensity;
13259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    }
13359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta
13459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    /**
13559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta     * returns the fog distance
13659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta     * @return
13759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta     */
13859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    public float getFogDistance() {
13959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        return fogDistance;
14059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    }
14159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta
14259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    /**
14359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta     * the distance of the fog. the higer the value the distant the fog looks
14459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta     * @param fogDistance
14559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta     */
14659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    public void setFogDistance(float fogDistance) {
14759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        if (material != null) {
14859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta            material.setFloat("FogDistance", fogDistance);
14959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        }
15059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        this.fogDistance = fogDistance;
15159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    }
15259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta
15359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    @Override
15459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    public void write(JmeExporter ex) throws IOException {
15559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        super.write(ex);
15659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        OutputCapsule oc = ex.getCapsule(this);
15759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        oc.write(fogColor, "fogColor", ColorRGBA.White.clone());
15859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        oc.write(fogDensity, "fogDensity", 0.7f);
15959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        oc.write(fogDistance, "fogDistance", 1000);
16059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    }
16159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta
16259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    @Override
16359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    public void read(JmeImporter im) throws IOException {
16459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        super.read(im);
16559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        InputCapsule ic = im.getCapsule(this);
16659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        fogColor = (ColorRGBA) ic.readSavable("fogColor", ColorRGBA.White.clone());
16759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        fogDensity = ic.readFloat("fogDensity", 0.7f);
16859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        fogDistance = ic.readFloat("fogDistance", 1000);
16959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    }
17059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta
17159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta
17259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta}
173