Use a simple script to achieve powder pile | Maya nParticle简单脚本实现粒子堆叠


Youtube

Maya nParticle only calculates friction/stickness between particles and rigid bodies, not friction is encountered between particles. My guess is the algorithm complexity is too high. Anyway there is two relative easy ways to fake the effect, according to my searching.

想要用一个一包糖粒堆在物体(对又是它..)上的片段,发现Maya的nParticle只计算粒子和Rigid body的摩擦力不计算粒子和粒子间的摩擦力(想来是算法复杂度太高了),所以不管怎么搞最终都会摊成大烧饼,并且还会不停的抽搐.. 查了下大概有两个相对简单的方法

1. hold particles with a invisible rigid body. 2. Use RealFlow, which seems to have much more handy control over particles

I’m not happy with either. 1 is lame. 2 too much trouble, have to get hang of another environment. If I must do scripting, I choose to learn mel.

So the algorithm is:

It’s unlikely to mess with friction as I guessed above. I tried to modify per particle mass to let them stuck in the space. Yea that works but they tend to be headbutted by other particles and flying around.. for science. Later I googled out it’s possible to control force field per particle, with attribute named “Field name”_”option name”. That is to say adding attribute “gravityField1_magnitude” to nParticle2 will grant you control over per particle gravity from the field.

On creation:

global int $freezeCap=5;	//be completely frozen after the cap rounds
global int $gravityPP=500;	
global float $dragPP=100;
global float $accelerationTrigger=15;	//the speed parameter to trigger 'I'm free again!", which leads to free droping.
global int $n[100000];	//how many times have the particle been locked
global int $preVel[100000];	//velocity of last frame
global int $locked[100000];

nParticleShape4.dragField1_magnitude=0;
nParticleShape4.gravityField1_magnitude=$gravityPP;

global int $init=1;
if($init==1) {
	int $i;
	for($i=0;$i<100000;$i++) {
		$n[$i]=0;
		$locked[$i]=0;
	}
	$init=0;
}

Before dynamics:

int $id=nParticleShape4.particleId;
float $vel=abs(nParticleShape4.velocity);

//print("nParticleShape2.velocity "+$nParticleShape2.velocity+"\n");

if($vel>$preVel[$id]+$accelerationTrigger) {
	$n[$id]=0;
	nParticleShape4.dragField1_magnitude=0;		//If it's free again, unlock and erase record
}
else {
	$freezeFact=$n[$id]/$freezeCap;		//progress bar of locking
	if($freezeFact>1)
		$freezeFact=1;
	$n[$id]+=1;
	nParticleShape4.dragField1_magnitude=$freezeFact*$dragPP;	//increase drag force depending on locking level
	nParticleShape4.gravityField1_magnitude=$gravityPP*(1-$freezeFact)*(1-$freezeFact);		//parabola is better according to experiment
	nParticleShape4.velocity=<<0,0,0>>;
}

$preVel[$id]=nParticleShape4.velocity;

The script is not bad to me.. adjustable parameters are handy. Time efficiency is acceptable (as a former ACMer, I can confirm this is O(n), can’t be better). 20 million particles * 100 frames cost my i7 half an hour. Not perfect but enough for bragging.
1. 弄个透明的动画的rigid body来把粒子圈起来。 2. 用RealFlow,我只用RealFlow模拟水,但是显然那它对粒子的控制比Maya厉害得多。

两个方法我都不高兴,1太不屌爆,2太麻烦,要做很多熟悉另一个环境的工作,且要写脚本,所以不如来学下mel。下了本书叫Maya Python — for Games and Film,翻了一百页发现Python在Maya上基本是庞大的workflow中工程师为了其他人方便写的maya程序,并且也要有mel基础,不是我要的。

总之,思路是:

想要计算摩擦力显然是不现实的,说过了。我先通过改质量的途径让粒子满足条件时失去质量以停在原地不受重力影响,停是能停住但是会被别的particle撞飞.. 非常科学。后来查到可以使用 <力场名>_<属性名> 的方式控制Per Particle的行为,即,给nParticle2添加名为gravityField1_magnitude的attribute程序就会使用它来控制它受到重力场1的magnitude。然后通过DragField让particle稳定。我在注释里详细说。

粒子生成时脚本:

global int $freezeCap=5;	//be completely frozen after the cap rounds
global int $gravityPP=500;	
global float $dragPP=100;
global float $accelerationTrigger=15;	//the speed parameter to trigger 'I'm free again!", which leads to free droping.
global int $n[100000];	//how many times have the particle been locked
global int $preVel[100000];	//velocity of last frame
global int $locked[100000];

nParticleShape4.dragField1_magnitude=0;
nParticleShape4.gravityField1_magnitude=$gravityPP;

global int $init=1;
if($init==1) {
	int $i;
	for($i=0;$i<100000;$i++) {
		$n[$i]=0;
		$locked[$i]=0;
	}
	$init=0;
}

动态前的脚本:

int $id=nParticleShape4.particleId;
float $vel=abs(nParticleShape4.velocity);

//print("nParticleShape2.velocity "+$nParticleShape2.velocity+"\n");

if($vel>$preVel[$id]+$accelerationTrigger) {
	$n[$id]=0;
	nParticleShape4.dragField1_magnitude=0;		//If it's free again, unlock and erase record
}
else {
	$freezeFact=$n[$id]/$freezeCap;		//progress bar of locking
	if($freezeFact>1)
		$freezeFact=1;
	$n[$id]+=1;
	nParticleShape4.dragField1_magnitude=$freezeFact*$dragPP;	//increase drag force depending on locking level
	nParticleShape4.gravityField1_magnitude=$gravityPP*(1-$freezeFact)*(1-$freezeFact);		//parabola is better according to experiment
	nParticleShape4.velocity=<<0,0,0>>;
}

$preVel[$id]=nParticleShape4.velocity;

个人感觉这个还不错,有很多参数可以控制,效率也还行(前ACMer表示这个就是O(n)..不能再低了),200万粒子100帧i7跑半个小时。显然不完美,蒙人没问题。

Leave a Reply

Your email address will not be published. Required fields are marked *