Ejemplo n.º 1
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);
        }
        /// <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--;
            });
        }
Ejemplo n.º 3
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);
            });
        }
Ejemplo n.º 4
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--;
            });
        }
Ejemplo n.º 5
0
    //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);
            }
        });
    }
Ejemplo n.º 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);
            }
        }
Ejemplo n.º 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);
        }
Ejemplo n.º 8
0
 private void OnTerrainRecieved(UnityTile tile, RawPngRasterTile pngRasterTile)
 {
     if (tile != null)
     {
         Progress--;
         tile.SetHeightData(pngRasterTile.Data, _elevationOptions.requiredOptions.exaggerationFactor, _elevationOptions.modificationOptions.useRelativeHeight);
         Strategy.RegisterTile(tile);
     }
 }
Ejemplo n.º 9
0
    public void OnNext(RawPngRasterTile tile)
    {
        var id       = tile.Id.ToString();
        var contains = tiles.ContainsKey(id);

        if (tile.CurrentState == Tile.State.Loaded && tile.Error == null && contains)
        {
            tiles[id].SetElevation(tile.Data);
        }
    }
 private void OnDataError(UnityTile tile, RawPngRasterTile rawTile, TileErrorEventArgs e)
 {
     if (tile != null)
     {
         _tilesWaitingResponse.Remove(tile);
         if (tile.HeightDataState != TilePropertyState.Unregistered)
         {
             Strategy.DataErrorOccurred(tile, e);
             tile.HeightDataState = TilePropertyState.Error;
         }
     }
 }
Ejemplo n.º 11
0
        //public override void UpdateTileProperty(UnityTile tile, LayerUpdateArgs updateArgs)
        //{
        //	updateArgs.property.UpdateProperty(tile);

        //	if (updateArgs.property.NeedsForceUpdate())
        //	{
        //		Register(tile);
        //	}

        //	//if (updateArgs.property is TerrainColliderOptions)
        //	//{
        //	//	var existingCollider = tileBundleValue.Collider;
        //	//	if (Properties.colliderOptions.addCollider)
        //	//	{
        //	//		if (existingCollider == null)
        //	//		{
        //	//			tileBundleValue.gameObject.AddComponent<MeshCollider>();
        //	//		}
        //	//	}
        //	//	else
        //	//	{
        //	//		Destroy(tileBundleValue.Collider);
        //	//	}
        //	//}
        //}

        #endregion

        #region DataFetcherEvents
        private void OnTerrainRecieved(UnityTile tile, RawPngRasterTile pngRasterTile)
        {
            if (tile != null)
            {
                _tilesWaitingResponse.Remove(tile);
                if (tile.HeightDataState != TilePropertyState.Unregistered)
                {
                    tile.SetHeightData(pngRasterTile.Data, _elevationOptions.requiredOptions.exaggerationFactor, _elevationOptions.modificationOptions.useRelativeHeight, _elevationOptions.colliderOptions.addCollider, _elevationOptions.requiredOptions.displacement);
                    Strategy.RegisterTile(tile);
                }
            }
        }
Ejemplo n.º 12
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);
        });
    }
Ejemplo n.º 13
0
        public void OnNext(RawPngRasterTile tile)
        {
            if (tile.CurrentState == Tile.State.Loaded && !tile.HasError)
            {
                _elevationTexture = new Texture2D(2, 2);
                _elevationTexture.LoadImage(tile.Data);
                TextureScale.Point(_elevationTexture, _tileWidthInVoxels, _tileWidthInVoxels);

                if (ShouldBuildWorld())
                {
                    BuildVoxelWorld();
                }
            }
        }
Ejemplo n.º 14
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();
            });
        }
Ejemplo n.º 15
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);
        }
Ejemplo n.º 16
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--;
            });
        }
Ejemplo n.º 17
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);
            }
        });
    }
Ejemplo n.º 19
0
 void Awake()
 {
     _elevation  = new RawPngRasterTile();
     _meshFilter = GetComponent <MeshFilter>();
     _vertices   = _meshFilter.mesh.vertices;
 }