Example #1
0
        void BuildVoxelWorld()
        {
            var baseHeight = (int)MapboxConvenience.GetRelativeHeightFromColor((_elevationTexture.GetPixel(_elevationTexture.width / 2, _elevationTexture.height / 2)), 1 / _elevationMultiplier);

            for (int x = 0; x < _rasterTexture.width; x++)
            {
                for (int z = 0; z < _rasterTexture.height; z++)
                {
                    var height = (int)MapboxConvenience.GetRelativeHeightFromColor(_elevationTexture.GetPixel(x, z), 1 / _elevationMultiplier) - baseHeight;

                    height = (int)(height * _tileScale);
                    var startHeight = height - _voxelDepthPadding - 1;
                    var color       = _rasterTexture.GetPixel(x, z);

                    for (int y = startHeight; y < height; y++)
                    {
                        _voxels.Add(new VoxelData()
                        {
                            Position = new Vector3(x, y, z), Prefab = _voxelFetcher.GetVoxelFromColor(color)
                        });
                    }
                }
            }

            // TODO: decouple me!
            _player.transform.position = new Vector3(_tileWidthInVoxels * .5f, 2f, _tileWidthInVoxels * .5f);
            StartCoroutine(BuildRoutine());
            var cc = _player.GetComponent <CharacterController>();

            if (cc)
            {
                cc.enabled = true;
            }
        }
Example #2
0
        void FetchWorldData(GeoCoordinate coordinates)
        {
            _tileScale = _tileWidthInVoxels / MapboxConvenience.GetTileScaleInMeters((float)coordinates.Latitude, _zoom);
            var bounds = new GeoCoordinateBounds();

            bounds.Center = coordinates;
            _raster.SetGeoCoordinateBoundsZoom(bounds, _zoom);
            _elevation.SetGeoCoordinateBoundsZoom(bounds, _zoom);
        }
        void ExtrudeHeight()
        {
            // FIXME: what does this number represent?
            var relativeScale = TileScaleInMeters / 256 * MapboxConvenience.Instance.TileScale / transform.localScale.x;

            var elevationTexture = new Texture2D(0, 0);

            elevationTexture.LoadImage(_elevation.Data);

            // HACK: UV's are "flipped" on Unity plane. Undo that flip.
            var flip = Quaternion.Euler(0, 180, 0);

            var pixelsPerMeter = 256f / MapboxConvenience.Instance.TileScale;
            var newVertices    = new List <Vector3>();

            // TODO: optimize with GetPixels()
            foreach (var vert in _vertices)
            {
                var localVert = flip * vert;

                // Account for local scale.
                localVert *= transform.localScale.x;

                var x       = (int)Mathf.Clamp((localVert.x * pixelsPerMeter + 128), 0, 255);
                var y       = (int)Mathf.Clamp((localVert.z * pixelsPerMeter + 128), 0, 255);
                var color   = elevationTexture.GetPixel(x, y);
                var newVert = vert;

                newVert.y = MapboxConvenience.GetRelativeHeightFromColor(color, relativeScale);
                newVertices.Add(newVert);
            }

            _meshFilter.mesh.SetVertices(newVertices);

            var collider = GetComponent <MeshCollider>();

            if (collider)
            {
                collider.sharedMesh = _meshFilter.mesh;
            }

            // This fixes rendering bugs if vertices are too far offset from their original locations.
            _meshFilter.mesh.RecalculateBounds();
        }
Example #4
0
        void Build()
        {
            foreach (var tile in _instantiatedTiles)
            {
                Destroy(tile);
            }

            _instantiatedTiles.Clear();

            _unwrappedTileId = MapboxConvenience.LatitudeLongitudeToTileId(_latitude, _longitude, _zoom);

            _parameters = new Tile.Parameters
            {
                Fs = MapboxConvenience.Instance.FileSource,
                Id = new CanonicalTileId(_zoom, _unwrappedTileId.X, _unwrappedTileId.Y)
            };

            Initialize(_parameters);
        }
Example #5
0
 public void Initialize(Tile.Parameters parameters)
 {
     _center            = MapboxConvenience.TileIdToLatitudeLongitude(parameters.Id.X, parameters.Id.Y, parameters.Id.Z);
     _tileScaleInMeters = MapboxConvenience.GetTileScaleInMeters(_center.x, parameters.Id.Z);
     OnInitialized(parameters);
 }