protected void initializeParticles(final int numParticles) {
if (_particleMesh != null) {
detachChild(_particleMesh);
}
final Mesh mesh = new Mesh(getName() + "_mesh") {
@Override
public void updateWorldTransform(final boolean recurse) {
; // Do nothing.
}
@Override
public void updateWorldBound(final boolean recurse) {
super.updateWorldTransform(recurse);
super.updateWorldBound(recurse);
}
};
_particleMesh = mesh;
attachChild(mesh);
_particles = new Particle[numParticles];
if (numParticles == 0) {
return;
}
Vector2 sharedTextureData[];
// setup texture coords and index mode
final MeshData meshData = mesh.getMeshData();
switch (getParticleType()) {
case GeomMesh:
case Triangle:
sharedTextureData = new Vector2[] { new Vector2(2.0, 0.0), new Vector2(0.0, 2.0), new Vector2(0.0, 0.0) };
meshData.setIndexMode(IndexMode.Triangles);
break;
case Quad:
sharedTextureData = new Vector2[] { new Vector2(1.0, 0.0), new Vector2(1.0, 1.0),
new Vector2(0.0, 1.0), new Vector2(0.0, 0.0) };
meshData.setIndexMode(IndexMode.Quads);
break;
default:
throw new IllegalStateException(
"Particle Mesh may only have particle type of ParticleType.Quad, ParticleType.GeomMesh or ParticleType.Triangle");
}
final int verts = getVertsForParticleType(getParticleType());
_geometryCoordinates = BufferUtils.createVector3Buffer(numParticles * verts);
_appearanceColors = BufferUtils.createColorBuffer(numParticles * verts);
meshData.setVertexBuffer(_geometryCoordinates);
meshData.setColorBuffer(_appearanceColors);
meshData.setTextureBuffer(BufferUtils.createVector2Buffer(numParticles * verts), 0);
final Vector2 temp = Vector2.fetchTempInstance();
for (int k = 0; k < numParticles; k++) {
_particles[k] = new Particle(this);
_particles[k].init();
_particles[k].setStartIndex(k * verts);
for (int a = verts - 1; a >= 0; a--) {
final int ind = (k * verts) + a;
if (_particleType == ParticleSystem.ParticleType.GeomMesh && _useMeshTexCoords) {
final MeshEmitter source = (MeshEmitter) getParticleEmitter();
final Mesh sourceMesh = source.getSource();
final int index = sourceMesh.getMeshData().getIndices() != null ? sourceMesh.getMeshData()
.getIndices().get(ind) : ind;
BufferUtils.populateFromBuffer(temp, sourceMesh.getMeshData().getTextureCoords(0).getBuffer(),
index);
BufferUtils.setInBuffer(temp, meshData.getTextureCoords(0).getBuffer(), ind);
} else {
BufferUtils.setInBuffer(sharedTextureData[a], meshData.getTextureCoords(0).getBuffer(), ind);
}