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.material.Material;
     36 import com.jme3.post.Filter;
     37 import com.jme3.renderer.RenderManager;
     38 import com.jme3.renderer.ViewPort;
     39 
     40 /**
     41  * A Post Processing filter to change colors appear with sharp edges as if the
     42  * available amount of colors available was not enough to draw the true image.
     43  * Possibly useful in cartoon styled games. Use the strength variable to lessen
     44  * influence of this filter on the total result. Values from 0.2 to 0.7 appear
     45  * to give nice results.
     46  *
     47  * Based on an article from Geeks3D:
     48  *    <a href="http://www.geeks3d.com/20091027/shader-library-posterization-post-processing-effect-glsl/" rel="nofollow">http://www.geeks3d.com/20091027/shader-library-posterization-post-processing-effect-glsl/</a>
     49  *
     50  * @author: Roy Straver a.k.a. Baal Garnaal
     51  */
     52 public class PosterizationFilter extends Filter {
     53 
     54     private int numColors = 8;
     55     private float gamma = 0.6f;
     56     private float strength = 1.0f;
     57 
     58     /**
     59      * Creates a posterization Filter
     60      */
     61     public PosterizationFilter() {
     62         super("PosterizationFilter");
     63     }
     64 
     65     /**
     66      * Creates a posterization Filter with the given number of colors
     67      * @param numColors
     68      */
     69     public PosterizationFilter(int numColors) {
     70         this();
     71         this.numColors = numColors;
     72     }
     73 
     74     /**
     75      * Creates a posterization Filter with the given number of colors and gamma
     76      * @param numColors
     77      * @param gamma
     78      */
     79     public PosterizationFilter(int numColors, float gamma) {
     80         this(numColors);
     81         this.gamma = gamma;
     82     }
     83 
     84     @Override
     85     protected void initFilter(AssetManager manager, RenderManager renderManager, ViewPort vp, int w, int h) {
     86         material = new Material(manager, "Common/MatDefs/Post/Posterization.j3md");
     87         material.setInt("NumColors", numColors);
     88         material.setFloat("Gamma", gamma);
     89         material.setFloat("Strength", strength);
     90     }
     91 
     92     @Override
     93     protected Material getMaterial() {
     94         return material;
     95     }
     96 
     97     /**
     98      * Sets number of color levels used to draw the screen
     99      */
    100     public void setNumColors(int numColors) {
    101         this.numColors = numColors;
    102         if (material != null) {
    103             material.setInt("NumColors", numColors);
    104         }
    105     }
    106 
    107     /**
    108      * Sets gamma level used to enhange visual quality
    109      */
    110     public void setGamma(float gamma) {
    111         this.gamma = gamma;
    112         if (material != null) {
    113             material.setFloat("Gamma", gamma);
    114         }
    115     }
    116 
    117     /**
    118      * Sets urrent strength value, i.e. influence on final image
    119      */
    120     public void setStrength(float strength) {
    121         this.strength = strength;
    122         if (material != null) {
    123             material.setFloat("Strength", strength);
    124         }
    125     }
    126 
    127     /**
    128      * Returns number of color levels used
    129      */
    130     public int getNumColors() {
    131         return numColors;
    132     }
    133 
    134     /**
    135      * Returns current gamma value
    136      */
    137     public float getGamma() {
    138         return gamma;
    139     }
    140 
    141     /**
    142      * Returns current strength value, i.e. influence on final image
    143      */
    144     public float getStrength() {
    145         return strength;
    146     }
    147 }