PDA

View Full Version : Particle Color by Distance to Object


Deracus
05-21-2011, 10:37 PM
Hi there,

i am working myself into PFlow and got a nice little project going on right now.
I created a voxelated shape of an object via Birthgrid and two Group Select/ Split Group Operators to make only the particles inside the reference object visible.

It is rather slow - as i expected it to be, because i read about that in another thread here - but it works as i want it to. Speed is not a problem right now and i only got Box #2 to work with and no Box #3 so it is like it is.

The problem i got now is, that i want to color my objects based on the distance to another reference object.
I tried it by using a material with Distance-Blend-Falloff, but that is not exactly what i wanted, because the falloff also works on the surface of each particle, but i want them to be solid colors.

Is there a way to change the color of each single particle based on the distance and then use that color in a standard VRay-Material? Thanks for the help in advance ;)

JohnnyRandom
05-22-2011, 01:09 AM
Without Box#3 your pretty much going to have to using a Mapping Object operator more than likely set to continuous, it also gives you the option of Uniform Color Per Particle. The only bummer about it is the speed factor, you already have a fairly taxing particle system and Mapping Object is a pretty heavy operator in itself especially when set to Continuous evaluation.

It should do the trick for you though :)

I haven't checked 2012 but there is a pretty crappy bug with the falloff map - distance blend/object and pflow, it wants to work in world center instead of local/object space.

Deracus
05-22-2011, 07:56 AM
Without Box#3 your pretty much going to have to using a Mapping Object operator more than likely set to continuous, it also gives you the option of Uniform Color Per Particle. The only bummer about it is the speed factor, you already have a fairly taxing particle system and Mapping Object is a pretty heavy operator in itself especially when set to Continuous evaluation.

It should do the trick for you though :)

I haven't checked 2012 but there is a pretty crappy bug with the falloff map - distance blend/object and pflow, it wants to work in world center instead of local/object space.

Thanks for your help :)
I will give mapping object a try....and maybe decide to get Box #3 afterwards ;)

As i am on 2011 here, the bug is not really important for me, but thanks ;)

Bobo
05-22-2011, 09:06 AM
Calculating the distance from a particle to a reference object (assuming you are using its position and not its surface) is pretty trivial using a Script Operator. And if you dump the resulting color into any Mapping Channel, you could use a Material with a Vertex Color Map to color the complete particle in the same color.

Here is an example of the ScriptOperator code:

on ChannelsUsed pCont do
(
pCont.useMapping = true --enable mapping channels access
)

on Init pCont do ( )

on Proceed pCont do
(
count = pCont.NumParticles() --get particle count
for i in 1 to count do --loop through particles
(
pCont.particleIndex = i --set the current particle
--calculate distance to an object, then normalize in the range from 0 to 500
blendValue = (distance pCont.particlePosition $Teapot01.pos)/500.0
if blendValue > 1.0 do blendValue = 1.0 --clamp if distance > 500
mapValue = [1,0,0] * blendValue + [0,0,1] * (1.0-blendValue) --blend red (far) and blue (near)
pCont.setParticleMapping i 0 mapValue --set the Vertex Color Channel
)
)

on Release pCont do ( )

If you add a Material Static with a Material using Vertex Color Map for the diffuse color, you will get the coloring by distance without Box #3. (Of course, Box #3 would be faster).
Also, in the Particle View, you can select the Emitter and enable Vertex Color shading in its properties to show the colors in the viewports.

Deracus
05-22-2011, 05:06 PM
Calculating the distance from a particle to a reference object (assuming you are using its position and not its surface) is pretty trivial using a Script Operator. And if you dump the resulting color into any Mapping Channel, you could use a Material with a Vertex Color Map to color the complete particle in the same color.

Here is an example of the ScriptOperator code:

on ChannelsUsed pCont do
(
pCont.useMapping = true --enable mapping channels access
)

on Init pCont do ( )

on Proceed pCont do
(
count = pCont.NumParticles() --get particle count
for i in 1 to count do --loop through particles
(
pCont.particleIndex = i --set the current particle
--calculate distance to an object, then normalize in the range from 0 to 500
blendValue = (distance pCont.particlePosition $Teapot01.pos)/500.0
if blendValue > 1.0 do blendValue = 1.0 --clamp if distance > 500
mapValue = [1,0,0] * blendValue + [0,0,1] * (1.0-blendValue) --blend red (far) and blue (near)
pCont.setParticleMapping i 0 mapValue --set the Vertex Color Channel
)
)

on Release pCont do ( )

If you add a Material Static with a Material using Vertex Color Map for the diffuse color, you will get the coloring by distance without Box #3. (Of course, Box #3 would be faster).
Also, in the Particle View, you can select the Emitter and enable Vertex Color shading in its properties to show the colors in the viewports.

Wow Bobo, thats awesome. I wanted to get a hang on PFlow-Scripting anyway...so thanks for giving me that great example :)

IŽll try it out and build it into my solution. Thanks!

CGTalk Moderation
05-22-2011, 05:06 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.