| Previous Topic (MagmaFlow Practical Example GIJoe) | Up (Tutorials) | Next Topic (Passing Data Between KCM and DataOp) |
Controlling Particle Density and Color By Age Using MagmaFlow
Applicable to Krakatoa 1.5.0 and higher
Overview
- In previous versions of Krakatoa, fading off particles by Age required pre-processing of the Age, LifeSpan and Density data using either Script Operators or Box #3 DataOps inside of Particle Flow - see the following Tutorial for details.
- In Krakatoa 1.5.0 and higher, the same can be done as a post-processing step as long as the PRT files contain the Age and LifeSpan channels generated by Particle Flow.
- In addition, Krakatoa v1.5.1 introduces a Curve Operator which can be used to completely customize the falloff values.
Workflow
Creating a Sample Particle Flow
- In the following example, we will use a simple Particle Flow - quite similar to the one found in the previous Tutorial mentioned above.
- We create a Standard PFlow, set the Emitter shape to Circle with Radius of 20.0.
- We set the Birth Operator to emit 100K particles from frame 0 to 100.
- We set the Speed Operator to 100.0 units per second without any variation and Flip the direction.
- We delete the Rotation and Shape Operators
- We add a Delete operator and set it to By Particle Age, Life Span 60 Variation 0.
- The result is a simple column of particles rising up and dying after 60 frames.
Saving the Particles to PRT Sequence
- We open the Krakatoa GUI, switch to "Save Particles To File Sequence" and specify a PRT file name to save to in the Save Particles rollout.
- We expand the Channels rollout and specify the following channels for saving:
- Position
- Velocity (optional)
- ID (optional)
- LifeSpan
- Age
- We can then save frames 0 to 100 of the Particle Flow to the PRT file sequence.
- Once the saving is done, we can disable the Particle Flow
Creating a PRT Loader with KCMs
- Now we can create a PRT Loader - if you have the Krakatoa MacroScripts installed on a toolbar, hold down the SHIFT key and click the PRT icon.
- In the PRT Loader created at the origin, select the PRT sequence saved in the previous step.
- Set the Viewport % to 100.0 to show all particles.
- Add a KCM to the PRT Loader (if you have the Krakatoa MacroScripts installed, just press the KCM icon, otherwise add a Krakatoa Channels modifier from the Modifiers list).
- Open the MagmaFlow editor and set the Output node to Density.
- Change the Input node to Age channel.
- With the Age Channel Input node selected, drag a ToFloat Operator node from the Converst category of the Depot, or use the right-click menu to add one. We have to do this because the Age and LifeSpan are generated and stored as Integer Ticks and not as Floats.
- Press SHIFT+L to create another Input node with the LifeSpan channel.
- With the LifeSpan Channel Input node selected, drag a ToFloat Operator node from the Convert category of the Depot, or use the right-click menu to add one.
- Select the two ToFloat Operators and hit the Divide / key on the Numeric Keypad to create a Divide Operator node. It will be connected to both ToFloat nodes.
- Drag a connection from the output socket of the Divide to the input socket of the Output operator.
- Add a second KCM to the PRT Loader
- Switch its input node to Density.
- Since the Output is a Vector (Color) and the Input is a Float (Density), hit the T key with the Input node still selected to create a ToVector operator.
- Select the Input node and the ToVector Operator node and press the Spacebar key twice to connect the Input to the second and third sockets, thus turning the Density into a grayscale color.
- To avoid rendering the black&white gradient, you can optionally right-click the Modifier and set it to Off In Renderer. This way, the Density will be shown as Color only in the viewports!
- In the viewport, you will notice that the Color of the particles (which is now representing their Density) is changing gradually from black (where Age is 0 and thus Age/LifeSpan=0) to white (where Age/LifeSpan=1.0)
- Since we want our density to start at 1.0 and end at 0.0, we have to invert the values.
- Select the KCM_Density modifier and in the MagmaFlow, select the Divide Operator node.
- Hit the - (Minus) key on the Numeric Keypad to insert a Subtract Operator node between the Divide and the Output nodes.
- Press Ctrl+W to swap the first and second input sockets of the Subtract Operator.
- Press F to create a Float Input node with a value of 1.0.
- As result, our particles are now colored white in the beginning and black at the end of their life.
Exponent Curve
- Currently, our Density falloff is linear - particles with an Age of 30 frames have half the Density of particles with Age of 0.
- We could change the falloff curve to an exponential one by adding a Power Operator between the Subtract and the Output Operators.
- Select the Subtract Operator and press the 6 key (it also has the ^ symbol on it!) to add a Power Operator.
- With the Power Operator still selected, hit Ctrl+6 to create a Float Input with a value of 6.0.
- The result in the viewport will look like this:
- By changing the Power Float Input to values below 1.0, we can change the curvature of the falloff in the opposite direction, producing more dense particles throughout most of their life.
- For example a value of 0.5 produces the following falloff:
Krakatoa 1.5.1 Curve Operator Node
Available in Krakatoa v1.5.1 and higher
- Instead of using mathematical operators to control the inversion and the falloff of the Age curve, we can use the Curve Operator in Krakatoa v1.5.1 and higher.
- The Curve Operator takes an input in the range from 0.0 to 1.0 and outputs a value based on a user-defined curve.
- We will have to remove some of the operators first:
- Select the Power Operator added in the previous step and press the Backspace key to remove it while preserving the flow.
- Select the now disconnected Float Input of the Power Operator and press the Backspace or Delete key to delete it.
- Select the Subtract Operator, switch it to Function and select Curve from the type list.
- The default Curve control is set to a line mapping input value of 0.0 to output value of 0.0 and 1.0 to 1.0.
- From the Presets [>>] list, we can select some preset curves, for example Accelerate:
- The result in the viewport is flipped again, since the 0.0 value corresponds to black and not to white.
- We can fix this by flipping the Curve horizontally using the Horizontal Flip button, thus mapping 0.0 to 1.0 and 1.0 to 0.0:
- Similarly, we can select the Decelerate preset and flip it to get the slower falloff of Density:
- The Curve Operator lets us define ANY curve using Corner, Bezier Corner and Bezier points. Thus, we can create rather fancy particle Density falloffs:
- The results of the Accelerate, Accelerate Flipped, Decelerate Flipped and Custom Curves shown above can be seen on the four copies of the PRT Loader in the following screenshot:
Color Gradient By Age
- Based on the above setups, we could easily produce gradients between two colors by just feeding the Density value in the range from 0.0 to 1.0 into a Blend Operator with two vector inputs.
- If we want to use a large number of intermediate colors though, going the procedural way by introducing even more vector nodes and trying to blend between them inside the KCM would be rather convoluted.
- A simpler approach would be to convert the Density into the U coordinate of the UVW Mapping Channel and use it to map a Gradient or Gradient Ramp map to the particles. This is a lot closer to the Particle Age Map approach of 3ds Max, but with a lot more flexibility!
- We change the second KCM to convert the Density into the U component of the Texture Coordinate and we set Y and W to 0 using a Float Input Node.
- Then we switch the Output to TextureCoord channel which corresponds to Mapping Channel 1 - we could use ANY of the 99 channels though.
- We assign a Standard Material to all PRT Loaders and add a Gradient Ramp map to the Diffuse Channel.
- The Gradient Ramp is set to Explicit Map Channel 1 by default and we can define any colors we want in it.
- The result in the viewport looks like this:
- As you can see, the gradient is going in the wrong direction because the left of the map corresponds to U value of 0.0 and the right to 1.0, but we have flipped the Density to start with 1.0 and end with 0.0.
- Instead of reworking the whole Gradient Ramp flags, we can simply enter a U Tiling of -1 - this will mirror the map colors and give us the result we want:
- Obviously, you could apply the same approach to Bitmap textures and paint the colors into a bitmap in Photoshop, pointing at the correct texel using the TextureCoord channel as needed.
- Also keep in mind that every flag of the Gradient Ramp can be right-clicked and set to any Texture Map tree you want.
- For example, in the following example the left red flag was replaced with a Cellular Map, the middle cyan flag was replaced with a Checker Map (turquoise and orange) and the right red flag was replaced with a Noise Map: