Updating A Solid Particle's Shape
SPS.updateParticleVertex()usage :
It happens before particle scaling, rotation, and translation, and it allows you to update the vertex coordinates, color, and UV of each particle.
This function is called for each vertex of each particle, and it is passed the current particle, the current vertex, and its current index in the particle shape.
The vertex is a SolidParticleVertex object, so you can access or set its properties :vertex.position: Vector3 (x, y, z)vertex.color: Color4 (r, g, b, a)vertex.uv: Vector2 (x, y)
SPS.computeParticleVertex = true; // false by default for performance reasonSPS.updateParticleVertex = function(particle, vertex, v) { // particle : the current particle object // vertex : the current vertex, a solidParticleVertex object // the index of the current vertex in the particle shape // example : if (particle.shapeID == 1) { vertex.position.x *= Math.random() + 1; vertex.position.y *= Math.random() + 1; vertex.position.z *= Math.random() + 1; vertex.color.r = Math.abs(Math.sin(v)); vertex.color.g = 1 - vertex.color.r vertex.uv.x = particle.idx + v; vertex.uv.y = vertex.uv.x; }
};Note that this vertex update is not stored (the particle shape is not modified) but only computed in the next call to setParticles(). So there is no value accumulation: the vertex coordinates, colors, or UVs are always the initial ones when entering this function.
Note also that the shape reference for each particle is the original shape of the mesh model you passed in addShape(), even if you also passed a custom vertexFunction (see the part: "Going further in immutable SPS").
The good news is that the very same function can be used for SPS.updateParticleVertex and for the custom vertexFunction expected by addShape().
So, to better understand how it works, here is another general pseudocode outline:
var particles: SolidParticles[] = [array of SolidParticle objects];function setParticles() { beforeUpdateParticles(); // your custom function for (let p = 0; p < nbParticles; p++) { var particle = particles[p]; updateParticles(particle); // your custom position function for(let v = 0; particle.vertices.length; v++) { var vertex = particle.vertices[v]; updateParticleVertex(particle, vertex, v); // your ustom vertex function computeAllTheVertexStuff(); } } updateTheWholeMesh(); // does the WebGL work afterUpdateParticles(); // your ustom function}Example: Updating Solid Particle Geometry Or dancing glow-worms: Dancing Glow Worm Solid Particles