Monday, October 29, 2012

nParticle Instance Geometry and random rotation

Instance different geometry to random particles and give particles random rotation and rotation speed.

nparticles are a pain, here is a fairly simple way to get the nparticles to grab random geometry from the instancer, all you need is an instancer that contains your geometry that's connected to your nParticles. next you'll want to "create a dynamic attribute" in the nparicle shape node, you'll need 4 new attributes:
* all are per particle because we want each particle to be different.
1) randomPeicePP - FLOAT and will be the random geometry from the insatncer
2) randInit - this is a VECTOR and will contain the random starting rotation of the instanced geo
3) rotateSpeed - FLOAT this will be the ransom speed of the rotation
4) rotationFinal -FLOAT this is where our final particle rotation values are sent to for the nparticle to look at and apply to particles

go to the expression editor for your nparticleshape node and create the following expressions.

//creation//
nParticleShape1.randomPeicePP = rand(0,7);
this is generating a random number between 0 and 7, go one higher than the number in the instancer.
nParticleShape1.randInit = <<rand(-180,180), rand(-180,180), rand(-180,180)>>;
this gives a random vector rotation (X,Y,Z)
nParticleShape1.rotateSpeed = rand(-1,1);
 this is the speed of rotation, it will be spinning between 100% forward and 100% backwards
 
//runtime//
vector $rotInit = nParticleShape1.randInit;
this just stores the random start rotation in a variable for reference  below
float $spin = $rotInit.z + (frame * 80.0 * nParticleShape1.rotateSpeed);
this creates the final rotation variable, it takes the random start rotation of the particle and adds rotation to it based off time and speed, "80" is just multiplying the grand total because it was too slow, if you wanted to you could change "rotate speed to "rand(80,-80) " and get rid of this but this allows me to easily change the max speed in one number.
nParticleShape1.rotationFinal = <<$rotInit.x, $rotInit.y, $spin>>;
this just assigns the final rotation to an axis of the particles, this is what is assigned to the particles come runtime. only the Z axis is used because if you assign rotations to more then one axis you get weird rotations, like a Frisbee or axe being through thing normally only rotate around one axis.
 
the final step is going to the instance options and in "object index" and find your "randomPeicePP" attribute you created and in the rotation section set it to your "rotationFinal" attribute.

No comments:

Post a Comment