├── .gitignore ├── Chunk.cs ├── Chunks ├── GroupNode.cs ├── Layer.cs ├── Main.cs ├── Material.cs ├── MaterialOld.cs ├── Model.cs ├── Pack.cs ├── Palette.cs ├── ShapeNode.cs ├── Size.cs ├── TransformNode.cs └── Unknown.cs ├── Extensions.cs ├── GenericsReader.cs ├── IVoxLoader.cs ├── LICENSE ├── README.md └── VoxReader.cs /.gitignore: -------------------------------------------------------------------------------- 1 | ## Ignore Visual Studio temporary files, build results, and 2 | ## files generated by popular Visual Studio add-ons. 3 | ## 4 | ## Get latest from https://github.com/github/gitignore/blob/master/VisualStudio.gitignore 5 | 6 | # User-specific files 7 | *.suo 8 | *.user 9 | *.userosscache 10 | *.sln.docstates 11 | 12 | # User-specific files (MonoDevelop/Xamarin Studio) 13 | *.userprefs 14 | 15 | # Build results 16 | [Dd]ebug/ 17 | [Dd]ebugPublic/ 18 | [Rr]elease/ 19 | [Rr]eleases/ 20 | x64/ 21 | x86/ 22 | bld/ 23 | [Bb]in/ 24 | [Oo]bj/ 25 | [Ll]og/ 26 | 27 | # Visual Studio 2015 cache/options directory 28 | .vs/ 29 | # Uncomment if you have tasks that create the project's static files in wwwroot 30 | #wwwroot/ 31 | 32 | # MSTest test Results 33 | [Tt]est[Rr]esult*/ 34 | [Bb]uild[Ll]og.* 35 | 36 | # NUNIT 37 | *.VisualState.xml 38 | TestResult.xml 39 | 40 | # Build Results of an ATL Project 41 | [Dd]ebugPS/ 42 | [Rr]eleasePS/ 43 | dlldata.c 44 | 45 | # .NET Core 46 | project.lock.json 47 | project.fragment.lock.json 48 | artifacts/ 49 | **/Properties/launchSettings.json 50 | 51 | *_i.c 52 | *_p.c 53 | *_i.h 54 | *.ilk 55 | *.meta 56 | *.obj 57 | *.pch 58 | *.pdb 59 | *.pgc 60 | *.pgd 61 | *.rsp 62 | *.sbr 63 | *.tlb 64 | *.tli 65 | *.tlh 66 | *.tmp 67 | *.tmp_proj 68 | *.log 69 | *.vspscc 70 | *.vssscc 71 | .builds 72 | *.pidb 73 | *.svclog 74 | *.scc 75 | 76 | # Chutzpah Test files 77 | _Chutzpah* 78 | 79 | # Visual C++ cache files 80 | ipch/ 81 | *.aps 82 | *.ncb 83 | *.opendb 84 | *.opensdf 85 | *.sdf 86 | *.cachefile 87 | *.VC.db 88 | *.VC.VC.opendb 89 | 90 | # Visual Studio profiler 91 | *.psess 92 | *.vsp 93 | *.vspx 94 | *.sap 95 | 96 | # TFS 2012 Local Workspace 97 | $tf/ 98 | 99 | # Guidance Automation Toolkit 100 | *.gpState 101 | 102 | # ReSharper is a .NET coding add-in 103 | _ReSharper*/ 104 | *.[Rr]e[Ss]harper 105 | *.DotSettings.user 106 | 107 | # JustCode is a .NET coding add-in 108 | .JustCode 109 | 110 | # TeamCity is a build add-in 111 | _TeamCity* 112 | 113 | # DotCover is a Code Coverage Tool 114 | *.dotCover 115 | 116 | # Visual Studio code coverage results 117 | *.coverage 118 | *.coveragexml 119 | 120 | # NCrunch 121 | _NCrunch_* 122 | .*crunch*.local.xml 123 | nCrunchTemp_* 124 | 125 | # MightyMoose 126 | *.mm.* 127 | AutoTest.Net/ 128 | 129 | # Web workbench (sass) 130 | .sass-cache/ 131 | 132 | # Installshield output folder 133 | [Ee]xpress/ 134 | 135 | # DocProject is a documentation generator add-in 136 | DocProject/buildhelp/ 137 | DocProject/Help/*.HxT 138 | DocProject/Help/*.HxC 139 | DocProject/Help/*.hhc 140 | DocProject/Help/*.hhk 141 | DocProject/Help/*.hhp 142 | DocProject/Help/Html2 143 | DocProject/Help/html 144 | 145 | # Click-Once directory 146 | publish/ 147 | 148 | # Publish Web Output 149 | *.[Pp]ublish.xml 150 | *.azurePubxml 151 | # TODO: Comment the next line if you want to checkin your web deploy settings 152 | # but database connection strings (with potential passwords) will be unencrypted 153 | *.pubxml 154 | *.publishproj 155 | 156 | # Microsoft Azure Web App publish settings. Comment the next line if you want to 157 | # checkin your Azure Web App publish settings, but sensitive information contained 158 | # in these scripts will be unencrypted 159 | PublishScripts/ 160 | 161 | # NuGet Packages 162 | *.nupkg 163 | # The packages folder can be ignored because of Package Restore 164 | **/packages/* 165 | # except build/, which is used as an MSBuild target. 166 | !**/packages/build/ 167 | # Uncomment if necessary however generally it will be regenerated when needed 168 | #!**/packages/repositories.config 169 | # NuGet v3's project.json files produces more ignorable files 170 | *.nuget.props 171 | *.nuget.targets 172 | 173 | # Microsoft Azure Build Output 174 | csx/ 175 | *.build.csdef 176 | 177 | # Microsoft Azure Emulator 178 | ecf/ 179 | rcf/ 180 | 181 | # Windows Store app package directories and files 182 | AppPackages/ 183 | BundleArtifacts/ 184 | Package.StoreAssociation.xml 185 | _pkginfo.txt 186 | 187 | # Visual Studio cache files 188 | # files ending in .cache can be ignored 189 | *.[Cc]ache 190 | # but keep track of directories ending in .cache 191 | !*.[Cc]ache/ 192 | 193 | # Others 194 | ClientBin/ 195 | ~$* 196 | *~ 197 | *.dbmdl 198 | *.dbproj.schemaview 199 | *.jfm 200 | *.pfx 201 | *.publishsettings 202 | orleans.codegen.cs 203 | 204 | # Since there are multiple workflows, uncomment next line to ignore bower_components 205 | # (https://github.com/github/gitignore/pull/1529#issuecomment-104372622) 206 | #bower_components/ 207 | 208 | # RIA/Silverlight projects 209 | Generated_Code/ 210 | 211 | # Backup & report files from converting an old project file 212 | # to a newer Visual Studio version. Backup files are not needed, 213 | # because we have git ;-) 214 | _UpgradeReport_Files/ 215 | Backup*/ 216 | UpgradeLog*.XML 217 | UpgradeLog*.htm 218 | 219 | # SQL Server files 220 | *.mdf 221 | *.ldf 222 | *.ndf 223 | 224 | # Business Intelligence projects 225 | *.rdl.data 226 | *.bim.layout 227 | *.bim_*.settings 228 | 229 | # Microsoft Fakes 230 | FakesAssemblies/ 231 | 232 | # GhostDoc plugin setting file 233 | *.GhostDoc.xml 234 | 235 | # Node.js Tools for Visual Studio 236 | .ntvs_analysis.dat 237 | node_modules/ 238 | 239 | # Typescript v1 declaration files 240 | typings/ 241 | 242 | # Visual Studio 6 build log 243 | *.plg 244 | 245 | # Visual Studio 6 workspace options file 246 | *.opt 247 | 248 | # Visual Studio 6 auto-generated workspace file (contains which files were open etc.) 249 | *.vbw 250 | 251 | # Visual Studio LightSwitch build output 252 | **/*.HTMLClient/GeneratedArtifacts 253 | **/*.DesktopClient/GeneratedArtifacts 254 | **/*.DesktopClient/ModelManifest.xml 255 | **/*.Server/GeneratedArtifacts 256 | **/*.Server/ModelManifest.xml 257 | _Pvt_Extensions 258 | 259 | # Paket dependency manager 260 | .paket/paket.exe 261 | paket-files/ 262 | 263 | # FAKE - F# Make 264 | .fake/ 265 | 266 | # JetBrains Rider 267 | .idea/ 268 | *.sln.iml 269 | 270 | # CodeRush 271 | .cr/ 272 | 273 | # Python Tools for Visual Studio (PTVS) 274 | __pycache__/ 275 | *.pyc 276 | 277 | # Cake - Uncomment if you are using it 278 | # tools/** 279 | # !tools/packages.config 280 | 281 | # Telerik's JustMock configuration file 282 | *.jmconfig 283 | 284 | # BizTalk build output 285 | *.btp.cs 286 | *.btm.cs 287 | *.odx.cs 288 | *.xsd.cs 289 | 290 | *.meta -------------------------------------------------------------------------------- /Chunk.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.IO; 3 | using System.Collections.Generic; 4 | 5 | namespace CsharpVoxReader 6 | { 7 | public abstract class Chunk 8 | { 9 | private Int32 _Size; 10 | private Int32 _ChildrenSize; 11 | 12 | internal abstract string Id { get; } 13 | 14 | internal Int32 Size 15 | { 16 | get { return _Size; } 17 | } 18 | 19 | internal Int32 ChildrenSize 20 | { 21 | get { return _ChildrenSize; } 22 | } 23 | 24 | internal static string ReadChunkId(BinaryReader br) 25 | { 26 | if (br == null) throw new ArgumentNullException(nameof(br)); 27 | 28 | char[] id = br.ReadChars(4); 29 | return new string(id); 30 | } 31 | 32 | internal virtual int Read(BinaryReader br, IVoxLoader loader) 33 | { 34 | if (br == null) throw new ArgumentNullException(nameof(br)); 35 | if (loader == null) throw new ArgumentNullException(nameof(loader)); 36 | 37 | _Size = br.ReadInt32(); 38 | _ChildrenSize = br.ReadInt32(); 39 | 40 | return sizeof(Int32) * 2; 41 | } 42 | 43 | internal static Chunk CreateChunk(string id) 44 | { 45 | switch (id) 46 | { 47 | case Chunks.Main.ID : return new Chunks.Main(); 48 | case Chunks.Size.ID : return new Chunks.Size(); 49 | case Chunks.Model.ID : return new Chunks.Model(); 50 | case Chunks.Palette.ID : return new Chunks.Palette(); 51 | case Chunks.Pack.ID: return new Chunks.Pack(); 52 | case Chunks.MaterialOld.ID: return new Chunks.MaterialOld(); 53 | case Chunks.TransformNode.ID: return new Chunks.TransformNode(); 54 | case Chunks.GroupNode.ID: return new Chunks.GroupNode(); 55 | case Chunks.ShapeNode.ID: return new Chunks.ShapeNode(); 56 | case Chunks.Material.ID: return new Chunks.Material(); 57 | case Chunks.Layer.ID: return new Chunks.Layer(); 58 | default: return new Chunks.Unknown(id); 59 | } 60 | } 61 | 62 | public override string ToString() 63 | { 64 | return Id; 65 | } 66 | } 67 | } 68 | -------------------------------------------------------------------------------- /Chunks/GroupNode.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.IO; 3 | using System.Collections.Generic; 4 | 5 | namespace CsharpVoxReader.Chunks 6 | { 7 | public class GroupNode : Chunk 8 | { 9 | public const string ID = "nGRP"; 10 | 11 | internal override string Id 12 | { 13 | get { return ID; } 14 | } 15 | 16 | internal override int Read(BinaryReader br, IVoxLoader loader) 17 | { 18 | int readSize = base.Read(br, loader); 19 | 20 | Int32 id = br.ReadInt32(); 21 | Dictionary attributes = GenericsReader.ReadDict(br, ref readSize); 22 | 23 | Int32 numChildrenNodes = br.ReadInt32(); 24 | Int32[] childrenIds = new Int32[numChildrenNodes]; 25 | readSize += sizeof(Int32) * (numChildrenNodes + 2); 26 | 27 | for (int cnum=0; cnum < numChildrenNodes; cnum++) { 28 | childrenIds[cnum] = br.ReadInt32(); 29 | } 30 | 31 | loader.NewGroupNode(id, attributes, childrenIds); 32 | return readSize; 33 | } 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /Chunks/Layer.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.IO; 3 | using System.Collections.Generic; 4 | 5 | namespace CsharpVoxReader.Chunks 6 | { 7 | public class Layer : Chunk 8 | { 9 | public const string ID = "LAYR"; 10 | 11 | internal override string Id 12 | { 13 | get { return ID; } 14 | } 15 | 16 | internal override int Read(BinaryReader br, IVoxLoader loader) 17 | { 18 | int readSize = base.Read(br, loader); 19 | Int32 id = br.ReadInt32(); 20 | Dictionary attributes = GenericsReader.ReadDict(br, ref readSize); 21 | 22 | attributes.TryGetName(out var name); 23 | 24 | Int32 reservedId = br.ReadInt32(); 25 | readSize += sizeof(Int32) * 2; 26 | 27 | loader.NewLayer(id, name, attributes); 28 | return readSize; 29 | } 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /Chunks/Main.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.IO; 3 | using System.Linq; 4 | 5 | namespace CsharpVoxReader.Chunks 6 | { 7 | public class Main : Chunk 8 | { 9 | public const string ID = "MAIN"; 10 | 11 | internal override string Id 12 | { 13 | get { return ID; } 14 | } 15 | 16 | internal override int Read(BinaryReader br, IVoxLoader loader) 17 | { 18 | int readSize = base.Read(br, loader); 19 | 20 | if(Size > 0) 21 | { 22 | throw new InvalidDataException($"MAIN chunk size is expected to be 0 (was {Size}"); 23 | } 24 | 25 | int childrenReadSize = 0; 26 | 27 | while (childrenReadSize < ChildrenSize) 28 | { 29 | string id = Chunk.ReadChunkId(br); 30 | Chunk child = Chunk.CreateChunk(id); 31 | childrenReadSize += child.Read(br, loader) + 4; 32 | } 33 | 34 | return readSize + Size + ChildrenSize; 35 | } 36 | } 37 | } 38 | 39 | 40 | -------------------------------------------------------------------------------- /Chunks/Material.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.IO; 3 | using System.Collections.Generic; 4 | 5 | namespace CsharpVoxReader.Chunks 6 | { 7 | public class Material : Chunk 8 | { 9 | public const string ID = "MATL"; 10 | 11 | internal override string Id 12 | { 13 | get { return ID; } 14 | } 15 | 16 | internal override int Read(BinaryReader br, IVoxLoader loader) 17 | { 18 | int readSize = base.Read(br, loader); 19 | 20 | Int32 id = br.ReadInt32(); 21 | Dictionary attributes = GenericsReader.ReadDict(br, ref readSize); 22 | readSize += sizeof(Int32); 23 | 24 | loader.NewMaterial(id, attributes); 25 | return readSize; 26 | } 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /Chunks/MaterialOld.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace CsharpVoxReader.Chunks 4 | { 5 | public class MaterialOld : Chunk 6 | { 7 | public const string ID = "MATT"; 8 | 9 | internal override string Id 10 | { 11 | get { return ID; } 12 | } 13 | 14 | public enum MaterialTypes : Int32 15 | { 16 | Diffuse = 0, 17 | Metal = 1, 18 | Glass = 2, 19 | Emissive = 3 20 | }; 21 | 22 | [Flags] 23 | public enum PropertyBits : UInt32 24 | { 25 | Plastic = 1 << 0, 26 | Roughness = 1 << 1, 27 | Specular = 1 << 2, 28 | IOR = 1 << 3, 29 | Attenuation = 1 << 4, 30 | Power = 1 << 5, 31 | Glow = 1 << 6, 32 | IsTotalPower = 1 << 7 33 | }; 34 | 35 | internal override int Read(System.IO.BinaryReader br, IVoxLoader loader) 36 | { 37 | int readSize = base.Read(br, loader); 38 | 39 | Int32 paletteId = br.ReadInt32(); 40 | Int32 type = br.ReadInt32(); 41 | float weight = br.ReadSingle(); 42 | UInt32 property = br.ReadUInt32(); 43 | float normalized = br.ReadSingle(); 44 | readSize += sizeof(Int32) * 2 + sizeof(UInt32) + sizeof(float) * 2; 45 | 46 | loader.SetMaterialOld(paletteId, (MaterialTypes)type, weight, (PropertyBits)property, normalized); 47 | 48 | return readSize; 49 | } 50 | } 51 | } -------------------------------------------------------------------------------- /Chunks/Model.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.IO; 3 | 4 | namespace CsharpVoxReader.Chunks 5 | { 6 | public class Model : Chunk 7 | { 8 | public const string ID = "XYZI"; 9 | 10 | private byte[,,] _Indexes; 11 | 12 | internal override string Id 13 | { 14 | get { return ID; } 15 | } 16 | 17 | public byte[,,] Indexes 18 | { 19 | get { return _Indexes; } 20 | } 21 | 22 | internal override int Read(BinaryReader br, IVoxLoader loader) 23 | { 24 | int readSize = base.Read(br, loader); 25 | 26 | Int32 numVoxels = br.ReadInt32(); 27 | readSize += sizeof(Int32); 28 | 29 | for (int i = 0; i < numVoxels; i++) 30 | { 31 | byte x = br.ReadByte(); 32 | byte z = br.ReadByte(); 33 | byte y = br.ReadByte(); 34 | byte index = br.ReadByte(); 35 | _Indexes[x, y, z] = index; 36 | readSize += 4; 37 | } 38 | return readSize; 39 | } 40 | 41 | public void Init(int sizeX, int sizeY, int sizeZ) 42 | { 43 | _Indexes = new byte[sizeX, sizeY, sizeZ]; 44 | } 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /Chunks/Pack.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace CsharpVoxReader.Chunks 4 | { 5 | public class Pack : Chunk 6 | { 7 | public const string ID = "PACK"; 8 | 9 | internal override string Id 10 | { 11 | get { return ID; } 12 | } 13 | 14 | 15 | internal override int Read(System.IO.BinaryReader br, IVoxLoader loader) 16 | { 17 | int readSize = base.Read(br, loader); 18 | 19 | Int32 numModels = br.ReadInt32(); 20 | readSize += sizeof(Int32); 21 | 22 | loader.SetModelCount(numModels); 23 | 24 | return readSize; 25 | } 26 | } 27 | } -------------------------------------------------------------------------------- /Chunks/Palette.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace CsharpVoxReader.Chunks 4 | { 5 | public class Palette : Chunk 6 | { 7 | public const string ID = "RGBA"; 8 | 9 | internal override string Id 10 | { 11 | get { return ID; } 12 | } 13 | 14 | public static readonly UInt32[] DefaultPalette = 15 | { 16 | 0x00000000, 0xffffffff, 0xffccffff, 0xff99ffff, 0xff66ffff, 0xff33ffff, 0xff00ffff, 0xffffccff, 0xffccccff, 0xff99ccff, 0xff66ccff, 0xff33ccff, 0xff00ccff, 0xffff99ff, 0xffcc99ff, 0xff9999ff, 17 | 0xff6699ff, 0xff3399ff, 0xff0099ff, 0xffff66ff, 0xffcc66ff, 0xff9966ff, 0xff6666ff, 0xff3366ff, 0xff0066ff, 0xffff33ff, 0xffcc33ff, 0xff9933ff, 0xff6633ff, 0xff3333ff, 0xff0033ff, 0xffff00ff, 18 | 0xffcc00ff, 0xff9900ff, 0xff6600ff, 0xff3300ff, 0xff0000ff, 0xffffffcc, 0xffccffcc, 0xff99ffcc, 0xff66ffcc, 0xff33ffcc, 0xff00ffcc, 0xffffcccc, 0xffcccccc, 0xff99cccc, 0xff66cccc, 0xff33cccc, 19 | 0xff00cccc, 0xffff99cc, 0xffcc99cc, 0xff9999cc, 0xff6699cc, 0xff3399cc, 0xff0099cc, 0xffff66cc, 0xffcc66cc, 0xff9966cc, 0xff6666cc, 0xff3366cc, 0xff0066cc, 0xffff33cc, 0xffcc33cc, 0xff9933cc, 20 | 0xff6633cc, 0xff3333cc, 0xff0033cc, 0xffff00cc, 0xffcc00cc, 0xff9900cc, 0xff6600cc, 0xff3300cc, 0xff0000cc, 0xffffff99, 0xffccff99, 0xff99ff99, 0xff66ff99, 0xff33ff99, 0xff00ff99, 0xffffcc99, 21 | 0xffcccc99, 0xff99cc99, 0xff66cc99, 0xff33cc99, 0xff00cc99, 0xffff9999, 0xffcc9999, 0xff999999, 0xff669999, 0xff339999, 0xff009999, 0xffff6699, 0xffcc6699, 0xff996699, 0xff666699, 0xff336699, 22 | 0xff006699, 0xffff3399, 0xffcc3399, 0xff993399, 0xff663399, 0xff333399, 0xff003399, 0xffff0099, 0xffcc0099, 0xff990099, 0xff660099, 0xff330099, 0xff000099, 0xffffff66, 0xffccff66, 0xff99ff66, 23 | 0xff66ff66, 0xff33ff66, 0xff00ff66, 0xffffcc66, 0xffcccc66, 0xff99cc66, 0xff66cc66, 0xff33cc66, 0xff00cc66, 0xffff9966, 0xffcc9966, 0xff999966, 0xff669966, 0xff339966, 0xff009966, 0xffff6666, 24 | 0xffcc6666, 0xff996666, 0xff666666, 0xff336666, 0xff006666, 0xffff3366, 0xffcc3366, 0xff993366, 0xff663366, 0xff333366, 0xff003366, 0xffff0066, 0xffcc0066, 0xff990066, 0xff660066, 0xff330066, 25 | 0xff000066, 0xffffff33, 0xffccff33, 0xff99ff33, 0xff66ff33, 0xff33ff33, 0xff00ff33, 0xffffcc33, 0xffcccc33, 0xff99cc33, 0xff66cc33, 0xff33cc33, 0xff00cc33, 0xffff9933, 0xffcc9933, 0xff999933, 26 | 0xff669933, 0xff339933, 0xff009933, 0xffff6633, 0xffcc6633, 0xff996633, 0xff666633, 0xff336633, 0xff006633, 0xffff3333, 0xffcc3333, 0xff993333, 0xff663333, 0xff333333, 0xff003333, 0xffff0033, 27 | 0xffcc0033, 0xff990033, 0xff660033, 0xff330033, 0xff000033, 0xffffff00, 0xffccff00, 0xff99ff00, 0xff66ff00, 0xff33ff00, 0xff00ff00, 0xffffcc00, 0xffcccc00, 0xff99cc00, 0xff66cc00, 0xff33cc00, 28 | 0xff00cc00, 0xffff9900, 0xffcc9900, 0xff999900, 0xff669900, 0xff339900, 0xff009900, 0xffff6600, 0xffcc6600, 0xff996600, 0xff666600, 0xff336600, 0xff006600, 0xffff3300, 0xffcc3300, 0xff993300, 29 | 0xff663300, 0xff333300, 0xff003300, 0xffff0000, 0xffcc0000, 0xff990000, 0xff660000, 0xff330000, 0xff0000ee, 0xff0000dd, 0xff0000bb, 0xff0000aa, 0xff000088, 0xff000077, 0xff000055, 0xff000044, 30 | 0xff000022, 0xff000011, 0xff00ee00, 0xff00dd00, 0xff00bb00, 0xff00aa00, 0xff008800, 0xff007700, 0xff005500, 0xff004400, 0xff002200, 0xff001100, 0xffee0000, 0xffdd0000, 0xffbb0000, 0xffaa0000, 31 | 0xff880000, 0xff770000, 0xff550000, 0xff440000, 0xff220000, 0xff110000, 0xffeeeeee, 0xffdddddd, 0xffbbbbbb, 0xffaaaaaa, 0xff888888, 0xff777777, 0xff555555, 0xff444444, 0xff222222, 0xff111111 32 | }; 33 | 34 | internal override int Read(System.IO.BinaryReader br, IVoxLoader loader) 35 | { 36 | int readSize = base.Read(br, loader); 37 | 38 | UInt32[] palette = new UInt32[256]; 39 | for (int i = 0; i <= 254; i++) 40 | { 41 | byte r = br.ReadByte(); 42 | byte g = br.ReadByte(); 43 | byte b = br.ReadByte(); 44 | byte a = br.ReadByte(); 45 | 46 | palette[i + 1] = BitConverter.ToUInt32(new byte[] { b, g, r, a }, 0); 47 | readSize += 4; 48 | } 49 | 50 | br.ReadBytes(4); 51 | readSize += 4; 52 | 53 | loader.LoadPalette(palette); 54 | return readSize; 55 | } 56 | } 57 | } -------------------------------------------------------------------------------- /Chunks/ShapeNode.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.IO; 3 | using System.Collections.Generic; 4 | 5 | namespace CsharpVoxReader.Chunks 6 | { 7 | public class ShapeNode : Chunk 8 | { 9 | public const string ID = "nSHP"; 10 | 11 | internal override string Id 12 | { 13 | get { return ID; } 14 | } 15 | 16 | internal override int Read(BinaryReader br, IVoxLoader loader) 17 | { 18 | int readSize = base.Read(br, loader); 19 | 20 | Int32 id = br.ReadInt32(); 21 | Dictionary attributes = GenericsReader.ReadDict(br, ref readSize); 22 | 23 | Int32 numModels = br.ReadInt32(); 24 | readSize += sizeof(Int32) * 2; 25 | 26 | Int32[] modelIds = new Int32[numModels]; 27 | Dictionary[] modelsAttributes = new Dictionary[numModels]; 28 | 29 | for (int mnum=0; mnum < numModels; mnum++) { 30 | modelIds[mnum] = br.ReadInt32(); 31 | modelsAttributes[mnum] = GenericsReader.ReadDict(br, ref readSize); 32 | readSize += sizeof(Int32); 33 | } 34 | 35 | loader.NewShapeNode(id, attributes, modelIds, modelsAttributes); 36 | return readSize; 37 | } 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /Chunks/Size.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.IO; 3 | 4 | namespace CsharpVoxReader.Chunks 5 | { 6 | public class Size : Chunk 7 | { 8 | public const string ID = "SIZE"; 9 | 10 | internal override string Id 11 | { 12 | get { return ID; } 13 | } 14 | 15 | internal override int Read(BinaryReader br, IVoxLoader loader) 16 | { 17 | int readSize = base.Read(br, loader); 18 | 19 | Int32 x = br.ReadInt32(); 20 | Int32 z = br.ReadInt32(); 21 | Int32 y = br.ReadInt32(); 22 | readSize += sizeof(Int32) * 3; 23 | 24 | string id = Chunk.ReadChunkId(br); 25 | readSize += 4; 26 | if(id != Model.ID) throw new InvalidDataException($"Can't read VOX file : XYZI chunk expected (was {id})"); 27 | 28 | Model model = Chunk.CreateChunk(id) as Model; 29 | model.Init(x, y, z); 30 | readSize += model.Read(br, loader); 31 | 32 | loader.LoadModel(x, y, z, model.Indexes); 33 | return readSize; 34 | } 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /Chunks/TransformNode.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.IO; 3 | using System.Collections.Generic; 4 | 5 | namespace CsharpVoxReader.Chunks 6 | { 7 | public class TransformNode : Chunk 8 | { 9 | public const string ID = "nTRN"; 10 | 11 | internal override string Id 12 | { 13 | get { return ID; } 14 | } 15 | 16 | internal override int Read(BinaryReader br, IVoxLoader loader) 17 | { 18 | int readSize = base.Read(br, loader); 19 | 20 | Int32 id = br.ReadInt32(); 21 | Dictionary attributes = GenericsReader.ReadDict(br, ref readSize); 22 | 23 | attributes.TryGetName(out var name); 24 | 25 | Int32 childNodeId = br.ReadInt32(); 26 | Int32 reservedId = br.ReadInt32(); 27 | Int32 layerId = br.ReadInt32(); 28 | Int32 numOfFrames = br.ReadInt32(); 29 | 30 | readSize += sizeof(Int32) * 5; 31 | 32 | Dictionary[] framesAttributes = new Dictionary[numOfFrames]; 33 | 34 | for (int fnum=0; fnum < numOfFrames; fnum++) { 35 | framesAttributes[fnum] = GenericsReader.ReadDict(br, ref readSize); 36 | /*int[] rotationMatrix = GenericsReader.ReadRotation(br, ref readSize); 37 | int[] translationVector = { 0, 0, 0 }; 38 | translationVector[0] = br.ReadInt32(); 39 | translationVector[1] = br.ReadInt32(); 40 | translationVector[2] = br.ReadInt32();*/ 41 | // TODO: Add frame info 42 | } 43 | 44 | // TODO: Notify the IVoxLoader of the transform node 45 | loader.NewTransformNode(id, childNodeId, layerId, name, framesAttributes); 46 | return readSize; 47 | } 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /Chunks/Unknown.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.IO; 4 | using System.Linq; 5 | using System.Text; 6 | using System.Threading.Tasks; 7 | 8 | namespace CsharpVoxReader.Chunks 9 | { 10 | public class Unknown : Chunk 11 | { 12 | private string _Id; 13 | 14 | public Unknown(string id) 15 | { 16 | _Id = id; 17 | } 18 | 19 | internal override string Id 20 | { 21 | get 22 | { 23 | return _Id; 24 | } 25 | } 26 | 27 | internal override int Read(System.IO.BinaryReader br, IVoxLoader loader) 28 | { 29 | int readSize = base.Read(br, loader); 30 | 31 | br.ReadBytes(Size); 32 | br.ReadBytes(ChildrenSize); 33 | return readSize + Size + ChildrenSize; 34 | } 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /Extensions.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace CsharpVoxReader 8 | { 9 | public static class Extensions 10 | { 11 | /// 12 | /// Read a color (ARGB) value from a 13 | /// 14 | /// Input value 15 | /// Alpha value 16 | /// Red value 17 | /// Green value 18 | /// Blue value 19 | public static void ToARGB(this UInt32 value, out byte a, out byte r, out byte g, out byte b) 20 | { 21 | byte[] bytes = BitConverter.GetBytes(value); 22 | a = bytes[3]; 23 | r = bytes[2]; 24 | g = bytes[1]; 25 | b = bytes[0]; 26 | } 27 | 28 | /// 29 | /// Try Get name from attributes Dictionary. 30 | /// 31 | /// Chunk attributes. 32 | /// result name. If the returned value is false, null will be null. 33 | /// Success or failure of name acquisition 34 | public static bool TryGetName(this Dictionary attributes, out string name) 35 | { 36 | if (attributes.TryGetValue("_name", out var bytes)) 37 | { 38 | name = System.Text.Encoding.ASCII.GetString(bytes); 39 | return true; 40 | } 41 | 42 | name = null; 43 | return false; 44 | } 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /GenericsReader.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.IO; 3 | using System.Text; 4 | using System.Collections.Generic; 5 | 6 | namespace CsharpVoxReader 7 | { 8 | public class GenericsReader { 9 | 10 | public static byte[] ReadByteArray(BinaryReader br, ref int readsize) { 11 | Int32 numChars = br.ReadInt32(); 12 | readsize += sizeof(Int32) + numChars; 13 | 14 | return br.ReadBytes(numChars); 15 | } 16 | 17 | public static Dictionary ReadDict(BinaryReader br, ref int readsize) { 18 | Dictionary result = new Dictionary(); 19 | 20 | Int32 numElements = br.ReadInt32(); 21 | readsize += sizeof(Int32); 22 | 23 | for (int i=0; i < numElements; i++) { 24 | string key = Encoding.UTF8.GetString(ReadByteArray(br, ref readsize)); 25 | byte[] value = ReadByteArray(br, ref readsize); 26 | 27 | result[key] = value; 28 | } 29 | 30 | return result; 31 | } 32 | 33 | public static int[] ReadRotation(BinaryReader br, ref int readsize) { 34 | byte rot = br.ReadByte(); 35 | readsize += 1; 36 | 37 | int r0v = ((rot & 8) == 0)?1:-1; 38 | int r1v = ((rot & 16) == 0)?1:-1; 39 | int r2v = ((rot & 32) == 0)?1:-1; 40 | 41 | int r0i = rot & 3; 42 | int r1i = (rot & 12) >> 2; 43 | /* 44 | Truth table for the third index 45 | r0| 0 | 1 | 2 | 46 | r1--+---+---+---+ 47 | 0 | X | 2 | 1 | 48 | ----+---+---+---+ 49 | 1 | 2 | X | 0 | 50 | ----+---+---+---+ 51 | 2 | 1 | 0 | X | 52 | ----+---+---+---+ 53 | 54 | Derived function 55 | f(r0, r1) = 3 - r0 - r1 56 | */ 57 | int r2i = 3 - r0i - r1i; 58 | 59 | int[] result = { 0, 0, 0, 0, 0, 0, 0, 0, 0 }; 60 | 61 | result[r0i] = r0v; 62 | result[r1i + 3] = r1v; 63 | result[r2i + 6] = r2v; 64 | 65 | return result; 66 | } 67 | 68 | } 69 | } 70 | -------------------------------------------------------------------------------- /IVoxLoader.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace CsharpVoxReader 8 | { 9 | public interface IVoxLoader 10 | { 11 | void LoadModel(Int32 sizeX, Int32 sizeY, Int32 sizeZ, byte[,,] data); 12 | void LoadPalette(UInt32[] palette); 13 | void SetModelCount(Int32 count); 14 | void SetMaterialOld(Int32 paletteId, Chunks.MaterialOld.MaterialTypes type, float weight, Chunks.MaterialOld.PropertyBits property, float normalized); 15 | // VOX Extensions 16 | void NewTransformNode(Int32 id, Int32 childNodeId, Int32 layerId, string name, Dictionary[] framesAttributes); 17 | void NewGroupNode(Int32 id, Dictionary attributes, Int32[] childrenIds); 18 | void NewShapeNode(Int32 id, Dictionary attributes, Int32[] modelIds, Dictionary[] modelsAttributes); 19 | void NewMaterial(Int32 id, Dictionary attributes); 20 | void NewLayer(Int32 id, string name, Dictionary attributes); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2017 BARRAUD Fabien 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # CsharpVoxReader 2 | A generic C# reader for [MagicaVoxel](https://ephtracy.github.io/)'s vox file format. It should be usable with any C# project (monogame, unity, ...). If it doesn't, please let me know. Contributions are welcome. 3 | 4 | ## Usage 5 | First clone the repository as a submodule for your current project. 6 | 7 | Then you have to make your own implementation of `IVoxLoader` so that you can receive vox data from a file (palettes, models, ...). 8 | 9 | ```csharp 10 | using CsharpVoxReader; 11 | using System; 12 | 13 | 14 | class MyLoader : IVoxLoader 15 | { 16 | public void LoadModel(Int32 sizeX, Int32 sizeY, Int32 sizeZ, byte[,,] data) 17 | { 18 | // Create a model 19 | } 20 | 21 | public void LoadPalette(UInt32[] palette) 22 | { 23 | // Set palette 24 | 25 | // You can use extension method ToARGB from namespace CsharpVoxReader 26 | // To get rgba values 27 | // ie: palette[1].ToARGB(out a, out r, out g, out b) 28 | } 29 | } 30 | ``` 31 | 32 | Finally pass it to `VoxReader` to read a vox file. 33 | 34 | ```csharp 35 | using CsharpVoxReader; 36 | using System; 37 | 38 | 39 | class Program 40 | { 41 | static void Main(string[] args) 42 | { 43 | VoxReader r = new VoxReader(@"C:\path\to\my\file.vox", new MyLoader()); 44 | 45 | r.Read(); 46 | } 47 | } 48 | ``` 49 | 50 | ## Note about vox file format 51 | Magicavoxel was recently updated to v0.99a. Vox file format number was unchanged, but includes a lot of new stuff, not yet documented in the [Vox file format description](https://github.com/ephtracy/voxel-model). 52 | 53 | As such, `CsharpVoxReader` can only read documented chunks : palettes and models (Material and Pack chunks are read from older magicavoxel versions, but material has changed and pack doesn't seem to be used anymore). New stuff like layers, model position in the world won't be readable until the documentated. 54 | 55 | -------------------------------------------------------------------------------- /VoxReader.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.IO; 4 | using System.Linq; 5 | 6 | namespace CsharpVoxReader 7 | { 8 | public class VoxReader 9 | { 10 | private const Int32 FILE_FORMAT_VERSION = 150; 11 | 12 | protected string Path { get; set; } 13 | protected IVoxLoader Loader { get; set; } 14 | protected Stream Origin { get; set; } 15 | 16 | public VoxReader(string path, IVoxLoader loader) 17 | { 18 | if ( ! File.Exists(path)) throw new FileNotFoundException("Can't open vox file : file not found", path); 19 | if (loader == null) throw new ArgumentNullException(nameof(loader)); 20 | 21 | Path = path; 22 | Loader = loader; 23 | } 24 | 25 | public VoxReader(Stream s, IVoxLoader loader) 26 | { 27 | if (loader == null) throw new ArgumentNullException(nameof(loader)); 28 | 29 | Origin = s; 30 | Loader = loader; 31 | } 32 | 33 | private VoxReader() 34 | { 35 | } 36 | 37 | public Chunk Read() 38 | { 39 | using (FileStream fs = File.OpenRead(Path)) { 40 | Origin = fs; 41 | return ReadFromStream(); 42 | } 43 | } 44 | 45 | public Chunk ReadFromStream() { 46 | using(BinaryReader br = new BinaryReader(Origin)) 47 | { 48 | char[] magicNumber = br.ReadChars(4); 49 | if( ! magicNumber.SequenceEqual("VOX ".ToCharArray())) 50 | { 51 | throw new InvalidDataException("Can't read VOX file : invalid vox signature"); 52 | } 53 | 54 | Int32 version = br.ReadInt32(); 55 | if(version > FILE_FORMAT_VERSION) 56 | { 57 | throw new InvalidDataException($"Can't read VOX file : file format version ({version}) is newer than reader version ({FILE_FORMAT_VERSION})"); 58 | } 59 | 60 | string id = Chunk.ReadChunkId(br); 61 | if(id != Chunks.Main.ID) 62 | { 63 | throw new InvalidDataException($"Can't read VOX file : MAIN chunk expected (was {id}"); 64 | } 65 | 66 | Chunk main = Chunk.CreateChunk(id); 67 | main.Read(br, Loader); 68 | 69 | return main; 70 | } 71 | } 72 | } 73 | } 74 | --------------------------------------------------------------------------------