- Home /
Saving 3D color array to file
I've created myself a little voxel (cube) editor which basically creates a 3D array of colors:
var Model:Color[,,];
One of these colors is handled by the code as a non-block (pitch-black = 0,0,0), so there's no case of null blocks or something of the like. The Model is a member variable of the script file/class, so my functions don't need it as a parameter (for now).
The size (at the moment) is always 16x16x16.
Now that I've got a basic editor I'd like to create a simple save/load function:
function Save(File:string) ...
function Load(File:string) ...
I've found examples of text and xml files, but I'd like to serialize/pack this variable. Is it possible to save/load this variable directly or is a conversion required? (example: converting Color[16,16,16] to Float[16,16,16,3]). I'd really like to see a code example of how this (or something similar) is achieved.
Thanks in advance.
Ps. Should it be helpful, I don't need the alpha channel, it's always 1.
Answer by Smots · Jun 18, 2013 at 09:12 AM
After tinkering for a couple of hours I found the solution.
For storing I loop through each voxel and convert each color channel to a byte value (0..255). I do this by multiplying by 255. So basically I convert to 24BPP (or BPV).
When loading I convert each byte back by dividing by 255.0
I can post the code here later if anyone is interested.
"I can post the code here later if anyone is interested."
Why not just do it anyway? Why wait to see if "anyone is interested"?
There is no reason NOT to post it anyway, in CASE anyone is interested, rather than IF anyone is interested.
Doing it anyway, builds Unity Answers to become a greater resource. Hiding it but freely giving it "if someone asks" is silly because it's as if you never even said anything.