Example #1
0
        bool PerformGridAction(MyVoxelMap voxelMap, MyvoxelImportAction action, int gridX, int gridZ, ref MyMwcVector3Int minChanged, ref MyMwcVector3Int maxChanged, MyMwcVoxelMaterialsEnum? voxelMaterial, ref bool changed)
        {
            MinerWars.AppCode.Game.Render.MyRender.GetRenderProfiler().StartProfilingBlock("PerformGridAction");

            MinerWars.AppCode.Game.Render.MyRender.GetRenderProfiler().StartProfilingBlock("get content sum");
            int[, ,] voxelContentSum = GetVoxelContentSum(voxelMap, gridX, gridZ);
            if (voxelContentSum == null)
            {
                MinerWars.AppCode.Game.Render.MyRender.GetRenderProfiler().EndProfilingBlock();

                MinerWars.AppCode.Game.Render.MyRender.GetRenderProfiler().EndProfilingBlock();

                return false;
            }

            MinerWars.AppCode.Game.Render.MyRender.GetRenderProfiler().StartNextBlock("loop");
            bool anyContentChanged = false;

            //  Transform grid values to voxels
            int voxelStartX = gridX * VOXELS_IN_GRID_IN_ONE_DIRECTION;
            int voxelStartZ = gridZ * VOXELS_IN_GRID_IN_ONE_DIRECTION;


            switch (action)
            {
                case MyvoxelImportAction.AddVoxels:
                    MinerWars.AppCode.Game.Render.MyRender.GetRenderProfiler().StartProfilingBlock("MyvoxelImportAction.AddVoxels");
                    for (int x = 0; x < VOXELS_IN_GRID_IN_ONE_DIRECTION; x++) for (int y = 0; y < voxelMap.Size.Y; y++) for (int z = 0; z < VOXELS_IN_GRID_IN_ONE_DIRECTION; z++)
                    {
                        MyMwcVector3Int voxelCoord = new MyMwcVector3Int(voxelStartX + x, y, voxelStartZ + z);

                        byte newContent =  (byte)(voxelContentSum[x, y, z] * MyVoxelConstants.VOXEL_CONTENT_FULL / GRID_POINTS_IN_ONE_VOXEL_TOTAL);
                        if (newContent == 0) continue;

                        bool c = PerformAddVoxels(voxelMap, voxelCoord, newContent, ref changed);
                        c |= PerformChangeMaterialVoxels(voxelMap, voxelCoord, voxelMaterial, ref changed);
                        if (c)
                        {
                            anyContentChanged = true;
                            if (voxelCoord.X < minChanged.X) minChanged.X = voxelCoord.X;
                            if (voxelCoord.Y < minChanged.Y) minChanged.Y = voxelCoord.Y;
                            if (voxelCoord.Z < minChanged.Z) minChanged.Z = voxelCoord.Z;
                            if (voxelCoord.X > maxChanged.X) maxChanged.X = voxelCoord.X;
                            if (voxelCoord.Y > maxChanged.Y) maxChanged.Y = voxelCoord.Y;
                            if (voxelCoord.Z > maxChanged.Z) maxChanged.Z = voxelCoord.Z;
                        }
                    }
                    MinerWars.AppCode.Game.Render.MyRender.GetRenderProfiler().EndProfilingBlock();
                    break;

                case MyvoxelImportAction.RemoveVoxels:
                    for (int x = 0; x < VOXELS_IN_GRID_IN_ONE_DIRECTION; x++) for (int y = 0; y < voxelMap.Size.Y; y++) for (int z = 0; z < VOXELS_IN_GRID_IN_ONE_DIRECTION; z++)
                    {
                        MyMwcVector3Int voxelCoord = new MyMwcVector3Int(voxelStartX + x, y, voxelStartZ + z);

                        byte newContent = (byte)(voxelContentSum[x, y, z] * MyVoxelConstants.VOXEL_CONTENT_FULL / GRID_POINTS_IN_ONE_VOXEL_TOTAL);
                        if (newContent == 0) continue;

                        if (PerformRemoveVoxels(voxelMap, voxelCoord, newContent, ref changed))
                        {
                            anyContentChanged = true;
                            if (voxelCoord.X < minChanged.X) minChanged.X = voxelCoord.X;
                            if (voxelCoord.Y < minChanged.Y) minChanged.Y = voxelCoord.Y;
                            if (voxelCoord.Z < minChanged.Z) minChanged.Z = voxelCoord.Z;
                            if (voxelCoord.X > maxChanged.X) maxChanged.X = voxelCoord.X;
                            if (voxelCoord.Y > maxChanged.Y) maxChanged.Y = voxelCoord.Y;
                            if (voxelCoord.Z > maxChanged.Z) maxChanged.Z = voxelCoord.Z;
                        }
                    }
                    break;

                case MyvoxelImportAction.ChangeMaterial:
                    for (int x = 0; x < VOXELS_IN_GRID_IN_ONE_DIRECTION; x++) for (int y = 0; y < voxelMap.Size.Y; y++) for (int z = 0; z < VOXELS_IN_GRID_IN_ONE_DIRECTION; z++)
                    {
                        MyMwcVector3Int voxelCoord = new MyMwcVector3Int(voxelStartX + x, y, voxelStartZ + z);

                        byte newContent = (byte)(voxelContentSum[x, y, z] * MyVoxelConstants.VOXEL_CONTENT_FULL / GRID_POINTS_IN_ONE_VOXEL_TOTAL);
                        if (newContent == 0) continue;

                        if (PerformChangeMaterialVoxels(voxelMap, voxelCoord, voxelMaterial, ref changed))
                        {
                            anyContentChanged = true;
                            if (voxelCoord.X < minChanged.X) minChanged.X = voxelCoord.X;
                            if (voxelCoord.Y < minChanged.Y) minChanged.Y = voxelCoord.Y;
                            if (voxelCoord.Z < minChanged.Z) minChanged.Z = voxelCoord.Z;
                            if (voxelCoord.X > maxChanged.X) maxChanged.X = voxelCoord.X;
                            if (voxelCoord.Y > maxChanged.Y) maxChanged.Y = voxelCoord.Y;
                            if (voxelCoord.Z > maxChanged.Z) maxChanged.Z = voxelCoord.Z;
                        }
                    }
                    break;

                case MyvoxelImportAction.SoftenVoxels:
                    for (int x = 0; x < VOXELS_IN_GRID_IN_ONE_DIRECTION; x++) for (int y = 0; y < voxelMap.Size.Y; y++) for (int z = 0; z < VOXELS_IN_GRID_IN_ONE_DIRECTION; z++)
                    {
                        MyMwcVector3Int voxelCoord = new MyMwcVector3Int(voxelStartX + x, y, voxelStartZ + z);

                        byte newContent = (byte)(voxelContentSum[x, y, z] * MyVoxelConstants.VOXEL_CONTENT_FULL / GRID_POINTS_IN_ONE_VOXEL_TOTAL);
                        if (newContent == 0) continue;

                        if (PerformSoftenVoxels(voxelMap, voxelCoord, ref changed))
                        {
                            anyContentChanged = true;
                            if (voxelCoord.X < minChanged.X) minChanged.X = voxelCoord.X;
                            if (voxelCoord.Y < minChanged.Y) minChanged.Y = voxelCoord.Y;
                            if (voxelCoord.Z < minChanged.Z) minChanged.Z = voxelCoord.Z;
                            if (voxelCoord.X > maxChanged.X) maxChanged.X = voxelCoord.X;
                            if (voxelCoord.Y > maxChanged.Y) maxChanged.Y = voxelCoord.Y;
                            if (voxelCoord.Z > maxChanged.Z) maxChanged.Z = voxelCoord.Z;
                        }
                    }
                    break;

                case MyvoxelImportAction.WrinkleVoxels:
                    for (int x = 0; x < VOXELS_IN_GRID_IN_ONE_DIRECTION; x++) for (int y = 0; y < voxelMap.Size.Y; y++) for (int z = 0; z < VOXELS_IN_GRID_IN_ONE_DIRECTION; z++)
                    {
                        MyMwcVector3Int voxelCoord = new MyMwcVector3Int(voxelStartX + x, y, voxelStartZ + z);

                        byte newContent = (byte)(voxelContentSum[x, y, z] * MyVoxelConstants.VOXEL_CONTENT_FULL / GRID_POINTS_IN_ONE_VOXEL_TOTAL);

                        if (newContent == 0) continue;

                        if (PerformWrinkleVoxels(voxelMap, voxelCoord, ref changed))
                        {
                            anyContentChanged = true;
                            if (voxelCoord.X < minChanged.X) minChanged.X = voxelCoord.X;
                            if (voxelCoord.Y < minChanged.Y) minChanged.Y = voxelCoord.Y;
                            if (voxelCoord.Z < minChanged.Z) minChanged.Z = voxelCoord.Z;
                            if (voxelCoord.X > maxChanged.X) maxChanged.X = voxelCoord.X;
                            if (voxelCoord.Y > maxChanged.Y) maxChanged.Y = voxelCoord.Y;
                            if (voxelCoord.Z > maxChanged.Z) maxChanged.Z = voxelCoord.Z;
                        }
                    }
                    break;
            }

            MinerWars.AppCode.Game.Render.MyRender.GetRenderProfiler().EndProfilingBlock();

            MinerWars.AppCode.Game.Render.MyRender.GetRenderProfiler().EndProfilingBlock();
            return anyContentChanged;
        }
Example #2
0
        void PerformAction(MyVoxelMap voxelMap, MyvoxelImportAction action, MyMwcVoxelMaterialsEnum? voxelMaterial, ref bool changed)
        {
            MinerWars.AppCode.Game.Render.MyRender.GetRenderProfiler().StartProfilingBlock("PerformAction");

            //  Voxel map size must be multiple of grid size. Or, whole grid must fit exactly into X times into voxel map, without crossing border!
            MyCommonDebugUtils.AssertRelease((voxelMap.Size.X % VOXELS_IN_GRID_IN_ONE_DIRECTION) == 0);

            //  Voxel map size must be multiple of grid size. Or, whole grid must fit exactly into X times into voxel map, without crossing border!
            MyCommonDebugUtils.AssertRelease((voxelMap.Size.Z % VOXELS_IN_GRID_IN_ONE_DIRECTION) == 0);

            int gridsCountX = voxelMap.Size.X / VOXELS_IN_GRID_IN_ONE_DIRECTION;
            int gridsCountZ = voxelMap.Size.Z / VOXELS_IN_GRID_IN_ONE_DIRECTION;

            //  Space between two grid points in metres
            m_gridPointsSize = MyVoxelConstants.VOXEL_SIZE_IN_METRES / (float)GRID_POINTS_IN_ONE_VOXEL_IN_ONE_DIRECTION;
            m_gridPointsSizeHalf = m_gridPointsSize / 2.0f;

            //  Get min corner of the box
            MyMwcVector3Int minCorner = voxelMap.GetVoxelCoordinateFromMeters(m_minCoord);

            //  Get max corner of the box
            MyMwcVector3Int maxCorner = voxelMap.GetVoxelCoordinateFromMeters(m_maxCoord);

            voxelMap.FixVoxelCoord(ref minCorner);
            voxelMap.FixVoxelCoord(ref maxCorner);

            //  We are tracking which voxels were changed, so we can invalidate only needed cells in the cache
            MyMwcVector3Int minChanged = maxCorner;
            MyMwcVector3Int maxChanged = minCorner;

            bool contentChanged = false;

            for (int gridX = 0; gridX < gridsCountX; gridX++)
            {
                for (int gridZ = 0; gridZ < gridsCountZ; gridZ++)
                {
                    if (PerformGridAction(voxelMap, action, gridX, gridZ, ref minChanged, ref maxChanged, voxelMaterial, ref changed))
                    {
                        contentChanged = true;
                    }
                }
            }

            if (contentChanged)
            {
                //  Extend borders for cleaning, so it's one pixel on both sides
                minChanged.X -= 1;
                minChanged.Y -= 1;
                minChanged.Z -= 1;
                maxChanged.X += 1;
                maxChanged.Y += 1;
                maxChanged.Z += 1;
                voxelMap.FixVoxelCoord(ref minChanged);
                voxelMap.FixVoxelCoord(ref maxChanged);

                voxelMap.InvalidateCache(minChanged, maxChanged);
            }

            MinerWars.AppCode.Game.Render.MyRender.GetRenderProfiler().EndProfilingBlock();
        }
Example #3
0
        MyVoxelImport(MyVoxelMap voxelMap, MyModel model, MyvoxelImportAction importAction, Matrix modelWorld, float modelScale, MyMwcVoxelMaterialsEnum? voxelMaterial, ref bool changed)
        {
            // Load model, get triangles transformed to model's world matrix
            LoadModel(model, modelWorld, modelScale);

            RescaleModel(voxelMap, MyVoxelImportOptions.KeepScale);

            //  Fill lookup array with triangles located at specified voxel positions. Array is 2D.
            FillTrianglesLookup(voxelMap);

            // Performs action
           //PerformAction(voxelMap, importAction, voxelMaterial, ref changed);
            Import(voxelMap);
        }
Example #4
0
        MyVoxelImport(MyVoxelMap voxelMap, Vector3[] vertexes, MyTriangleVertexIndices[] triangles, MyvoxelImportAction importAction, Matrix modelWorld, MyMwcVoxelMaterialsEnum? voxelMaterial, ref bool changed)
        {
            MinerWars.AppCode.Game.Render.MyRender.GetRenderProfiler().StartProfilingBlock("voxel import");

            // Load model, get triangles transformed to model's world matrix
            MinerWars.AppCode.Game.Render.MyRender.GetRenderProfiler().StartProfilingBlock("load model");
            LoadModel(vertexes, triangles, modelWorld);

            //  Fill lookup array with triangles located at specified voxel positions. Array is 2D.
            MinerWars.AppCode.Game.Render.MyRender.GetRenderProfiler().StartNextBlock("fill lookup");
            FillTrianglesLookup(voxelMap);

            // Performs action
            MinerWars.AppCode.Game.Render.MyRender.GetRenderProfiler().StartNextBlock("perform action");
            PerformAction(voxelMap, importAction, voxelMaterial, ref changed);

            MinerWars.AppCode.Game.Render.MyRender.GetRenderProfiler().EndProfilingBlock();
            MinerWars.AppCode.Game.Render.MyRender.GetRenderProfiler().EndProfilingBlock();
        }
Example #5
0
 public static void Run(MyVoxelMap voxelMap, Vector3[] vertexes, MyTriangleVertexIndices[] triangles, MyvoxelImportAction importAction, Matrix modelWorld, MyMwcVoxelMaterialsEnum? voxelMaterial, ref bool changed)
 {
     MyVoxelImport voxelImport = new MyVoxelImport(voxelMap, vertexes, triangles, importAction, modelWorld, voxelMaterial, ref changed);
 }
Example #6
0
 public static void Run(MyVoxelMap voxelMap, MyModel model, MyvoxelImportAction importAction, Matrix modelWorld, float modelScale, MyMwcVoxelMaterialsEnum? voxelMaterial, ref bool changed)
 {
     MyVoxelImport voxelImport = new MyVoxelImport(voxelMap, model, importAction, modelWorld, modelScale, voxelMaterial, ref changed);
 }