/// <summary> /// Builds an asteroid Voxel. Voxel detail will be completed by function callbacks. /// This allows for muti-threading, and generating content via algorithims. /// </summary> /// <param name="multiThread"></param> /// <param name="size"></param> /// <param name="material"></param> /// <param name="faceMaterial"></param> /// <param name="func"></param> /// <returns></returns> public static MyVoxelMap BuildAsteroid(bool multiThread, Vector3I size, string material, string faceMaterial, Action <MyVoxelBuilderArgs> func) { var voxelMap = new MyVoxelMap(); var actualSize = new Vector3I(size.X.RoundUpToNearest(64), size.Y.RoundUpToNearest(64), size.Z.RoundUpToNearest(64)); voxelMap.Create(actualSize, material); //voxelMap.Init(VRageMath.Vector3D.Zero, actualSize, material); ProcessAsteroid(voxelMap, multiThread, material, func, false); //if (faceMaterial != null) //{ // voxelMap.ForceVoxelFaceMaterial(faceMaterial); //} return(voxelMap); }
/// <summary> /// Builds an asteroid Voxel. Voxel detail will be completed by function callbacks. /// This allows for muti-threading, and generating content via algorithims. /// </summary> public static MyVoxelMap BuildAsteroid(bool multiThread, Vector3I size, byte materialIndex, byte?faceMaterialIndex, Action <MyVoxelBuilderArgs> func) { var voxelMap = new MyVoxelMap(); var buildSize = CalcRequiredSize(size); voxelMap.Create(buildSize, materialIndex); ProcessAsteroid(voxelMap, multiThread, materialIndex, func, true); // This should no longer be required. //if (faceMaterialIndex != null) //{ // voxelMap.ForceVoxelFaceMaterial(faceMaterialIndex.Value); //} return(voxelMap); }
public void RotateAsteroid(VRageMath.Quaternion quaternion) { var sourceFile = SourceVoxelFilepath ?? VoxelFilepath; var asteroid = new MyVoxelMap(); asteroid.Load(sourceFile); var newAsteroid = new MyVoxelMap(); var transSize = Vector3I.Transform(asteroid.Size, quaternion); var newSize = Vector3I.Abs(transSize); newAsteroid.Create(newSize, SpaceEngineersCore.Resources.GetDefaultMaterialName()); Vector3I coords; for (coords.Z = 0; coords.Z < asteroid.Size.Z; coords.Z++) { for (coords.Y = 0; coords.Y < asteroid.Size.Y; coords.Y++) { for (coords.X = 0; coords.X < asteroid.Size.X; coords.X++) { byte volume = 0xff; string cellMaterial; asteroid.GetVoxelMaterialContent(ref coords, out cellMaterial, out volume); var newCoord = Vector3I.Transform(coords, quaternion); // readjust the points, as rotation occurs arround 0,0,0. newCoord.X = newCoord.X < 0 ? newCoord.X - transSize.X : newCoord.X; newCoord.Y = newCoord.Y < 0 ? newCoord.Y - transSize.Y : newCoord.Y; newCoord.Z = newCoord.Z < 0 ? newCoord.Z - transSize.Z : newCoord.Z; newAsteroid.SetVoxelContent(volume, ref newCoord); newAsteroid.SetVoxelMaterialAndIndestructibleContent(cellMaterial, 0xff, ref newCoord); } } } var tempfilename = TempfileUtil.NewFilename(MyVoxelMap.V2FileExtension); newAsteroid.Save(tempfilename); SourceVoxelFilepath = tempfilename; }
public MyObjectBuilder_EntityBase BuildEntity() { // Realign both asteroids to a common grid, so voxels can be lined up. Vector3I roundedPosLeft = SelectionLeft.WorldAABB.Min.RoundToVector3I(); Vector3D offsetPosLeft = SelectionLeft.WorldAABB.Min - (Vector3D)roundedPosLeft; // Use for everything. Vector3I roundedPosRight = (SelectionRight.WorldAABB.Min - offsetPosLeft).RoundToVector3I(); Vector3D offsetPosRight = SelectionRight.WorldAABB.Min - (Vector3D)roundedPosRight; // Use for everything. // calculate smallest allowable size for contents of both. const int paddCells = 3; // Force a calculation of the ContentBounds, as multi select in the ListView doesn't necessarily make it happen, or make it happen fast enough. SelectionLeft.LoadDetailsSync(); SelectionRight.LoadDetailsSync(); var minLeft = SelectionLeft.WorldAABB.Min + SelectionLeft.InflatedContentBounds.Min - offsetPosLeft; var minRight = SelectionRight.WorldAABB.Min + SelectionRight.InflatedContentBounds.Min - offsetPosRight; var min = Vector3D.Zero; var posOffset = Vector3D.Zero; var asteroidSize = Vector3I.Zero; switch (VoxelMergeType) { case VoxelMergeType.UnionVolumeLeftToRight: case VoxelMergeType.UnionVolumeRightToLeft: min = Vector3D.Min(minLeft, minRight) - paddCells; var max = Vector3D.Max(SelectionLeft.WorldAABB.Min + SelectionLeft.InflatedContentBounds.Max - offsetPosLeft, SelectionRight.WorldAABB.Min + SelectionRight.InflatedContentBounds.Max - offsetPosRight) + paddCells; posOffset = new Vector3D(minLeft.X < minRight.X ? offsetPosLeft.X : offsetPosRight.X, minLeft.Y < minRight.Y ? offsetPosLeft.Y : offsetPosRight.Y, minLeft.Z < minRight.Z ? offsetPosLeft.Z : offsetPosRight.Z); var size = (max - min).RoundToVector3I(); asteroidSize = MyVoxelBuilder.CalcRequiredSize(size); break; case VoxelMergeType.UnionMaterialLeftToRight: min = SelectionRight.WorldAABB.Min - offsetPosRight; posOffset = new Vector3D(minLeft.X < minRight.X ? offsetPosLeft.X : offsetPosRight.X, minLeft.Y < minRight.Y ? offsetPosLeft.Y : offsetPosRight.Y, minLeft.Z < minRight.Z ? offsetPosLeft.Z : offsetPosRight.Z); asteroidSize = SelectionRight.Size; break; case VoxelMergeType.UnionMaterialRightToLeft: min = SelectionLeft.WorldAABB.Min - offsetPosLeft; posOffset = new Vector3D(minLeft.X < minRight.X ? offsetPosLeft.X : offsetPosRight.X, minLeft.Y < minRight.Y ? offsetPosLeft.Y : offsetPosRight.Y, minLeft.Z < minRight.Z ? offsetPosLeft.Z : offsetPosRight.Z); asteroidSize = SelectionLeft.Size; break; case VoxelMergeType.SubtractVolumeLeftFromRight: min = SelectionRight.WorldAABB.Min - offsetPosRight; posOffset = new Vector3D(minLeft.X < minRight.X ? offsetPosLeft.X : offsetPosRight.X, minLeft.Y < minRight.Y ? offsetPosLeft.Y : offsetPosRight.Y, minLeft.Z < minRight.Z ? offsetPosLeft.Z : offsetPosRight.Z); asteroidSize = SelectionRight.Size; break; case VoxelMergeType.SubtractVolumeRightFromLeft: min = SelectionLeft.WorldAABB.Min - offsetPosLeft; posOffset = new Vector3D(minLeft.X < minRight.X ? offsetPosLeft.X : offsetPosRight.X, minLeft.Y < minRight.Y ? offsetPosLeft.Y : offsetPosRight.Y, minLeft.Z < minRight.Z ? offsetPosLeft.Z : offsetPosRight.Z); asteroidSize = SelectionLeft.Size; break; } // Prepare new asteroid. var newAsteroid = new MyVoxelMap(); newAsteroid.Create(asteroidSize, SpaceEngineersCore.Resources.GetDefaultMaterialIndex()); if (string.IsNullOrEmpty(MergeFileName)) { MergeFileName = "merge"; } var filename = MainViewModel.CreateUniqueVoxelStorageName(MergeFileName); // merge. switch (VoxelMergeType) { case VoxelMergeType.UnionVolumeLeftToRight: MergeAsteroidVolumeInto(ref newAsteroid, min, SelectionRight, SelectionLeft, minRight, minLeft); break; case VoxelMergeType.UnionVolumeRightToLeft: MergeAsteroidVolumeInto(ref newAsteroid, min, SelectionLeft, SelectionRight, minLeft, minRight); break; case VoxelMergeType.UnionMaterialLeftToRight: MergeAsteroidMaterialFrom(ref newAsteroid, min, SelectionRight, SelectionLeft, minRight, minLeft); break; case VoxelMergeType.UnionMaterialRightToLeft: MergeAsteroidMaterialFrom(ref newAsteroid, min, SelectionLeft, SelectionRight, minLeft, minRight); break; case VoxelMergeType.SubtractVolumeLeftFromRight: SubtractAsteroidVolumeFrom(ref newAsteroid, min, SelectionRight, SelectionLeft, minRight, minLeft); break; case VoxelMergeType.SubtractVolumeRightFromLeft: SubtractAsteroidVolumeFrom(ref newAsteroid, min, SelectionLeft, SelectionRight, minLeft, minRight); break; } // Generate Entity var tempfilename = TempfileUtil.NewFilename(MyVoxelMap.V2FileExtension); newAsteroid.Save(tempfilename); SourceFile = tempfilename; var position = min + posOffset; var entity = new MyObjectBuilder_VoxelMap(position, filename) { EntityId = SpaceEngineersApi.GenerateEntityId(IDType.ASTEROID), PersistentFlags = MyPersistentEntityFlags2.CastShadows | MyPersistentEntityFlags2.InScene, StorageName = Path.GetFileNameWithoutExtension(filename), PositionAndOrientation = new MyPositionAndOrientation { Position = position, Forward = Vector3.Forward, Up = Vector3.Up } }; return(entity); }
public void RotateAsteroid(Quaternion quaternion) { var sourceFile = SourceVoxelFilepath ?? VoxelFilepath; var asteroid = new MyVoxelMap(); asteroid.Load(sourceFile); var newAsteroid = new MyVoxelMap(); var newSize = asteroid.Size; newAsteroid.Create(newSize, SpaceEngineersCore.Resources.GetDefaultMaterialIndex()); Vector3I block; var halfSize = asteroid.Storage.Size / 2; // Don't use anything smaller than 64 for smaller voxels, as it trashes the cache. var cacheSize = new Vector3I(64); var halfCacheSize = new Vector3I(32); // This should only be used for the Transform, not the cache. // read the asteroid in chunks of 64 to avoid the Arithmetic overflow issue. for (block.Z = 0; block.Z < asteroid.Storage.Size.Z; block.Z += 64) { for (block.Y = 0; block.Y < asteroid.Storage.Size.Y; block.Y += 64) { for (block.X = 0; block.X < asteroid.Storage.Size.X; block.X += 64) { #region source voxel var cache = new MyStorageData(); cache.Resize(cacheSize); // LOD1 is not detailed enough for content information on asteroids. asteroid.Storage.ReadRange(cache, MyStorageDataTypeFlags.ContentAndMaterial, 0, block, block + cacheSize - 1); #endregion #region target Voxel // the block is a cubiod. The entire space needs to rotate, to be able to gauge where the new block position starts from. var newBlockMin = Vector3I.Transform(block - halfSize, quaternion) + halfSize; var newBlockMax = Vector3I.Transform(block + 64 - halfSize, quaternion) + halfSize; var newBlock = Vector3I.Min(newBlockMin, newBlockMax); var newCache = new MyStorageData(); newCache.Resize(cacheSize); newAsteroid.Storage.ReadRange(newCache, MyStorageDataTypeFlags.ContentAndMaterial, 0, newBlock, newBlock + cacheSize - 1); #endregion bool changed = false; Vector3I p; for (p.Z = 0; p.Z < cacheSize.Z; ++p.Z) { for (p.Y = 0; p.Y < cacheSize.Y; ++p.Y) { for (p.X = 0; p.X < cacheSize.X; ++p.X) { byte volume = cache.Content(ref p); byte cellMaterial = cache.Material(ref p); var newP1 = Vector3I.Transform(p - halfCacheSize, quaternion) + halfCacheSize; var newP2 = Vector3I.Transform(p + 1 - halfCacheSize, quaternion) + halfCacheSize; var newP = Vector3I.Min(newP1, newP2); newCache.Content(ref newP, volume); newCache.Material(ref newP, cellMaterial); changed = true; } } } if (changed) { newAsteroid.Storage.WriteRange(newCache, MyStorageDataTypeFlags.ContentAndMaterial, newBlock, newBlock + cacheSize - 1); } } } } var tempfilename = TempfileUtil.NewFilename(MyVoxelMap.V2FileExtension); newAsteroid.Save(tempfilename); SourceVoxelFilepath = tempfilename; }