//tile here should be totally optional and used only not to have keep a dictionary in terrain factory base
    public override void FetchData(DataFetcherParameters parameters)
    {
        var terrainDataParameters = parameters as TerrainDataFetcherParameters;

        if (terrainDataParameters == null)
        {
            return;
        }
        var pngRasterTile = new RawPngRasterTile();

        pngRasterTile.Initialize(_fileSource, terrainDataParameters.canonicalTileId, terrainDataParameters.mapid, () =>
        {
            if (terrainDataParameters.tile.CanonicalTileId != pngRasterTile.Id)
            {
                //this means tile object is recycled and reused. Returned data doesn't belong to this tile but probably the previous one. So we're trashing it.
                return;
            }

            if (pngRasterTile.HasError)
            {
                FetchingError(terrainDataParameters.tile, pngRasterTile, new TileErrorEventArgs(terrainDataParameters.canonicalTileId, pngRasterTile.GetType(), null, pngRasterTile.Exceptions));
            }
            else
            {
                DataRecieved(terrainDataParameters.tile, pngRasterTile);
            }
        });
    }
        /// <summary>
        /// Creates the non-flat terrain using a height multiplier
        /// </summary>
        /// <param name="tile"></param>
        /// <param name="heightMultiplier">Multiplier for queried height value</param>
        private void CreateTerrainHeight(UnityTile tile)
        {
            tile.HeightDataState = TilePropertyState.Loading;
            var pngRasterTile = new RawPngRasterTile();

            tile.AddTile(pngRasterTile);
            Progress++;

            pngRasterTile.Initialize(_fileSource, tile.CanonicalTileId, _mapId, () =>
            {
                if (pngRasterTile.HasError)
                {
                    tile.HeightDataState = TilePropertyState.Error;

                    // Handle missing elevation from server (404)!
                    // TODO: optimize this search!
                    if (pngRasterTile.ExceptionsAsString.Contains("404"))
                    {
                        ResetToFlatMesh(tile);
                    }
                    Progress--;
                    return;
                }

                tile.SetHeightData(pngRasterTile.Data, _heightModifier, _useRelativeHeight);
                GenerateTerrainMesh(tile);
                Progress--;
            });
        }
Beispiel #3
0
        /// <summary>
        /// Creates the non-flat terrain using a height multiplier
        /// </summary>
        /// <param name="tile"></param>
        // <param name="heightMultiplier">Multiplier for queried height value</param>
        private void CreateTerrainHeight(UnityTile tile)
        {
            tile.HeightDataState = TilePropertyState.Loading;
            var pngRasterTile = new RawPngRasterTile();

            tile.AddTile(pngRasterTile);
            Progress++;

            pngRasterTile.Initialize(_fileSource, tile.CanonicalTileId, MapId, () =>
            {
                if (tile == null)
                {
                    return;
                }

                if (pngRasterTile.HasError)
                {
                    OnErrorOccurred(new TileErrorEventArgs(tile.CanonicalTileId, pngRasterTile.GetType(), tile, pngRasterTile.Exceptions));
                    tile.HeightDataState = TilePropertyState.Error;

                    // Handle missing elevation from server (404)!
                    // TODO: optimize this search!
                    if (pngRasterTile.ExceptionsAsString.Contains("404"))
                    {
                        ResetToFlatMesh(tile);
                    }
                    Progress--;
                    return;
                }

                tile.SetHeightData(pngRasterTile.Data, _elevationOptions.requiredOptions.exaggerationFactor);
                GenerateTerrainMesh(tile);
                Progress--;
            });
        }
Beispiel #4
0
        public void States()
#endif
        {
            var parameters = new Tile.Parameters();

            parameters.Fs = _fs;
            parameters.Id = new CanonicalTileId(1, 1, 1);

            var tile = new RawPngRasterTile();

            Assert.AreEqual(Tile.State.New, tile.CurrentState);

            tile.Initialize(parameters, () => { });
            Assert.AreEqual(Tile.State.Loading, tile.CurrentState);

#if UNITY_5_6_OR_NEWER
            IEnumerator enumerator = _fs.WaitForAllRequests();
            while (enumerator.MoveNext())
            {
                yield return(null);
            }
#else
            _fs.WaitForAllRequests();
#endif

            Assert.AreEqual(Tile.State.Loaded, tile.CurrentState);

            tile.Cancel();
            Assert.AreEqual(Tile.State.Canceled, tile.CurrentState);
        }
Beispiel #5
0
        private void Run(UnityTile tile)
        {
            var parameters = new Tile.Parameters
            {
                Fs    = this.FileSource,
                Id    = new CanonicalTileId(tile.Zoom, (int)tile.TileCoordinate.x, (int)tile.TileCoordinate.y),
                MapId = _mapId
            };

            tile.HeightDataState = TilePropertyState.Loading;
            var pngRasterTile = new RawPngRasterTile();

            pngRasterTile.Initialize(parameters, () =>
            {
                if (pngRasterTile.Error != null)
                {
                    tile.HeightDataState = TilePropertyState.Error;
                    return;
                }

                var texture      = new Texture2D(256, 256);
                texture.wrapMode = TextureWrapMode.Clamp;
                texture.LoadImage(pngRasterTile.Data);
                CreateTerrain(tile, texture);

                tile.HeightData      = texture;
                tile.HeightDataState = TilePropertyState.Loaded;

                FixStitches(tile);
            });
        }
Beispiel #6
0
        /// <summary>
        /// Creates the non-flat terrain using a height multiplier
        /// </summary>
        /// <param name="tile"></param>
        /// <param name="heightMultiplier">Multiplier for queried height value</param>
        private void CreateTerrainHeight(UnityTile tile, float heightMultiplier = 1)
        {
            if (tile.HeightData == null)
            {
                var parameters = new Tile.Parameters
                {
                    Fs    = this.FileSource,
                    Id    = new CanonicalTileId(tile.Zoom, (int)tile.TileCoordinate.x, (int)tile.TileCoordinate.y),
                    MapId = _mapId
                };

                tile.HeightDataState = TilePropertyState.Loading;
                var pngRasterTile = new RawPngRasterTile();
                pngRasterTile.Initialize(parameters, () =>
                {
                    if (pngRasterTile.Error != null)
                    {
                        tile.HeightDataState = TilePropertyState.Error;
                        return;
                    }
                    var texture      = new Texture2D(256, 256);
                    texture.wrapMode = TextureWrapMode.Clamp;
                    texture.LoadImage(pngRasterTile.Data);
                    tile.HeightData      = texture;
                    tile.HeightDataState = TilePropertyState.Loaded;
                    GenerateTerrainMesh(tile, heightMultiplier);
                });
            }
            else
            {
                GenerateTerrainMesh(tile, heightMultiplier);
            }
        }
Beispiel #7
0
        public void TileLoading()
#endif
        {
            byte[] data;

            var parameters = new Tile.Parameters();

            parameters.Fs = _fs;
            parameters.Id = new CanonicalTileId(1, 1, 1);

            var tile = new RawPngRasterTile();

            tile.Initialize(parameters, () => { data = tile.Data; });

#if UNITY_5_6_OR_NEWER
            IEnumerator enumerator = _fs.WaitForAllRequests();
            while (enumerator.MoveNext())
            {
                yield return(null);
            }
#else
            _fs.WaitForAllRequests();
#endif

            Assert.Greater(tile.Data.Length, 1000);
        }
Beispiel #8
0
    //tile here should be totally optional and used only not to have keep a dictionary in terrain factory base
    public void FetchTerrain(CanonicalTileId canonicalTileId, string mapid, UnityTile tile = null)
    {
        var pngRasterTile = new RawPngRasterTile();

        pngRasterTile.Initialize(_fileSource, canonicalTileId, mapid, () =>
        {
            if (pngRasterTile.HasError)
            {
                FetchingError(tile, new TileErrorEventArgs(canonicalTileId, pngRasterTile.GetType(), null, pngRasterTile.Exceptions));
            }

            DataRecieved(tile, pngRasterTile);
        });
    }
Beispiel #9
0
        private void BuildTerrainTile()
        {
            var parameters = new Tile.Parameters
            {
                Fs = _fileSource,
                Id = new CanonicalTileId(zoomLevel, (int)tileCoordinate.x, (int)tileCoordinate.y)
            };

            // Use RawPngRasterTile class for terrain. requests mapbox.terrain-rgb mapid by default.
            var heightTile = new RawPngRasterTile();

            heightTile.Initialize(parameters, () =>
            {
                if (heightTile.Error != null)
                {
                    Debug.Log(heightTile.Error);
                    return;
                }

                heightData = new Texture2D(256, 256);
                heightData.LoadImage(heightTile.Data);

                //var heightOffset = GetHeightFromColor(heightData.GetPixel(0, 0));

                var verts = new List <Vector3>();
                for (float x = 0; x < tileResolution; x++)
                {
                    for (float y = 0; y < tileResolution; y++)
                    {
                        var stepx  = Mathf.Lerp(-tileHalfEdge, tileHalfEdge, x / (tileResolution - 1));
                        var stepy  = 1 - Mathf.Lerp(-tileHalfEdge, tileHalfEdge, y / (tileResolution - 1));
                        var height = GetHeightFromColor(heightData.GetPixel((int)Mathf.Clamp((x / (tileResolution - 1) * 256), 0, 255), (int)Mathf.Clamp((256 - (y / (tileResolution - 1) * 256)), 0, 255)));
                        verts.Add(new Vector3(stepx,
                                              height,
                                              stepy));
                    }
                }

                //tileObject.transform.position = new Vector3(0, heightOffset, 0);

                var mf = tileObject.GetComponent <MeshFilter>();
                mf.mesh.SetVertices(verts);
                mf.mesh.RecalculateNormals();
                mf.mesh.RecalculateBounds();
                SnapCamera();
            });
        }
Beispiel #10
0
        public void TileLoading()
        {
            byte[] data;

            var parameters = new Tile.Parameters();

            parameters.Fs = this.fs;
            parameters.Id = new CanonicalTileId(1, 1, 1);

            var tile = new RawPngRasterTile();

            tile.Initialize(parameters, () => { data = tile.Data; });

            this.fs.WaitForAllRequests();

            Assert.Greater(tile.Data.Length, 1000);
        }
Beispiel #11
0
        /// <summary>
        /// Creates the non-flat terrain using a height multiplier
        /// </summary>
        /// <param name="tile"></param>
        /// <param name="heightMultiplier">Multiplier for queried height value</param>
        private void CreateTerrainHeight(UnityTile tile)
        {
            tile.HeightDataState = TilePropertyState.Loading;
            var pngRasterTile = new RawPngRasterTile();

            tile.AddTile(pngRasterTile);
            Progress++;

            pngRasterTile.Initialize(_fileSource, tile.CanonicalTileId, _mapId, () =>
            {
                if (tile == null)
                {
                    return;
                }

                if (pngRasterTile.HasError)
                {
                    OnErrorOccurred(new TileErrorEventArgs(tile.CanonicalTileId, pngRasterTile.GetType(), tile, pngRasterTile.Exceptions));
                    tile.HeightDataState = TilePropertyState.Error;

                    // Handle missing elevation from server (404)!
                    // TODO: optimize this search!
                    if (pngRasterTile.ExceptionsAsString.Contains("404"))
                    {
                        ResetToFlatMesh(tile);
                    }
                    Progress--;
                    return;
                }

                tile.SetHeightData(pngRasterTile.Data, _heightModifier, _useRelativeHeight);
                GenerateTerrainMesh(tile);
                // Add a collider this may or may not be worse than the mesh collider that the terrain factory
                // can generate
                // tile.gameObject.AddComponent<BoxCollider>();
                // tile.gameObject.GetComponent<BoxCollider>().size = new Vector3( 99, .1f, 99 );
                // tile.gameObject.GetComponent<BoxCollider>().center = new Vector3( 0 , 0, 0 );
                // Add a tag for generating navmesh
                // tile.gameObject.AddComponent<NavMeshSourceTag>();
                Progress--;
            });
        }
Beispiel #12
0
        public void States()
        {
            var parameters = new Tile.Parameters();

            parameters.Fs = this.fs;
            parameters.Id = new CanonicalTileId(1, 1, 1);

            var tile = new RawPngRasterTile();

            Assert.AreEqual(Tile.State.New, tile.CurrentState);

            tile.Initialize(parameters, () => { });
            Assert.AreEqual(Tile.State.Loading, tile.CurrentState);

            this.fs.WaitForAllRequests();
            Assert.AreEqual(Tile.State.Loaded, tile.CurrentState);

            tile.Cancel();
            Assert.AreEqual(Tile.State.Canceled, tile.CurrentState);
        }
    //tile here should be totally optional and used only not to have keep a dictionary in terrain factory base
    public void FetchTerrain(CanonicalTileId canonicalTileId, string mapid, UnityTile tile = null)
    {
        var pngRasterTile = new RawPngRasterTile();

        pngRasterTile.Initialize(_fileSource, canonicalTileId, mapid, () =>
        {
            if (tile.CanonicalTileId != pngRasterTile.Id)
            {
                //this means tile object is recycled and reused. Returned data doesn't belong to this tile but probably the previous one. So we're trashing it.
                return;
            }

            if (pngRasterTile.HasError)
            {
                FetchingError(tile, pngRasterTile, new TileErrorEventArgs(canonicalTileId, pngRasterTile.GetType(), null, pngRasterTile.Exceptions));
            }
            else
            {
                DataRecieved(tile, pngRasterTile);
            }
        });
    }
 protected override void OnInitialized(Tile.Parameters parameters)
 {
     _elevation.Initialize(parameters, HandleTileLoaded);
 }