mvaganov

LineLib, code to help me with drawing lines, and math

Jun 1st, 2013
994
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4.  
  5. public class LineLib : MonoBehaviour
  6. {
  7.     static LineLib instance = null;
  8.     public float widthStart = 0.1f;
  9.     public float widthFinish = 0.1f;
  10.     public ShaderOptionEnum shader = ShaderOptionEnum.SelfIlluminDiffuse;
  11.     public Color color = Color.red;
  12.     public int numPointsPerCircle = 24;
  13.  
  14.     private float originalStart, originalFinish;
  15.     private ShaderOptionEnum originalShader;
  16.     private Color originalColor;
  17.     private int originalPointsPerCircle;
  18.  
  19.     private void RememberOriginals()
  20.     {
  21.         originalStart = widthStart;
  22.         originalFinish = widthFinish;
  23.         originalShader = shader;
  24.         originalColor = color;
  25.         originalPointsPerCircle = numPointsPerCircle;
  26.     }
  27.     public void ResetSettings()
  28.     {
  29.         widthStart = originalStart;
  30.         widthFinish = originalFinish;
  31.         shader = originalShader;
  32.         color = originalColor;
  33.         numPointsPerCircle = originalPointsPerCircle;
  34.     }
  35.     static public void Reset()
  36.     {
  37.         FindGlobal().ResetSettings();
  38.     }
  39.  
  40.     string[] shaders = { "Self-Illumin/Diffuse", "Particles/Additive" };//, "VertexLit", "Diffuse"};   
  41.     public enum ShaderOptionEnum { SelfIlluminDiffuse = 0, ParticlesAdditive };
  42.     //public class LineData{Vector3[] line; float s, f; ShaderOptionEnum shader; Color c1, c2;}
  43.  
  44.     public void SetWidth_(float start, float finish)
  45.     {
  46.         widthStart = start;
  47.         widthFinish = finish;
  48.     }
  49.     public float GetWidthStart_() { return widthStart; }
  50.     public float GetWidthFinish_() { return widthFinish; }
  51.  
  52.     public void SetShaderOption_(ShaderOptionEnum shader)
  53.     {
  54.         this.shader = shader;
  55.     }
  56.  
  57.     public void SetColor_(Color color) { this.color = color; }
  58.     public Color GetColor_() { return this.color; }
  59.  
  60.     public static void SetShaderOption(ShaderOptionEnum shader) { FindGlobal().SetShaderOption_(shader); }
  61.     public static Color GetColor() { return FindGlobal().GetColor_(); }
  62.     public static void SetColor(Color color) { FindGlobal().SetColor_(color); }
  63.     public static void SetWidth(float widthStart, float widthFinish)
  64.     { FindGlobal().SetWidth_(widthStart, widthFinish); }
  65.     public static void SetWidth(float width) { FindGlobal().SetWidth_(width, width); }
  66.     public static float GetWidthStart() { return FindGlobal().GetWidthStart_(); }
  67.     public static float GetWidthFinish() { return FindGlobal().GetWidthFinish_(); }
  68.  
  69.     public static LineRenderer CreateLineRender(Vector3 a, Vector3 b)
  70.     {
  71.         LineRenderer lr = CreateLineRendererGameObject();
  72.         SetupLineRender(a, b, lr);
  73.         return lr;
  74.     }
  75.     public static LineRenderer CreateLineRender(Vector3[] line)
  76.     {
  77.         LineRenderer lr = CreateLineRendererGameObject();
  78.         SetupLineRender(line, lr);
  79.         return lr;
  80.     }
  81.     public static LineRenderer CreateCircleRender(Vector3 center, float radius, Vector3 normal)
  82.     {
  83.         LineRenderer lr = CreateLineRendererGameObject();
  84.         SetupCircleRender(center, radius, normal, 360, 0, instance.numPointsPerCircle, lr);
  85.         return lr;
  86.     }
  87.     public static LineRenderer CreateArcRender(Vector3 center, float radius, float start, float end, int segmentsPerCircle, Vector3 normal)
  88.     {
  89.         LineRenderer lr = CreateLineRendererGameObject();
  90.         SetupCircleRender(center, radius, normal, start, end, segmentsPerCircle, lr);
  91.         return lr;
  92.     }
  93.     public static LineRenderer CreateSpiralRender(Vector3 center, int size, Vector3 normal)
  94.     {
  95.         LineRenderer lr = CreateLineRendererGameObject();
  96.         SetupSpiralRender(center, size, normal, instance.numPointsPerCircle, lr);
  97.         return lr;
  98.     }
  99.     public static LineRenderer CreateQuaternionRender(Vector3 p, float radius, Quaternion q)
  100.     {
  101.         LineRenderer lr = CreateLineRendererGameObject();
  102.         SetupQuaternionRender(p, radius, q, instance.numPointsPerCircle, lr);
  103.         return lr;
  104.     }
  105.  
  106.     static public LineRenderer CreateLineRendererGameObject()
  107.     {
  108.         return FindGlobal().CreateLineRendererGameObjectInternal();
  109.     }
  110.     // MonoBehavior upkeep stuff, including test code -----------------------------
  111.     void Start()
  112.     {
  113.         if (instance == null)
  114.         {
  115.             instance = this;
  116.             instance.RememberOriginals();
  117.         }
  118. // TEST CODE
  119.         //LineRenderer g = LineLib.CreateLineRender(new Vector3(-1, -1, -2), new Vector3(5, 1, -2));
  120.         //Destroy(g.gameObject, 3);
  121.         //Vector3 v = new Vector3(1, 1, 1);
  122.         //v.Normalize();
  123.         //LineLib.CreateSpiralRender(new Vector3(-2, 2, -1), 2, v);
  124.         //Vector3 p = new Vector3(2, -2, -1), n = new Vector3(-1, 1, 1).normalized;
  125.         //for (int i = 0; i < 5; ++i)
  126.         //{
  127.         //    CreateArcRender(p, 1.0f + (0.4f * i), 360 - (i * 15), 0 + (i * 30), 24, n);
  128.         //}
  129.         //LineLib.SetColor(Color.white);
  130.         //Quaternion q = transform.rotation;
  131.         //CreateQuaternionRender(new Vector3(0, -5, 0), .5f, q);
  132.         //LineLib.SetColor(Color.blue);
  133.     }
  134.     void LateUpdate()
  135.     {
  136.         ResetSettings();
  137.     }
  138.  
  139.     // THE MEATY CODE -------------------------------------------------------------
  140.     /// <summary>if no LineLib exists in the scene, one is created</summary>
  141.     static LineLib FindGlobal()
  142.     {
  143.         if (instance == null)
  144.         {
  145.             Object[] objects = Resources.FindObjectsOfTypeAll(typeof(GameObject));
  146.             for (int i = 0; instance == null && i < objects.Length; ++i)
  147.             {
  148.                 if (objects[i] is GameObject)
  149.                 {
  150.                     GameObject go = (GameObject)objects[i];
  151.                     instance = go.GetComponent<LineLib>();
  152.                     //if (instance != null)print("found it!");
  153.                 }
  154.             }
  155.             if (instance == null)
  156.             {
  157.                 GameObject linelib = new GameObject("LineLib (auto generated)");
  158.                 instance = linelib.AddComponent<LineLib>();
  159.                 //print("made it myself!");
  160.             }
  161.             instance.RememberOriginals();
  162.         }
  163.         return instance;
  164.     }
  165.     static public LineRenderer CreateRenderer(Shader shaderObject, Color color, float widthStart, float widthFinish)
  166.     {
  167.         GameObject go = new GameObject("line");
  168.         LineRenderer lineRenderer = go.AddComponent<LineRenderer>();
  169.         lineRenderer.material = new Material(shaderObject);
  170.         lineRenderer.material.color = color;
  171.         lineRenderer.castShadows = false;
  172.         lineRenderer.receiveShadows = false;
  173.         lineRenderer.SetColors(color, color);
  174.         lineRenderer.SetWidth(widthStart, widthFinish);
  175.         return lineRenderer;
  176.     }
  177.     public LineRenderer CreateLineRendererGameObjectInternal()
  178.     {
  179.         string shaderName = shaders[(int)shader];
  180.         UnityEngine.Shader shaderObject = Shader.Find(shaderName);
  181.         LineRenderer lineRenderer = CreateRenderer(shaderObject, color, widthStart, widthFinish);
  182.         lineRenderer.transform.parent = transform;
  183.         return lineRenderer;
  184.     }
  185.     static public void SetupLineRender(Vector3 a, Vector3 b, LineRenderer liner)
  186.     {
  187.         liner.SetVertexCount(2);
  188.         liner.SetPosition(0, a);
  189.         liner.SetPosition(1, b);
  190.     }
  191.     static public LineRenderer SetupLineRender(Vector3[] line, LineRenderer liner)
  192.     {
  193.         liner.SetVertexCount(line.Length);
  194.         for (int i = 0; i < line.Length; ++i)
  195.             liner.SetPosition(i, line[i]);
  196.         return liner;
  197.     }
  198.     static public void SetupCircleRender(Vector3 p, float r, Vector3 normal, float start, float end, int pointsPerCircle, LineRenderer liner)
  199.     {
  200.         Vector3[] circle = CreateArc(p, r, start, end, pointsPerCircle, normal);
  201.         SetupLineRender(circle, liner);
  202.     }
  203.     /// <summary>creates an Arc with the given details. TODO refactor/optimize?</summary>
  204.     /// <param name="start">angle start</param>
  205.     /// <param name="end">angle end</param>
  206.     /// <param name="pointsPerCircle">how many even segments make a circle</param>
  207.     /// <returns>vertices that create the described arc</returns>
  208.     static public Vector3[] CreateArc(float start, float end, int pointsPerCircle)
  209.     {
  210.         return CreateArc(Vector3.zero, 1, start, end, pointsPerCircle, Vector3.forward);
  211.     }
  212.     /// <summary>creates an Arc with the given details. TODO refactor/optimize?</summary>
  213.     /// <param name="center">focus of the arc, center if a circle</param>
  214.     /// <param name="radius">distance vertices are from center</param>
  215.     /// <param name="start">angle start</param>
  216.     /// <param name="end">angle end</param>
  217.     /// <param name="pointsPerCircle">how many even segments make a circle</param>
  218.     /// <param name="normal">where 'up' is. Circle will be clockwise from here</param>
  219.     /// <returns>vertices that create the described arc</returns>
  220.     static public Vector3[] CreateArc(Vector3 center, float radius, float start, float end, int pointsPerCircle, Vector3 normal)
  221.     {
  222.         bool reverse = end < start;
  223.         if (reverse)
  224.         {
  225.             float temp = start;
  226.             start = end;
  227.             end = temp;
  228.         }
  229.         while (start < 360) { start += 360; end += 360; }
  230.         while (start > 360) { start -= 360; end -= 360; }
  231.         if (normal == Vector3.zero)
  232.             normal = Vector3.forward;
  233.         Vector3[] lineStrip;
  234.         Quaternion q = Quaternion.LookRotation(normal);
  235.         Vector3 right = GetUpVector(q);
  236.         Vector3 r = right * radius;
  237.         if (end == start)
  238.         {
  239.             lineStrip = new Vector3[1];
  240.             q = Quaternion.AngleAxis(start, normal);
  241.             lineStrip[0] = center + q * r;
  242.             return lineStrip;
  243.         }
  244.         float degreesPerSegment = 360f / pointsPerCircle;
  245.  
  246.         float startIndexF = start * pointsPerCircle / 360f;//start / degreesPerSegment;
  247.         int startIndex = (int)startIndexF;
  248.         float startRemainder = startIndexF - startIndex;
  249.         startIndex = startIndex + ((startRemainder > 0) ? 1 : ((startRemainder < 0) ? -1 : 0));
  250.         float endIndexF = end * pointsPerCircle / 360f;// end / degreesPerSegment;
  251.         int endIndex = (int)endIndexF;
  252.         float endRemainder = endIndexF - endIndex;
  253.  
  254.         int inBetweenSegments = (endIndex >= startIndex) ? (endIndex - startIndex + 1) : 0;
  255.         int numPoints = inBetweenSegments + ((startRemainder != 0) ? 1 : 0) + ((endRemainder != 0) ? 1 : 0);
  256.         // allocate the required memory
  257.         lineStrip = new Vector3[numPoints];
  258.         // fill the memory with the points of the arc
  259.         int index = reverse ? (lineStrip.Length - 1) : 0;
  260.         if (startRemainder != 0)
  261.         {
  262.             q = Quaternion.AngleAxis((startIndex - 1) * degreesPerSegment, normal);
  263.             Vector3 preStart = center + q * r;
  264.             q = Quaternion.AngleAxis((startIndex) * degreesPerSegment, normal);
  265.             Vector3 actualStart = center + q * r;
  266.             lineStrip[index] = Vector3.Lerp(preStart, actualStart, startRemainder);
  267.             index += reverse ? -1 : 1;
  268.         }
  269.         for (int v = startIndex; v <= endIndex; ++v)
  270.         {
  271.             q = Quaternion.AngleAxis(v * degreesPerSegment, normal);
  272.             lineStrip[index] = center + q * r;
  273.             index += reverse ? -1 : 1;
  274.         }
  275.         if (endRemainder != 0)
  276.         {
  277.             q = Quaternion.AngleAxis((endIndex + 1) * degreesPerSegment, normal);
  278.             Vector3 postEnd = center + q * r;
  279.             q = Quaternion.AngleAxis((endIndex) * degreesPerSegment, normal);
  280.             Vector3 actualEnd = center + q * r;
  281.             lineStrip[index] = Vector3.Lerp(actualEnd, postEnd, endRemainder);
  282.             index += reverse ? -1 : 1;
  283.         }
  284.         return lineStrip;
  285.     }
  286.     static public Vector3[] CreateAngle(Vector3 center, float radius, float start, float end, Vector3 normal)
  287.     {
  288.         Quaternion q = Quaternion.LookRotation(normal);
  289.         Vector3 right = GetUpVector(q);
  290.         Vector3 r = right * radius;
  291.         Vector3[] lineStrip = new Vector3[3];
  292.         q = Quaternion.AngleAxis(start, normal);
  293.         lineStrip[0] = center + q * r;
  294.         lineStrip[1] = center;
  295.         q = Quaternion.AngleAxis(end, normal);
  296.         lineStrip[2] = center + q * r;
  297.         return lineStrip;
  298.     }
  299.     static public void SetupQuaternionRender(Vector3 p, float r, Quaternion quaternion, int pointsPerCircle, LineRenderer liner)
  300.     {
  301.         Vector3 axis; float angle;
  302.         quaternion.ToAngleAxis(out angle, out axis);
  303.         Vector3 r2 = axis * r * 2;
  304.         liner = CreateLineRender(p - r2, p + r2);
  305.         LineRenderer curve = CreateArcRender(p, r, 0, angle, pointsPerCircle, axis);
  306.         curve.transform.parent = liner.transform;
  307.     }
  308.     static public void SetupSpiralRender(Vector3 p, int size, Vector3 normal, int pointsPerCircle, LineRenderer liner)
  309.     {
  310.         int numPoints = size * pointsPerCircle;
  311.         Vector3 right = GetRightVector(Quaternion.LookRotation(normal));
  312.         liner.SetVertexCount(numPoints);
  313.         float rad = 0;
  314.         for (int i = 0; i < numPoints; ++i)
  315.         {
  316.             Quaternion q = Quaternion.AngleAxis(i * 360.0f / pointsPerCircle, normal);
  317.             Vector3 delta = q * right * rad;
  318.             liner.SetPosition(i, p + (delta * 1));
  319.             rad += 2f / pointsPerCircle;
  320.         }
  321.     }
  322.     public static Vector3 GetForwardVector(Quaternion q)
  323.     {
  324.         return new Vector3(2 * (q.x * q.z + q.w * q.y),
  325.                            2 * (q.y * q.z - q.w * q.x),
  326.                            1-2*(q.x * q.x + q.y * q.y));
  327.     }
  328.     public static Vector3 GetUpVector(Quaternion q)
  329.     {
  330.         return new Vector3(2 * (q.x * q.y - q.w * q.z),
  331.                            1-2*(q.x * q.x + q.z * q.z),
  332.                            2 * (q.y * q.z + q.w * q.x));
  333.     }
  334.     public static Vector3 GetRightVector(Quaternion q)
  335.     {
  336.         return new Vector3(1-2*(q.y * q.y + q.z * q.z),
  337.                            2 * (q.x * q.y + q.w * q.z),
  338.                            2 * (q.x * q.z - q.w * q.y));
  339.     }
  340.  
  341.     /// <summary>
  342.     /// </summary>
  343.     /// <param name="a_vertices"></param>
  344.     /// <param name="a_newSize"></param>
  345.     /// <returns></returns>
  346.  
  347.     public static Vector3[] InsertVertices(Vector3[] a_vertices, int a_numNewPoints)
  348.     {
  349.         if (a_numNewPoints <= 0)
  350.         {
  351.             return a_vertices;
  352.         }
  353.         /*
  354.         +           +           +           +           +   old
  355.         0-----------1-----------2-----------3-----------4
  356.         oldSegmentCount = 5
  357.         addableSegments = 4
  358.  
  359.         newSize = 20
  360.  
  361.         + .  . L  . +  .  .  .  + .  M .  . + .  R .  . +   new
  362.         0-1--3-4--5-6--7--8--9--A-B--C-D--E-F-G--H-I--J-K
  363.         newSegmentCount = 20
  364.         numNewSegments = 15
  365.         numMidSegmentsPerSegment = 3 (remainderSegments 3)
  366.         addToMiddle = 1
  367.         addToBeg = 1
  368.         addToEnd = 1
  369.         */
  370.         int oldSegmentCount = a_vertices.Length;
  371.         // how many old segments can have new points inserted after them (+ excluding the last one)
  372.         int addableSegments = (oldSegmentCount - 1);
  373.         // how many pairs total the new model will have (+,L,R,M,.)
  374.         int newSegmentCount = oldSegmentCount + a_numNewPoints;
  375.         // how many of those pairs need to be calculated (L,R,M,.)
  376.         int numNewSegments = newSegmentCount - oldSegmentCount;
  377.         // how many regular segments to add between existing segments (.)
  378.         int numMidSegmentsPerSegment = numNewSegments / (addableSegments);
  379.         // how many segments are left to add (L,R,M)
  380.         int remainderSegments = numNewSegments - numMidSegmentsPerSegment * addableSegments;//numNewSegments % addableSegments;
  381.         // how many segments to add to the middle (M)
  382.         int addToMiddle = remainderSegments % 2;
  383.         // how many segments to add to the beginning (L)
  384.         int addToBeg = remainderSegments / 2;
  385.         // how many segments to add to the end (R)
  386.         int addToEnd = remainderSegments / 2;
  387.  
  388.         int middleGroup = addableSegments / 2;
  389.         int additionalAdded = 0;
  390.  
  391.         //print("+new " + numNewSegments +
  392.         //    "    +put " + addableSegments +
  393.         //    "    +avg " + numMidSegmentsPerSegment +
  394.         //    "    +rem " + remainderSegments +
  395.         //    "    +beg " + addToBeg +
  396.         //    "    +mid " + addToMiddle +
  397.         //    "    +end " + addToEnd);
  398.  
  399.         //for (int i = 0; i < a_vertices.Length; ++i)
  400.         //{
  401.         //    print("->"+a_vertices[i]);
  402.         //}
  403.         //print("----------------");
  404.         Vector3[] a_list = new Vector3[newSegmentCount];
  405.         int cursor = 0;
  406.         // start adding elements into the list. wait, zero element first.
  407.         // ok, no start adding element to the list.
  408.         for (int seg = 0; seg < addableSegments; ++seg)
  409.         {
  410.             // calculate how many extra segments to add here
  411.             int additionalSeg = 0;
  412.             additionalSeg += (seg >= addableSegments - addToEnd) ? 1 : 0;
  413.             //if ((seg >= oldSegmentCount - addToEnd))print("add to end");
  414.             additionalSeg += (seg < addToBeg) ? 1 : 0;
  415.             //if (seg < addToBeg)print("add to beg");
  416.             additionalSeg += (addToMiddle > 0 && seg == middleGroup) ? 1 : 0;
  417.             //if (addToMiddle > 0 && seg == middleGroup)print("add to mid");
  418.             if (additionalSeg != 0) additionalAdded++;
  419.             int numPointsToInsertHere = 1 + numMidSegmentsPerSegment + additionalSeg;
  420.             //print(numPointsToInsertHere);
  421.             Vector3 segStart = a_vertices[seg];
  422.             Vector3 segEnd = a_vertices[seg + 1];
  423.  
  424.             for (int i = 0; i < numPointsToInsertHere; ++i)
  425.             {
  426.                 a_list[cursor] = Vector3.Lerp(segStart, segEnd, (float)i / numPointsToInsertHere);
  427.                 //if (i == 0) print("->" + a_list[cursor]);else print("   " + a_list[cursor]);
  428.                 cursor++;
  429.             }
  430.         }
  431.         a_list[cursor] = a_vertices[a_vertices.Length - 1];
  432.         //print("->" + a_list[cursor]);
  433.         // final check
  434.         if (additionalAdded != remainderSegments) print("ADDED A DIFFERENT AMOUNT! MATH WRONG! PLZ CHECK! " + additionalAdded + " vs " + remainderSegments);
  435.         return a_list;
  436.     }
  437.  
  438.     /// <example>CreateSpiralSphere(transform.position, 0.5f, transform.up, transform.forward, 16, 8);</example>
  439.     /// <summary>
  440.     /// creates a line spiraled onto a sphere
  441.     /// </summary>
  442.     /// <param name="center"></param>
  443.     /// <param name="radius"></param>
  444.     /// <param name="axis"></param>
  445.     /// <param name="axisFace"></param>
  446.     /// <param name="sides"></param>
  447.     /// <param name="rotations"></param>
  448.     /// <returns></returns>
  449.     public static Vector3[] CreateSpiralSphere(Vector3 center, float radius, Vector3 axis, Vector3 axisFace,
  450.         float sides, float rotations)
  451.     {
  452.         List<Vector3> points = new List<Vector3>();
  453.         if (sides != 0 && rotations != 0)
  454.         {
  455.             float iter = 0;
  456.             float increment = 1f / (rotations * sides);
  457.             points.Add(center + axis * radius);
  458.             do
  459.             {
  460.                 iter += increment;
  461.                 Quaternion faceTurn = Quaternion.AngleAxis(iter * 360 * rotations, axis);
  462.                 Vector3 newFace = faceTurn * axisFace;
  463.                 Quaternion q = Quaternion.LookRotation(newFace);
  464.                 Vector3 right = LineLib.GetUpVector(q);
  465.                 Vector3 r = right * radius;
  466.                 q = Quaternion.AngleAxis(iter * 180, newFace);
  467.                 Vector3 newPoint = center + q * r;
  468.                 points.Add(newPoint);
  469.             }
  470.             while (iter < 1);
  471.         }
  472.         return points.ToArray();
  473.     }
  474.  
  475.     public static Vector3[] CreateSpiralSphereTriangleStrip(
  476.         Vector3 center, float radius, Vector3 axis, Vector3 axisFace,
  477.         float sides, float rotations, bool twoPeels)
  478.     {
  479.         List<Vector3> both = new List<Vector3>();
  480.         if (sides != 0 && rotations != 0)
  481.         {
  482.             Vector3[] swirl0, swirl1;
  483.             swirl0 = CreateSpiralSphere(center, radius, axis, axisFace, sides, rotations);
  484.             swirl1 = CreateSpiralSphere(center, radius, axis, -axisFace, sides, rotations);
  485.             List<Vector3> insides = new List<Vector3>();
  486.             List<Vector3> insides2 = new List<Vector3>();
  487.             int offset = (int)(sides / 2);
  488.             Vector3 v0, v1;
  489.             if (twoPeels)
  490.             {
  491.                 for (int i = -offset; i < swirl0.Length; ++i)
  492.                 {
  493.                     int s0 = i;
  494.                     int s1 = i + offset;
  495.                     v0 = s0 >= 0 ? swirl0[s0] : swirl1[-s0];
  496.                     v1 = s1 < swirl1.Length ? swirl1[s1] : swirl0[swirl0.Length - (s1 - swirl1.Length) - 1];
  497.                     insides.Add(v0);
  498.                     insides.Add(v1);
  499.                     v0 = s0 >= 0 ? swirl1[s0] : swirl0[-s0];
  500.                     v1 = s1 < swirl0.Length ? swirl0[s1] : swirl1[swirl1.Length - (s1 - swirl0.Length) - 1];
  501.                     insides2.Add(v0);
  502.                     insides2.Add(v1);
  503.                 }
  504.             }
  505.             else
  506.             {
  507.                 for (int i = -offset; i < swirl0.Length; ++i)
  508.                 {
  509.                     int s0 = (i >= 0) ? i : 0;
  510.                     int s1 = (i + offset < swirl1.Length) ? i + offset : swirl1.Length - 1;
  511.                     insides.Add(swirl0[s0]);
  512.                     insides.Add(swirl1[s1]);
  513.                     insides2.Add(swirl1[s0]);
  514.                     insides2.Add(swirl0[s1]);
  515.                 }
  516.             }
  517.             both.AddRange(insides);
  518.             insides2.Reverse();
  519.             both.AddRange(insides2);
  520.         }
  521.         return both.ToArray();
  522.     }
  523.  
  524.     static public int[] CreateTriangleMapForTriangleStrip(Vector3[] triStrip)
  525.     {
  526.         int[] triMap = new int[triStrip.Length * 3];
  527.         for (int i = 0; i < triStrip.Length - 3; i += 1)
  528.         {
  529.             triMap[i * 3 + 0] = i + 0;
  530.             triMap[i * 3 + 1] = i + 1;
  531.             triMap[i * 3 + 2] = i + 2;
  532.             i++;
  533.             triMap[i * 3 + 0] = i + 0;
  534.             triMap[i * 3 + 1] = i + 2;
  535.             triMap[i * 3 + 2] = i + 1;
  536.         }
  537.         return triMap;
  538.     }
  539. }
RAW Paste Data