public void LoadTileImageAsync(MapTileVisual tile, int x, int y, int zoom) { Task.Run(() => { ImageSource image = TileService.GetTileImage(zoom, x, y); ImageBrush imageBrush = new ImageBrush(); DiffuseMaterial material = new DiffuseMaterial(); material.Brush = imageBrush; imageBrush.ImageSource = image; imageBrush.Freeze(); material.Freeze(); //var foo = ImageWpfToGDI(image); // We've already set the Source to null before calling this method. if (image != null && imageBrush != null) { tile.Dispatcher.Invoke(() => { tile.Material = material; }); } }); }
public void InitGrid() { int midNum = (int)Math.Ceiling(GridSize / 2d); MapCenter = new Point3D(0d, 0d, 0d); Random ran = new Random(); currentTileX = 0; currentTileY = 0; OSMWorldScale scale = new OSMWorldScale(); double mapCenterNormalizedX = scale.NormalizeX(TileService.xTileOffset); double mapCenterNormalizedY = scale.NormalizeY(TileService.yTileOffset); double maxTiles = Math.Pow(2, mapZoom); double tileNumX = mapCenterNormalizedX * maxTiles; double tileNumY = mapCenterNormalizedY * maxTiles; //X and y are flipped double fracX = tileNumX - Math.Floor(tileNumX); double fracY = tileNumY - Math.Floor(tileNumY); double posFracX = fracX; double posFracY = fracY; if (posFracX <= 0.5) { posFracX += 1; } if (posFracY >= 0.5) { posFracY -= 1; } if (mapZoom == 15 || mapZoom == 12) { posFracY -= 1; } double tileTranslateX = scale.GetTileXTranslateFix(mapCenterNormalizedX, mapZoom, TileSize); //TileSize * fracX; double tileTranslateY = scale.GetTileYTranslateFix(mapCenterNormalizedY, mapZoom, TileSize); //TileSize * fracY; Debug.WriteLine($"TranslateFixes: {tileTranslateX} / {tileTranslateY} TileSize {TileSize}"); Debug.WriteLine($"TranslateFixesWOLessTHanFixes: {TileSize * fracX} / {TileSize * fracY}"); Debug.WriteLine($"TranslateFixesPositive: {posFracX} / {posFracY}"); //double fooTest2 = //double fooTest2 = scale.GetTileXTranslateFix(mapCenterNormalizedX, mapZoom, TileSize); BoundingBoxWireFrameVisual3D box = new BoundingBoxWireFrameVisual3D(); box.Color = Colors.Yellow; double bSize = 1 * TileSize / 32; box.Thickness = 1; box.BoundingBox = new Rect3D((tileTranslateX) - bSize / 2, tileTranslateY - bSize / 2, 0, bSize, bSize, bSize); this.Children.Add(box); BoundingBoxWireFrameVisual3D box2 = new BoundingBoxWireFrameVisual3D(); box2.Color = Colors.LimeGreen; double b2Size = 1 * TileSize / 32; box2.Thickness = 1; box2.BoundingBox = new Rect3D((posFracX * TileSize * -1) - b2Size / 2, (posFracY * TileSize) - b2Size / 2, 0, b2Size, b2Size, b2Size); this.Children.Add(box2); for (int x = 0; x < GridSize; x++) { for (int y = 0; y < GridSize; y++) { int gridPosX = ((x + 1) - midNum); int gridPosY = ((y + 1) - midNum); MapTileVisual tile = new MapTileVisual(); tile.Fill = new SolidColorBrush(Color.FromArgb(255, (byte)ran.Next(30, 255), (byte)ran.Next(32, 255), (byte)ran.Next(28, 255))); TranslateTransform3D position = new TranslateTransform3D(); position.OffsetX = (gridPosX * TileSize) - TileSize / 2; position.OffsetY = (gridPosY * TileSize) - TileSize / 2; if (TranslateOnZoom) { position.OffsetX -= tileTranslateX; position.OffsetY -= tileTranslateY; } tile.Transform = position; tile.Width = TileSize; tile.Length = TileSize; tile.TileX = gridPosX * -1; tile.TileY = gridPosY; tile.TileZoom = MapZoomLevel; this.Children.Add(tile); Tiles.Add(tile); if (TileGenerator != null) { //log.DebugFormat("Requesting Image for x/y/zoom {0} / {1} / {2}", tile.TileX, tile.TileY, mapZoom); TileGenerator.LoadTileImageAsync(tile, tile.TileX, tile.TileY, MapZoomLevel); } else { Aegir.Util.DebugUtil.LogWithLocation("Could not load image, TileGeneratorWas Null"); } tile.UpdateDebugLabels(); } } }