/******************************************************************************* * Copyright (c) 2007 VTT Technical Research Centre of Finland and others. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at * http://www.eclipse.org/legal/epl-v10.html * * Contributors: * VTT Technical Research Centre of Finland - initial API and implementation *******************************************************************************/ package fi.vtt.simantics.processeditor.animations; import java.net.URL; import java.util.ArrayList; import java.util.List; import org.eclipse.core.runtime.FileLocator; import org.eclipse.core.runtime.Path; import org.simantics.db.Graph; import org.simantics.layer0.utils.IEntity; import org.simantics.proconf.g3d.animation.Animatable; import org.simantics.proconf.g3d.animation.AnimationController; import org.simantics.proconf.g3d.base.JmeRenderingComponent; import com.jme.bounding.BoundingBox; import com.jme.image.Texture; import com.jme.intersection.PickResults; import com.jme.math.Ray; import com.jme.renderer.Renderer; import com.jme.scene.Node; import com.jme.scene.state.AlphaState; import com.jme.scene.state.TextureState; import com.jme.scene.state.ZBufferState; import com.jme.util.TextureManager; import com.jmex.effects.particles.ParticleGeometry; import fi.vtt.simantics.processeditor.Activator; import fi.vtt.simantics.processeditor.ProcessResource; import fi.vtt.simantics.processeditor.stubs.PipeRun; import fi.vtt.simantics.processeditor.stubs.VariableLengthInlineComponent; public class PipeAnimationController implements AnimationController { private List pipeAnimations = new ArrayList(); List animatables = new ArrayList(); private double d = Math.random(); private double delta = 0.01; AlphaState as1; TextureState ts; Node particleNode = new Node() { private static final long serialVersionUID = 7477121374264124684L; // Without this picking code tries to pick particles which doesn't work (Causes class cast exception) public void findPick(Ray toTest, PickResults results) { return; } }; public PipeAnimationController(JmeRenderingComponent component, PipeRun pipeline) { as1 = component.getDisplaySystem().getRenderer().createAlphaState(); as1.setBlendEnabled(true); as1.setSrcFunction(AlphaState.SB_SRC_ALPHA); as1.setDstFunction(AlphaState.DB_ONE); //as1.setDstFunction(AlphaState.DB_ONE_MINUS_SRC_ALPHA); as1.setTestEnabled(true); as1.setTestFunction(AlphaState.TF_GREATER); as1.setEnabled(true); TextureState ts = component.getDisplaySystem().getRenderer().createTextureState(); //URL url = FileLocator.find(com.jme.eclipse.Activator.getDefault().getBundle(),new Path("data/textures/flaresmall.jpg"),null); URL url = FileLocator.find(Activator.getDefault().getBundle(),new Path("icons/bubble.png"),null); ts.setTexture( TextureManager.loadTexture(url, Texture.MM_LINEAR_LINEAR, Texture.FM_LINEAR)); ts.setEnabled(true); ZBufferState zs = component.getDisplaySystem().getRenderer().createZBufferState(); zs.setFunction(ZBufferState.CF_LEQUAL); zs.setWritable(false); //zs.setFunction(ZBufferState.CF_ALWAYS); particleNode.setRenderState(as1); particleNode.setRenderState(ts); particleNode.setRenderState(zs); particleNode.setRenderQueueMode(Renderer.QUEUE_TRANSPARENT); component.getShadowRoot().attachChild(particleNode); createAnimations(pipeline); } public void addAnimatable(Animatable animatable) { animatables.add((PipeFlowAnimation)animatable); } public void setAnimatable(Animatable animatable) { animatables.clear(); animatables.add((PipeFlowAnimation)animatable); } public void updateAnimation(Graph g, double frameTime) { d += delta; if (d > 1.0) { d = 1.0; delta = -delta; } else if (d < 0.0) { delta = -delta; d = 0.0; } for (Animatable a : animatables) a.animate(d,frameTime); } public void dispose() { for (ParticleGeometry p : pipeAnimations) p.removeFromParent(); pipeAnimations.clear(); } private void createAnimations(PipeRun pipeline) { List straights = new ArrayList(); for (IEntity t : pipeline.getChild()) { if (t.isInstanceOf(ProcessResource.plant3Dresource.VariableLengthInlineComponent)) straights.add(t); } for (IEntity r : straights) { animatables.add(new PipeFlowAnimation(pipeAnimations, particleNode, new VariableLengthInlineComponent(r),false)); } particleNode.setModelBound(new BoundingBox()); particleNode.updateModelBound(); particleNode.updateRenderState(); } }