Advertisement
Guest User

Nebula.java

a guest
Sep 6th, 2015
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.54 KB | None | 0 0
  1. package celestial;
  2.  
  3. import mainpkg.CPU;
  4. import setup.MakeWorld;
  5. import util.Methods;
  6.  
  7. import com.jme3.math.ColorRGBA;
  8. import com.jme3.math.FastMath;
  9. import com.jme3.math.Vector3f;
  10. import com.jme3.renderer.queue.RenderQueue.Bucket;
  11. import com.jme3.renderer.queue.RenderQueue.ShadowMode;
  12. import com.jme3.scene.Geometry;
  13. import com.jme3.scene.Node;
  14. import com.jme3.scene.Spatial;
  15. import com.jme3.scene.shape.Quad;
  16.  
  17. public class Nebula extends Node{
  18.    
  19.     float size;
  20.     ColorRGBA template;
  21.    
  22.     public Nebula(Vector3f loc, float size, ColorRGBA col)
  23.     {
  24.         super();
  25.         setLocalTranslation(loc);
  26.         this.size = size;
  27.        
  28.         if(col != null)
  29.             template = col; //predefined color
  30.         else
  31.             template = new ColorRGBA( //generates a random color
  32.                 FastMath.nextRandomFloat()*0.8f,
  33.                 FastMath.nextRandomFloat()*0.8f,
  34.                 FastMath.nextRandomFloat()*0.8f,
  35.                 0f
  36.             );
  37.     }
  38.    
  39.     public void disable(){
  40.         //delete untill rebuilt again
  41.         if(getChildren().size() > 0)
  42.             detachAllChildren();
  43.     }
  44.    
  45.     public void enable(){
  46.         if(getChildren().size() == 0)
  47.         {
  48.             //construct the nebula         
  49.             for (int i = 0; i < size/50; i++) {
  50.                 float rand1 = FastMath.nextRandomFloat()*FastMath.pow((size/75f), 1.85f);
  51.                 float rand2 = FastMath.nextRandomFloat()*FastMath.pow((size/75f), 1.85f);
  52.                 float rand3 = FastMath.nextRandomFloat()*FastMath.pow((size/75f), 1.85f);
  53.                 Quad side = new Quad(size,size);
  54.                 Geometry plate = new Geometry("plane",side);
  55.                 plate.setMaterial(MakeWorld.nebula.get(FastMath.rand.nextInt(MakeWorld.nebula.size())).clone());
  56.                 plate.setQueueBucket(Bucket.Transparent);
  57.                 plate.setLocalTranslation(-size/2+rand1, -size/2+rand2, rand3);
  58.                 plate.setShadowMode(ShadowMode.Off);           
  59.                 Node n = new Node();
  60.                 n.attachChild(plate);
  61.                 n.getLocalRotation().fromAngleAxis(i*36, Methods.randomVector3f());
  62.                 attachChild(n);
  63.             }
  64.         }
  65.     }
  66.    
  67.     public void cycle(float tpf)
  68.     {      
  69.         Node temp = new Node();
  70.         for (Spatial plate : getChildren()) {
  71.            
  72.             //get both vectors (don't ask)
  73.             Vector3f one = plate.getLocalRotation().getRotationColumn(2);
  74.             temp.setLocalTranslation(((Geometry)((Node)plate).getChild(0)).getWorldTranslation());
  75.             temp.lookAt(CPU.game.getCamera().getLocation().mult(0.7f), Vector3f.UNIT_Y);
  76.             Vector3f two = temp.getLocalRotation().getRotationColumn(2);
  77.            
  78.             //calculate opacity
  79.             float opacity = FastMath.abs(one.dot(two))*0.75f;
  80.            
  81.             //set opacity
  82.             ((Geometry)((Node)plate).getChild(0)).getMaterial()
  83.             .setColor("Color", new ColorRGBA(template.r,template.g,template.b,opacity));
  84.         }
  85.     }
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement