Shader Model 4.0 based particle system
Shader Model 4 raises the GPUs flexibility. Two important new features beside the
geometry shader and instancing are bindable uniforms and Transform feedback.
With bindable uniforms it is possible to access a buffer object with random access. This
is useful for accessing the data of other vertices.
Transform feedback allows to stream out the results of a vertex shader, into a buffer
object. This feature makes it possible to handle particle physics on a GPU without
processing data on a CPU.
As first the required stuff should be generated by entering some lines into the console:
Node = World.addNode();
Node.addScript();
Node.addVertexShader("ParticleVertexshader");
Node.addVertexShader("SphereVertexshader");
Node.addFragmentShader("SphereFragmentshader");
Node.addMesh("Particle");
Node.Particle.setNumOfVertices(256);
Node.Particle.addVertex().setDim(4);
Node.Particle.addVertex().setDim(4); //rename to "Position_1"
Node.Particle.addVertex().setDim(4); //rename to "Position_2"
Node.Particle.addVector().setDim(3); //rename to "Speed_1"
Node.Particle.addVector().setDim(3); //rename to "Speed_1"
for ( i = 0; i < 256; i++)
Node.Particle.Vertex.set(i,Math.random()*2-1,Math.random()*2-1,Math.random()*2-1);
Finally add a sphere Mesh (Testobjects from Nodes context menu). And copy the code into
the codeboxes:
ParticleVertexshader:
#define active
active varying vec4 position_out;
active varying vec3 speed_out;
//dummy can be used for rendering pointsprites
//like in the youtube video
//elsewhere the result will be discarded
vec4 v = vec4(position_out.xyz,1.0);
gl_Position = gl_ModelViewProjectionMatrix * v;
//vec4 mod = gl_ModelViewMatrix * v;
//gl_PointSize = 5 *Pointsize/ -mod.z ;
}
Now start the script. The result should be similar like in that video (but without the
pointsprite glow). The 256 in these scripts can be replaces by a higher number. the
limit (by the bindable uniforms) are 4096 Spheres (=16384 floats or 64k bytes).
If nothing is displayed, save the project (unknown bug)
4096 Spheres (only a few values in ParticleVertexshader are modified)
Older Version rendering Pointsprites instead spheres