static public IVoxelMesh Load(string path) { if (string.IsNullOrEmpty(path)) return null; path = FileUtil.GetProjectRelativePath(path); IVoxelMesh mesh = new MapVoxelMesh(); mesh.Clear(); // Create a file to write to. using (BinaryReader br = new BinaryReader(new FileStream(path, FileMode.Open))) { // Use BaseStream. int length = (int)br.BaseStream.Length; int width = br.ReadInt32(); int height = br.ReadInt32(); int depth = br.ReadInt32(); int pos = sizeof(int) * 3; int cpt = 0; while (pos < length) { int x = br.ReadInt32(); int y = br.ReadInt32(); int z = br.ReadInt32(); pos += sizeof(int) * 3; byte r = br.ReadByte(); byte g = br.ReadByte(); byte b = br.ReadByte(); byte a = br.ReadByte(); pos += sizeof(byte) * 4; mesh[x, y, z] = new Color32(r, g, b, a); cpt++; } br.Close(); } return mesh; }
/// <summary> /// Copy an existing MapVoxelMesh. /// </summary> /// <param name="toCopy"></param> public MapVoxelMesh(MapVoxelMesh toCopy) { this._chunks = new Dictionary<VoxelLocation, IVoxelMesh>(); this._chunkBuilder = (IChunckBuilder)toCopy._chunkBuilder.Copy(); this._max = new VoxelLocation(toCopy._max); this._min = new VoxelLocation(toCopy._min); this.Copy(toCopy.Start, toCopy.End, toCopy.Start, toCopy); }
/// <summary> /// Rebuild the voxel mesh. /// </summary> public void RefreshMesh() { int size = this.Radius * 2; IVoxelMesh sphere = new MapVoxelMesh(); for (int x = 0; x < size; ++x) { for (int y = 0; y < size; ++y) { for (int z = 0; z < size; ++z) { Vector3 point = this.GetPoint(x, y, z); if (point.magnitude <= this.Radius) { sphere[x - this.Radius, y - this.Radius, z - this.Radius] = new Color32( (byte) (this.Color.r * (y / (float) size)), (byte) (this.Color.g * (y / (float) size)), (byte) (this.Color.b * (y / (float) size)), this.Color.a ); } else { sphere[x - this.Radius, y - this.Radius, z - this.Radius] = Voxels.Empty; } } } } this.Mesh = sphere; }