示例#1
0
        public bool Intersect2D(SBSLine2D line, out SBSVector3 point)
        {
            float det = this.A * line.B - line.A * this.B;
            point = SBSVector3.zero;

            if (det == 0)
                return false;

            point.x = (line.B * this.C - this.B * line.C) / det;
            point.y = (this.A * line.C - line.A * this.C) / det;
            return true;
        }
示例#2
0
        public static void CreateLine(Vector3[] pointList, float width, Color color, bool isClosed, out Mesh lineMesh)
        {
            /*
             * A -------- C
             * B -------- D
            */

            lineMesh = new Mesh();
            lineMesh.name = "lineMesh";

            if (pointList.Length < 2)
                return;

            int vNum = pointList.Length * 2;
            int tNum = (pointList.Length - 1) * 2 * 3;
            int vIndex = 0;
            int tIndex = 0;
            Vector3[] vertices = new Vector3[vNum];
            Vector2[] uv = new Vector2[vNum];
            Vector3[] normals = new Vector3[vNum];
            Color[] colors = new Color[vNum];
            int[] triangles = new int[tNum];
            if (isClosed)
                triangles = new int[tNum + 2 * 3];

            for (int i = 0; i < pointList.Length - 1; i++)
            {
                Vector3 direction = (pointList[i + 1] - pointList[i]).normalized;
                Vector3 ortho = Vector3.Cross(direction, Vector3.forward);
                SBSVector3 A = pointList[i] - ortho * width * 0.5f;
                SBSVector3 B = pointList[i] + ortho * width * 0.5f;

                direction = (pointList[i] - pointList[i + 1]).normalized;
                ortho = Vector3.Cross(direction, Vector3.forward);
                SBSVector3 C = pointList[i + 1] + ortho * width * 0.5f;
                SBSVector3 D = pointList[i + 1] - ortho * width * 0.5f;

                if (i == 0)
                {
                    vertices[vIndex] = A;
                    vertices[vIndex + 1] = B;
                }
                else
                {
                    SBSLine2D line1 = new SBSLine2D(vertices[vIndex - 2], vertices[vIndex]);
                    SBSLine2D line2 = new SBSLine2D(A, C);
                    SBSVector3 intersection;
                    bool parallel = !line1.Intersect2D(line2, out intersection);
                    if (parallel)
                        vertices[vIndex] = A;
                    else
                        vertices[vIndex] = intersection;

                    line1 = new SBSLine2D(vertices[vIndex - 1], vertices[vIndex + 1]);
                    line2 = new SBSLine2D(B, D);
                    parallel = !line1.Intersect2D(line2, out intersection);
                    if (parallel)
                        vertices[vIndex + 1] = B;
                    else
                        vertices[vIndex + 1] = intersection;
                }

                vertices[vIndex + 2] = C;
                vertices[vIndex + 3] = D;

                triangles[tIndex] = vIndex;
                triangles[tIndex + 1] = vIndex + 2;
                triangles[tIndex + 2] = vIndex + 1;
                triangles[tIndex + 3] = vIndex + 1;
                triangles[tIndex + 4] = vIndex + 2;
                triangles[tIndex + 5] = vIndex + 3;

                tIndex += 6;
                vIndex += 2;
            }

            if (isClosed)
            {
                Vector3 lastPoint = pointList[pointList.Length - 1];
                Vector3 firstPoint = pointList[0];

                SBSVector3 A1 = vertices[0];
                SBSVector3 B1 = vertices[1];
                SBSVector3 C1 = vertices[2];
                SBSVector3 D1 = vertices[3];

                SBSVector3 A2 = vertices[vNum - 1 - 3];
                SBSVector3 B2 = vertices[vNum - 1 - 2];
                SBSVector3 C2 = vertices[vNum - 1 - 1];
                SBSVector3 D2 = vertices[vNum - 1];

                Vector3 direction = (firstPoint - lastPoint).normalized;
                Vector3 ortho = Vector3.Cross(direction, Vector3.forward);
                SBSVector3 A = lastPoint - ortho * width * 0.5f;
                SBSVector3 B = lastPoint + ortho * width * 0.5f;

                direction = (lastPoint - firstPoint).normalized;
                ortho = Vector3.Cross(direction, Vector3.forward);
                SBSVector3 C = firstPoint + ortho * width * 0.5f;
                SBSVector3 D = firstPoint - ortho * width * 0.5f;

                SBSLine2D line1 = new SBSLine2D(A2, C2);
                SBSLine2D line2 = new SBSLine2D(A, C);
                SBSVector3 intersection;
                bool parallel = !line1.Intersect2D(line2, out intersection);
                if (parallel)
                    vertices[vIndex] = A;
                else
                    vertices[vIndex] = intersection;

                line1 = new SBSLine2D(B2, D2);
                line2 = new SBSLine2D(B, D);
                parallel = !line1.Intersect2D(line2, out intersection);
                if (parallel)
                    vertices[vIndex + 1] = B;
                else
                    vertices[vIndex + 1] = intersection;

                line1 = new SBSLine2D(A, C);
                line2 = new SBSLine2D(A1, C1);
                parallel = !line1.Intersect2D(line2, out intersection);
                if (parallel)
                    vertices[0] = A1;
                else
                    vertices[0] = intersection;

                line1 = new SBSLine2D(B, D);
                line2 = new SBSLine2D(B1, D1);
                parallel = !line1.Intersect2D(line2, out intersection);
                if (parallel)
                    vertices[1] = B1;
                else
                    vertices[1] = intersection;

                triangles[tIndex] = vIndex;
                triangles[tIndex + 1] = 0;
                triangles[tIndex + 2] = vIndex + 1;
                triangles[tIndex + 3] = vIndex + 1;
                triangles[tIndex + 4] = 0;
                triangles[tIndex + 5] = 1;
            }

            for (int i = 0; i < vNum; i++)
            {
                uv[i] = Vector2.zero;
                normals[i] = -Vector3.forward;
                colors[i] = color;
            }

            lineMesh.vertices = vertices;
            lineMesh.uv = uv;
            lineMesh.normals = normals;
            lineMesh.colors = colors;

            string log = string.Empty;
            for (int i = 0; i < triangles.Length; i++)
                log += triangles[i] + ", ";
            //Debug.Log(" triangles: " + log);

            lineMesh.triangles = triangles;
        }