Home | History | Annotate | Download | only in filters
      1 /*
      2  * Copyright (c) 2009-2010 jMonkeyEngine
      3  * All rights reserved.
      4  *
      5  * Redistribution and use in source and binary forms, with or without
      6  * modification, are permitted provided that the following conditions are
      7  * met:
      8  *
      9  * * Redistributions of source code must retain the above copyright
     10  *   notice, this list of conditions and the following disclaimer.
     11  *
     12  * * Redistributions in binary form must reproduce the above copyright
     13  *   notice, this list of conditions and the following disclaimer in the
     14  *   documentation and/or other materials provided with the distribution.
     15  *
     16  * * Neither the name of 'jMonkeyEngine' nor the names of its contributors
     17  *   may be used to endorse or promote products derived from this software
     18  *   without specific prior written permission.
     19  *
     20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
     21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     22  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     23  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
     24  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
     25  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
     26  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
     27  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
     28  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
     29  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
     30  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     31  */
     32 package com.jme3.post.filters;
     33 
     34 import com.jme3.asset.AssetManager;
     35 import com.jme3.export.InputCapsule;
     36 import com.jme3.export.JmeExporter;
     37 import com.jme3.export.JmeImporter;
     38 import com.jme3.export.OutputCapsule;
     39 import com.jme3.material.Material;
     40 import com.jme3.post.Filter;
     41 import com.jme3.renderer.RenderManager;
     42 import com.jme3.renderer.ViewPort;
     43 import java.io.IOException;
     44 
     45 /**
     46  *
     47  * Fade Filter allows you to make an animated fade effect on a scene.
     48  * @author Rmy Bouquet aka Nehon
     49  * implemented from boxjar implementation
     50  * @see <a href="http://jmonkeyengine.org/groups/graphics/forum/topic/newbie-question-general-fade-inout-effect/#post-105559">http://jmonkeyengine.org/groups/graphics/forum/topic/newbie-question-general-fade-inout-effect/#post-105559</a>
     51  */
     52 public class FadeFilter extends Filter {
     53 
     54     private float value = 1;
     55     private boolean playing = false;
     56     private float direction = 1;
     57     private float duration = 1;
     58 
     59     /**
     60      * Creates a FadeFilter
     61      */
     62     public FadeFilter() {
     63         super("Fade In/Out");
     64     }
     65 
     66     /**
     67      * Creates a FadeFilter with the given duration
     68      * @param duration
     69      */
     70     public FadeFilter(float duration) {
     71         this();
     72         this.duration = duration;
     73     }
     74 
     75     @Override
     76     protected Material getMaterial() {
     77         material.setFloat("Value", value);
     78         return material;
     79     }
     80 
     81     @Override
     82     protected void initFilter(AssetManager manager, RenderManager renderManager, ViewPort vp, int w, int h) {
     83         material = new Material(manager, "Common/MatDefs/Post/Fade.j3md");
     84     }
     85 
     86     @Override
     87     protected void preFrame(float tpf) {
     88         if (playing) {
     89             value += tpf * direction / duration;
     90 
     91             if (direction > 0 && value > 1) {
     92                 value = 1;
     93                 playing = false;
     94                 setEnabled(false);
     95             }
     96             if (direction < 0 && value < 0) {
     97                 value = 0;
     98                 playing = false;
     99                 setEnabled(false);
    100             }
    101         }
    102     }
    103 
    104     /**
    105      * returns the duration of the effect
    106      * @return
    107      */
    108     public float getDuration() {
    109         return duration;
    110     }
    111 
    112     /**
    113      * Sets the duration of the filter default is 1 second
    114      * @param duration
    115      */
    116     public void setDuration(float duration) {
    117         this.duration = duration;
    118     }
    119 
    120     /**
    121      * fades the scene in (black to scene)
    122      */
    123     public void fadeIn() {
    124         setEnabled(true);
    125         direction = 1;
    126         playing = true;
    127     }
    128 
    129     /**
    130      * fades the scene out (scene to black)
    131      */
    132     public void fadeOut() {
    133         setEnabled(true);
    134         direction = -1;
    135         playing = true;
    136 
    137     }
    138 
    139     public void pause() {
    140         playing = false;
    141     }
    142 
    143     @Override
    144     public void write(JmeExporter ex) throws IOException {
    145         super.write(ex);
    146         OutputCapsule oc = ex.getCapsule(this);
    147         oc.write(duration, "duration", 1);
    148     }
    149 
    150     @Override
    151     public void read(JmeImporter im) throws IOException {
    152         super.read(im);
    153         InputCapsule ic = im.getCapsule(this);
    154         duration = ic.readFloat("duration", 1);
    155     }
    156 
    157     /**
    158      * return the current value of the fading
    159      * can be used to chack if fade is complete (eg value=1)
    160      * @return
    161      */
    162     public float getValue() {
    163         return value;
    164     }
    165 
    166     /**
    167      * sets the fade value
    168      * can be used to force complete black or compete scene
    169      * @param value
    170      */
    171     public void setValue(float value) {
    172         this.value = value;
    173         if (material != null) {
    174             material.setFloat("Value", value);
    175         }
    176     }
    177 }
    178