void DrawMultipleTiles(int3 startPosition, int3 endPosition, TileObject tile, TileAtlas.TagLayer layer)
        {
            int3 brushSize   = tile.GetSize(layer.rasterSize);
            int3 minPosition = math.min(startPosition, endPosition);
            int3 maxPosition = math.max(startPosition, endPosition);
            int  widthX      = Mathf.FloorToInt((maxPosition.x - minPosition.x) / brushSize.x);
            int  widthZ      = Mathf.FloorToInt((maxPosition.z - minPosition.z) / brushSize.z);

            for (int x = 0; x <= widthX; x++)
            {
                for (int z = 0; z <= widthZ; z++)
                {
                    t.PlaceTile(tile.guid,
                                minPosition + new int3(
                                    x * brushSize.x,
                                    0,
                                    z * brushSize.z),
                                brushRotation,
                                layerIndex, layer, true); // <-- turn to false if performance becomes problematic
                }
            }
            //t.UpdateMultiple(minPosition - new int3(1, 0, 1), maxPosition + new int3(2,0,2), layerIndex);
        }
        void OnSceneGridBrush(Event e, SceneView sceneView, TileAtlas.TagLayer activeLayer)
        {
            if (painting && activeLayer != t.tileSet.defaultLayer)
            {
                int controlId = GUIUtility.GetControlID(FocusType.Passive);
                if (e.control && (e.type == EventType.MouseDown ||
                                  e.type == EventType.MouseDrag ||
                                  e.type == EventType.ScrollWheel))
                {
                    GUIUtility.hotControl = controlId;
                    if (e.type == EventType.ScrollWheel)
                    {
                        if (e.delta.y > 0)
                        {
                            targetHeigth -= 1;
                        }
                        else
                        {
                            targetHeigth += 1;
                        }
                        e.Use();
                        GUIUtility.hotControl = 0;
                    }
                }
                if (e.isMouse)
                {
                    Ray   guiRay = HandleUtility.GUIPointToWorldRay(e.mousePosition);
                    float d;
                    floorPlane.Raycast(guiRay, out d);
                    Vector3 worldPos = guiRay.GetPoint(d);
                    //Debug.Log("worldPos: " + worldPos+"; mousePos: "+tileMousePosition);
                    var newTileMousePosition = t.WorldPositionToTilePosition(worldPos, activeLayer);
                    if (t.TilePositionInFloorSize(newTileMousePosition, activeLayer))
                    {
                        tileMousePosition = newTileMousePosition;
                    }
                }
                //Handles.DrawWireDisc(t.TilePositionToLocalPosition(tileMousePosition, selectedTile.size), Vector3.up, 0.5f * selectedTile.size.x);
                Handles.color = activeLayer.color + Color.gray;
                if (t.GetTile(tileMousePosition, layerIndex) != null)
                {
                    if (e.modifiers == EventModifiers.Control && !tileMousePosition.Equals(lastTileMousePos))
                    {
                        tileMousePosition = StackedPosition(tileMousePosition, layerIndex);
                    }
                    else
                    {
                        Handles.color = new Color(1f, 0.1f, 0f);
                    }
                }
                if (e.type == EventType.MouseUp)
                {
                    stackingHeight = null;
                }
                int3 brushSize = selectedTile ? selectedTile.GetSize(activeLayer.rasterSize) : new int3(1, 1, 1);
                DrawBrushGizmo(tileMousePosition, brushSize, activeLayer);
                if (rectStartTile != null && !rectStartTile.Value.Equals(tileMousePosition)) // rectbrush
                {
                    for (int x = 0; x <= Mathf.Abs(Mathf.FloorToInt((tileMousePosition.x - rectStartTile.Value.x) / brushSize.x)); x++)
                    {
                        for (int z = 0; z <= Mathf.Abs(Mathf.FloorToInt((tileMousePosition.z - rectStartTile.Value.z) / brushSize.z)); z++)
                        {
                            DrawBrushGizmo(rectStartTile.Value
                                           + new int3(
                                               tileMousePosition.x > rectStartTile.Value.x ? x : -x,
                                               0,
                                               tileMousePosition.z > rectStartTile.Value.z ? z : -z), brushSize, activeLayer);
                        }
                    }
                }
                if ((e.type == EventType.MouseDown || e.type == EventType.MouseDrag) && e.button == 0 && (e.modifiers == EventModifiers.None || e.modifiers == EventModifiers.Control))
                {
                    if (!tileMousePosition.Equals(lastTileMousePos) || e.type == EventType.MouseDown)
                    {
                        DrawTiles(sceneView, e, tileMousePosition, true);
                    }
                    else
                    {
                        DrawTiles(sceneView, e, tileMousePosition, false);
                    }
                }
                else if ((e.type == EventType.MouseDown || e.type == EventType.MouseDrag) && e.button == 1 &&
                         (e.modifiers == EventModifiers.None || e.modifiers == EventModifiers.Control))
                {
                    var pos = tileMousePosition;
                    if (e.modifiers == EventModifiers.Control)
                    {
                        int startY = pos.y;
                        pos = StackedPosition(tileMousePosition, layerIndex, false);
                        while (pos.y > startY)
                        {
                            EraseTiles(sceneView, e, tileMousePosition);
                            pos.y -= 1;
                        }
                    }
                    EraseTiles(sceneView, e, tileMousePosition);
                }
                else if (e.type == EventType.MouseDown && (e.button == 0 || e.button == 1) && e.modifiers == EventModifiers.Shift)
                {
                    rectStartTile         = tileMousePosition;
                    GUIUtility.hotControl = 0;
                    e.Use();
                }
                else if (e.type == EventType.MouseDrag && (e.button == 0 || e.button == 1) && rectStartTile != null && e.modifiers == EventModifiers.Shift)
                {
                    GUIUtility.hotControl = 0;
                    e.Use();
                }
                else if ((e.type == EventType.MouseUp) && (e.button == 0 || e.button == 1) && rectStartTile != null)
                {
                    GUIUtility.hotControl = 0;
                    e.Use();
                    if (e.button == 0)
                    {
                        DrawMultipleTiles(rectStartTile.Value, tileMousePosition, selectedTile, activeLayer);
                    }
                    else if (e.button == 1)
                    {
                        EraseMultipleTiles(rectStartTile.Value, tileMousePosition);
                    }
                    rectStartTile = null;
                }
                //else if (e.type == EventType.MouseMove && e.button == 0 && e.modifiers == EventModifiers.Control)
                //{
                //    brushRotation += new Vector3(0, e.delta.x, 0);
                //    GUIUtility.hotControl = 0;
                //    e.Use();
                //    Repaint();
                //}

                lastTileMousePos = tileMousePosition;
            }
        }