Hi all,
Does anyone know what’s the algorithm of curve that Shuriken Particle System used?
Is that a Bézier Curves?
Thanks.
Hi all,
Does anyone know what’s the algorithm of curve that Shuriken Particle System used?
Is that a Bézier Curves?
Thanks.
The Shuriken Particle System just uses Unity’s AnimationCurve class and the built-in editor. Yes they are bezier curves, but the control points are calculated by the “tangent angle”.
edit
Finally had some time and i quickly created an editor window with a CurveField. This is the result:
Here’s the most important part of my sample code:
float w = R.width;
float h = R.height;
var keys = curve.keys;
for(int i = 0; i < keys.Length-1;i++)
{
Keyframe K1 = keys[i];
Keyframe K2 = keys[i+1];
Vector2 start = new Vector2(K1.time*w,K1.value*h);
Vector2 end = new Vector2(K2.time*w,K2.value*h);
float d = (end.x-start.x) / 3.0f;
float a = h / w;
Vector2 st = start + new Vector2(d,d*a*K1.outTangent);
Vector2 et = end + new Vector2(-d,-d*a*K2.inTangent);
Drawing.BezierLineGL(start,st,end, et,Color.red,20);
}
So basically you just take 1/3 of the distance between two points on the x-axis as the x position of the control point. When your viewing area’s aspect ratio differs from 1.0 you have to correct the y value accordingly (like i did).
So at the end you have a start point (start) two control points (st, et) and the end point (end) for each segment to draw a cubic bezier between two KeyFrames.
ps: R is a Rect which represents the drawing area’s size.
pps: This is an old post (10+ years old). Unity now actually supports a custom “weight” for both tangents which adds a lot more flexibility. So this code does not represent the current implementation. The fix factor of “1/3” is now a variable. Though the basic idea is essentially the same.
Another question, If the curves in Shuriken Particle System is Bezier, How do I apply the tangent parameter to the curve? e.g, If I changed the inTangent and outTangent member variable of Keyframes in AnimationCurve(AnimationCurve.keys), I watched the curve changed ,but why? the formula of Cubic Bezier curves does not have the parameter of tanget, right? Thanks.
– crtappsAs i said you have to calculate the control points yourself. The in and out tangent is not, [like stated in the docs][1], an angle in degree but the tangent of the desired angle. InTangent is positive when coming in from below and outTangent is positive when going up. a value of 0 is flat positiveInfinity represents an immediate step. [1]: http://docs.unity3d.com/Documentation/ScriptReference/Keyframe-inTangent.html
– Bunny83Thanks for your answer Bunny. I have understood the inTangent and outTanget in AnimationCurve. But how do I calculate the control points from tangent? I did not find the relevant information from google. Could you give me some tips or links about this area? Thanks.
– crtappsWell, i would suggest to use the tangent value directly ;) I would have to do some tests myself, but that should do it. So just go from your curvepoint x units to the right and x*outTangent upwards to get the control point. What x is the right one is hard to say without testing ;)
– Bunny83If you want to know what is the exact equation behind an AnimationCurve,
you should check Runtime Curve Editor (from Unity Asset Store).
The package replicates 100% the equation by which the Animation Curve built-in editor plots the curve.
Also the package supports visually editing of pairs of curves, which is used in the particle editor.
No one know?:(
– crtapps