2 * Copyright (c) 2003-2009 jMonkeyEngine
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
9 * * Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
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.
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.
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.
33 package jmetest.awt.applet;
35 import com.jme.bounding.BoundingBox;
36 import com.jme.image.Texture;
37 import com.jme.math.FastMath;
38 import com.jme.math.Vector3f;
39 import com.jme.renderer.ColorRGBA;
40 import com.jme.renderer.Renderer;
41 import com.jme.scene.shape.Sphere;
42 import com.jme.scene.state.BlendState;
43 import com.jme.scene.state.TextureState;
44 import com.jme.scene.state.ZBufferState;
45 import com.jme.util.TextureManager;
46 import com.jmex.awt.applet.SimpleJMEApplet;
47 import com.jmex.effects.particles.ParticleFactory;
48 import com.jmex.effects.particles.ParticleSystem;
49 import com.jmex.effects.particles.SwarmInfluence;
51 public class AppletTestParticles extends SimpleJMEApplet {
52 private static final long serialVersionUID = 1L;
54 private ParticleSystem particles;
55 private Vector3f currentPos = new Vector3f(), newPos = new Vector3f();
56 private SwarmInfluence swarm;
57 private Sphere sphere;
59 public void simpleAppletUpdate() {
60 float tpf = getTimePerFrame();
62 tpf = 1.0f; // do this to prevent a long pause at start
64 if ((int) currentPos.x == (int) newPos.x
65 && (int) currentPos.y == (int) newPos.y
66 && (int) currentPos.z == (int) newPos.z) {
67 newPos.x = (float) Math.random() * 50 - 25;
68 newPos.y = (float) Math.random() * 50 - 25;
69 newPos.z = (float) Math.random() * 50 - 150;
72 if (!Float.isInfinite(tpf) && !Float.isNaN(tpf)) {
73 currentPos.x -= (currentPos.x - newPos.x) * tpf;
74 currentPos.y -= (currentPos.y - newPos.y) * tpf;
75 currentPos.z -= (currentPos.z - newPos.z) * tpf;
78 particles.setOriginOffset(currentPos);
79 sphere.getLocalTranslation().set(currentPos);
82 public void simpleAppletSetup() {
83 getLightState().setEnabled(false);
85 sphere = new Sphere("sp", 12, 12, 3f);
86 sphere.setModelBound(new BoundingBox());
87 sphere.updateModelBound();
88 sphere.setDefaultColor(ColorRGBA.blue.clone());
89 sphere.setRenderQueueMode(Renderer.QUEUE_OPAQUE);
91 particles = ParticleFactory.buildParticles("particles", 30);
92 particles.setEmissionDirection(new Vector3f(0, 1, 0));
93 particles.setStartSize(3f);
94 particles.setEndSize(1.5f);
95 particles.setOriginOffset(new Vector3f(0, 0, 0));
96 particles.setInitialVelocity(.05f);
97 particles.setMinimumLifeTime( 5000f);
98 particles.setMaximumLifeTime(15000f);
99 particles.setStartColor(new ColorRGBA(1, 0, 0, 1));
100 particles.setEndColor(new ColorRGBA(0, 1, 0, 0));
101 particles.setMaximumAngle(360f * FastMath.DEG_TO_RAD);
102 particles.getParticleController().setControlFlow(false);
103 particles.getParticleController().setSpeed(0.75f);
104 swarm = new SwarmInfluence(new Vector3f(particles.getWorldTranslation()), .001f);
105 swarm.setMaxSpeed(.2f);
106 swarm.setSpeedBump(0.025f);
107 swarm.setTurnSpeed(FastMath.DEG_TO_RAD * 360);
108 particles.addInfluence(swarm);
109 particles.warmUp(60);
111 BlendState as1 = getRenderer().createBlendState();
112 as1.setBlendEnabled(true);
113 as1.setSourceFunction(BlendState.SourceFunction.SourceAlpha);
114 as1.setDestinationFunction(BlendState.DestinationFunction.One);
115 as1.setTestEnabled(true);
116 as1.setTestFunction(BlendState.TestFunction.GreaterThan);
117 as1.setEnabled(true);
118 particles.setRenderState(as1);
120 TextureState ts = getRenderer().createTextureState();
122 TextureManager.loadTexture(
123 AppletTestParticles.class.getClassLoader().getResource(
124 "jmetest/data/texture/flaresmall.jpg"),
125 Texture.MinificationFilter.Trilinear,
126 Texture.MagnificationFilter.Bilinear));
128 particles.setRenderState(ts);
130 ZBufferState zstate = getRenderer().createZBufferState();
131 zstate.setEnabled(false);
132 particles.setRenderState(zstate);
134 particles.setModelBound(new BoundingBox());
135 particles.updateModelBound();
137 getRootNode().attachChild(particles);
138 getRootNode().attachChild(sphere);