Esempio n. 1
0
 private static void CreateHouseRoofTriangles(List <Vector3> vertices, OnlineMapsBuildingRoofType roofType, List <Vector2> roofPoints, float baseHeight, float roofHeight, ref List <int> triangles)
 {
     if (roofType == OnlineMapsBuildingRoofType.flat)
     {
         triangles.AddRange(OnlineMapsUtils.Triangulate(roofPoints).Select(index => index));
     }
     else if (roofType == OnlineMapsBuildingRoofType.dome)
     {
         CreateHouseRoofDome(baseHeight + roofHeight, vertices, triangles);
     }
 }
Esempio n. 2
0
 private static void CreateHouseRoofTriangles(int countVertices, List <Vector3> vertices, RoofType roofType, float[] roofPoints, float baseHeight, float roofHeight, ref List <int> triangles)
 {
     if (roofType == RoofType.flat)
     {
         if (roofIndices == null)
         {
             roofIndices = new List <int>(60);
         }
         triangles.AddRange(OnlineMapsUtils.Triangulate(roofPoints, countVertices, roofIndices));
     }
     else if (roofType == RoofType.dome)
     {
         CreateHouseRoofDome(baseHeight + roofHeight, vertices, triangles);
     }
 }
    public override void DrawOnTileset(OnlineMapsTileSetControl control, int index)
    {
        base.DrawOnTileset(control, index);

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

        InitMesh(control, "Poly", 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 (Math.Abs((v1 - v2).magnitude) < float.Epsilon)
        {
            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);
            for (int i = 0; i < vertices.Count / 4; i++)
            {
                Vector3 p  = vertices[i * 4 + off1];
                Vector2 p2 = new Vector2(p.x, p.z);
                if (i > 0)
                {
                    if ((lastPoint - p2).magnitude > borderWidth / 2)
                    {
                        internalIndices.Add(i * 4 + off1);
                        internalPoints.Add(p2);
                        lastPoint = p2;
                    }
                }
                else
                {
                    internalIndices.Add(i * 4 + off1);
                    internalPoints.Add(p2);
                    lastPoint = p2;
                }
                p  = vertices[i * 4 + off2];
                p2 = new Vector2(p.x, p.z);
                if ((lastPoint - p2).magnitude > borderWidth / 2)
                {
                    internalIndices.Add(i * 4 + off2);
                    internalPoints.Add(p2);
                    lastPoint = p2;
                }
            }

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

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

            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;

#if UNITY_4_6 || UNITY_4_7 || UNITY_5_0 || UNITY_5_1
        mesh.vertices = vertices.ToArray();
        mesh.normals  = normals.ToArray();
        mesh.uv       = uv.ToArray();
#else
        mesh.SetVertices(vertices);
        mesh.SetNormals(normals);
        mesh.SetUVs(0, uv);
#endif

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

        UpdateMaterialsQuote(control, index);
    }
Esempio n. 4
0
        private void Update()
        {
            if (_borderWeight != borderWeight)
            {
                _borderWeight = borderWeight;
                if (polygon != null)
                {
                    polygon.borderWeight = borderWeight;
                    map.Redraw();
                }
            }

            // Check the position of the markers.
            CheckMarkerPositions();

            // If nothing happens, then return.
            if (!changed)
            {
                return;
            }
            changed = false;

            // If the number of points is less than 3, then return.
            if (markers.Count < 3)
            {
                map.Redraw();
                return;
            }

            // If the polygon is not created, then create.
            if (polygon == null)
            {
                // For points, reference to markerPositions.
                // If you change the values ​​in markerPositions, value in the polygon will be adjusted automatically.
                polygon = new OnlineMapsDrawingPoly(markerPositions, Color.black, borderWeight, new Color(1, 1, 1, 0.3f));

                // Add an element to the map.
                map.AddDrawingElement(polygon);
            }

            // Calculates area of ​​the polygon.
            // Important: this algorithm works correctly only if the lines do not intersect.
            float area = 0;

            // Triangulate points.
            int[] indexes = OnlineMapsUtils.Triangulate(markerPositions).ToArray();

            // Calculate the area of each triangle.
            for (int i = 0; i < indexes.Length / 3; i++)
            {
                // Get the points of the triangle.
                Vector2 p1 = markerPositions[indexes[i * 3]];
                Vector2 p2 = markerPositions[indexes[i * 3 + 1]];
                Vector2 p3 = markerPositions[indexes[i * 3 + 2]];

                // Calculate the distance between points.
                float d1 = OnlineMapsUtils.DistanceBetweenPoints(p1, p2).magnitude;
                float d2 = OnlineMapsUtils.DistanceBetweenPoints(p2, p3).magnitude;
                float d3 = OnlineMapsUtils.DistanceBetweenPoints(p3, p1).magnitude;

                // Calculate the area.
                float p = (d1 + d2 + d3) / 2;
                area += Mathf.Sqrt(p * (p - d1) * (p - d2) * (p - d2));
            }

            Debug.Log("Area: " + area + " km^2");

            map.Redraw();
        }
Esempio n. 5
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);
    }