Advertisement
Guest User

Nebula MCVE

a guest
Sep 14th, 2016
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.04 KB | None | 0 0
  1. import com.jme3.app.SimpleApplication;
  2. import com.jme3.material.Material;
  3. import com.jme3.material.RenderState.BlendMode;
  4. import com.jme3.material.RenderState.FaceCullMode;
  5. import com.jme3.math.ColorRGBA;
  6. import com.jme3.math.FastMath;
  7. import com.jme3.math.Vector3f;
  8. import com.jme3.renderer.queue.RenderQueue.Bucket;
  9. import com.jme3.renderer.queue.RenderQueue.ShadowMode;
  10. import com.jme3.scene.Geometry;
  11. import com.jme3.scene.Mesh;
  12. import com.jme3.scene.Node;
  13. import com.jme3.scene.VertexBuffer.Type;
  14. import com.jme3.scene.shape.Quad;
  15. import com.jme3.util.BufferUtils;
  16. import jme3tools.optimize.GeometryBatchFactory;
  17.  
  18. public class MCVE extends SimpleApplication{
  19.    
  20.     public static void main(String[] args) {
  21.         MCVE app = new MCVE();
  22.         app.start();
  23.     }
  24.  
  25.     @Override
  26.     public void simpleInitApp(){
  27.         boolean batchQuads = true;
  28.         float size = 80000;
  29.        
  30.         flyCam.setMoveSpeed(size*2f+1000);
  31.         cam.setFrustumPerspective(70f, (float) cam.getWidth() / cam.getHeight(), 1f, 800000f);
  32.         cam.setLocation(Vector3f.UNIT_Z.mult(size*4f));
  33.  
  34.         ColorRGBA template = new ColorRGBA(0.0f,0.0f,0.5f,1.2f);
  35.        
  36.         Quad[] plates = new Quad[4];
  37.        
  38.         Quad side = new Quad(size*2.5f,size*2.5f);
  39.         side.setBuffer(Type.TexCoord, 2, new float[]{0,0.5f,     0.5f,0.5f,     0.5f,0,    0,0});
  40.         plates[0]= side;
  41.        
  42.         side = new Quad(size*2.5f,size*2.5f);
  43.         side.setBuffer(Type.TexCoord, 2, new float[]{0.5f,1,     1,1,     1,0.5f,    0.5f,0.5f});
  44.         plates[1]= side;
  45.        
  46.         side = new Quad(size*2.5f,size*2.5f);
  47.         side.setBuffer(Type.TexCoord, 2, new float[]{0.5f,0.5f,     1,0.5f,     1,0,    0.5f,0});
  48.         plates[2]= side;
  49.        
  50.         side = new Quad(size*2.5f,size*2.5f);
  51.         side.setBuffer(Type.TexCoord, 2, new float[]{0,1,     0.5f,1,     0.5f,0.5f,    0,0.5f});
  52.         plates[3]= side;
  53.        
  54.        
  55.         Material tex = new Material(assetManager,"assets/shaders/rings/Unshaded.j3md");
  56.         tex.setTexture("ColorMap", assetManager.loadTexture("Effects/Explosion/flame.png"));
  57.         tex.setTransparent(true);
  58.         tex.setBoolean("VertexColor", true);
  59.         tex.getAdditionalRenderState().setDepthWrite(false);
  60.         tex.getAdditionalRenderState().setDepthTest(true);
  61.         tex.getAdditionalRenderState().setBlendMode(BlendMode.AlphaAdditive);
  62.         tex.getAdditionalRenderState().setFaceCullMode(FaceCullMode.Off);
  63.         tex.setFloat("AlphaDiscardThreshold", 0.01f);
  64.        
  65.         Node batch = new Node();
  66.         for (int i = 0; i < size/100; i++) {
  67.             float rand1 = (float)(Math.random()*size);
  68.             float rand2 = (float)(Math.random()*size);
  69.             float rand3 = (float)(Math.random()*size);
  70.            
  71.             Mesh m = plates[FastMath.nextRandomInt(0, 3)].clone();
  72.            
  73.             ColorRGBA dif = template.clone();              
  74.             float [] vertices = new float[16];
  75.             for(int k = 0, j = 0; j < 4; k+=4, j++) {
  76.                
  77.                 ColorRGBA add = new ColorRGBA(FastMath.nextRandomFloat()-0.5f, FastMath.nextRandomFloat()-0.5f, FastMath.nextRandomFloat()-0.5f,0);
  78.                 add = dif.add(add.mult(0.85f));
  79.                
  80.                 vertices[k] = add.r;
  81.                 vertices[k+1] = add.g;
  82.                 vertices[k+2] = add.b;
  83.                 vertices[k+3] = add.a;
  84.             }
  85.            
  86.             m.setBuffer(Type.Color, 4, BufferUtils.createFloatBuffer(vertices));
  87.            
  88.             Geometry plate = new Geometry("nb",m);
  89.             plate.setMaterial(tex);
  90.             plate.setQueueBucket(Bucket.Transparent);
  91.             plate.setLocalTranslation(-size*0.5f+rand1, -size*0.5f+rand2, rand3);
  92.             plate.setShadowMode(ShadowMode.Off);
  93.             Node n = new Node();
  94.             n.attachChild(plate);
  95.             n.getLocalRotation().fromAngleAxis(i*36, randomVector3f());
  96.             batch.attachChild(n);
  97.         }
  98.        
  99.         if(batchQuads)
  100.         {
  101.             GeometryBatchFactory.optimize(batch);
  102.            
  103.             Geometry part = (Geometry) batch.getChild(batch.getChildren().size()-1);
  104.             part.setQueueBucket(Bucket.Transparent);
  105.             part.setShadowMode(ShadowMode.Off);
  106.             rootNode.attachChild(part);
  107.            
  108.             batch.detachAllChildren();
  109.         }
  110.         else
  111.             rootNode.attachChild(batch);
  112.     }
  113.    
  114.     public static Vector3f randomVector3f() {
  115.         Vector3f rand = new Vector3f(FastMath.rand.nextFloat()*2f-1f,FastMath.rand.nextFloat()*2f-1f,FastMath.rand.nextFloat()*2f-1f);
  116.         return rand.normalizeLocal();
  117.     }
  118. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement