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