Ejemplo n.º 1
0
        private void OnMapClick()
        {
            control.GetCoords(out targetLng, out targetLat);

            if (targetMarker == null)
            {
                targetMarker       = OnlineMapsMarker3DManager.CreateItem(targetLng, targetLat, targetPrefab);
                targetMarker.scale = targetScale;
            }
            else
            {
                targetMarker.SetPosition(targetLng, targetLat);
            }

            double tx1, ty1, tx2, ty2;

            map.projection.CoordinatesToTile(lng, lat, map.zoom, out tx1, out ty1);
            map.projection.CoordinatesToTile(targetLng, targetLat, map.zoom, out tx2, out ty2);

            rotation       = (float)OnlineMapsUtils.Angle2D(tx1, ty1, tx2, ty2) - 90;
            hasTargetPoint = true;

            if (lineRenderer == null)
            {
                GameObject go = new GameObject("LineRenderer");
                go.transform.SetParent(transform, false);
                lineRenderer          = go.AddComponent <LineRenderer>();
                lineRenderer.material = lineRendererMaterial;
#if UNITY_2017_3_OR_NEWER
                lineRenderer.positionCount = 2;
                lineRenderer.widthCurve    = AnimationCurve.Constant(0, 1, 10);
#elif UNITY_2017_1_OR_NEWER
                lineRenderer.positionCount = 2;
                lineRenderer.widthCurve    = AnimationCurve.Linear(0, 10, 1, 10);
#else
                lineRenderer.SetVertexCount(2);
                lineRenderer.SetWidth(10, 10);
#endif
            }
            else
            {
                lineRenderer.enabled = true;
            }

            Vector3 p1 = control.GetWorldPosition(lng, lat);
            lineRenderer.SetPosition(0, p1);
            lineRenderer.SetPosition(1, p1);

            lineRendererProgress = 0;
        }
Ejemplo n.º 2
0
        private void Start()
        {
            map              = OnlineMaps.instance;
            control          = OnlineMapsTileSetControl.instance;
            elevationManager = OnlineMapsElevationManagerBase.instance;

            double tlx, tly, brx, bry;

            map.GetCorners(out tlx, out tly, out brx, out bry);

            Vector3 position = control.GetWorldPosition(map.position);

            position.y = altitude;
            if (elevationManager != null)
            {
                position.y *= OnlineMapsElevationManagerBase.GetBestElevationYScale(tlx, tly, brx, bry) * elevationManager.scale;
            }

            gameObject.transform.position = position;
            map.GetPosition(out px, out py);
        }
Ejemplo n.º 3
0
        private void UpdateMesh()
        {
            int zoom = map.zoom - zoomOffset;

            if (zoom < 3)
            {
                zoom = 3;
            }

            for (int i = 0; i < tiles.Length; i++)
            {
                if (tiles[i] != null)
                {
                    tiles[i].Unblock(this);
                }
            }

            double tx, ty;

            map.GetTilePosition(out tx, out ty, zoom);

            int itx = Mathf.RoundToInt((float)(tx - countX / 2f));
            int ity = Mathf.RoundToInt((float)(ty - countY / 2f));

            Vector3 offset = new Vector3(0, positionYOffset, 0) - transform.position;

            int max = 1 << zoom;

            Material[] materials = meshRenderer.sharedMaterials;

            float r1 = resolution - 1;
            int   vi = 0;

            for (int x = 0; x < countX; x++)
            {
                int tileX     = itx + x;
                int nextTileX = tileX + 1;
                if (tileX >= max)
                {
                    tileX -= max;
                }
                if (nextTileX >= max)
                {
                    nextTileX -= max;
                }

                for (int y = 0; y < countY; y++)
                {
                    int tileY     = ity + y;
                    int nextTileY = tileY + 1;

                    if (tileY >= max)
                    {
                        tileY -= max;
                    }
                    if (nextTileY >= max)
                    {
                        nextTileY -= max;
                    }

                    OnlineMapsTile tile = map.tileManager.GetTile(zoom, tileX, tileY);
                    if (tile == null)
                    {
                        OnlineMapsTile parentTile = map.tileManager.GetTile(zoom - 1, tileX / 2, tileY / 2);
                        tile        = new OnlineMapsRasterTile(tileX, tileY, zoom, map);
                        tile.parent = parentTile;
                    }

                    int tileIndex = x * countY + y;
                    tiles[tileIndex] = tile;
                    tile.Block(this);

                    double px, py;

                    map.projection.TileToCoordinates(tileX, tileY, zoom, out px, out py);
                    Vector3 v1 = control.GetWorldPosition(px, py) + offset;

                    map.projection.TileToCoordinates(nextTileX, nextTileY, zoom, out px, out py);
                    Vector3 v2 = control.GetWorldPosition(px, py) + offset;
                    Vector3 ov = (v2 - v1) / r1;

                    float   uvScale  = 1;
                    Vector2 uvOffset = Vector2.zero;

                    if (tile.texture == null)
                    {
                        while (tile.parent != null)
                        {
                            tile = tile.parent;

                            int s = 1 << (zoom - tile.zoom);

                            uvScale    = 1f / s;
                            uvOffset.x = tileX % s * uvScale;
                            uvOffset.y = (s - tileY % s - 1) * uvScale;

                            if (tile.texture != null)
                            {
                                break;
                            }
                        }
                    }

                    for (int vx = 0; vx < resolution; vx++)
                    {
                        for (int vz = 0; vz < resolution; vz++)
                        {
                            Vector3 v = new Vector3(ov.x * vx + v1.x, 0, ov.z * vz + v1.z);
                            v.y            = positionYOffset;
                            uv[vi]         = new Vector2((vx / r1) * uvScale + uvOffset.x, (1 - vz / r1) * uvScale + uvOffset.y);
                            vertices[vi++] = v;
                        }
                    }

                    materials[tileIndex].mainTexture = tile.texture;
                    materials[tileIndex].color       = new Color(1, 1, 1, tile.texture != null ? 1 : 0);
                }
            }

            mesh.vertices = vertices;
            mesh.uv       = uv;

            PreloadBorders();
        }
Ejemplo n.º 4
0
    private void UpdateBuildingsPosition()
    {
        OnlineMapsTileSetControl control = OnlineMapsTileSetControl.instance;

        Bounds bounds = new Bounds();

        double tlx, tly, brx, bry;

        api.GetTopLeftPosition(out tlx, out tly);
        api.GetBottomRightPosition(out brx, out bry);

        bounds.min = new Vector3((float)tlx, (float)bry);
        bounds.max = new Vector3((float)brx, (float)tly);

        List <string> unusedKeys = new List <string>();

        bool useElevation = control.useElevation;

        foreach (KeyValuePair <string, OnlineMapsBuildingBase> building in buildings)
        {
            if (!bounds.Intersects(building.Value.boundsCoords))
            {
                unusedKeys.Add(building.Key);
            }
            else
            {
                if (useElevation)
                {
                    Vector3 newPosition = control.GetWorldPositionWithElevation(building.Value.centerCoordinates.x, building.Value.centerCoordinates.y, tlx, tly, brx, bry);
                    building.Value.transform.position = newPosition;
                }
                else
                {
                    Vector3 newPosition = control.GetWorldPosition(building.Value.centerCoordinates.x, building.Value.centerCoordinates.y);
                    building.Value.transform.position = newPosition;
                }
            }
        }

        List <string> usedKeys    = new List <string>();
        List <string> destroyKeys = new List <string>();

        double px, py;

        api.GetPosition(out px, out py);
        api.projection.CoordinatesToTile(px, py, api.zoom, out px, out py);

        float maxDistance = Mathf.Sqrt(Mathf.Pow(api.width / 2 / OnlineMapsUtils.tileSize, 2) + Mathf.Pow(api.height / 2 / OnlineMapsUtils.tileSize, 2)) * 2;

        foreach (KeyValuePair <string, OnlineMapsBuildingBase> building in unusedBuildings)
        {
            OnlineMapsBuildingBase value = building.Value;
            if (bounds.Intersects(value.boundsCoords))
            {
                usedKeys.Add(building.Key);
                if (useElevation)
                {
                    Vector3 newPosition = control.GetWorldPositionWithElevation(building.Value.centerCoordinates, tlx, tly, brx, bry);
                    building.Value.transform.position = newPosition;
                }
                else
                {
                    Vector3 newPosition = control.GetWorldPosition(building.Value.centerCoordinates.x, building.Value.centerCoordinates.y);
                    building.Value.transform.position = newPosition;
                }
            }
            else
            {
                double bx, by;
                api.projection.CoordinatesToTile(value.centerCoordinates.x, value.centerCoordinates.y, api.zoom, out bx, out by);
                if (OnlineMapsUtils.Magnitude(0, 0, bx - px, by - py) > maxDistance)
                {
                    destroyKeys.Add(building.Key);
                }
            }
        }

        foreach (string key in unusedKeys)
        {
            OnlineMapsBuildingBase value = buildings[key];
            value.gameObject.SetActive(false);
            unusedBuildings.Add(key, value);
            buildings.Remove(key);
        }

        foreach (string key in usedKeys)
        {
            OnlineMapsBuildingBase value = unusedBuildings[key];
            if (maxActiveBuildings > 0 && buildings.Count >= maxActiveBuildings)
            {
                break;
            }
            if (OnShowBuilding != null && !OnShowBuilding(value))
            {
                continue;
            }
            value.gameObject.SetActive(true);
            buildings.Add(key, value);
            unusedBuildings.Remove(key);
        }

        foreach (string key in destroyKeys)
        {
            OnlineMapsBuildingBase value = unusedBuildings[key];
            if (OnBuildingDispose != null)
            {
                OnBuildingDispose(value);
            }
            OnlineMapsUtils.DestroyImmediate(value.gameObject);
            unusedBuildings.Remove(key);
        }

        if (destroyKeys.Count > 0)
        {
            OnlineMaps.instance.needGC = true;
        }
    }