PDA

View Full Version : RGB Intensity of a particle's mat based on speed.


Cryptite
03-25-2011, 05:14 PM
Trying to see if I can influence a particle's map's rgb color/intensity based on the speed. I have no problems grabbing the speed of the particle with a script operator, but any ideas on how I can take the current material applied to the particle and then multiple the RGB Color of that map based on the speed? Thanks!

Bobo
03-25-2011, 06:46 PM
Usually you would put the value into any of the 99 mapping channels and use a Vertex Color Map in something like an RGB Multiply map to tweak the color of the material... Or you could calculate the final color itself and just use the Vertex Color Map in the Diffuse slot to pass it to the Material. But if you have a complex map tree you want to affect, the former approach might be what you want...

Cryptite
03-25-2011, 07:58 PM
That's pretty close to what I came up with too. However I'm having trouble as the particles aren't getting an independently different color setting. They're all getting the same value of color. Here's my code. I attempted to sort of adopt your "Inherit Texel Color From Emitter" tutorial.

Essentially this just gets the speed and multiplies it (so you can see the difference), then sets the diffuse map's rgb color value to it.


on Init pCont do
(
global temp_emesh = $Temp_Emesh01
if temp_emesh == undefined do
(
temp_emesh = Editable_mesh()
temp_emesh.name = "Temp_Emesh01"
temp_emesh.renderable = false
)
)

on Proceed pCont do
(
count = pCont.NumParticles()

for i in 1 to count do
(
pCont.particleIndex = i
temp_emesh.mesh = pCont.particleShape
speed = length(pCont.particleSpeed)
mapColorValue = speed*200
temp_emesh.mat.maps[2].output.output_amount = mapColorValue
pCont.particleShape = temp_emesh.mesh
)
)

Bobo
03-25-2011, 08:45 PM
That won't work for many reasons.

What I suggested was to set the Particle's Mapping Channel to the value, then use Vertex Color Map to render it. You should never try to modify a material property out of the PFlow code, because you never know how the renderer is evaluating that...


Try this:

*Create a Standard Flow in Particle View.
*Change Speed to Speed 300 / Variation 300
*Add a script operator and enter the following code

on ChannelsUsed pCont do
(
pCont.useMapping = true
pCont.useSpeed = true
)

on Init pCont do ( )

on Proceed pCont do
(
count = pCont.NumParticles()
for i in 1 to count do
(
pCont.particleIndex = i
theFactor = ((length pCont.particleSpeed)*600.0/FrameRate)
if theFactor > 1.0 do theFactor = 1.0
pCont.setParticleMapping i 0 ([0,0,1] + [1,0,-1] * theFactor)
)
)

on Release pCont do ( )

*Right-click the PF Source in the Particle View, Properties..., enable the "Vertex Channel Display" > Vertex Color / Shaded.
*Add a Material Static, assign a Standard Material, add a Vertex Color Map to the Diffuse channel

Result: Particles moving at max. speed of 600 units will be red, particles moving at lowest speed (0) will be blue. And will render the same way, too.

You can simplify the code as

pCont.setParticleMapping i 0 ([1,1,1] * theFactor)

to produce a black-white gradient.
If you want it really fancy, you could add a Gradient Ramp to the Material's Diffuse Slot, disable its U Tiling, set it to use the Vertex Color Channel for mapping (or whatever channel you might be writing to from the script) and set the Gradient flags to the colors you want for complete freedom in the speed gradient generation....
For it to work well,

Cryptite
03-25-2011, 09:31 PM
The gradient ramp control idea sounds stellar, but I'm having no luck on getting it to work. Attached is the attempted max scene (Max 2011).

I wasn't quite sure what you mean by using the VCC for the gradient ramp, whether you meant as the texture provider for Coordinates? or as the actual Source Map. At times I was getting the right-end of the gradient ramp to successfully control the color, but not so with the left.

Thanks for all your help so far, Bobo.

Also, any chance you could explain what the "pCont.setParticleMapping i 0 ([0,0,1] + [1,0,-1] * theFactor)" part of the code does? I tried messing around with the values a bit to achieve the color-span that I wanted, but I was pretty sure I was just guessing, and I'd like to know what I'm really doing.

Thanks!

Bobo
03-25-2011, 09:51 PM
The Tiling can be in the way. When you have a pure 0.0 or 1.0 value, the Gradient Ramp can Warp around, so 1.0 appears as 0.0 color. That's why I suggested disabling the Tiling in the Gradient Ramp.

Alternatively, you can clamp the values in the code, like

if theFactor >= 1.0 do theFactor = 0.99
if theFactor == 0.0 do theFactor = 0.01


As for the
pCont.setParticleMapping i 0 ([0,0,1] + [1,0,-1] * theFactor)

it means that when theFactor is 0.0, the color will be [0,0,1] (blue).
When theFactor is 1.0, the color would be [0,0,1]+[1,0,-1] = [1,0,0] (red)
When theFactor is in between, say 0.5, you get [0,0,1]+[0.5,0,-0.5] = [0.5,0,0.5] (purple)

Just a way to blend linearly between two colors.

Don't forget to DISABLE the Vertex Color in the viewport when using the Gradient Ramp approach, and enable "Show Map In Viewport" in Medit to see the colors on the particles...

CGTalk Moderation
03-25-2011, 09:51 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.