Home | History | Annotate | Download | only in values
      1 package com.badlogic.gdx.graphics.g3d.particles.values;
      2 
      3 import com.badlogic.gdx.graphics.Mesh;
      4 import com.badlogic.gdx.graphics.VertexAttributes.Usage;
      5 import com.badlogic.gdx.graphics.g3d.Model;
      6 import com.badlogic.gdx.math.MathUtils;
      7 import com.badlogic.gdx.math.Vector3;
      8 
      9 /** Encapsulate the formulas to spawn a particle on a mesh shape.
     10 * @author Inferno */
     11 public final class UnweightedMeshSpawnShapeValue extends MeshSpawnShapeValue {
     12 	private float[] vertices;
     13 	private short[] indices;
     14 	private int positionOffset, vertexSize, vertexCount, triangleCount;
     15 
     16 	public UnweightedMeshSpawnShapeValue (UnweightedMeshSpawnShapeValue value) {
     17 		super(value);
     18 		load(value);
     19 	}
     20 
     21 	public UnweightedMeshSpawnShapeValue () {}
     22 
     23 	@Override
     24 	public void setMesh(Mesh mesh, Model model){
     25 		super.setMesh(mesh, model);
     26 		vertexSize = mesh.getVertexSize()/4;
     27 		positionOffset = mesh.getVertexAttribute(Usage.Position).offset/4;
     28 		int indicesCount = mesh.getNumIndices();
     29 		if(indicesCount >0){
     30 			indices = new short[indicesCount];
     31 			mesh.getIndices(indices);
     32 			triangleCount = indices.length/3;
     33 		}
     34 		else indices = null;
     35 		vertexCount = mesh.getNumVertices();
     36 		vertices = new float[ vertexCount* vertexSize];
     37 		mesh.getVertices(vertices);
     38 	}
     39 
     40 	@Override
     41 	public void spawnAux (Vector3 vector, float percent) {
     42 		if(indices == null){
     43 			//Triangles
     44 			int triangleIndex = MathUtils.random(vertexCount -3)*vertexSize;
     45 			int 	p1Offset = triangleIndex+positionOffset,
     46 					p2Offset = p1Offset + vertexSize,
     47 					p3Offset = p2Offset + vertexSize;
     48 			float x1 = vertices[p1Offset], y1 = vertices[p1Offset+1], z1 = vertices[p1Offset+2],
     49 					x2 = vertices[p2Offset], y2 = vertices[p2Offset+1], z2 = vertices[p2Offset+2],
     50 					x3 = vertices[p3Offset], y3 = vertices[p3Offset+1], z3 = vertices[p3Offset+2];
     51 			Triangle.pick(x1, y1, z1, x2, y2, z2, x3, y3, z3, vector);
     52 		}
     53 		else {
     54 			//Indices
     55 			int triangleIndex = MathUtils.random(triangleCount-1)*3;
     56 			int p1Offset = indices[triangleIndex]*vertexSize + positionOffset,
     57 				 p2Offset = indices[triangleIndex+1]*vertexSize + positionOffset,
     58 				 p3Offset = indices[triangleIndex+2]*vertexSize + positionOffset;
     59 			float x1 = vertices[p1Offset], y1 = vertices[p1Offset+1], z1 = vertices[p1Offset+2],
     60 				x2 = vertices[p2Offset], y2 = vertices[p2Offset+1], z2 = vertices[p2Offset+2],
     61 				x3 = vertices[p3Offset], y3 = vertices[p3Offset+1], z3 = vertices[p3Offset+2];
     62 			Triangle.pick(x1, y1, z1, x2, y2, z2, x3, y3, z3, vector);
     63 		}
     64 	}
     65 
     66 	@Override
     67 	public SpawnShapeValue copy () {
     68 		return new UnweightedMeshSpawnShapeValue(this);
     69 	}
     70 
     71 }
     72