{"id":482,"date":"2013-01-06T03:41:20","date_gmt":"2013-01-06T02:41:20","guid":{"rendered":"http:\/\/piflik.de\/?p=482"},"modified":"2013-01-06T05:19:52","modified_gmt":"2013-01-06T04:19:52","slug":"procedural-splines-2","status":"publish","type":"post","link":"http:\/\/piflik.de\/?p=482","title":{"rendered":"Procedural Splines 2"},"content":{"rendered":"<p>After trying to wrap my head around procedural mesh generation in Unity for hours and failing repeatedly, I decided to use Unity&#8217;s Line Renderer instead of generating a real mesh. Besides being easier by orders of magnitude it also helps keeping the polycount down, while offering good visuals, although it seems to ignore most of my material&#8217;s properties. Not sure if I can get this under control. I kept the curves I calculated, and created an empty for each sub-Spline with a Line Renderer Component using the points calculated for the respective subspline.<\/p>\n<p>Here is the finished code. Sadly it doesn&#8217;t have the instant feedback in the Editor anymore, but I had to remove the ExecuteInEditMode. The objects created weren&#8217;t cleaned up properly when returning from PlayMode. If I find a way to have both, I will update the code.<\/p>\n<p><!--more--><\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\n\r\nusing UnityEngine;\r\nusing System.Collections;\r\n\r\npublic class Spliner : MonoBehaviour {\r\n\r\n   public GameObject next;\r\n   public float gravity;\r\n   public float radius;\r\n   public int steps;\r\n\r\n   public int subSplines;\r\n   public float frequency;\r\n   public float amplitude;\r\n\r\n   public Material material;\r\n\r\n   private Vector3 curPosition;\r\n   private Vector3 nextPosition;\r\n   private Vector3[] mainPointArr = new Vector3[0];\r\n\r\n   private Vector3[,] subPointArr = new Vector3[0,0];\r\n\r\n   private GameObject[] children;\r\n\r\n   void Start() {\r\n\r\n      if(next) {\r\n         nextPosition = next.transform.position;\r\n         curPosition = transform.position;\r\n\r\n         if(steps &lt; 1) steps= 1;\r\n\r\n         mainPointArr = new Vector3[steps + 1];\r\n\r\n         for(int i = 0; i &lt;= steps; i++) {\r\n            mainPointArr[i] = DrawCosH(i);\r\n         }\r\n\r\n         if(subSplines &lt; 1) subSplines = 1;\r\n\r\n         subPointArr = new Vector3[subSplines, steps];\r\n\r\n         for(int i = 0; i &lt; subSplines; i++) {\r\n            DrawSubSpline(i);\r\n         }\r\n\r\n         children = new GameObject[subSplines];\r\n\r\n         for(int i = 0; i &lt; subSplines; i++) {\r\n            children[i] = new GameObject(&quot;Child0&quot; + i);\r\n            children[i].transform.parent = transform;\r\n            LineRenderer temp = children[i].AddComponent&lt;LineRenderer&gt;();\r\n            temp.SetVertexCount(steps);\r\n            temp.SetWidth(radius, radius);\r\n            temp.material = material;\r\n            for(int j = 0; j &lt; steps; j++) {\r\n               temp.SetPosition(j, subPointArr[i, j]);\r\n            }\r\n         }\r\n      }\r\n   }\r\n\r\n   public Vector3 DrawCosH(int i) {\r\n      Vector3 direction = nextPosition - curPosition;\r\n      int locStep = steps - 1;\r\n\r\n      return curPosition + direction\/locStep * i + new Vector3(0, gravity * (CosH((1.0f * i \/ locStep) - 0.5f) - CosH(0.5f)), 0);\r\n   }\r\n\r\n   float CosH(float t) {\r\n      return (Mathf.Exp(t) + Mathf.Exp(-t))\/2;\r\n   }\r\n\r\n   void DrawSubSpline(int i) {\r\n      float locAmp = amplitude * Random.Range(0.5f, 1.2f) \/ 10;\r\n      float locFreq = frequency * Random.Range(0.8f, 1.2f);\r\n      float offset = Random.Range(0.0f, 1.0f);\r\n      int cw = (Random.Range(0.0f, 1.0f) &gt; 0.5 ? -1 : 1);\r\n\r\n      for(int j = 0; j &lt; steps; j++) {\r\n         Vector3 direction = mainPointArr[j + 1] - mainPointArr[j];\r\n         Quaternion rot = Quaternion.LookRotation(direction);\r\n\r\n         subPointArr[i, j] = mainPointArr[j] + rot * (Vector3.up * Mathf.Sin(1.0f * j * locFreq \/ steps + offset) + Vector3.right * cw * Mathf.Cos(1.0f * j * locFreq \/ steps + offset)) * locAmp;\r\n      }\r\n   }\r\n}\r\n\r\n<\/pre>\n<p>Anyway, here is a picture of how it looks like right now.<\/p>\n<p><a rel=\"attachment wp-att-485\" href=\"http:\/\/piflik.de\/?attachment_id=485\"><img loading=\"lazy\" class=\"aligncenter size-full wp-image-485\" title=\"BlobScreen07\" src=\"http:\/\/piflik.de\/wp-content\/uploads\/2013\/01\/BlobScreen07.jpg\" alt=\"\" width=\"947\" height=\"709\" srcset=\"http:\/\/piflik.de\/wp-content\/uploads\/2013\/01\/BlobScreen07.jpg 947w, http:\/\/piflik.de\/wp-content\/uploads\/2013\/01\/BlobScreen07-300x224.jpg 300w\" sizes=\"(max-width: 947px) 100vw, 947px\" \/><\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>After trying to wrap my head around procedural mesh generation in Unity for hours and failing repeatedly, I decided to use Unity&#8217;s Line Renderer instead of generating a real mesh. Besides being easier by orders of magnitude it also helps keeping the polycount down, while offering good visuals, although it seems to ignore most of [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[7,13,12],"tags":[],"_links":{"self":[{"href":"http:\/\/piflik.de\/index.php?rest_route=\/wp\/v2\/posts\/482"}],"collection":[{"href":"http:\/\/piflik.de\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/piflik.de\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/piflik.de\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/piflik.de\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=482"}],"version-history":[{"count":7,"href":"http:\/\/piflik.de\/index.php?rest_route=\/wp\/v2\/posts\/482\/revisions"}],"predecessor-version":[{"id":490,"href":"http:\/\/piflik.de\/index.php?rest_route=\/wp\/v2\/posts\/482\/revisions\/490"}],"wp:attachment":[{"href":"http:\/\/piflik.de\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=482"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/piflik.de\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=482"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/piflik.de\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=482"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}