{"id":655,"date":"2011-12-04T23:58:56","date_gmt":"2011-12-04T23:58:56","guid":{"rendered":"http:\/\/www.west-racing.com\/mf\/?page_id=655"},"modified":"2013-04-29T12:36:42","modified_gmt":"2013-04-29T12:36:42","slug":"curve-deform","status":"publish","type":"page","link":"http:\/\/www.west-racing.com\/mf\/?page_id=655","title":{"rendered":"Curve Deform"},"content":{"rendered":"<p><a href=\"http:\/\/www.west-racing.com\/mf\/wp-content\/uploads\/2011\/12\/megacrvex-Copy1.jpg\"><img loading=\"lazy\" src=\"http:\/\/www.west-racing.com\/mf\/wp-content\/uploads\/2011\/12\/megacrvex-Copy1-300x150.jpg\" alt=\"\" title=\"megacrvex - Copy\" width=\"300\" height=\"150\" class=\"alignright size-medium wp-image-1491 fancy\" srcset=\"http:\/\/www.west-racing.com\/mf\/wp-content\/uploads\/2011\/12\/megacrvex-Copy1-300x150.jpg 300w, http:\/\/www.west-racing.com\/mf\/wp-content\/uploads\/2011\/12\/megacrvex-Copy1-1024x514.jpg 1024w, http:\/\/www.west-racing.com\/mf\/wp-content\/uploads\/2011\/12\/megacrvex-Copy1-680x341.jpg 680w, http:\/\/www.west-racing.com\/mf\/wp-content\/uploads\/2011\/12\/megacrvex-Copy1.jpg 1905w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/a><br \/>\nCurve deform is like Path deform but instead uses a 2D curve to describe the deformation along a chosen axis, so for example a section of road could be made to have a hump in it, or a rope bridge could be made to sag. The system uses the Unity curve editor and you can add as many points as you like to get the shape you need, and you can also do a little bit of scripting if you wanted to animate the shape. Below is an example script that was used to control the shape of a rope bridge object depending on where about&#8217;s an object was on the bridge.<\/p>\n<p><a href=\"http:\/\/www.west-racing.com\/mf\/wp-content\/uploads\/2011\/12\/curvedefpar.jpg\"><img loading=\"lazy\" src=\"http:\/\/www.west-racing.com\/mf\/wp-content\/uploads\/2011\/12\/curvedefpar.jpg\" alt=\"\" title=\"Curve Deform Params\" width=\"257\" height=\"134\" class=\"alignright size-full wp-image-3864 fancy\" \/><\/a><\/p>\n<h3>Curve Deform Param Description<\/h3>\n<h4><strong>Axis<\/strong><\/h4>\n<p>Which axis of the mesh will the curve run along, currently the curve deformation affects the Y Axis so you should only pick X or Z here, any you can use the Gizmo values to reposition the effect or rotate it.<\/p>\n<h4><strong>Curve<\/strong><\/h4>\n<p><a href=\"http:\/\/www.west-racing.com\/mf\/wp-content\/uploads\/2011\/12\/megacurvedeformcrv.jpg\"><img loading=\"lazy\" src=\"http:\/\/www.west-racing.com\/mf\/wp-content\/uploads\/2011\/12\/megacurvedeformcrv.jpg\" alt=\"\" title=\"megacurvedeformcrv\" width=\"372\" height=\"111\" class=\"alignleft size-full wp-image-691 fancy\" srcset=\"http:\/\/www.west-racing.com\/mf\/wp-content\/uploads\/2011\/12\/megacurvedeformcrv.jpg 372w, http:\/\/www.west-racing.com\/mf\/wp-content\/uploads\/2011\/12\/megacurvedeformcrv-300x89.jpg 300w\" sizes=\"(max-width: 372px) 100vw, 372px\" \/><\/a><br \/>\nThe curve used to deform the mesh, click the curve to open the curve editor window where you can drag the control points, add and remove points or change tangents.<\/p>\n<h4><strong>Max Deviation<\/strong><\/h4>\n<p>Can be used to multiply the effect of the curve value to the deformation<\/p>\n<h4><strong>Use Pos<\/strong><\/h4>\n<p>Allow the Pos offset value to be used<\/p>\n<h4><strong>Pos<\/strong><\/h4>\n<p>An offset to add to the curve lookup position so you can easily move the effect along the mesh.<br \/>\n[cc lang=&#8221;c#&#8221;]public class MegaCurveDeform : MegaModifier<br \/>\n{<br \/>\n\tpublic MegaAxis        axis;<br \/>\n\tpublic AnimationCurve  defCurve;<br \/>\n\tpublic float           MaxDeviation;<br \/>\n    public bool            UsePos;<br \/>\n    public float           Pos;<br \/>\n}[\/cc]<\/p>\n<h3>Video showing Curve Deform<\/h3>\n<p><a href=\"http:\/\/www.youtube.com\/watch?v=VQWK1PtFGh0&#038;hd=1&#038;fs=1&#038;autoplay=1\" class=\"fancybox-youtube\"><img loading=\"lazy\" src=\"http:\/\/www.west-racing.com\/mf\/wp-content\/uploads\/2011\/12\/ytcurvedef.jpg\" alt=\"\" title=\"ytcurvedef\" width=\"680\" height=\"384\" class=\"aligncenter size-full wp-image-1479 fancy\" srcset=\"http:\/\/www.west-racing.com\/mf\/wp-content\/uploads\/2011\/12\/ytcurvedef.jpg 680w, http:\/\/www.west-racing.com\/mf\/wp-content\/uploads\/2011\/12\/ytcurvedef-300x169.jpg 300w\" sizes=\"(max-width: 680px) 100vw, 680px\" \/><\/a><\/p>\n<h3>Bridge Example<\/h3>\n<p>In this example the bridge value is the GameObject with the modifier on, offset is a vertical adjustment for the object to get the feet to line up if needed and smooth controls how the tangents are calculated for the two end points. The curve should have the default 3 points for this to work correctly.<br \/>\n[cc lang=&#8221;c#&#8221;]using UnityEngine;<\/p>\n<p>[ExecuteInEditMode]<br \/>\npublic class WalkBridge : MonoBehaviour<br \/>\n{<br \/>\n\tpublic GameObject\t\tbridge;<br \/>\n\t[HideInInspector]<br \/>\n\tpublic MegaCurveDeform\tmod;<br \/>\n\tpublic float offset = 0.0f;\t\/\/ Character offset<br \/>\n\tpublic float smooth = 0.0f;<\/p>\n<p>\tvoid Update()<br \/>\n\t{<br \/>\n\t\tif ( bridge )<br \/>\n\t\t{<br \/>\n\t\t\t\/\/ Get the bridge modifier<br \/>\n\t\t\tif ( mod == null )<br \/>\n\t\t\t\tmod = bridge.GetComponent<MegaCurveDeform>();<\/p>\n<p>\t\t\tif ( mod )<br \/>\n\t\t\t{<br \/>\n\t\t\t\tint ax = (int)mod.axis;<br \/>\n\t\t\t\tVector3 pos = transform.position;<\/p>\n<p>\t\t\t\t\/\/ Get into local space<br \/>\n\t\t\t\tVector3 lpos = mod.transform.worldToLocalMatrix.MultiplyPoint(pos);<\/p>\n<p>\t\t\t\t\/\/ Are we on the bridge<br \/>\n\t\t\t\tif ( lpos.x > mod.bbox.min.x &#038;&#038; lpos.x < mod.bbox.max.x &#038;&#038; lpos.z > mod.bbox.min.z &#038;&#038; lpos.z < mod.bbox.max.z )\n\t\t\t\t{\n\t\t\t\t\t\/\/ How far across are we\n\t\t\t\t\tfloat alpha = (lpos[ax] - mod.bbox.min[ax]) \/ (mod.bbox.max[ax] - mod.bbox.min[ax]);\n\n\t\t\t\t\t\/\/ Deform the bridge\n\t\t\t\t\tSetPos(mod, alpha);\n\t\t\t\t\t\/\/ Place object on deformed bridge\n\t\t\t\t\tlpos.y = mod.GetPos(alpha) + (offset * 0.01f);\n\n\t\t\t\t\ttransform.position = bridge.transform.localToWorldMatrix.MultiplyPoint(lpos);\n\t\t\t\t}\n\t\t\t\telse\n\t\t\t\t\tSetPos(mod, 0.0f);\n\t\t\t}\n\t\t}\n\t}\n\n\tprivate float easeInOutSine(float start, float end, float value)\n\t{\n\t\tend -= start;\n\t\treturn -end \/ 2.0f * (Mathf.Cos(Mathf.PI * value \/ 1.0f) - 1.0f) + start;\n\t}\n\n\t\/\/ Change the keys\n\tpublic void SetPos(MegaCurveDeform mod, float alpha)\n\t{\n\t\tfloat val = 0.0f;\n\n\t\tif ( alpha < 0.5f )\n\t\t\tval = easeInOutSine(0.0f, -mod.MaxDeviation * 0.01f, alpha \/ 0.5f);\n\t\telse\n\t\t\tval = easeInOutSine(-mod.MaxDeviation * 0.01f, 0.0f, (alpha - 0.5f) \/ 0.5f);\n\n\t\tmod.SetKey(0, 0.0f, 0.0f, Mathf.Tan(0.0f), Mathf.Tan(Mathf.Atan2(val, alpha)));\n\n\t\tfloat inTangent = Mathf.Lerp(Mathf.Tan(0.0f), Mathf.Tan(Mathf.Atan2(val, alpha)), smooth);\n\t\tfloat outTangent = Mathf.Lerp(Mathf.Tan(Mathf.PI), Mathf.Tan(Mathf.Atan2(val, alpha - 1.0f)), smooth);\n\n\t\tmod.SetKey(1, Mathf.Clamp(alpha, 0.001f, 0.999f), val, inTangent, outTangent);\n\n\t\tmod.SetKey(2, 1.0f, 0.0f, Mathf.Tan(Mathf.Atan2(-val, 1.0f - alpha)), Mathf.Tan(Mathf.PI));\n\t}\n}[\/cc]\n<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Curve deform is like Path deform but instead uses a 2D curve to describe the deformation along a chosen axis, so for example a section of road could be made [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":38,"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\/655"}],"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=655"}],"version-history":[{"count":23,"href":"http:\/\/www.west-racing.com\/mf\/index.php?rest_route=\/wp\/v2\/pages\/655\/revisions"}],"predecessor-version":[{"id":3865,"href":"http:\/\/www.west-racing.com\/mf\/index.php?rest_route=\/wp\/v2\/pages\/655\/revisions\/3865"}],"up":[{"embeddable":true,"href":"http:\/\/www.west-racing.com\/mf\/index.php?rest_route=\/wp\/v2\/pages\/38"}],"wp:attachment":[{"href":"http:\/\/www.west-racing.com\/mf\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=655"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}