Particle tips FluxFactorExpression

From PopcornFX
Jump to: navigation, search

For PK-Fx Editor version : 1.4.0 and above
Main page: Particle tutorials


It is possible to control the spawn-rate of particle spawners through particle attributes, thus making it controllable by gameplay.

We'll start with a simple effect rig, just a simple upward particle fountain:


Tips_FluxFactorExpr_01.png


Using an Attribute to control spawn-rate

Let's start by boosting up the particle count from 10 per second to 10000.
Go in the attributes panel, and click on the 'Create' button to add a new attribute:

Tips_FluxFactorExpr_03.png

Flux factor binding


Click on the small arrow on the left hand-side to unroll the attribute and access its parameters, we'll rename it to 'Strength'.
Set its default value at '0.1' (which will produce a particle count of 10000 * 0.1 = 1000 particles/second by default),
and activate min/max bounds in the [0, 1] range, to allow the editor to display a slider for easier tweaking.

Flux factor binding



Then go back to the particle spawner, and in the 'FluxFactorExpression', enter the name of the newly created attribute: 'Strength'.
The particle spawner will now use that attribute to weight the spawn-rate.

Flux factor binding



Tweaking the slider will dynamically change the preview in the realtime viewport, like changing the attribute in gameplay code would change the effect instance in your game.
(Attributes can have different values across different instances of the same effect).

Medium flux factor Medium flux factor

Small flux factor Small flux factor

Large flux factor Large flux factor



If you want to pack values in a float2, float3, or float4 attributes, the 'FluxFactorExpression' allows you to use a non-scalar (non-float1) attribute,
and specify which component you want to use as the flux factor, using a simple syntax like in particle scripts:

MyAttribute.x // use the 'x' component
MyAttribute.y // use the 'y' component
MyAttribute.z // use the 'z' component
MyAttribute.w // use the 'w' component

that's why it's called 'FluxFactorExpression', and not just 'FluxFactor' :)


Flux factor expression : swizzles

Flux factor expression : swizzles


Side-Notes

If you want to change the spawn-rate across the effect lifetime in a predictable manner, if possible it will be better if you use Flux function Flux functions rather than animating the attribute through one of your game engine's curves or animations.

The reason for this is simple: the attribute is set once per frame ("discretized"). the animation slope and overall shape of the anim curve is lost at that point.
Popcorn will have no way to know how it changes, and will treat it as discrete 'steps' from one frame to another, therefore producing a potentially jerky/unstable animation, depending on the fps.

However, when using flux functions, popcorn has an intimate knowing of the whole function and is able to:

  • perform internal optimizations
  • smoothly integrate the flux function across the frame, guaranteeing the correct spawn-flux no matter the FPS.


FluxDiscretization1.png FluxDiscretization2.png
Ingame curve, discretized each frame and fed to popcorn
through an attribute.

Frame2 misses the spawn-rate peak.

The same curve as a flux-function inside popcorn.
The curve integral is used.

Frame2 correctly takes the peak into account.


You can combine the two if you want both smooth integration of the flux curve, and control of the overall flux by gameplay:

Flux function

Small emission flux Small emission flux

Large emission flux Large emission flux


NOTE: using one of your ingame curves without missing spikes is still possible, but some extra computations will be required, your programmers will need to manually compute the curve integral over the update timestep, and divide by that timestep to fake an average 'spawn-rate' that will mimic the flux-functions calculation, and pass that as an attribute.


It's probably easier to just use popcorn's flux functions :)