示例#1
0
    private void SetVertices()
    {
        int iDelta = pipeSegmentCount * 4;

        for (int u = 1, i = iDelta; u <= points.Count - 1; u++, i += iDelta)
        {
            SegmentPoints First  = points[u];
            SegmentPoints Second = points[u - 1];

            List <Vector3> dotsFirst        = First.SegmentPointsOutside;
            List <Vector3> dotsSecond       = Second.SegmentPointsOutside;
            List <Vector3> dotsFirstInside  = First.SegmentPointsInside;
            List <Vector3> dotsSecondInside = Second.SegmentPointsInside;

            Vector3 vertexA       = dotsFirst[dotsFirst.Count - 1];
            Vector3 vertexB       = dotsSecond[dotsSecond.Count - 1];
            Vector3 vertexAInside = dotsFirstInside[dotsFirstInside.Count - 1];
            Vector3 vertexBInside = dotsSecondInside[dotsSecondInside.Count - 1];

            // Caps
            if (u == 1)
            {
                for (int v = 0, t = 0; v < pipeSegmentCount; v++, t += 4)
                {
                    vertices[t]     = vertexB;
                    vertices[t + 1] = vertexB = dotsSecond[v];
                    vertices[t + 2] = vertexBInside;
                    vertices[t + 3] = vertexBInside = dotsSecondInside[v];
                }
            }

            if (u == points.Count - 1)
            {
                for (int v = 0, t = 0; v < pipeSegmentCount; v++, t += 4)
                {
                    var innerTubeStart = vertices.Length / 2 + t;
                    vertices[innerTubeStart]     = vertexAInside;
                    vertices[innerTubeStart + 1] = vertexAInside = dotsFirstInside[v];
                    vertices[innerTubeStart + 2] = vertexA;
                    vertices[innerTubeStart + 3] = vertexA = dotsFirst[v];
                }
            }

            // Pipe
            for (int v = 0, t = i; v < pipeSegmentCount; v++, t += 4)
            {
                vertices[t]     = vertexA;
                vertices[t + 1] = vertexA = dotsFirst[v];
                vertices[t + 2] = vertexB;
                vertices[t + 3] = vertexB = dotsSecond[v];

                var innerTubeStart = vertices.Length / 2 + t;
                vertices[innerTubeStart]     = vertexBInside;
                vertices[innerTubeStart + 1] = vertexBInside = dotsSecondInside[v];
                vertices[innerTubeStart + 2] = vertexAInside;
                vertices[innerTubeStart + 3] = vertexAInside = dotsFirstInside[v];
            }
        }
        mesh.vertices = vertices;
    }
    public SegmentPoints CalculatePoints(Transform _transform, int _segmentCount, float _radius, float _width)
    {
        List <Vector3> outside = new List <Vector3>();
        List <Vector3> inside  = new List <Vector3>();

        float      vStep    = (2f * Mathf.PI) / _segmentCount;
        Quaternion rotation = Quaternion.Euler(_transform.rotation.x, _transform.rotation.y, _transform.rotation.z);

        for (int v = 0; v < _segmentCount; v++)
        {
            var pointOnCircle = GetPointOnTorus(0, v * vStep, _radius);
            var point         = rotation * _transform.TransformPoint(pointOnCircle);
            outside.Add(point);

            var pointOnCircleInside = GetPointOnTorus(0, v * vStep, _width);
            var pointInside         = rotation * _transform.TransformPoint(pointOnCircleInside);
            inside.Add(pointInside);
        }

        SegmentPoints points = new SegmentPoints(outside, inside);

        return(points);
    }