protected void DrawActivePoints(OnlineMapsTileSetControl control, ref List <Vector2> activePoints, ref List <Vector3> vertices, ref List <Vector3> normals, ref List <int> triangles, ref List <Vector2> uv, float width)
    {
        if (activePoints.Count < 2)
        {
            activePoints.Clear();
            return;
        }

        float w2 = width * 2;

        Vector3 prevS1 = Vector3.zero;
        Vector3 prevS2 = Vector3.zero;

        int  c = activePoints.Count - 1;
        bool extraPointAdded = false;

        for (int i = 0; i < activePoints.Count; i++)
        {
            float px = -activePoints[i].x;
            float pz = activePoints[i].y;

            Vector3 s1;
            Vector3 s2;

            if (i == 0 || i == c)
            {
                float p1x, p1z, p2x, p2z;

                if (i == 0)
                {
                    p1x = px;
                    p1z = pz;
                    p2x = -activePoints[i + 1].x;
                    p2z = activePoints[i + 1].y;
                }
                else
                {
                    p1x = -activePoints[i - 1].x;
                    p1z = activePoints[i - 1].y;
                    p2x = px;
                    p2z = pz;
                }

                float a = OnlineMapsUtils.Angle2DRad(p1x, p1z, p2x, p2z, 90);

                float offX = Mathf.Cos(a) * width;
                float offZ = Mathf.Sin(a) * width;

                float s1x = px + offX;
                float s1z = pz + offZ;
                float s2x = px - offX;
                float s2z = pz - offZ;

                float s1y = OnlineMapsElevationManagerBase.GetElevation(s1x, s1z, bestElevationYScale, tlx, tly, brx, bry);
                float s2y = OnlineMapsElevationManagerBase.GetElevation(s2x, s2z, bestElevationYScale, tlx, tly, brx, bry);

                s1 = new Vector3(s1x, s1y, s1z);
                s2 = new Vector3(s2x, s2y, s2z);
            }
            else
            {
                float p1x = -activePoints[i - 1].x;
                float p1z = activePoints[i - 1].y;
                float p2x = -activePoints[i + 1].x;
                float p2z = activePoints[i + 1].y;

                float a1 = OnlineMapsUtils.Angle2DRad(p1x, p1z, px, pz, 90);
                float a3 = OnlineMapsUtils.AngleOfTriangle(activePoints[i - 1], activePoints[i + 1], activePoints[i]) * Mathf.Rad2Deg;
                if (a3 < 60 && !extraPointAdded)
                {
                    activePoints.Insert(i + 1, Vector2.Lerp(activePoints[i], activePoints[i + 1], 0.001f));
                    activePoints[i] = Vector2.Lerp(activePoints[i], activePoints[i - 1], 0.001f);
                    c++;
                    i--;
                    extraPointAdded = true;
                    continue;
                }

                extraPointAdded = false;
                float a2 = OnlineMapsUtils.Angle2DRad(px, pz, p2x, p2z, 90);

                float off1x = Mathf.Cos(a1) * width;
                float off1z = Mathf.Sin(a1) * width;
                float off2x = Mathf.Cos(a2) * width;
                float off2z = Mathf.Sin(a2) * width;

                float p21x = px + off1x;
                float p21z = pz + off1z;
                float p22x = px - off1x;
                float p22z = pz - off1z;
                float p31x = px + off2x;
                float p31z = pz + off2z;
                float p32x = px - off2x;
                float p32z = pz - off2z;

                float is1x, is1z, is2x, is2z;

                int state1 = OnlineMapsUtils.GetIntersectionPointOfTwoLines(p1x + off1x, p1z + off1z, p21x, p21z, p31x, p31z, p2x + off2x, p2z + off2z, out is1x, out is1z);
                int state2 = OnlineMapsUtils.GetIntersectionPointOfTwoLines(p1x - off1x, p1z - off1z, p22x, p22z, p32x, p32z, p2x - off2x, p2z - off2z, out is2x, out is2z);

                if (state1 == 1 && state2 == 1)
                {
                    float o1x = is1x - px;
                    float o1z = is1z - pz;
                    float o2x = is2x - px;
                    float o2z = is2z - pz;

                    float m1 = Mathf.Sqrt(o1x * o1x + o1z * o1z);
                    float m2 = Mathf.Sqrt(o2x * o2x + o2z * o2z);

                    if (m1 > w2)
                    {
                        is1x = o1x / m1 * w2 + px;
                        is1z = o1z / m1 * w2 + pz;
                    }
                    if (m2 > w2)
                    {
                        is2x = o2x / m2 * w2 + px;
                        is2z = o2z / m2 * w2 + pz;
                    }

                    s1 = new Vector3(is1x, OnlineMapsElevationManagerBase.GetElevation(is1x, is1z, bestElevationYScale, tlx, tly, brx, bry), is1z);
                    s2 = new Vector3(is2x, OnlineMapsElevationManagerBase.GetElevation(is2x, is2z, bestElevationYScale, tlx, tly, brx, bry), is2z);
                }
                else
                {
                    float po1x = p1x + off1x;
                    float po1z = p1z + off1z;
                    float po2x = p2x - off1x;
                    float po2z = p2z - off1z;

                    s1 = new Vector3(po1x, OnlineMapsElevationManagerBase.GetElevation(po1x, po1z, bestElevationYScale, tlx, tly, brx, bry), po1z);
                    s2 = new Vector3(po2x, OnlineMapsElevationManagerBase.GetElevation(po2x, po2z, bestElevationYScale, tlx, tly, brx, bry), po2z);
                }
            }

            if (i > 0)
            {
                AddLineSegment(vertices, normals, triangles, uv, s1, s2, prevS1, prevS2);
            }

            prevS1 = s1;
            prevS2 = s2;
        }

        activePoints.Clear();
    }
Пример #2
0
    public override void DrawOnTileset(OnlineMapsTileSetControl control, int index)
    {
        if (points == null)
        {
            return;
        }

        base.DrawOnTileset(control, index);

        if (!visible)
        {
            active = false;
            return;
        }

        if (range != null && !range.InRange(control.map.floatZoom))
        {
            active = false;
            return;
        }

        InitMesh(control, borderColor, backgroundColor);
        InitLineMesh(points, control, ref vertices, ref normals, ref triangles, ref uv, borderWidth, true, false);

        mesh.Clear();

        if (vertices.Count < 4)
        {
            return;
        }

        active = true;

        Vector3 v1 = (vertices[0] + vertices[3]) / 2;
        Vector3 v2 = (vertices[vertices.Count - 3] + vertices[vertices.Count - 2]) / 2;

        if ((v1.x - v2.x) * (v1.x - v2.x) + (v1.z - v2.z) * (v1.z - v2.z) < float.Epsilon)
        {
            int     s1, s2;
            Vector3 v0 = vertices[0];
            v1 = vertices[1];
            v2 = vertices[2];
            Vector3 v3 = vertices[3];
            Vector3 vs1 = vertices[vertices.Count - 1];
            Vector3 vs2 = vertices[vertices.Count - 2];
            Vector3 vs3 = vertices[vertices.Count - 3];
            Vector3 vs4 = vertices[vertices.Count - 4];
            Vector3 nv1 = Vector3.zero, nv2 = Vector3.zero;
            s1 = OnlineMapsUtils.GetIntersectionPointOfTwoLines(v0.x, v0.z, v1.x, v1.z, vs4.x, vs4.z, vs3.x, vs3.z, out nv1.x, out nv1.z);
            s2 = OnlineMapsUtils.GetIntersectionPointOfTwoLines(v3.x, v3.z, v2.x, v2.z, vs1.x, vs1.z, vs2.x, vs2.z, out nv2.x, out nv2.z);

            if (s1 == 1 && s2 == 1)
            {
                nv1.y       = OnlineMapsElevationManagerBase.GetElevation(nv1.x, nv1.z, bestElevationYScale, tlx, tly, brx, bry);
                nv2.y       = OnlineMapsElevationManagerBase.GetElevation(nv2.x, nv2.z, bestElevationYScale, tlx, tly, brx, bry);
                vertices[0] = vertices[vertices.Count - 3] = nv1;
                vertices[3] = vertices[vertices.Count - 2] = nv2;
            }
            else
            {
                vertices[0] = vertices[vertices.Count - 3] = (vertices[0] + vertices[vertices.Count - 3]) / 2;
                vertices[3] = vertices[vertices.Count - 2] = (vertices[3] + vertices[vertices.Count - 2]) / 2;
            }
        }

        int[] fillTriangles = null;

        if (!checkMapBoundaries && backgroundColor.a > 0 && vertices.Count > 0)
        {
            float l1 = 0;
            float l2 = 0;

            for (int i = 0; i < vertices.Count / 4 - 1; i++)
            {
                Vector3 p11 = vertices[i * 4];
                Vector3 p12 = vertices[(i + 1) * 4];

                Vector3 p21 = vertices[i * 4 + 3];
                Vector3 p22 = vertices[(i + 1) * 4 + 3];

                l1 += (p11 - p12).magnitude;
                l2 += (p21 - p22).magnitude;
            }

            bool side = l2 < l1;
            int  off1 = side ? 3 : 0;
            int  off2 = side ? 2 : 1;

            Vector2        lastPoint       = Vector2.zero;
            List <int>     internalIndices = new List <int>(vertices.Count / 4);
            List <Vector2> internalPoints  = new List <Vector2>(vertices.Count / 4);
            float          w = borderWidth / 2;
            w *= w;
            for (int i = 0, j = 0; i < vertices.Count / 4; i++, j += 4)
            {
                Vector3 p  = vertices[j + off1];
                Vector2 p2 = new Vector2(p.x, p.z);
                if (i > 0)
                {
                    if ((lastPoint - p2).sqrMagnitude > w)
                    {
                        internalIndices.Add(j + off1);
                        internalPoints.Add(p2);
                        lastPoint = p2;
                    }
                }
                else
                {
                    internalIndices.Add(j + off1);
                    internalPoints.Add(p2);
                    lastPoint = p2;
                }
                p  = vertices[j + off2];
                p2 = new Vector2(p.x, p.z);
                if ((lastPoint - p2).sqrMagnitude > w)
                {
                    internalIndices.Add(j + off2);
                    internalPoints.Add(p2);
                    lastPoint = p2;
                }
            }

            if (internalPoints[0] == internalPoints[internalPoints.Count - 1])
            {
                internalPoints.RemoveAt(internalPoints.Count - 1);
            }

            fillTriangles = OnlineMapsUtils.Triangulate(internalPoints).ToArray();
            //fillTriangles = OMTriangulator.Triangulate(internalPoints);


            if (fillTriangles.Length > 2)
            {
                for (int i = 0; i < fillTriangles.Length; i++)
                {
                    fillTriangles[i] = internalIndices[fillTriangles[i]];
                }

                Vector3 side1 = vertices[fillTriangles[1]] - vertices[fillTriangles[0]];
                Vector3 side2 = vertices[fillTriangles[2]] - vertices[fillTriangles[0]];
                Vector3 perp  = Vector3.Cross(side1, side2);

                bool reversed = perp.y < 0;
                if (reversed)
                {
                    fillTriangles = fillTriangles.Reverse().ToArray();
                }
            }
            else
            {
                fillTriangles = null;
            }
        }

        mesh.subMeshCount = 2;

        mesh.SetVertices(vertices);
        mesh.SetNormals(normals);
        mesh.SetUVs(0, uv);

        mesh.SetTriangles(triangles.ToArray(), 0);
        if (fillTriangles != null)
        {
            mesh.SetTriangles(fillTriangles.ToArray(), 1);
        }

        UpdateMaterialsQuote(control, index);
    }
    protected void DrawActivePoints(OnlineMapsTileSetControl control, ref List <Vector2> activePoints, ref List <Vector3> verticles, ref List <Vector3> normals, ref List <int> triangles, ref List <Vector2> uv, float weight)
    {
        if (activePoints.Count < 2)
        {
            activePoints.Clear();
            return;
        }
        List <Vector3> side1 = new List <Vector3>();
        List <Vector3> side2 = new List <Vector3>();

        for (int i = 0; i < activePoints.Count; i++)
        {
            Vector3 p = new Vector3(-activePoints[i].x, 0, activePoints[i].y);
            if (i == 0)
            {
                float   a   = OnlineMapsUtils.Angle2DRad(p, new Vector3(-activePoints[i + 1].x, 0, activePoints[i + 1].y), 90);
                Vector3 off = new Vector3(Mathf.Cos(a) * weight, 0, Mathf.Sin(a) * weight);
                Vector3 s1  = p + off;
                Vector3 s2  = p - off;
                s1.y = control.GetElevationValue(s1.x, s1.z, bestElevationYScale, tlx, tly, brx, bry);
                s2.y = control.GetElevationValue(s2.x, s2.z, bestElevationYScale, tlx, tly, brx, bry);
                side1.Add(s1);
                side2.Add(s2);
            }
            else if (i == activePoints.Count - 1)
            {
                float   a   = OnlineMapsUtils.Angle2DRad(new Vector3(-activePoints[i - 1].x, 0, activePoints[i - 1].y), p, 90);
                Vector3 off = new Vector3(Mathf.Cos(a) * weight, 0, Mathf.Sin(a) * weight);
                Vector3 s1  = p + off;
                Vector3 s2  = p - off;
                s1.y = control.GetElevationValue(s1.x, s1.z, bestElevationYScale, tlx, tly, brx, bry);
                s2.y = control.GetElevationValue(s2.x, s2.z, bestElevationYScale, tlx, tly, brx, bry);
                side1.Add(s1);
                side2.Add(s2);
            }
            else
            {
                Vector3 p1 = new Vector3(-activePoints[i - 1].x, 0, activePoints[i - 1].y);
                Vector3 p2 = new Vector3(-activePoints[i + 1].x, 0, activePoints[i + 1].y);
                float   a1 = OnlineMapsUtils.Angle2DRad(p1, p, 90);
                float   a2 = OnlineMapsUtils.Angle2DRad(p, p2, 90);
                Vector3 off1 = new Vector3(Mathf.Cos(a1) * weight, 0, Mathf.Sin(a1) * weight);
                Vector3 off2 = new Vector3(Mathf.Cos(a2) * weight, 0, Mathf.Sin(a2) * weight);
                Vector3 p21 = p + off1;
                Vector3 p22 = p - off1;
                Vector3 p31 = p + off2;
                Vector3 p32 = p - off2;
                int     state1, state2;
                Vector2 is1 = OnlineMapsUtils.GetIntersectionPointOfTwoLines(p1 + off1, p21, p31, p2 + off2, out state1);
                Vector2 is2 = OnlineMapsUtils.GetIntersectionPointOfTwoLines(p1 - off1, p22, p32, p2 - off2, out state2);
                if (state1 == 1)
                {
                    side1.Add(new Vector3(is1.x, control.GetElevationValue(is1.x, is1.y, bestElevationYScale, tlx, tly, brx, bry), is1.y));
                }
                if (state2 == 1)
                {
                    side2.Add(new Vector3(is2.x, control.GetElevationValue(is2.x, is2.y, bestElevationYScale, tlx, tly, brx, bry), is2.y));
                }
            }
        }

        for (int i = 0; i < Mathf.Min(side1.Count, side2.Count) - 1; i++)
        {
            int ti = verticles.Count;

            verticles.Add(side1[i]);
            verticles.Add(side1[i + 1]);
            verticles.Add(side2[i + 1]);
            verticles.Add(side2[i]);

            normals.Add(Vector3.up);
            normals.Add(Vector3.up);
            normals.Add(Vector3.up);
            normals.Add(Vector3.up);

            uv.Add(new Vector2(0, 0));
            uv.Add(new Vector2(0, 1));
            uv.Add(new Vector2(1, 1));
            uv.Add(new Vector2(1, 0));

            triangles.Add(ti);
            triangles.Add(ti + 1);
            triangles.Add(ti + 2);
            triangles.Add(ti);
            triangles.Add(ti + 2);
            triangles.Add(ti + 3);
        }

        activePoints.Clear();
    }
Пример #4
0
    protected void DrawActivePoints(OnlineMapsTileSetControl control, ref List <Vector2> activePoints, ref List <Vector3> verticles, ref List <Vector3> normals, ref List <int> triangles, ref List <Vector2> uv, float weight)
    {
        if (activePoints.Count < 2)
        {
            activePoints.Clear();
            return;
        }

        float w2 = weight * 2;

        Vector3 prevS1 = Vector3.zero;
        Vector3 prevS2 = Vector3.zero;

        int c = activePoints.Count - 1;

        for (int i = 0; i < activePoints.Count; i++)
        {
            float px = -activePoints[i].x;
            float pz = activePoints[i].y;

            Vector3 s1 = Vector3.zero;
            Vector3 s2 = Vector3.zero;

            if (i == 0 || i == c)
            {
                float p1x, p1z, p2x, p2z;

                if (i == 0)
                {
                    p1x = px;
                    p1z = pz;
                    p2x = -activePoints[i + 1].x;
                    p2z = activePoints[i + 1].y;
                }
                else
                {
                    p1x = -activePoints[i - 1].x;
                    p1z = activePoints[i - 1].y;
                    p2x = px;
                    p2z = pz;
                }

                float a    = OnlineMapsUtils.Angle2DRad(p1x, p1z, p2x, p2z, 90);
                float offX = Mathf.Cos(a) * weight;
                float offZ = Mathf.Sin(a) * weight;
                float s1x  = px + offX;
                float s1z  = pz + offZ;
                float s2x  = px - offX;
                float s2z  = pz - offZ;

                float s1y = control.GetElevationValue(s1x, s1z, bestElevationYScale, tlx, tly, brx, bry);
                float s2y = control.GetElevationValue(s2x, s2z, bestElevationYScale, tlx, tly, brx, bry);

                s1 = new Vector3(s1x, s1y, s1z);
                s2 = new Vector3(s2x, s2y, s2z);
            }
            else
            {
                float p1x = -activePoints[i - 1].x;
                float p1z = activePoints[i - 1].y;
                float p2x = -activePoints[i + 1].x;
                float p2z = activePoints[i + 1].y;

                float a1 = OnlineMapsUtils.Angle2DRad(p1x, p1z, px, pz, 90);
                float a2 = OnlineMapsUtils.Angle2DRad(px, pz, p2x, p2z, 90);

                float off1x = Mathf.Cos(a1) * weight;
                float off1z = Mathf.Sin(a1) * weight;
                float off2x = Mathf.Cos(a2) * weight;
                float off2z = Mathf.Sin(a2) * weight;

                float p21x = px + off1x;
                float p21z = pz + off1z;
                float p22x = px - off1x;
                float p22z = pz - off1z;
                float p31x = px + off2x;
                float p31z = pz + off2z;
                float p32x = px - off2x;
                float p32z = pz - off2z;

                float is1x, is1z, is2x, is2z;

                int state1 = OnlineMapsUtils.GetIntersectionPointOfTwoLines(p1x + off1x, p1z + off1z, p21x, p21z, p31x, p31z, p2x + off2x, p2z + off2z, out is1x, out is1z);
                int state2 = OnlineMapsUtils.GetIntersectionPointOfTwoLines(p1x - off1x, p1z - off1z, p22x, p22z, p32x, p32z, p2x - off2x, p2z - off2z, out is2x, out is2z);

                if (state1 == 1 && state2 == 1)
                {
                    float o1x = is1x - px;
                    float o1z = is1z - pz;
                    float o2x = is2x - px;
                    float o2z = is2z - pz;

                    float m1 = Mathf.Sqrt(o1x * o1x + o1z * o1z);
                    float m2 = Mathf.Sqrt(o2x * o2x + o2z * o2z);

                    if (m1 > w2)
                    {
                        is1x = o1x / m1 * w2 + px;
                        is1z = o1z / m1 * w2 + pz;
                    }
                    if (m2 > w2)
                    {
                        is2x = o2x / m2 * w2 + px;
                        is2z = o2z / m2 * w2 + pz;
                    }

                    s1 = new Vector3(is1x, control.GetElevationValue(is1x, is1z, bestElevationYScale, tlx, tly, brx, bry), is1z);
                    s2 = new Vector3(is2x, control.GetElevationValue(is2x, is2z, bestElevationYScale, tlx, tly, brx, bry), is2z);
                }
                else
                {
                    float po1x = p1x + off1x;
                    float po1z = p1z + off1z;
                    float po2x = p2x - off1x;
                    float po2z = p2z - off1z;

                    s1 = new Vector3(po1x, control.GetElevationValue(po1x, po1z, bestElevationYScale, tlx, tly, brx, bry), po1z);
                    s2 = new Vector3(po2x, control.GetElevationValue(po2x, po2z, bestElevationYScale, tlx, tly, brx, bry), po2z);
                }
            }

            if (i > 0)
            {
                int ti = verticles.Count;

                verticles.Add(prevS1);
                verticles.Add(s1);
                verticles.Add(s2);
                verticles.Add(prevS2);

                normals.Add(Vector3.up);
                normals.Add(Vector3.up);
                normals.Add(Vector3.up);
                normals.Add(Vector3.up);

                uv.Add(new Vector2(0, 0));
                uv.Add(new Vector2(0, 1));
                uv.Add(new Vector2(1, 1));
                uv.Add(new Vector2(1, 0));

                triangles.Add(ti);
                triangles.Add(ti + 1);
                triangles.Add(ti + 2);
                triangles.Add(ti);
                triangles.Add(ti + 2);
                triangles.Add(ti + 3);
            }

            prevS1 = s1;
            prevS2 = s2;
        }

        activePoints.Clear();
    }