Beispiel #1
0
        private void AddPointToLine(Vector3 point, Vector3 nextPoint, float R, int ANZ)
        {
            const float FULL = 2.0f * Mathf.PI;

            int count = linePoints.Length;
            int index = count;

            if (count > 0)
            {
                if (point == linePoints[count - 1])
                {
                    index = count - 1;
                }

                count = index;
            }
            count++;

            System.Array.Resize(ref linePoints, count);
            System.Array.Resize(ref lineRadius, count);

            System.Array.Resize(ref vertices, count * ANZ);
            System.Array.Resize(ref normals, count * ANZ);

            if (count > 1)
            {
                System.Array.Resize(ref triangles, (count - 1) * 6 * ANZ);
            }

            linePoints[index] = point;
            lineRadius[index] = R;
            float   R2    = R;
            float   R1    = 0;
            Vector3 prevP = point;

            if (index > 0)
            {
                prevP = linePoints[index - 1];
                R1    = lineRadius[index - 1];
            }

            Vector3 prevPrevP = prevP;

            if (index > 1)
            {
                prevPrevP = linePoints[index - 2];
            }

            // reorient current circle
            Vector3 dir        = new Vector3(1f, 0f, 0f);
            Vector3 firstPerp  = new Vector3();
            Vector3 secondPerp = new Vector3();

            Vector3 center;
            float   radius;

            if (index > 0)
            {
                dir        = (point - prevPrevP).normalized;
                firstPerp  = Maths.GetFirstPerpVector(dir);
                secondPerp = Vector3.Cross(dir, firstPerp).normalized;

                center = linePoints[index - 1];
                radius = R1;
                for (int j = 0; j < ANZ; j++)
                {
                    float   angle = FULL * (j / (float)ANZ);
                    Vector3 pos   = radius * (Mathf.Cos(angle) * firstPerp + Mathf.Sin(angle) * secondPerp);
                    vertices[(index - 1) * ANZ + j] = center + pos;
                    normals[(index - 1) * ANZ + j]  = pos.normalized;
                }

                // reorder circles by distance to avoid twists
                if (index > 1)
                {
                    Reorder(ref vertices, ref normals, index - 1, ANZ);
                }
            }

            dir        = (nextPoint - prevP).normalized;
            firstPerp  = Maths.GetFirstPerpVector(dir);
            secondPerp = Vector3.Cross(dir, firstPerp).normalized;

            center = linePoints[index];
            radius = R2;
            for (int j = 0; j < ANZ; j++)
            {
                float   angle = FULL * (j / (float)ANZ);
                Vector3 pos   = radius * (Mathf.Cos(angle) * firstPerp + Mathf.Sin(angle) * secondPerp);
                vertices[index * ANZ + j] = center + pos;
                normals[index * ANZ + j]  = pos.normalized;
            }

            // reorder circles by distance to avoid twists
            if (index > 0)
            {
                Reorder(ref vertices, ref normals, index, ANZ);

                int i = 0, quadIndex;
                int tIndex = 6 * ANZ * (index - 1);
                int vIndex = ANZ * (index - 1);
                for (quadIndex = 0; quadIndex < (ANZ - 1); quadIndex++)
                {
                    triangles[tIndex + i + 0] = vIndex + quadIndex + 0;
                    triangles[tIndex + i + 1] = vIndex + quadIndex + 1;
                    triangles[tIndex + i + 2] = vIndex + quadIndex + ANZ;


                    triangles[tIndex + i + 3] = vIndex + quadIndex + 1;
                    triangles[tIndex + i + 4] = vIndex + quadIndex + ANZ + 1;
                    triangles[tIndex + i + 5] = vIndex + quadIndex + ANZ;

                    // debug
                    i += 6;
                }

                triangles[tIndex + i + 0] = vIndex + ANZ - 1;
                triangles[tIndex + i + 1] = vIndex + 0;
                triangles[tIndex + i + 2] = vIndex + 2 * ANZ - 1;

                triangles[tIndex + i + 3] = vIndex + 0;
                triangles[tIndex + i + 4] = vIndex + ANZ;
                triangles[tIndex + i + 5] = vIndex + 2 * ANZ - 1;
            }
        }