1/*
2 * Copyright (c) 2009-2012 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 */
32package com.jme3.post.filters;
33
34import com.jme3.asset.AssetManager;
35import com.jme3.export.InputCapsule;
36import com.jme3.export.JmeExporter;
37import com.jme3.export.JmeImporter;
38import com.jme3.export.OutputCapsule;
39import com.jme3.material.Material;
40import com.jme3.post.Filter;
41import com.jme3.renderer.RenderManager;
42import com.jme3.renderer.ViewPort;
43import com.jme3.shader.VarType;
44import java.io.IOException;
45
46/**
47 * Radially blurs the scene from the center of it
48 * @author Rémy Bouquet aka Nehon
49 */
50public class RadialBlurFilter extends Filter {
51
52    private float sampleDist = 1.0f;
53    private float sampleStrength = 2.2f;
54    private float[] samples = {-0.08f, -0.05f, -0.03f, -0.02f, -0.01f, 0.01f, 0.02f, 0.03f, 0.05f, 0.08f};
55
56    /**
57     * Creates a RadialBlurFilter
58     */
59    public RadialBlurFilter() {
60        super("Radial blur");
61    }
62
63    /**
64     * Creates a RadialBlurFilter
65     * @param sampleDist the distance between samples
66     * @param sampleStrength the strenght of each sample
67     */
68    public RadialBlurFilter(float sampleDist, float sampleStrength) {
69        this();
70        this.sampleDist = sampleDist;
71        this.sampleStrength = sampleStrength;
72    }
73
74    @Override
75    protected Material getMaterial() {
76
77        material.setFloat("SampleDist", sampleDist);
78        material.setFloat("SampleStrength", sampleStrength);
79        material.setParam("Samples", VarType.FloatArray, samples);
80
81        return material;
82    }
83
84    /**
85     * return the sample distance
86     * @return
87     */
88    public float getSampleDistance() {
89        return sampleDist;
90    }
91
92    /**
93     * sets the samples distances default is 1
94     * @param sampleDist
95     */
96    public void setSampleDistance(float sampleDist) {
97        this.sampleDist = sampleDist;
98    }
99
100    /**
101     *
102     * @return
103     * @deprecated use {@link #getSampleDistance()}
104     */
105    @Deprecated
106    public float getSampleDist() {
107        return sampleDist;
108    }
109
110    /**
111     *
112     * @param sampleDist
113     * @deprecated use {@link #setSampleDistance(float sampleDist)}
114     */
115    @Deprecated
116    public void setSampleDist(float sampleDist) {
117        this.sampleDist = sampleDist;
118    }
119
120    /**
121     * Returns the sample Strength
122     * @return
123     */
124    public float getSampleStrength() {
125        return sampleStrength;
126    }
127
128    /**
129     * sets the sample streanght default is 2.2
130     * @param sampleStrength
131     */
132    public void setSampleStrength(float sampleStrength) {
133        this.sampleStrength = sampleStrength;
134    }
135
136    @Override
137    protected void initFilter(AssetManager manager, RenderManager renderManager, ViewPort vp, int w, int h) {
138        material = new Material(manager, "Common/MatDefs/Blur/RadialBlur.j3md");
139    }
140
141    @Override
142    public void write(JmeExporter ex) throws IOException {
143        super.write(ex);
144        OutputCapsule oc = ex.getCapsule(this);
145        oc.write(sampleDist, "sampleDist", 1.0f);
146        oc.write(sampleStrength, "sampleStrength", 2.2f);
147    }
148
149    @Override
150    public void read(JmeImporter im) throws IOException {
151        super.read(im);
152        InputCapsule ic = im.getCapsule(this);
153        sampleDist = ic.readFloat("sampleDist", 1.0f);
154        sampleStrength = ic.readFloat("sampleStrength", 2.2f);
155    }
156}
157