]> gerrit.simantics Code Review - simantics/3d.git/blob
ed5c216dd0d30dd39560a6e9487949f266d525ad
[simantics/3d.git] /
1 /*******************************************************************************\r
2  * Copyright (c) 2007- VTT Technical Research Centre of Finland.\r
3  * All rights reserved. This program and the accompanying materials\r
4  * are made available under the terms of the Eclipse Public License v1.0\r
5  * which accompanies this distribution, and is available at\r
6  * http://www.eclipse.org/legal/epl-v10.html\r
7  *\r
8  * Contributors:\r
9  *     VTT Technical Research Centre of Finland - initial API and implementation\r
10  *******************************************************************************/\r
11 package org.simantics.processeditor.animations;\r
12 \r
13 import java.util.List;\r
14 \r
15 import javax.vecmath.Point3d;\r
16 import javax.vecmath.Vector3d;\r
17 \r
18 import org.simantics.db.Graph;\r
19 import org.simantics.db.Resource;\r
20 import org.simantics.processeditor.common.ControlPointTools;\r
21 import org.simantics.processeditor.stubs.PipeControlPoint;\r
22 import org.simantics.processeditor.stubs.VariableLengthInlineComponent;\r
23 import org.simantics.proconf.g3d.animation.Animatable;\r
24 import org.simantics.proconf.g3d.base.VecmathJmeTools;\r
25 \r
26 import com.jme.renderer.ColorRGBA;\r
27 import com.jme.scene.Node;\r
28 import com.jmex.effects.particles.ParticleFactory;\r
29 import com.jmex.effects.particles.ParticleGeometry;\r
30 \r
31 \r
32 \r
33 \r
34 public class PipeFlowAnimation implements Animatable{ \r
35         \r
36         ParticleGeometry particle;\r
37         float length;\r
38         int numParticles;\r
39         \r
40         public PipeFlowAnimation(List<ParticleGeometry> pipeAnimations, Node particleNode, VariableLengthInlineComponent s, boolean reversed) {\r
41         PipeControlPoint pcp = s.getControlPoint();\r
42         \r
43         Point3d p1 = new Point3d();\r
44         Point3d p2 = new Point3d();\r
45         \r
46         ControlPointTools.getInlineControlPointEnds(pcp, p2, p1);\r
47 \r
48         if (reversed) {\r
49                 Point3d t = p1;\r
50                 p1 = p2;\r
51                 p2 = t;\r
52         }\r
53         \r
54         \r
55         Vector3d dir = new Vector3d(p2);\r
56         dir.sub(p1);\r
57         length = (float)dir.length();\r
58         //if (length < 0.1f)\r
59         //      return;\r
60                 //continue;\r
61         // with longer pipes particles will travel too far so the length of the pipe must be scaled\r
62         length *= 0.83f;\r
63         float vel = 0.1f;\r
64         //float size = (float)s.getPipeRadiusValue() + 0.1f;\r
65         //size *= 2.f;\r
66         float size = (float)s.getPipeDiameter()[0];\r
67         //size *= 1.2f;\r
68         float life = length/vel;\r
69         \r
70         //int releaseRate = 40;\r
71         //int numParticles = (int)(releaseRate * life / 100.f);//(int)(releaseRate * life / 500.f);\r
72         \r
73         numParticles = (int)(length * 2.0);\r
74         int releaseRate = (int)((numParticles * 500.0) / life);\r
75         \r
76         if (numParticles < 2)\r
77                 numParticles = 2;\r
78         \r
79         particle = ParticleFactory.buildParticles("Animation of " + s.getResource().getResourceId(),numParticles, ParticleGeometry.PT_QUAD);//new ParticleMesh("Animation of " + r.getId(),40);\r
80         particle.setEmissionDirection(VecmathJmeTools.get(dir).normalize());\r
81         particle.setLocalTranslation(VecmathJmeTools.get(p1));\r
82         particle.setEmitType(ParticleGeometry.ET_POINT);\r
83         particle.setInitialVelocity(vel);\r
84         particle.setMinimumAngle(0.f);\r
85         particle.setMaximumAngle(0.f);\r
86         particle.setReleaseRate(releaseRate);\r
87         particle.getParticleController().setReleaseVariance(0.f);\r
88         particle.getParticleController().setControlFlow(true);\r
89         particle.setStartColor(new ColorRGBA(1.f,1.f,0.f,1.f));\r
90         particle.setEndColor(new ColorRGBA(1.f,1.f,0.f,1.f));\r
91         particle.setStartSize(size);\r
92         particle.setEndSize(size);\r
93         setFlow(vel);\r
94         particle.getParticleController().setSpeed(vel);\r
95         particle.warmUp(60);\r
96         pipeAnimations.add(particle);\r
97         particleNode.attachChild(particle);\r
98         }\r
99         \r
100         ParticleGeometry getParticleGeometry() {\r
101                 return particle;\r
102         }\r
103         \r
104         private void setFlow(float vel) {\r
105                 float life;\r
106                 if (vel > 0.f)\r
107                         life = length/vel;\r
108                 else\r
109                         life = 0.f;\r
110                 particle.setInitialVelocity(vel);\r
111         particle.setMaximumLifeTime(life);\r
112         particle.setMinimumLifeTime(life);\r
113         particle.getParticleController().setSpeed(vel);\r
114 \r
115         }\r
116         \r
117         private void setReleaserate(float vel) {\r
118                 float life;\r
119                 if (vel > 0.f)\r
120                         life = length/vel;\r
121                 else\r
122                         life = 0.f;\r
123                 particle.setInitialVelocity(vel);\r
124         particle.setMaximumLifeTime(life);\r
125         particle.setMinimumLifeTime(life);\r
126         particle.setReleaseRate((int)((numParticles * 500.0) / life));\r
127         }\r
128         \r
129         public void animate(double delta,double frameRate) {\r
130                 //setFlow(0.1f*(float)delta);\r
131                 setReleaserate(0.05f*(float)delta);\r
132         }\r
133         \r
134         public boolean setAnimation(Graph graph, Resource animation) {\r
135                 return false;\r
136         }\r
137         \r
138         public boolean setRandomAnimation(Graph graph) {\r
139                 return false;\r
140         }\r
141 \r
142 }\r