]> gerrit.simantics Code Review - simantics/3d.git/blob - dev/org.simantics.proconf.processeditor/src/fi/vtt/simantics/processeditor/animations/PipeAnimationController.java
0524171f9602445025cb3dedf159c6f6819215ab
[simantics/3d.git] / dev / org.simantics.proconf.processeditor / src / fi / vtt / simantics / processeditor / animations / PipeAnimationController.java
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.net.URL;\r
14 import java.util.ArrayList;\r
15 import java.util.List;\r
16 \r
17 import org.eclipse.core.runtime.FileLocator;\r
18 import org.eclipse.core.runtime.Path;\r
19 import org.simantics.db.Graph;\r
20 import org.simantics.layer0.utils.IEntity;\r
21 import org.simantics.proconf.g3d.animation.Animatable;\r
22 import org.simantics.proconf.g3d.animation.AnimationController;\r
23 import org.simantics.proconf.g3d.base.JmeRenderingComponent;\r
24 \r
25 import com.jme.bounding.BoundingBox;\r
26 import com.jme.image.Texture;\r
27 import com.jme.intersection.PickResults;\r
28 import com.jme.math.Ray;\r
29 import com.jme.renderer.Renderer;\r
30 import com.jme.scene.Node;\r
31 import com.jme.scene.state.AlphaState;\r
32 import com.jme.scene.state.TextureState;\r
33 import com.jme.scene.state.ZBufferState;\r
34 import com.jme.util.TextureManager;\r
35 import com.jmex.effects.particles.ParticleGeometry;\r
36 \r
37 import fi.vtt.simantics.processeditor.Activator;\r
38 import fi.vtt.simantics.processeditor.ProcessResource;\r
39 import fi.vtt.simantics.processeditor.stubs.PipeRun;\r
40 import fi.vtt.simantics.processeditor.stubs.VariableLengthInlineComponent;\r
41 \r
42 \r
43 public class PipeAnimationController implements AnimationController {\r
44         private List<ParticleGeometry> pipeAnimations = new ArrayList<ParticleGeometry>();\r
45         \r
46         List<PipeFlowAnimation> animatables = new ArrayList<PipeFlowAnimation>();\r
47         \r
48         private double d = Math.random();\r
49     private double delta = 0.01;\r
50     \r
51     AlphaState as1;\r
52         TextureState ts;\r
53         Node particleNode = new Node() {\r
54                 private static final long serialVersionUID = 7477121374264124684L;\r
55 \r
56                 // Without this picking code tries to pick particles which doesn't work (Causes class cast exception)\r
57                 public void findPick(Ray toTest, PickResults results) {\r
58                         return;\r
59                 }\r
60         };\r
61     \r
62     public PipeAnimationController(JmeRenderingComponent component, PipeRun pipeline) {\r
63         as1 = component.getDisplaySystem().getRenderer().createAlphaState();\r
64         as1.setBlendEnabled(true);\r
65         as1.setSrcFunction(AlphaState.SB_SRC_ALPHA);\r
66         as1.setDstFunction(AlphaState.DB_ONE);\r
67         //as1.setDstFunction(AlphaState.DB_ONE_MINUS_SRC_ALPHA);\r
68         as1.setTestEnabled(true);\r
69         as1.setTestFunction(AlphaState.TF_GREATER);\r
70         as1.setEnabled(true);\r
71 \r
72         TextureState ts = component.getDisplaySystem().getRenderer().createTextureState();\r
73         //URL url = FileLocator.find(com.jme.eclipse.Activator.getDefault().getBundle(),new Path("data/textures/flaresmall.jpg"),null);\r
74         URL url = FileLocator.find(Activator.getDefault().getBundle(),new Path("icons/bubble.png"),null);\r
75         \r
76         ts.setTexture(\r
77             TextureManager.loadTexture(url,\r
78             Texture.MM_LINEAR_LINEAR,\r
79             Texture.FM_LINEAR));\r
80         ts.setEnabled(true);\r
81         \r
82         ZBufferState zs = component.getDisplaySystem().getRenderer().createZBufferState();\r
83         zs.setFunction(ZBufferState.CF_LEQUAL);\r
84         zs.setWritable(false);\r
85         //zs.setFunction(ZBufferState.CF_ALWAYS);\r
86         particleNode.setRenderState(as1);\r
87         particleNode.setRenderState(ts);\r
88         particleNode.setRenderState(zs);\r
89         particleNode.setRenderQueueMode(Renderer.QUEUE_TRANSPARENT);\r
90         component.getShadowRoot().attachChild(particleNode);\r
91         createAnimations(pipeline);\r
92     }\r
93         \r
94     public void addAnimatable(Animatable animatable) {\r
95                 animatables.add((PipeFlowAnimation)animatable);\r
96                 \r
97         }\r
98         \r
99         public void setAnimatable(Animatable animatable) {\r
100                 animatables.clear();\r
101                 animatables.add((PipeFlowAnimation)animatable);\r
102                 \r
103         }\r
104         \r
105         \r
106         public void updateAnimation(Graph g, double frameTime) {\r
107                 d += delta;\r
108         if (d > 1.0) {\r
109             d = 1.0;\r
110             delta = -delta;\r
111         } else if (d < 0.0) {\r
112             delta = -delta;\r
113             d = 0.0;\r
114         }\r
115         for (Animatable a : animatables)\r
116             a.animate(d,frameTime);     \r
117         }\r
118         \r
119         public void dispose() {\r
120                 for (ParticleGeometry p : pipeAnimations)\r
121                         p.removeFromParent();\r
122                 pipeAnimations.clear();\r
123                 \r
124         }\r
125         \r
126         private void createAnimations(PipeRun pipeline) {\r
127                 List<IEntity> straights = new ArrayList<IEntity>();\r
128                 for (IEntity t : pipeline.getChild()) {\r
129                         if (t.isInstanceOf(ProcessResource.plant3Dresource.VariableLengthInlineComponent))\r
130                                 straights.add(t);\r
131                 }\r
132                 \r
133                 for (IEntity r : straights) {\r
134                 animatables.add(new PipeFlowAnimation(pipeAnimations, particleNode, new VariableLengthInlineComponent(r),false));\r
135         }\r
136         particleNode.setModelBound(new BoundingBox());\r
137         particleNode.updateModelBound();\r
138         particleNode.updateRenderState();\r
139         }\r
140 \r
141 }\r