PDA

View Full Version : random value between 0 to 1 from particle ID


Eraq
10-28-2009, 07:21 PM
Hey Folks.

Here is a noob question :-)



How can I get an random value that is between 0 and 1 based on particle ID?


Cheers

Erik

Aikiman
10-28-2009, 07:47 PM
x = b/a,

where b = particleId
and a = total number of particles.

So if you have 700 particles and you want to find the normalised value for the 333rd particle, then...

333/700 = 0.48

Im no mathematician but I know that one :). Does that help?

AndersEgleus
10-28-2009, 09:17 PM
Jeremy: that's not very random is it? ;)

I'm guessing by random you mean that if you input two ids next to each other (e.g. 7 and 8), the random numbers should look completely unrelated (e.g. 0.684312 and 0.154789 respectively), shouldn't look like a sequence if you go through a lot of successive ids etc. Well, maya unfortunately doesn't really have a good function for that - you can
seed (particleId)
float $myRandomNumber = rand(0.0, 1.0);

but that will look very sequential if you study the numbers from successive ids. If you're emitting over a big area/volume you probably won't notice, but if you have an omni emitter the looping may get painfully obvious.
seed (particleId)
float $myRandomNumber;
for ($i = 0; $i < 20; $i++)
$myRandomNumber = rand(0.0, 1.0);
probably works better since a good random number in mel requires the sequence to run a few times, but it's of course more expensive.

Or I may have misunderstood your question in which case Jeremy is right :)

Eraq
10-28-2009, 09:36 PM
Hey Thanks for your answers guys. Much appriciated.

My problem is that I would like to give my particles a random contribution to their velocity.
So I thought that a random value based on the particles ID would do the trick. But unlike Houdini wich random functions return a value between 0 and 1, Maya´s random function lacks just that.

Ill try the method you mentions.

Thanks

Erik

stooch
10-28-2009, 10:14 PM
Hey Thanks for your answers guys. Much appriciated.

My problem is that I would like to give my particles a random contribution to their velocity.
So I thought that a random value based on the particles ID would do the trick. But unlike Houdini wich random functions return a value between 0 and 1, Maya´s random function lacks just that.

Ill try the method you mentions.

Thanks

Erik

you can do that.

rand() by default will return a value between 0-1

i think what the guy above you was talking about SEED. you can set the seed by making it equal the particle id. Im writing this off the top of my head so dont remember the function, just do a search for controlling seed in the function library.

Aikiman
10-28-2009, 10:21 PM
Jeremy: that's not very random is it? ;)

clearly I didnt understand what he was driving at it appears ;)

ok in that case you dont necessarily need to use your Id but instead create a custom per particle attribute and in creation make give it a random value rand(1). Then use that as a multiplier on your velocity per particle.

You could make it a vector thereby increasing the randomisation because you could affect each axis with a different value, or make it a float and multiply it over the entire vector.

I would only use Id if you actually want specific particles to be affected, but in your case I dont think you do.

AndersEgleus
10-29-2009, 06:29 AM
Yeah, Jeremy's technique is much better in practice than what I suggested in my previous post.

To assure that the random values are the same every time you play back (as long as everything else stays the same) you can write
if (particleId == 0)
seed (3212)
// (or whatever arbitary seed value you choose - i just punched the numeric keypad)
In the creation expression.

This, however, assumes that there is a particle with particleId = 0 - not the case e.g. if you use surface emission with texture rate.

But if that is the case it works because the particle with id = 0 is the first to be evaluated, so its creation expression is the first thing that's gonna be evaluated in the scene when you playback - thus everytime after that where you use rand (), that seed is gonna be used.

Aikiman
10-29-2009, 08:04 AM
This, however, assumes that there is a particle with particleId = 0 - not the case e.g. if you use surface emission with texture rate.


Is that a fact? I did not know that, do you have any idea why this is so?

Eraq
10-29-2009, 01:41 PM
Hello Folks.

Thanks for your responses.
It helped a lot

Erik

AndersEgleus
10-30-2009, 11:10 PM
Is that a fact? I did not know that, do you have any idea why this is so?


No, a quick test indicates that I was wrong - it seems that no matter how small a part of your mesh is emitted from with texture rate emission, the first particle always has id 0 anyway, which is good news to me - makes the seeding a lot safer.

I distinctly remember coming across the problem of disappearing ids with texture emission, as if the emitter bumped the id before dismissing a particle, but that was a long time ago so it might have been fixed in the newer maya versions.

Thanks for making me check, it's nice to find a maya particle feature for once which is actually MORE stable than you expected :).

Aikiman
10-30-2009, 11:55 PM
No, a quick test indicates that I was wrong - it seems that no matter how small a part of your mesh is emitted from with texture rate emission, the first particle always has id 0 anyway, which is good news to me - makes the seeding a lot safer.

I distinctly remember coming across the problem of disappearing ids with texture emission, as if the emitter bumped the id before dismissing a particle, but that was a long time ago so it might have been fixed in the newer maya versions.

Thanks for making me check, it's nice to find a maya particle feature for once which is actually MORE stable than you expected :).

mmm maybe the first object in the particle array was used for something else when emitting from texture, dont ask me what, or maybe it was just a bug. Glad to be of some help anyway ;)

CGTalk Moderation
10-30-2009, 11:55 PM
This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum.