public void CreateVOXObject() { if (!(generator is IVOXGenerator)) { return; } VOXObject obj = ((IVOXGenerator)generator).GenerateVOX(ground); Debug.Log(obj.Count); VOXParser vp = new VOXParser(); vp.CreateFile(obj, path: "Assets/Procedural.vox"); }
public VOXObject FromFile(string path) { BinaryReader br; uint r_version, r_main_size, r_size_x, r_size_y, r_size_z, r_xyzi_size, r_voxel_count; VOXObject result = null; try { br = new BinaryReader(System.IO.File.Open(path, FileMode.Open)); } catch (IOException e) { Debug.Log(e.Message + "\nCannot read file"); return(result); } byte[] temp; try { temp = br.ReadBytes(4); if (temp != System.Text.Encoding.ASCII.GetBytes(HEADER_ID)) { throw new FormatException("File is not .vox"); } temp = br.ReadBytes(4); // Debug.Log("Version is " + BitConverter.ToUInt32(temp,0)); r_version = BitConverter.ToUInt32(temp, 0); temp = br.ReadBytes(4); //"MAIN" temp = br.ReadBytes(4); temp = br.ReadBytes(4); r_main_size = BitConverter.ToUInt32(temp, 0); temp = br.ReadBytes(4); //"SIZE" temp = br.ReadBytes(4); temp = br.ReadBytes(4); temp = br.ReadBytes(4); r_size_x = BitConverter.ToUInt32(temp, 0); temp = br.ReadBytes(4); r_size_y = BitConverter.ToUInt32(temp, 0); temp = br.ReadBytes(4); r_size_z = BitConverter.ToUInt32(temp, 0); result = new VOXObject(r_size_x, r_size_y, r_size_z); // Debug.Log("Sizes: "+ r_size_x + ", " + r_size_y + ", " + r_size_z); temp = br.ReadBytes(4); //"XYZI" temp = br.ReadBytes(4); r_xyzi_size = BitConverter.ToUInt32(temp, 0); temp = br.ReadBytes(4); temp = br.ReadBytes(4); r_voxel_count = BitConverter.ToUInt32(temp, 0); Voxel parsed; for (uint i = 0; i < r_voxel_count; i++) { byte pos_x, pos_y, pos_z, color; pos_x = br.ReadByte(); pos_y = br.ReadByte(); pos_z = br.ReadByte(); color = br.ReadByte(); parsed = new Voxel(pos_x, pos_y, pos_z, color, false); // Debug.Log(parsed); result.voxels[pos_x][pos_y][pos_z].empty = false; result.voxels[pos_x][pos_y][pos_z].color = color; } //TODO read palette/materials } catch (IOException e) { Debug.Log(e.Message + "\nError reading file"); } br.Close(); return(result); }
public VOXObject GenerateVOX(Gradient colors) { map.Normalize(); Color palette_filler = new Color(128f, 128f, 128f), sea_color = new Color(35f, 100f, 189f), ground_color = new Color(73f, 36f, 14f); byte ground_index = (byte)(VOXObject.PALETTE_LENGTH - 1), sea_index = (byte)(VOXObject.PALETTE_LENGTH - 2); VOXObject vox_obj = new VOXObject((uint)map.width, (uint)map.length, (uint)map.height_limit); float[,] tau = map.Height_Ratio_Map; Color[] color_array = new Color[VOXObject.PALETTE_LENGTH]; int SAMPLING = Math.Min((int)sea_index, COLOR_SAMPLING); for (int i = 0; i < SAMPLING; i++) { float ratio = (float)i / (float)SAMPLING; color_array[i] = colors.Evaluate(ratio) * 255; } uint I_sea_level = (uint)(SeaLevel * vox_obj.size_z); Debug.Log("level@" + I_sea_level); if (COLOR_SAMPLING < 254) { for (int i = COLOR_SAMPLING; i < 254; i++) { color_array[i] = palette_filler; } } color_array[ground_index] = ground_color; color_array[sea_index] = sea_color; int height_tenth = (int)(map.height_limit / 10f), height_eighth = (int)(map.height_limit / 8f); for (int i = 0; i < map.width; i++) { for (int j = 0; j < map.length; j++) { int k = 0; byte col = (byte)(tau[i, j] * (SAMPLING - 1)); while (k < map[i, j].height && k < map.height_limit - 1) { vox_obj.voxels[i][j][k].empty = false; vox_obj.voxels[i][j][k].color = (byte)(ground_index + 1); k++; } vox_obj.voxels[i][j][k].empty = false; vox_obj.voxels[i][j][k].color = col; k++; // while(k <= map[i,j].height && k < map.height_limit - 1) { // vox_obj.voxels[i][j][k].empty = false; // vox_obj.voxels[i][j][k].color = col; // k++; // } while (k <= I_sea_level && k < map.height_limit - 1) { vox_obj.voxels[i][j][k].empty = false; vox_obj.voxels[i][j][k].color = (byte)(sea_index + 1); k++; } } } vox_obj.Palette = color_array; return(vox_obj); }
public void CreateFile(VOXObject obj, string path = "Assets/NewModel.vox") { BinaryWriter bin_writer; try { bin_writer = new BinaryWriter(System.IO.File.Create(path)); } catch (IOException e) { Debug.Log(e.Message + "\nCannot create file"); return; } //MAIN VOXChunk main_chunk = new VOXChunk(MAIN_ID); //SIZE VOXChunk size_chunk = new VOXChunk(SIZE_ID); byte[] size_x_byte = BitConverter.GetBytes(obj.size_x); byte[] size_y_byte = BitConverter.GetBytes(obj.size_y); byte[] size_z_byte = BitConverter.GetBytes(obj.size_z); byte[] sizes = new byte[12]; for (int i = 0; i < 4; i++) { sizes[i + 0] = size_x_byte[i]; sizes[i + 4] = size_y_byte[i]; sizes[i + 8] = size_z_byte[i]; } size_chunk.content = sizes; main_chunk.children.Add(size_chunk); //XYZI VOXChunk xyzi_chunk = new VOXChunk(XYZI_ID); List <Voxel> voxels = obj.NonEmptyVoxels; uint nb_voxels = (uint)voxels.Count; byte[] voxels_byte = new byte[4 * (nb_voxels + 1)]; BitConverter.GetBytes(nb_voxels).CopyTo(voxels_byte, 0); for (int i = 0; i < nb_voxels; i++) { voxels[i].ToBytes().CopyTo(voxels_byte, 4 * (i + 1)); } xyzi_chunk.content = voxels_byte; main_chunk.children.Add(xyzi_chunk); Color[] palette = obj.Palette; if (palette != null) { VOXChunk palette_chunk = new VOXChunk(PALETTE_ID); byte[] colors = new byte[VOXObject.PALETTE_LENGTH * 4]; byte[] byte_color; for (int i = 0; i < VOXObject.PALETTE_LENGTH; i++) { byte_color = palette[i].ToBytes(); colors[4 * i + 0] = byte_color[0]; colors[4 * i + 1] = byte_color[1]; colors[4 * i + 2] = byte_color[2]; colors[4 * i + 3] = byte_color[3]; } palette_chunk.content = colors; main_chunk.children.Add(palette_chunk); } try { bin_writer.Write(System.Text.Encoding.ASCII.GetBytes(HEADER_ID)); bin_writer.Write(VERSION); bin_writer.WriteVOXChunk(main_chunk); } catch (IOException e) { Debug.Log(e.Message + "\nError writing to file"); } /* DEPLETED * try { * bin_writer.Write(System.Text.Encoding.ASCII.GetBytes(header_id)); //"VOX " * bin_writer.Write(version); //version * * bin_writer.Write(System.Text.Encoding.ASCII.GetBytes(main_id)); //"MAIN" * bin_writer.Write((uint)0); //Content size of main * bin_writer.Write((uint) * 4 + //SIZE header * 4 + //SIZE content size * 4 + //SIZE children size * 12 + //SIZE content * 4 + //XYZI header * 4 + //XYZI content size * 4 + //XYZI children size * 4*(nb_voxels+1) * ); * * bin_writer.Write(System.Text.Encoding.ASCII.GetBytes(size_id)); //"SIZE" * bin_writer.Write((uint)12); * bin_writer.Write(empty4); //Children size of SIZE * //SIZE content: 3 x uint representing dimension length of the model * bin_writer.Write((uint)obj.size_x); * bin_writer.Write((uint)obj.size_y); * bin_writer.Write((uint)obj.size_z); * * bin_writer.Write(System.Text.Encoding.ASCII.GetBytes(xyzi_id)); //"XYZI" * bin_writer.Write(4*(nb_voxels+1)); //Content size of XYZI : (Nb de voxel*4) + 4 * bin_writer.Write((uint)0); //Children size of XYZI * bin_writer.Write(nb_voxels); * * for(int i = 0; i < nb_voxels; i++) { * bin_writer.Write(voxels[i].pos_x); * bin_writer.Write(voxels[i].pos_y); * bin_writer.Write(voxels[i].pos_z); * bin_writer.Write(voxels[i].color); * } * * } catch(IOException e) { * Debug.Log(e.Message + "\nError writing to file"); * }*/ bin_writer.Close(); }