{"id":2463,"date":"2012-07-13T15:12:27","date_gmt":"2012-07-13T15:12:27","guid":{"rendered":"http:\/\/www.west-racing.com\/mf\/?page_id=2463"},"modified":"2016-11-12T10:39:17","modified_gmt":"2016-11-12T10:39:17","slug":"megashape-api","status":"publish","type":"page","link":"http:\/\/www.west-racing.com\/mf\/?page_id=2463","title":{"rendered":"MegaShape API"},"content":{"rendered":"<p>This page guides you through the various classes that make up the Spline system inside MegaShapes. A MegaShape is made up of MegaSplines and each spline is in turn made from MegaKnots. Each MegaShape can have any number of MegaSplines to define its overall shape, each of those MegaSplines can be an open or closed shape and can contain any number of MegaKnots. A MegaKnot is the class which actually holds the position of the knot in local space as well as the position of the handles that control the in and out tangents to the knot.<\/p>\n<p>If you wish to add more knots to a spline you only need to create a new MegaKnot class using new and then Add the knot to the list or insert it into the list. You can also use the methods shown below to add knots to a spline.<\/p>\n<h3>MegaKnot<\/h3>\n<h3>Class<\/h3>\n<p>[cc lang=&#8221;c#&#8221;]public class MegaKnot<br \/>\n{<br \/>\n    public Vector3  p;<br \/>\n    public Vector3  invec;<br \/>\n    public Vector3  outvec;<br \/>\n    public float    seglength;<br \/>\n    public float    length;<br \/>\n    public bool     notlocked;<br \/>\n}[\/cc]<\/p>\n<h3>Param Members<\/h3>\n<h4><strong>p<\/strong><\/h4>\n<p>The position in local space of the actual knot<\/p>\n<h4><strong>invec<\/strong><\/h4>\n<p>The position in local space of the in vector handle<\/p>\n<h4><strong>outvec<\/strong><\/h4>\n<p>The position in local space of the out vector handle<\/p>\n<h4><strong>seglength<\/strong><\/h4>\n<p>The length of this curve segment to the next knot<\/p>\n<h4><strong>length<\/strong><\/h4>\n<p>The length from the start of the curve to this knot<\/p>\n<h4><strong>notlocked<\/strong><\/h4>\n<p>Used by the editor to say handles should be locked<\/p>\n<h3>Methods<\/h3>\n<p>[cc lang=&#8221;c#&#8221;]public MegaKnot()[\/cc]<br \/>\nCreates a new empty knot.<\/p>\n<p>[cc lang=&#8221;c#&#8221;]public Vector3 Interpolate(float t, MegaKnot k)[\/cc]<\/p>\n<div class=\"params\">\n<strong>t<\/strong> &#8211; Interpolation value, value in the 0 to 1 range<br \/>\n<strong>k<\/strong> &#8211; the knot to interpolate to, usually this is the next knot\n<\/div>\n<p>This will return the local position of a point on the curve segment between the knot and the one passed in, so if t = 0.0f it will return the current knot position, 1 will return the position of the passed in knot k.<\/p>\n<h3>MegaSpline<\/h3>\n<h3>Class<\/h3>\n<p>[cc lang=&#8221;c#&#8221;]public class MegaSpline<br \/>\n{<br \/>\n    public float              length;<br \/>\n    public bool               closed;<br \/>\n    public List<MegaKnot>     knots;<br \/>\n    public List<MegaKnotAnim> animations;<br \/>\n    public Vector3            offset;<br \/>\n    public Vector3            rotate;<br \/>\n    public Vector3            scale;<br \/>\n}[\/cc]<\/p>\n<h3>Param Members<\/h3>\n<h4><strong>length<\/strong><\/h4>\n<p>The length of the spline<\/p>\n<h4><strong>closed<\/strong><\/h4>\n<p>Is the spline closed or open<\/p>\n<h4><strong>knots<\/strong><\/h4>\n<p>The List of knots that make up this spline<\/p>\n<h4><strong>animations<\/strong><\/h4>\n<p>Animation data for the knots<\/p>\n<h4><strong>offset<\/strong><\/h4>\n<p>Offset in local space for the spline<\/p>\n<h4><strong>rotate<\/strong><\/h4>\n<p>Local euler rotation of the spline<\/p>\n<h4><strong>scale<\/strong><\/h4>\n<p>Local scale<\/p>\n<h3>Methods<\/h3>\n<p>[cc lang=&#8221;c#&#8221;]static public MegaSpline Copy(MegaSpline src)[\/cc]<\/p>\n<div class=\"params\">\n<strong>src<\/strong> &#8211; Spline to copy\n<\/div>\n<p>This will return a new copy of the spline passed to it.<\/p>\n<p>[cc lang=&#8221;c#&#8221;]public void AddKnot(Vector3 p, Vector3 invec, Vector3 outvec)[\/cc]<\/p>\n<div class=\"params\">\n<strong>p<\/strong> &#8211; Local space position of the knot<br \/>\n<strong>invec<\/strong> &#8211; local space position of the in vector handle<br \/>\n<strong>outvec<\/strong> &#8211; local space position of the out vector handle\n<\/div>\n<p>Will create a new knot from the values passed in and add it to the end of the knot list for this spline. Note a call should be made to MegaShape CalcLength after you have added or changed any knots.<\/p>\n<p>[cc lang=&#8221;c#&#8221;]public void AddKnot(Vector3 p, Vector3 invec, Vector3 outvec, Matrix4x4 tm)[\/cc]<\/p>\n<div class=\"params\">\n<strong>p<\/strong> &#8211; Local space position of the knot<br \/>\n<strong>invec<\/strong> &#8211; local space position of the in vector handle<br \/>\n<strong>outvec<\/strong> &#8211; local space position of the out vector handle<br \/>\n<strong>tm<\/strong> &#8211; The matrix to transform the values by before creating the knot\n<\/div>\n<p>Will create a new knot from the values passed in transformed by the matrix and add it to the end of the knot list for this spline. Note a call should be made to MegaShape CalcLength after you have added or changed any knots.<\/p>\n<p>[cc lang=&#8221;c#&#8221;]public bool Contains(Vector3 p)[\/cc]<\/p>\n<div class=\"params\">\n<strong>p<\/strong> &#8211; Local space point to check\n<\/div>\n<p>Will return true if the passed in local space point is inside the spline. If the spline is not closed the method will always return false.<\/p>\n<p>[cc lang=&#8221;c#&#8221;]public float Area()[\/cc]<br \/>\nWill return the area covered by the spline, if the spline is not closed it will return 0<\/p>\n<p>[cc lang=&#8221;c#&#8221;]public float CalcLength(int steps)[\/cc]<\/p>\n<div class=\"params\">\n<strong>steps<\/strong> &#8211; The number of iterations to use in the calculation of the length\n<\/div>\n<p>Calculates the length of the spline. The step value says how many interations to use in the calculation, a value of 1 would do a simple linear distance between each knot, a value of 10 will produce a very accurate length of the curve.<\/p>\n<p>[cc lang=&#8221;c#&#8221;]public Vector3 Interpolate(float alpha, bool type, ref int k)[\/cc]<\/p>\n<div class=\"params\">\n<strong>alpha<\/strong> &#8211; the point to return<br \/>\n<strong>type<\/strong> &#8211; true to use normalized interpolation, false to use linear interpolation<br \/>\n<strong>k<\/strong> &#8211; On return will hold the knot index the point is in\n<\/div>\n<p>Returns a local space position on the spline based on the alpha value passed. An alpha value of 0 will return the start of the spline and 1 to end and 0.5 will return the point half way along the length. The type value says whether to use normalized interpolation which is a little slower but gives a smoother result less prone to speed ups that can be seen in some splines. The value returned in &#8216;k&#8217; is the knot segment the point is in.<\/p>\n<p>[cc lang=&#8221;c#&#8221;]public Vector3 InterpCurve3D(float alpha, bool type, ref int k)[\/cc]<\/p>\n<div class=\"params\">\n<strong>alpha<\/strong> &#8211; the point to return<br \/>\n<strong>type<\/strong> &#8211; true to use normalized interpolation, false to use linear interpolation<br \/>\n<strong>k<\/strong> &#8211; On return will hold the knot index the point is in\n<\/div>\n<p>This is a more general interpolation method that deals with out of range alpha values correctly, the params are all the same as the above method.<\/p>\n<h3>MegaShape<\/h3>\n<h3>Class<\/h3>\n<p>[cc lang=&#8221;c#&#8221;]public class MegaShape : MonoBehaviour<br \/>\n{<br \/>\n    public MegaAxis         axis;<br \/>\n    public Color            col1;<br \/>\n    public Color            col2;<br \/>\n    public Color            KnotCol;<br \/>\n    public Color            HandleCol;<br \/>\n    public Color            VecCol;<br \/>\n    public float            KnotSize;<br \/>\n    public float            stepdist; \/\/ Distance along whole shape<br \/>\n    public bool             normalizedInterp;<br \/>\n    public bool             drawHandles;<br \/>\n    public bool             drawKnots;<br \/>\n    public bool             drawspline;<br \/>\n    public bool             lockhandles;<br \/>\n    public float            CursorPos;<br \/>\n    public List<MegaSpline> splines;<br \/>\n    public float            time;<br \/>\n    public bool             animate;<br \/>\n    public float            speed;<br \/>\n    public int              selcurve<br \/>\n    public MegaHandleType   handleType;<\/p>\n<p>    \/\/ Meshing options<br \/>\n    public bool             makeMesh;<br \/>\n    public MeshShapeType    meshType;<br \/>\n    public bool             DoubleSided;<br \/>\n    public bool             CalcTangents;<br \/>\n    public bool             GenUV;<br \/>\n    public bool             PhysUV;<br \/>\n    public float            Height;<br \/>\n    public int              HeightSegs;<br \/>\n    public int              Sides;<br \/>\n    public float            TubeStep;<br \/>\n    public float            Start;<br \/>\n    public float            End;<br \/>\n    public float            Rotate;<br \/>\n    public Vector3          Pivot;<\/p>\n<p>    \/\/ Material 1<br \/>\n    public Vector2          UVOffset;<br \/>\n    public Vector2          UVRotate;<br \/>\n    public Vector2          UVScale;<br \/>\n    public Vector2          UVOffset1;<br \/>\n    public Vector2          UVRotate1;<br \/>\n    public Vector2          UVScale1;<br \/>\n    public Vector2          UVOffset2;<br \/>\n    public Vector2          UVRotate3;<br \/>\n    public Vector2          UVScale3;<br \/>\n    public bool             UseHeightCurve;<br \/>\n    public AnimationCurve   heightCrv;<br \/>\n    public float            heightOff;<br \/>\n}<br \/>\n[\/cc]<\/p>\n<h3>Param Members<\/h3>\n<h4><strong>axis<\/strong><\/h4>\n<p>The up axis for when a helper shape is generated, normally this is the Y Axis.<\/p>\n<h4><strong>col1<\/strong><\/h4>\n<p>First color to use when the spline gizmos are drawn. Two colours are used so you can see how the interpolation will happen along the spline length, this is useful if you for example use the Path Follow helper script shorter sections will move slower than longer ones.<\/p>\n<h4><strong>col2<\/strong><\/h4>\n<p>Second color to use when the spline gizmos are drawn. Two colours are used so you can see how the interpolation will happen along the spline length, this is useful if you for example use the Path Follow helper script shorter sections will move slower than longer ones.<\/p>\n<h4><strong>KnotCol<\/strong><\/h4>\n<p>Color to use when displaying the knot gizmos.<\/p>\n<h4><strong>HandleCol<\/strong><\/h4>\n<p>Color to use when displaying the handle gizmos.<\/p>\n<h4><strong>KnotSize<\/strong><\/h4>\n<p>Size of the knot gizmos.<\/p>\n<h4><strong>stepDist<\/strong><\/h4>\n<p>This is the distance the gizmo will step along the spline for each drawn segment, this defines the smoothness of the spline for rendering of the gizmos. This value is also used if the spline is turned into a Mesh so will effect the number of vertices that are generated.<\/p>\n<h4><strong>normalizedInterp<\/strong><\/h4>\n<p>You can choose to have the spline interpolated using a simple linear approach or by using a normalized interpolation. The normalised approach will attempt to keep a more constant step along the spline but is slightly slower.<\/p>\n<h4><strong>drawHandles<\/strong><\/h4>\n<p>Whether or not the handle gizmos are drawn.<\/p>\n<h4><strong>drawKnots<\/strong><\/h4>\n<p>Whether or not the knots are drawn.<\/p>\n<h4><strong>drawSpline<\/strong><\/h4>\n<p>Whether or not the spline is displayed.<\/p>\n<h4><strong>lockhandles<\/strong><\/h4>\n<p>If lock handels is on dragging one handle will automatically drag the other handle so keeping the knots continuity. Turn this off if you want to control the sharpness of a corner or wish to have different in and out vectors to a knot.<\/p>\n<h4><strong>CursorPos<\/strong><\/h4>\n<p>This is the current position along the shape of the editor curor. The cursor is used when new knots are added to the shape in the editor.<\/p>\n<h4><strong>splines<\/strong><\/h4>\n<p>The List of splines that make up this shape. A MegaShape can be made up of any number of splines.<\/p>\n<h4><strong>time<\/strong><\/h4>\n<p>If the shape is animated then the time is used to control the animation.<\/p>\n<h4><strong>animate<\/strong><\/h4>\n<p>Turns the animation on or off.<\/p>\n<h4><strong>speed<\/strong><\/h4>\n<p>How fast the animation plays back, a value of 1 is normal speed, 0 will stop the animation and a negative value will play the animation backwards.<\/p>\n<h4><strong>selcurve<\/strong><\/h4>\n<p>In editor mode this value controls which spline is currently being edited in the scene.<\/p>\n<h4><strong>handleType<\/strong><\/h4>\n<p>Unused currently.<\/p>\n<h4><strong>makeMesh<\/strong><\/h4>\n<p>If this is on the mesh will be generated or updated, so if the shape is animated set this to true for the mesh to update as the spline changes.<\/p>\n<h4><strong>meshType<\/strong><\/h4>\n<p>Unused currently.<\/p>\n<h4><strong>DoubleSided<\/strong><\/h4>\n<p>Will the generated mesh have a back side.<\/p>\n<h4><strong>CalcTangents<\/strong><\/h4>\n<p>Calculate the tangents for the generated mesh, this is needed for bump mapping etc.<\/p>\n<h4><strong>GenUV<\/strong><\/h4>\n<p>Will UV coordinates be generated for the mesh.<\/p>\n<h4><strong>PhysUV<\/strong><\/h4>\n<p>Use physical UV values, this will mean the UV mapping will be based on the vertex positions instead of calculated UV positions.<\/p>\n<h4><strong>Height<\/strong><\/h4>\n<p>The height of the extruded mesh generated from the spline.<\/p>\n<h4><strong>HeightSegs<\/strong><\/h4>\n<p>Not used currently<\/p>\n<h4><strong>Sides<\/strong><\/h4>\n<p>Not used currently<\/p>\n<h4><strong>TubeStep<\/strong><\/h4>\n<p>Not used currently<\/p>\n<h4><strong>Start<\/strong><\/h4>\n<p>Not used currently<\/p>\n<h4><strong>End<\/strong><\/h4>\n<p>Not used currently<\/p>\n<h4><strong>Rotate<\/strong><\/h4>\n<p>Not used currently<\/p>\n<h4><strong>Pivot<\/strong><\/h4>\n<p>Not used currently<\/p>\n<h4><strong>UVOffset<\/strong><\/h4>\n<p>Value to add to the UV coordinates for the front face of the generated mesh.<\/p>\n<h4><strong>UVRotate<\/strong><\/h4>\n<p>Rotation amount of the UV coordinates for the front face of the generated mesh.<\/p>\n<h4><strong>UVScale<\/strong><\/h4>\n<p>Scale amount for the UV coordinates for the front face of the generated mesh.<\/p>\n<h4><strong>UVOffset1<\/strong><\/h4>\n<p>Offset amount for the UV coordinates for the extruded sides of the generated mesh.<\/p>\n<h4><strong>UVRotate1<\/strong><\/h4>\n<p>Rotation amount for the UV coordinates for the extruded sides of the generated mesh.<\/p>\n<h4><strong>UVScale1<\/strong><\/h4>\n<p>Scale amount for the UV coordinates for the extruded sides of the generated mesh.<\/p>\n<h4><strong>UVOffset2<\/strong><\/h4>\n<p>Value to add to the UV coordinates for the front face of the generated mesh.<\/p>\n<h4><strong>UVRotate3<\/strong><\/h4>\n<p>Rotation amount of the UV coordinates for the front face of the generated mesh.<\/p>\n<h4><strong>UVScale3<\/strong><\/h4>\n<p>Scale amount for the UV coordinates for the front face of the generated mesh.<\/p>\n<h4><strong>UseHeightCurve<\/strong><\/h4>\n<p>Controls whether the Height curve is used in the mesh generation.<\/p>\n<h4><strong>heightCrv<\/strong><\/h4>\n<p>The curve to control the Up offset of the mesh along its length.<\/p>\n<h4><strong>heightOff<\/strong><\/h4>\n<p>The value to multiple the height curve by to get the final height adjustment.<\/p>\n<h3>Methods<\/h3>\n<p>[cc lang=&#8221;c#&#8221;]public MegaSpline NewSpline()[\/cc]<\/p>\n<p>Creates a new empty spline and adds it to the spline list for this shape.<\/p>\n<p>[cc lang=&#8221;c#&#8221;]public void Scale(float scale)[\/cc]<\/p>\n<div class=\"params\">\n<strong>scale<\/strong> &#8211; the amount to scale the entire shape by\n<\/div>\n<p>This will scale the intire shape, so all curves and knots.<\/p>\n<p>[cc lang=&#8221;c#&#8221;]public float GetCurveLength(int curve)[\/cc]<\/p>\n<div class=\"params\">\n<strong>curve<\/strong> &#8211; The curve in the shape to get the length for\n<\/div>\n<p>Will return the length of the curve whose index is passed, if the index is out of range the length of the first curve will be returned.<\/p>\n<p>[cc lang=&#8221;c#&#8221;]public float CalcLength(int curve, int step)[\/cc]<\/p>\n<div class=\"params\">\n<strong>curve<\/strong> &#8211; the curve whoes length is to be calculated<br \/>\n<strong>step<\/strong> &#8211; The number of interpolatin steps to use in the length calculation\n<\/div>\n<p>This will return the length of the curve whoes index is passed in.<\/p>\n<p>[cc lang=&#8221;c#&#8221;]public float CalcLength(int step)[\/cc]<\/p>\n<div class=\"params\">\n<strong>step<\/strong> &#8211; The number of interpolation steps to use in the length calculation\n<\/div>\n<p>Use this to recalculate the length of the all the curves that make up the shape. It is important to call this of the method above if you have altered a shape in anyway, if not then the interpolation methods will not be quite right possibly with a alpha value of 1 not being the end of a curve or falling short.<\/p>\n<p>[cc lang=&#8221;c#&#8221;]public Vector3 GetKnotPos(int curve, int knot)[\/cc]<\/p>\n<div class=\"params\">\n<strong>curve<\/strong> &#8211; The curve to use<br \/>\n<strong>knot<\/strong> &#8211; the knot whose position you want\n<\/div>\n<p>This will return the local space position of the knot whose index is passed in on the curve whose index is passed in.<\/p>\n<p>[cc lang=&#8221;c#&#8221;]public Vector3 GetKnotInVec(int curve, int knot)[\/cc]<\/p>\n<div class=\"params\">\n<strong>curve<\/strong> &#8211; The curve to use<br \/>\n<strong>knot<\/strong> &#8211; the knot whose position you want\n<\/div>\n<p>This will return the local space position of the in vector handle whose index is passed in on the curve whose index is passed in.<\/p>\n<p>[cc lang=&#8221;c#&#8221;]public Vector3 GetKnotOutVec(int curve, int knot)[\/cc]<\/p>\n<div class=\"params\">\n<strong>curve<\/strong> &#8211; The curve to use<br \/>\n<strong>knot<\/strong> &#8211; the knot whose position you want\n<\/div>\n<p>This will return the local space position of the out vector handle whose index is passed in on the curve whose index is passed in.<\/p>\n<p>[cc lang=&#8221;c#&#8221;]public void SetKnotPos(int curve, int knot, Vector3 p)[\/cc]<\/p>\n<div class=\"params\">\n<strong>curve<\/strong> &#8211; The curve whose knot we are repositioning<br \/>\n<strong>knot<\/strong> &#8211; The knot we want to position<br \/>\n<strong>p<\/strong> &#8211; the new local space position of the knot\n<\/div>\n<p>This will update the knots local space position and update the curves length.<\/p>\n<p>[cc lang=&#8221;c#&#8221;]public void SetKnot(int curve, int knot, Vector3 p, Vector3 intan, Vector3 outtan)[\/cc]<\/p>\n<div class=\"params\">\n<strong>curve<\/strong> &#8211; The curve whose knot we are repositioning<br \/>\n<strong>knot<\/strong> &#8211; the knot we want to position<br \/>\n<strong>p<\/strong> &#8211; the new local space position of the knot<br \/>\n<strong>intan<\/strong> &#8211; the new local space position of the in vector handle<br \/>\n<strong>outtan<\/strong> &#8211; the new local space position of the out vector handle\n<\/div>\n<p>Use this method to completely change a knots position, you pass in the curve and knot index you wish to change and then the new local space positions of the knot and the in and out vector handles. The method will then call CalcLength to update the lengths of the whole shape. If you are going to change a lot of values on a shape at one time then it is best to access the knot data through the arrays directly and update all the values and then call CalcLength() yourself at the end to make it faster.<\/p>\n<p>[cc lang=&#8221;c#&#8221;]public void MoveKnot(int curve, int knot, Vector3 p)[\/cc]<\/p>\n<div class=\"params\">\n<strong>curve<\/strong> &#8211; The curve whose knot we are repositioning<br \/>\n<strong>knot<\/strong> &#8211; the knot we want to position<br \/>\n<strong>p<\/strong> &#8211; the new local space position of the knot\n<\/div>\n<p>This method will move a knot to a new position and move the tangents along with it. The CalcLength will then be called to update the lengths for the shape.  If you are going to change a lot of values on a shape at one time then it is best to access the knot data through the arrays directly and update all the values and then call CalcLength() yourself at the end to make it faster.<\/p>\n<p>[cc lang=&#8221;c#&#8221;]public Vector3 InterpCurve3D(int curve, float alpha, bool type)[\/cc]<\/p>\n<div class=\"params\">\n<strong>curve<\/strong> &#8211; the curve to interpolate along<br \/>\n<strong>alpha<\/strong> &#8211; the point to return<br \/>\n<strong>type<\/strong> &#8211; true to use normalized interpolation, false to use linear interpolation\n<\/div>\n<p>Returns a local space position on the spline based on the alpha value passed. An alpha value of 0 will return the start of the spline and 1 to end and 0.5 will return the point half way along the length. The type value says whether to use normalized interpolation which is a little slower but gives a smoother result less prone to speed ups that can be seen in some splines. If you pass in values out side of the 0 to 1 range for alpha the function will either interpolate a position before the first knot if the value is &lt; 0 and the spline is open, or interpolate a value past the end of the last knot if the value is &gt; 1 and the spline is open. If the spline is closed then the value will be adjusted to be in the range and then used, so if -1.5 is passed in the value will become 0.5 so the midpoint will be returned.<\/p>\n<p>[cc lang=&#8221;c#&#8221;]public Vector3 FindNearestPointWorld(Vector3 p, int iterations, ref int kn, ref Vector3 tangent, ref float alpha)[\/cc]<\/p>\n<div class=\"params\">\n<strong>p<\/strong> &#8211; The point in world space to find the nearest point to<br \/>\n<strong>iterations<\/strong> &#8211; the number of steps taken to find the nearest point, the higher the value the more accurate the result will be<br \/>\n<strong>kn<\/strong> &#8211; on return this will hold the knot the point is in<br \/>\n<strong>tangent<\/strong> &#8211; on return this will hold the tangent at the nearest point<br \/>\n<strong>alpha<\/strong> &#8211; on return this will hold the alpha value along the shape\n<\/div>\n<p>This method will find the nearest point in the shape to the world space point passed in and return the point in world space. It will also return the knot, the tangent and the alpha value for the point.<\/p>\n<p>[cc lang=&#8221;c#&#8221;]public Vector3 FindNearestPoint(Vector3 p, int iterations, ref int kn, ref Vector3 tangent, ref float alpha)[\/cc]<\/p>\n<div class=\"params\">\n<strong>p<\/strong> &#8211; The point in the shapes local space to find the nearest point to<br \/>\n<strong>iterations<\/strong> &#8211; the number of steps taken to find the nearest point, the higher the value the more accurate the result will be<br \/>\n<strong>kn<\/strong> &#8211; on return this will hold the knot the point is in<br \/>\n<strong>tangent<\/strong> &#8211; on return this will hold the tangent at the nearest point<br \/>\n<strong>alpha<\/strong> &#8211; on return this will hold the alpha value along the shape\n<\/div>\n<p>This method will find the nearest point in the shape to the local space point passed in and return the point in world space. It will also return the knot, the tangent and the alpha value for the point.<\/p>\n<p>[cc lang=&#8221;c#&#8221;]public void BuildSpline(int curve, Vector3[] points, bool closed)[\/cc]<\/p>\n<div class=\"params\">\n<strong>curve<\/strong> &#8211; The Curve to rebuild<br \/>\n<strong>points<\/strong> &#8211; The local space points that define the knot positions for the new pline<br \/>\n<strong>closed<\/strong> &#8211; Whether the new spline is open or closed\n<\/div>\n<p>This method will rebuild the spline using the Vector3 points array passed in and autocalc the handles for the knots to produce a smooth spline. You can also say whether the spline will be open or closed.<\/p>\n<p>[cc lang=&#8221;c#&#8221;]public void BuildSpline(Vector3[] points, bool closed)[\/cc]<\/p>\n<div class=\"params\">\n<strong>points<\/strong> &#8211; The local space points array that define the knot position for the new spline<br \/>\n<strong>closed<\/strong> &#8211; Is the new spline open or closed.\n<\/div>\n<p>This is like the BuildSpline method above but will create a new spline from the points and add it to the end of the spline list for the shape.<\/p>\n<p>[cc lang=&#8221;c#&#8221;]public void AddToSpline(int curve, Vector3[] points)[\/cc]<\/p>\n<div class=\"params\">\n<strong>curve<\/strong> &#8211; The curve to add to<br \/>\n<strong>points<\/strong> &#8211; The array of local space points to extend the spline with.\n<\/div>\n<p>This will extend the spline chosen by the curve value with the points passed in and calculate the handles.<\/p>\n<p>[cc lang=&#8221;c#&#8221;]public void AddToSpline(int curve, Vector3 point)[\/cc]<\/p>\n<div class=\"params\">\n<strong>curve<\/strong> &#8211; The spline to add to<br \/>\n<strong>point<\/strong> &#8211; The local space point to add to the spline\n<\/div>\n<p>Will extend the spline chosen by the curve value by a single point.<\/p>\n<p>[cc lang=&#8221;c#&#8221;]public void AutoCurve(MegaSpline spline)[\/cc]<\/p>\n<div class=\"params\">\n<strong>spline<\/strong> &#8211; The spline to calculate the handles for\n<\/div>\n<p>This will recalc the handles for the spline passed in to make the smoothest shape it can.<\/p>\n<p>[cc lang=&#8221;c#&#8221;]public void AutoCurve(MegaSpline spline, int start, int end)[\/cc]<\/p>\n<div class=\"params\">\n<strong>spline<\/strong> &#8211; The spline to adjust the handles on<br \/>\n<strong>start<\/strong> &#8211; The index of the starting knot to smooth<br \/>\n<strong>end<\/strong> &#8211; The index of the ending knot to smooth\n<\/div>\n<p>This will calculate the handles for the spline starting with the start index knot and going to the end index knot. This allows you to autosmooth part of a spline.<\/p>\n<p>[cc lang=&#8221;c#&#8221;]public void AutoCurve()[\/cc]<br \/>\nThis will recalc the handles for all the splines in a shape and recalc the handles to give the smoothest splines.<\/p>\n<p>[cc lang=&#8221;c#&#8221;]public void BuildMesh()[\/cc]<br \/>\nCall this to build or rebuild a mesh from the shape.<\/p>\n<h3>Code examples<\/h3>\n<p>The code below is taken from the MegaShapeCircle.cs and shows how to create a spline in code, this will make a closed circle spline from a radius value, the spline produced is made from 4 knots and the handles for each knot are calculated to form a perfect circle. The important thing to notice is once a spline has been created or altered in any way the CalcLength() method should be called so that the internal lengths of the spline can be calculated for quicker interpolation later, the value passed in will determine how accurate the lengths are, lower values can be used for quicker calculations, 10 gives a very accurate length without too much overhead.<\/p>\n<p>[cc lang=&#8221;c#&#8221;]public class MegaShapeCircle : MegaShape<br \/>\n{<br \/>\n\tpublic float Radius = 1.0f;<\/p>\n<p>\tconst float CIRCLE_VECTOR_LENGTH = 0.5517861843f;<\/p>\n<p>\tpublic override void MakeShape()<br \/>\n\t{<br \/>\n\t\tMatrix4x4 tm = GetMatrix();<\/p>\n<p>\t\tfloat vector = CIRCLE_VECTOR_LENGTH * Radius;<\/p>\n<p>\t\tMegaSpline spline = NewSpline();<\/p>\n<p>\t\tfor ( int ix = 0; ix < 4; ++ix )\n\t\t{\n\t\t\tfloat angle = (Mathf.PI * 2.0f) * (float)ix \/ (float)4;\n\t\t\tfloat sinfac = Mathf.Sin(angle);\n\t\t\tfloat cosfac = Mathf.Cos(angle);\n\t\t\tVector3 p = new Vector3(cosfac * Radius, sinfac * Radius, 0.0f);\n\t\t\tVector3 rotvec = new Vector3(sinfac * vector, -cosfac * vector, 0.0f);\n\t\t\tspline.AddKnot(p, p + rotvec, p - rotvec, tm);\n\t\t}\n\n\t\tspline.closed = true;\n\t\tCalcLength(10);\n\t}\n}[\/cc]\n\nThis is a very simple example that will build a spline from an array of world space points, note you can use local space if you use the BuildSpline method instead, you can also pick which spline in the shape you want to change and also whether or not the spline is open of closed by changing the last param of the BuildSpline method to true.\n\n[cc lang=\"c#\"]public class MakeSpline : MonoBehaviour\n{\n\tpublic MegaShape\tshape;\t\t\/\/ Shape object \n\tpublic Vector3[]\tpoints;\t\t\/\/ World space points to make into a spline\n\n\tvoid Start()\n\t{\n\t\tif ( shape &#038;&#038; points.Length > 1 )<br \/>\n\t\t\tshape.BuildSplineWorld(0, points, false);<br \/>\n\t}<br \/>\n}[\/cc]<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This page guides you through the various classes that make up the Spline system inside MegaShapes. A MegaShape is made up of MegaSplines and each spline is in turn made [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":390,"menu_order":0,"comment_status":"open","ping_status":"open","template":"","meta":[],"_links":{"self":[{"href":"http:\/\/www.west-racing.com\/mf\/index.php?rest_route=\/wp\/v2\/pages\/2463"}],"collection":[{"href":"http:\/\/www.west-racing.com\/mf\/index.php?rest_route=\/wp\/v2\/pages"}],"about":[{"href":"http:\/\/www.west-racing.com\/mf\/index.php?rest_route=\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"http:\/\/www.west-racing.com\/mf\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.west-racing.com\/mf\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2463"}],"version-history":[{"count":59,"href":"http:\/\/www.west-racing.com\/mf\/index.php?rest_route=\/wp\/v2\/pages\/2463\/revisions"}],"predecessor-version":[{"id":6596,"href":"http:\/\/www.west-racing.com\/mf\/index.php?rest_route=\/wp\/v2\/pages\/2463\/revisions\/6596"}],"up":[{"embeddable":true,"href":"http:\/\/www.west-racing.com\/mf\/index.php?rest_route=\/wp\/v2\/pages\/390"}],"wp:attachment":[{"href":"http:\/\/www.west-racing.com\/mf\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2463"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}