OSDN Git Service

80accde34533a7f29437edbfbb5206aba0dfb2b2
[mikumikustudio/MikuMikuStudio.git] / src / jmetest / effects / TestParticleSystem.java
1 /*
2  * Copyright (c) 2003-2009 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
33 package jmetest.effects;
34
35 import com.jme.app.SimpleGame;
36 import com.jme.bounding.BoundingSphere;
37 import com.jme.image.Texture;
38 import com.jme.math.FastMath;
39 import com.jme.math.Vector3f;
40 import com.jme.renderer.ColorRGBA;
41 import com.jme.scene.state.BlendState;
42 import com.jme.scene.state.TextureState;
43 import com.jme.scene.state.ZBufferState;
44 import com.jme.util.TextureManager;
45 import com.jmex.effects.particles.ParticleFactory;
46 import com.jmex.effects.particles.ParticleMesh;
47
48 /**
49  * @author Joshua Slack
50  * @version $Id: TestParticleSystem.java,v 1.34 2006/07/06 22:22:18 nca Exp $
51  */
52 public class TestParticleSystem extends SimpleGame {
53
54   private ParticleMesh pMesh;
55   private Vector3f currentPos = new Vector3f(), newPos = new Vector3f();
56   private float frameRate = 0;
57
58   public static void main(String[] args) {
59     TestParticleSystem app = new TestParticleSystem();
60     app.setConfigShowMode(ConfigShowMode.AlwaysShow);
61     app.start();
62   }
63
64   protected void simpleUpdate() {
65     if (tpf > 1f) tpf = 1.0f; // do this to prevent a long pause at start
66
67     if ( (int) currentPos.x == (int) newPos.x
68         && (int) currentPos.y == (int) newPos.y
69         && (int) currentPos.z == (int) newPos.z) {
70       newPos.x = (float) Math.random() * 50 - 25;
71       newPos.y = (float) Math.random() * 50 - 25;
72       newPos.z = (float) Math.random() * 50 - 150;
73     }
74
75     frameRate = timer.getFrameRate() / 2;
76     currentPos.x -= (currentPos.x - newPos.x)
77         / frameRate;
78     currentPos.y -= (currentPos.y - newPos.y)
79         / frameRate;
80     currentPos.z -= (currentPos.z - newPos.z)
81         / frameRate;
82
83     rootNode.setLocalTranslation(currentPos);
84
85   }
86
87   protected void simpleInitGame() {
88     display.setTitle("Particle System");
89     lightState.setEnabled(false);
90
91     BlendState as1 = display.getRenderer().createBlendState();
92     as1.setBlendEnabled(true);
93     as1.setSourceFunction(BlendState.SourceFunction.SourceAlpha);
94     as1.setDestinationFunction(BlendState.DestinationFunction.One);
95     as1.setTestEnabled(true);
96     as1.setTestFunction(BlendState.TestFunction.GreaterThan);
97     as1.setEnabled(true);
98     as1.setEnabled(true);
99
100     TextureState ts = display.getRenderer().createTextureState();
101     ts.setTexture(
102         TextureManager.loadTexture(
103         TestParticleSystem.class.getClassLoader().getResource(
104         "jmetest/data/texture/flaresmall.jpg"),
105         Texture.MinificationFilter.Trilinear,
106         Texture.MagnificationFilter.Bilinear));
107     ts.setEnabled(true);
108
109     pMesh = ParticleFactory.buildParticles("particles", 300);
110     pMesh.setEmissionDirection(new Vector3f(0,1,0));
111     pMesh.setInitialVelocity(.006f);
112     pMesh.setStartSize(2.5f);
113     pMesh.setEndSize(.5f);
114     pMesh.setMinimumLifeTime(1200f);
115     pMesh.setMaximumLifeTime(1400f);
116     pMesh.setStartColor(new ColorRGBA(1, 0, 0, 1));
117     pMesh.setEndColor(new ColorRGBA(0, 1, 0, 0));
118     pMesh.setMaximumAngle(360f * FastMath.DEG_TO_RAD);
119     pMesh.getParticleController().setControlFlow(false);
120     pMesh.setParticlesInWorldCoords(true);
121     pMesh.warmUp(60);
122
123     rootNode.setRenderState(ts);
124     rootNode.setRenderState(as1);
125                 ZBufferState zstate = display.getRenderer().createZBufferState();
126                 zstate.setEnabled(false);
127                 pMesh.setRenderState(zstate);
128     pMesh.setModelBound(new BoundingSphere());
129     pMesh.updateModelBound();
130
131     rootNode.attachChild(pMesh);
132   }
133 }