Home | History | Annotate | Download | only in shapes
      1 package com.jme3.effect.shapes;
      2 
      3 import com.jme3.math.FastMath;
      4 import com.jme3.math.Vector3f;
      5 import com.jme3.scene.Mesh;
      6 import com.jme3.scene.VertexBuffer.Type;
      7 import com.jme3.util.BufferUtils;
      8 import java.util.ArrayList;
      9 import java.util.List;
     10 
     11 /**
     12  * This emiter shape emits the particles from the given shape's faces.
     13  * @author Marcin Roguski (Kaelthas)
     14  */
     15 public class EmitterMeshFaceShape extends EmitterMeshVertexShape {
     16 
     17     /**
     18      * Empty constructor. Sets nothing.
     19      */
     20     public EmitterMeshFaceShape() {
     21     }
     22 
     23     /**
     24      * Constructor. It stores a copy of vertex list of all meshes.
     25      * @param meshes
     26      *        a list of meshes that will form the emitter's shape
     27      */
     28     public EmitterMeshFaceShape(List<Mesh> meshes) {
     29         super(meshes);
     30     }
     31 
     32     @Override
     33     public void setMeshes(List<Mesh> meshes) {
     34         this.vertices = new ArrayList<List<Vector3f>>(meshes.size());
     35         this.normals = new ArrayList<List<Vector3f>>(meshes.size());
     36         for (Mesh mesh : meshes) {
     37             Vector3f[] vertexTable = BufferUtils.getVector3Array(mesh.getFloatBuffer(Type.Position));
     38             int[] indices = new int[3];
     39             List<Vector3f> vertices = new ArrayList<Vector3f>(mesh.getTriangleCount() * 3);
     40             List<Vector3f> normals = new ArrayList<Vector3f>(mesh.getTriangleCount());
     41             for (int i = 0; i < mesh.getTriangleCount(); ++i) {
     42                 mesh.getTriangle(i, indices);
     43                 vertices.add(vertexTable[indices[0]]);
     44                 vertices.add(vertexTable[indices[1]]);
     45                 vertices.add(vertexTable[indices[2]]);
     46                 normals.add(FastMath.computeNormal(vertexTable[indices[0]], vertexTable[indices[1]], vertexTable[indices[2]]));
     47             }
     48             this.vertices.add(vertices);
     49             this.normals.add(normals);
     50         }
     51     }
     52 
     53     /**
     54      * This method fills the point with coordinates of randomly selected point on a random face.
     55      * @param store
     56      *        the variable to store with coordinates of randomly selected selected point on a random face
     57      */
     58     @Override
     59     public void getRandomPoint(Vector3f store) {
     60         int meshIndex = FastMath.nextRandomInt(0, vertices.size() - 1);
     61         // the index of the first vertex of a face (must be dividable by 3)
     62         int vertIndex = FastMath.nextRandomInt(0, vertices.get(meshIndex).size() / 3 - 1) * 3;
     63         // put the point somewhere between the first and the second vertex of a face
     64         float moveFactor = FastMath.nextRandomFloat();
     65         store.set(Vector3f.ZERO);
     66         store.addLocal(vertices.get(meshIndex).get(vertIndex));
     67         store.addLocal((vertices.get(meshIndex).get(vertIndex + 1).x - vertices.get(meshIndex).get(vertIndex).x) * moveFactor, (vertices.get(meshIndex).get(vertIndex + 1).y - vertices.get(meshIndex).get(vertIndex).y) * moveFactor, (vertices.get(meshIndex).get(vertIndex + 1).z - vertices.get(meshIndex).get(vertIndex).z) * moveFactor);
     68         // move the result towards the last face vertex
     69         moveFactor = FastMath.nextRandomFloat();
     70         store.addLocal((vertices.get(meshIndex).get(vertIndex + 2).x - store.x) * moveFactor, (vertices.get(meshIndex).get(vertIndex + 2).y - store.y) * moveFactor, (vertices.get(meshIndex).get(vertIndex + 2).z - store.z) * moveFactor);
     71     }
     72 
     73     /**
     74      * This method fills the point with coordinates of randomly selected point on a random face.
     75      * The normal param is filled with selected face's normal.
     76      * @param store
     77      *        the variable to store with coordinates of randomly selected selected point on a random face
     78      * @param normal
     79      *        filled with selected face's normal
     80      */
     81     @Override
     82     public void getRandomPointAndNormal(Vector3f store, Vector3f normal) {
     83         int meshIndex = FastMath.nextRandomInt(0, vertices.size() - 1);
     84         // the index of the first vertex of a face (must be dividable by 3)
     85         int faceIndex = FastMath.nextRandomInt(0, vertices.get(meshIndex).size() / 3 - 1);
     86         int vertIndex = faceIndex * 3;
     87         // put the point somewhere between the first and the second vertex of a face
     88         float moveFactor = FastMath.nextRandomFloat();
     89         store.set(Vector3f.ZERO);
     90         store.addLocal(vertices.get(meshIndex).get(vertIndex));
     91         store.addLocal((vertices.get(meshIndex).get(vertIndex + 1).x - vertices.get(meshIndex).get(vertIndex).x) * moveFactor, (vertices.get(meshIndex).get(vertIndex + 1).y - vertices.get(meshIndex).get(vertIndex).y) * moveFactor, (vertices.get(meshIndex).get(vertIndex + 1).z - vertices.get(meshIndex).get(vertIndex).z) * moveFactor);
     92         // move the result towards the last face vertex
     93         moveFactor = FastMath.nextRandomFloat();
     94         store.addLocal((vertices.get(meshIndex).get(vertIndex + 2).x - store.x) * moveFactor, (vertices.get(meshIndex).get(vertIndex + 2).y - store.y) * moveFactor, (vertices.get(meshIndex).get(vertIndex + 2).z - store.z) * moveFactor);
     95         normal.set(normals.get(meshIndex).get(faceIndex));
     96     }
     97 }
     98