private void Update()
    {
        if (undo)
        {
            undo = false;

            var editManager = GetComponent <VoxelEditManagerContainer>().Instance;
            if (editManager != null)
            {
                editManager.Undo();
            }
        }

        if (redo)
        {
            redo = false;

            var editManager = GetComponent <VoxelEditManagerContainer>().Instance;
            if (editManager != null)
            {
                editManager.Redo();
            }
        }

        if (!lockSelection)
        {
            Camera camera = Camera.current;
            if (camera != null)
            {
                Vector3 relPos = camera.transform.position - transform.position;
                Vector3 relDir = Quaternion.Inverse(transform.rotation) * camera.transform.forward.normalized;

                //if (field.RayCast(relPos, relDir, 16, out TestVoxelField.RayCastResult result))
                if (gameObject.GetComponent <VoxelWorldContainer>().Instance.RayCast(relPos, relDir, 64, out VoxelWorld <MortonIndexer> .RayCastResult result))
                {
                    selectedCell = new Vector3Int(Mathf.FloorToInt(result.pos.x), Mathf.FloorToInt(result.pos.y), Mathf.FloorToInt(result.pos.z));
                }
                else
                {
                    selectedCell = null;
                }

                if (selectedCell != null)
                {
                    lockedSelection = selectedCell.Value;
                }
                else
                {
                    lockedSelection = Vector3Int.zero;
                }
            }
            else
            {
                selectedCell = null;
            }
        }
        else
        {
            selectedCell = lockedSelection;
        }

        if (selectedCell != null && prevSelectedCell != selectedCell)
        {
            //field.FillCell(selectedCell.Value.x, selectedCell.Value.y, selectedCell.Value.z, 0, gizmoCellMaterials, gizmoCellIntersections, gizmoCellNormals);
            var sculpture = gameObject.GetComponent <VoxelWorldContainer>().Instance;
            VoxelChunk <MortonIndexer> chunk = sculpture.GetChunk(ChunkPos.FromVoxel(selectedCell.Value, sculpture.ChunkSize));
            if (chunk != null)
            {
                chunk.FillCell(
                    ((selectedCell.Value.x % sculpture.ChunkSize) + sculpture.ChunkSize) % sculpture.ChunkSize,
                    ((selectedCell.Value.y % sculpture.ChunkSize) + sculpture.ChunkSize) % sculpture.ChunkSize,
                    ((selectedCell.Value.z % sculpture.ChunkSize) + sculpture.ChunkSize) % sculpture.ChunkSize,
                    0, gizmoCellMaterials, gizmoCellIntersections, gizmoCellNormals);

                gizmoCell = new RawArrayVoxelCell(0, (Vector3)selectedCell.Value, gizmoCellMaterials, gizmoCellIntersections, gizmoCellNormals);

                NativeMemoryCache memoryCache = new NativeMemoryCache(Allocator.Persistent);

                var polygonizer = new CMSVoxelPolygonizer <RawArrayVoxelCell, CMSStandardProperties, SvdQefSolver <RawArrayVoxelCell>, IntersectionSharpFeatureSolver <RawArrayVoxelCell> >(new CMSStandardProperties(), new SvdQefSolver <RawArrayVoxelCell>(), new IntersectionSharpFeatureSolver <RawArrayVoxelCell>(), memoryCache);

                var components        = new NativeList <VoxelMeshComponent>(Allocator.Persistent);
                var componentIndices  = new NativeList <PackedIndex>(Allocator.Persistent);
                var componentVertices = new NativeList <VoxelMeshComponentVertex>(Allocator.Persistent);

                polygonizer.Polygonize(gizmoCell, components, componentIndices, componentVertices);

                gizmoComponents = new List <VoxelMeshComponent>(components.Length);
                for (int i = 0; i < components.Length; i++)
                {
                    gizmoComponents.Add(components[i]);
                }

                gizmoComponentIndices = new List <PackedIndex>(componentIndices.Length);
                for (int i = 0; i < componentIndices.Length; i++)
                {
                    gizmoComponentIndices.Add(componentIndices[i]);
                }

                gizmoComponentVertices = new List <VoxelMeshComponentVertex>(componentVertices.Length);
                for (int i = 0; i < componentVertices.Length; i++)
                {
                    gizmoComponentVertices.Add(componentVertices[i]);
                }

                memoryCache.Dispose();
                components.Dispose();
                componentIndices.Dispose();
                componentVertices.Dispose();

                gizmoPosition  = selectedCell.Value;
                gizmoTransform = Matrix4x4.TRS(transform.position, transform.rotation, transform.lossyScale);
            }
        }

        prevSelectedCell = selectedCell;

        float brushSize = 2.1f + 8;

        if (placeSdf)
        {
            placeSdf   = false;
            regenerate = true;

            var editManager = GetComponent <VoxelEditManagerContainer>().Instance;

            switch (brushType)
            {
            case BrushType.Sphere:
                gameObject.GetComponent <VoxelWorldContainer>().Instance.ApplySdf(new Vector3(gizmoPosition.x, gizmoPosition.y, gizmoPosition.z), Quaternion.Euler(sdfRotation), new SphereSDF(brushSize), MaterialColors.ToInteger(materialRed, materialGreen, materialBlue, materialTexture), replaceSdfMaterial, editManager.Consumer());
                break;

            case BrushType.Box:
                gameObject.GetComponent <VoxelWorldContainer>().Instance.ApplySdf(new Vector3(gizmoPosition.x, gizmoPosition.y, gizmoPosition.z), Quaternion.Euler(sdfRotation), new BoxSDF(brushSize), MaterialColors.ToInteger(materialRed, materialGreen, materialBlue, materialTexture), replaceSdfMaterial, editManager.Consumer());
                break;

            case BrushType.Cylinder:
                gameObject.GetComponent <VoxelWorldContainer>().Instance.ApplySdf(new Vector3(gizmoPosition.x, gizmoPosition.y, gizmoPosition.z), Quaternion.Euler(sdfRotation), new CylinderSDF(brushSize, brushSize), MaterialColors.ToInteger(materialRed, materialGreen, materialBlue, materialTexture), replaceSdfMaterial, editManager.Consumer());
                break;

            case BrushType.Pyramid:
                gameObject.GetComponent <VoxelWorldContainer>().Instance.ApplySdf(new Vector3(gizmoPosition.x, gizmoPosition.y - brushSize / 2, gizmoPosition.z), Quaternion.Euler(sdfRotation), new PyramidSDF(brushSize * 2, brushSize * 2), MaterialColors.ToInteger(materialRed, materialGreen, materialBlue, materialTexture), replaceSdfMaterial, editManager.Consumer());
                break;

            case BrushType.Mesh:
                var mesh      = voxelizeMesh.mesh;
                var triangles = mesh.triangles;
                var vertices  = mesh.vertices;
                var normals   = mesh.normals;

                var inVertices = new NativeArray <float3>(triangles.Length, Allocator.TempJob);
                var inNormals  = new NativeArray <float3>(triangles.Length, Allocator.TempJob);

                for (int l = triangles.Length, i = 0; i < l; i += 3)
                {
                    inVertices[i]     = vertices[triangles[i]];
                    inVertices[i + 1] = vertices[triangles[i + 1]];
                    inVertices[i + 2] = vertices[triangles[i + 2]];

                    inNormals[i]     = normals[triangles[i]];
                    inNormals[i + 1] = normals[triangles[i + 1]];
                    inNormals[i + 2] = normals[triangles[i + 2]];
                }

                int voxelizerSize = 64;
                var outVoxels     = new NativeArray3D <Voxel.Voxel, MortonIndexer>(new MortonIndexer(voxelizerSize, voxelizerSize, voxelizerSize), voxelizerSize, voxelizerSize, voxelizerSize, Allocator.TempJob);

                var voxelizationProperties = smoothVoxelizerNormals ? Voxelizer.VoxelizationProperties.SMOOTH : Voxelizer.VoxelizationProperties.FLAT;

                var watch = new System.Diagnostics.Stopwatch();
                watch.Start();

                using (var job = Voxelizer.Voxelize(inVertices, inNormals, outVoxels, MaterialColors.ToInteger(materialRed, materialGreen, materialBlue, materialTexture), voxelizationProperties))
                {
                    job.Handle.Complete();
                }

                watch.Stop();
                Debug.Log("Voxelized mesh: " + watch.ElapsedMilliseconds + "ms");
                watch.Reset();
                watch.Start();

                //TODO Make voxelizer also undoable?
                gameObject.GetComponent <VoxelWorldContainer>().Instance.ApplyGrid((int)gizmoPosition.x, (int)gizmoPosition.y, (int)gizmoPosition.z, outVoxels, true, false, null);

                watch.Stop();
                Debug.Log("Applied to grid: " + watch.ElapsedMilliseconds + "ms");

                inVertices.Dispose();
                inNormals.Dispose();
                outVoxels.Dispose();

                break;

            case BrushType.Custom:
                using (var sdf = customBrush.Instance.CreateSdf(Allocator.TempJob))
                {
                    gameObject.GetComponent <VoxelWorldContainer>().Instance.ApplySdf(new Vector3(gizmoPosition.x, gizmoPosition.y, gizmoPosition.z), Quaternion.Euler(sdfRotation), sdf, MaterialColors.ToInteger(materialRed, materialGreen, materialBlue, materialTexture), replaceSdfMaterial, editManager.Consumer());
                }
                break;
            }
        }

        if (gizmoPosition != null)
        {
            switch (brushType)
            {
            case BrushType.Sphere:
                gameObject.GetComponent <SdfShapeRenderHandler>().Render(new Vector3(gizmoPosition.x, gizmoPosition.y, gizmoPosition.z), Quaternion.Euler(sdfRotation), new SphereSDF(brushSize));
                break;

            case BrushType.Box:
                gameObject.GetComponent <SdfShapeRenderHandler>().Render(new Vector3(gizmoPosition.x, gizmoPosition.y, gizmoPosition.z), Quaternion.Euler(sdfRotation), new BoxSDF(brushSize));
                break;

            case BrushType.Cylinder:
                gameObject.GetComponent <SdfShapeRenderHandler>().Render(new Vector3(gizmoPosition.x, gizmoPosition.y, gizmoPosition.z), Quaternion.Euler(sdfRotation), new CylinderSDF(brushSize, brushSize));
                break;

            case BrushType.Pyramid:
                gameObject.GetComponent <SdfShapeRenderHandler>().Render(new Vector3(gizmoPosition.x, gizmoPosition.y - brushSize / 2, gizmoPosition.z), Quaternion.Euler(sdfRotation), new PyramidSDF(brushSize * 2, brushSize * 2));
                break;

            case BrushType.Custom:
                Matrix4x4 brushTransform = Matrix4x4.TRS(new Vector3(gizmoPosition.x, gizmoPosition.y, gizmoPosition.z), Quaternion.Euler(sdfRotation), new Vector3(1, 1, 1));
                using (var sdf = customBrush.Instance.CreateSdf(Allocator.TempJob))
                {
                    gameObject.GetComponent <SdfShapeRenderHandler>().Render(brushTransform, sdf);
                }

                Camera camera = Camera.current;
                if (camera != null)
                {
                    selectedPrimitive = -1;

                    var   ray    = camera.transform.forward.normalized;
                    float maxDst = 60.0f;
                    int   steps  = Mathf.CeilToInt(maxDst * 2);
                    for (int i = 0; i < steps && selectedPrimitive < 0; i++)
                    {
                        var pos = camera.transform.position + ray * maxDst / steps * i;

                        int j = 0;
                        foreach (var primitive in customBrush.Instance.Primitives)
                        {
                            var renderSdf = customBrush.Instance.Evaluator.GetRenderSdf(primitive);
                            if (renderSdf != null && renderSdf.Eval(math.mul(math.mul(brushTransform, primitive.invTransform), new float4(pos, 1.0f)).xyz) < 0)
                            {
                                selectedPrimitive = j;
                                break;
                            }
                            j++;
                        }
                    }
                }
                if (selectedPrimitive >= 0 && selectedPrimitive < customBrush.Instance.Primitives.Count)
                {
                    var primitive = customBrush.Instance.Primitives[selectedPrimitive];
                    var renderSdf = customBrush.Instance.Evaluator.GetRenderSdf(primitive);
                    if (renderSdf != null)
                    {
                        gameObject.GetComponent <SdfShapeRenderHandler>().Render(brushTransform * (Matrix4x4)primitive.transform, renderSdf);
                    }
                }
                break;
            }
        }

        if (generateEachFrame || regenerate)
        {
            regenerate = false;
            //GenerateMesh();
        }
    }