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
9 * VTT Technical Research Centre of Finland - initial API and implementation
\r
10 *******************************************************************************/
\r
11 package org.simantics.processeditor.animations;
\r
13 import java.net.URL;
\r
14 import java.util.ArrayList;
\r
15 import java.util.List;
\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.processeditor.Activator;
\r
22 import org.simantics.processeditor.ProcessResource;
\r
23 import org.simantics.processeditor.stubs.PipeRun;
\r
24 import org.simantics.processeditor.stubs.VariableLengthInlineComponent;
\r
25 import org.simantics.proconf.g3d.animation.Animatable;
\r
26 import org.simantics.proconf.g3d.animation.AnimationController;
\r
27 import org.simantics.proconf.g3d.base.JmeRenderingComponent;
\r
29 import com.jme.bounding.BoundingBox;
\r
30 import com.jme.image.Texture;
\r
31 import com.jme.intersection.PickResults;
\r
32 import com.jme.math.Ray;
\r
33 import com.jme.renderer.Renderer;
\r
34 import com.jme.scene.Node;
\r
35 import com.jme.scene.state.AlphaState;
\r
36 import com.jme.scene.state.TextureState;
\r
37 import com.jme.scene.state.ZBufferState;
\r
38 import com.jme.util.TextureManager;
\r
39 import com.jmex.effects.particles.ParticleGeometry;
\r
43 public class PipeAnimationController implements AnimationController {
\r
44 private List<ParticleGeometry> pipeAnimations = new ArrayList<ParticleGeometry>();
\r
46 List<PipeFlowAnimation> animatables = new ArrayList<PipeFlowAnimation>();
\r
48 private double d = Math.random();
\r
49 private double delta = 0.01;
\r
53 Node particleNode = new Node() {
\r
54 private static final long serialVersionUID = 7477121374264124684L;
\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
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
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
77 TextureManager.loadTexture(url,
\r
78 Texture.MM_LINEAR_LINEAR,
\r
79 Texture.FM_LINEAR));
\r
80 ts.setEnabled(true);
\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
94 public void addAnimatable(Animatable animatable) {
\r
95 animatables.add((PipeFlowAnimation)animatable);
\r
99 public void setAnimatable(Animatable animatable) {
\r
100 animatables.clear();
\r
101 animatables.add((PipeFlowAnimation)animatable);
\r
106 public void updateAnimation(Graph g, double frameTime) {
\r
111 } else if (d < 0.0) {
\r
115 for (Animatable a : animatables)
\r
116 a.animate(d,frameTime);
\r
119 public void dispose() {
\r
120 for (ParticleGeometry p : pipeAnimations)
\r
121 p.removeFromParent();
\r
122 pipeAnimations.clear();
\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
133 for (IEntity r : straights) {
\r
134 animatables.add(new PipeFlowAnimation(pipeAnimations, particleNode, new VariableLengthInlineComponent(r),false));
\r
136 particleNode.setModelBound(new BoundingBox());
\r
137 particleNode.updateModelBound();
\r
138 particleNode.updateRenderState();
\r