Skip to content

Commit

Permalink
Fix for palette importing and frames getting overwritten on scene reload
Browse files Browse the repository at this point in the history
Pallete fixes and frame overwrite fixes
  • Loading branch information
GN00T authored Mar 29, 2017
2 parents def0188 + ae9dd92 commit fe8c4cc
Show file tree
Hide file tree
Showing 23 changed files with 84 additions and 31 deletions.
Binary file modified Assets/Plugins/MagicaUnity/Examples/Models/Door/Door.asset
Binary file not shown.
Binary file modified Assets/Plugins/MagicaUnity/Examples/Models/Panel/Panel.asset
Binary file not shown.
Binary file modified Assets/Plugins/MagicaUnity/Examples/Models/Panels/Corner_Tile.asset
Binary file not shown.
Binary file modified Assets/Plugins/MagicaUnity/Examples/Models/Panels/Floor_Tile.asset
Binary file not shown.
Binary file modified Assets/Plugins/MagicaUnity/Examples/Models/Panels/Wall_Tile.asset
Binary file not shown.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file not shown.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file not shown.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file modified Assets/Plugins/MagicaUnity/Examples/Prefabs/Parts/Corner.prefab
Binary file not shown.
Binary file modified Assets/Plugins/MagicaUnity/Examples/Prefabs/Parts/Floor.prefab
Binary file not shown.
Binary file modified Assets/Plugins/MagicaUnity/Examples/Prefabs/Parts/Wall.prefab
Binary file not shown.
Binary file not shown.
27 changes: 19 additions & 8 deletions Assets/Plugins/MagicaUnity/Scripts/Editor/VoxModelEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,16 @@ namespace GN00T.MagicaUnity
public class VoxModelEditor : Editor
{
private string filePath = string.Empty;
private VoxModel targetModel;
public override void OnInspectorGUI()
{
base.OnInspectorGUI();
VoxModel model = target as VoxModel;
if (targetModel == null || model != targetModel)
{
filePath = model.modelSource;
targetModel = model;
}
GUILayout.BeginHorizontal();
GUILayout.Label("File:", GUILayout.ExpandWidth(false));
GUILayout.TextArea(filePath.Replace(Application.dataPath, ""));
Expand All @@ -27,23 +33,28 @@ public override void OnInspectorGUI()
{
if (GUILayout.Button("Generate Model", GUILayout.ExpandWidth(false)))
{
MagicaVoxelParser parser = new MagicaVoxelParser(model.modelScale);
MagicaVoxelParser parser = new MagicaVoxelParser();
model.modelSource = filePath;
parser.LoadModel(filePath, model);
EditorUtility.SetDirty(model);
string path = AssetDatabase.GetAssetPath(model);
string name = Path.GetFileNameWithoutExtension(path);
Object[] subAssets = AssetDatabase.LoadAllAssetsAtPath(AssetDatabase.GetAssetPath(model));
//load asset meshes
List<Mesh> assetMeshes = new List<Mesh>();
for (int i = 0; i < subAssets.Length; i++)
if (subAssets[i] is Mesh)
assetMeshes.Add(subAssets[i] as Mesh);
int max = model.meshes.Count;
bool update = false;
Mesh m,temp;
for (int i = max-1; i >=0; i--)
for (int i = 0; i <max; i++)
{
//they are stored backwards
if (i < subAssets.Length - 1)
//get mesh
if (i < assetMeshes.Count)
{
update = false;
m = subAssets[i] as Mesh;
//asset wasn't a mesh create new
m = assetMeshes[i];
if (m == null)
{
update = true;
Expand All @@ -69,7 +80,7 @@ public override void OnInspectorGUI()
Unwrapping.GenerateSecondaryUVSet(m);
//new mesh
if (update)
AssetDatabase.AddObjectToAsset(m, target);
AssetDatabase.AddObjectToAsset(m, targetModel);
model.meshes[i] = m;
}
}
Expand All @@ -82,7 +93,7 @@ public override void OnInspectorGUI()
}
}
}
EditorUtility.SetDirty(target);
EditorUtility.SetDirty(targetModel);
AssetDatabase.SaveAssets();
AssetDatabase.Refresh();
}
Expand Down
47 changes: 27 additions & 20 deletions Assets/Plugins/MagicaUnity/Scripts/MagicaVoxelParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,36 +19,48 @@ public class MagicaVoxelParser
private const string RGBA = "RGBA";
private const string MATT = "MATT";
private const string PACK = "PACK";
private const int VERSION = 150;
private int childCount = 0;
private float scale;
private Quaternion toUnity = Quaternion.AngleAxis(-90, Vector3.right);
public MagicaVoxelParser(float scale = 1)
public MagicaVoxelParser()
{
this.scale = scale;
}

public bool LoadModel(string absolutePath, VoxModel output)
{
//Load the whole file
BinaryReader reader = new BinaryReader(new MemoryStream(File.ReadAllBytes(absolutePath)));
string head = new string(reader.ReadChars(4));
if (!head.Equals(HEADER))
{
Debug.LogError("Not a MagicaVoxel File!");
return false;
}
int version = reader.ReadInt32();
if (default_loaded_palette == null)
LoadDefaultPallete();
if (version != VERSION)
Debug.LogWarning("Version number:" + version + " Was designed for " + VERSION);
resetModel(output);
childCount = 0;
while (reader.BaseStream.Position != reader.BaseStream.Length)
ReadChunk(reader, output);
reader.Close();
return false;
if (output.pallete == null)
output.pallete = LoadDefaultPallete();
VoxMesher mesher = new VoxMesher();
for (int i = 0; i < output.meshData.Count; i++) {
Mesh m = new Mesh();
mesher.MeshVoxelData(output, i, m);
output.meshes.Add(m);
}
return true;
}
/// <summary>
/// Clears model data
/// </summary>
/// <param name="model"></param>
private void resetModel(VoxModel model)
{
model.pallete = default_loaded_palette;
model.pallete = null;
if (model.meshData != null)
model.meshData.Clear();
else
Expand All @@ -61,10 +73,10 @@ private void resetModel(VoxModel model)
/// <summary>
/// Loads the default color pallete
/// </summary>
private void LoadDefaultPallete()
private Color[] LoadDefaultPallete()
{
int colorCount = default_palette.Length;
default_loaded_palette = new Color[colorCount];
Color[] default_loaded_palette = new Color[256];
byte r, g, b, a;
uint tcolor;
for (int i = 0; i < colorCount; i++)
Expand All @@ -76,17 +88,18 @@ private void LoadDefaultPallete()
a = (byte)((tcolor >> 24) & 0xff);
default_loaded_palette[i] = (Color)new Color32(r, g, b, a);
}
return default_loaded_palette;
}
/// <summary>
/// palletes are offset by 1
/// </summary>
/// <param name="pallete"></param>
/// <returns></returns>
private Color[] LoadPallete(byte[] pallete)
private Color[] LoadPallete(BinaryReader cr)
{
Color[] color = new Color[256];
for (int i = 0; i < 254; i++)
color[i + 1] = (Color)new Color32(pallete[i * 4], pallete[i * 4 + 1], pallete[i * 4 + 2], pallete[i * 4 + 3]);
for (int i = 0; i <=254; i++)
color[i+1] = (Color)new Color32(cr.ReadByte(), cr.ReadByte(), cr.ReadByte(), cr.ReadByte());
return color;
}

Expand All @@ -112,8 +125,6 @@ private void ReadChunk(BinaryReader reader, VoxModel output)
childCount++;
break;
case XYZ:
VoxMesher mesher = new VoxMesher();

Vector3 pos = new Vector3();
int voxelCount = chunkReader.ReadInt32();
VoxelData frame = output.meshData[childCount - 1];
Expand All @@ -124,12 +135,11 @@ private void ReadChunk(BinaryReader reader, VoxModel output)
y = chunkReader.ReadByte();
z = chunkReader.ReadByte();
pos = toUnity * pos;
frame.Set(x, z, y, (byte)(chunkReader.ReadByte() + 1));
frame.Set(x, z, y, (byte)(chunkReader.ReadByte()));
}
output.meshes.Add(mesher.MeshVoxelData(output, childCount - 1, new Mesh()));
break;
case RGBA:
output.pallete = LoadPallete(chunk);
output.pallete = LoadPallete(chunkReader);
break;
case MATT:
break;
Expand All @@ -150,9 +160,6 @@ private void ReadChunk(BinaryReader reader, VoxModel output)
childReader.Close();
}


private static Color[] default_loaded_palette = null;

private static uint[] default_palette = new uint[] {
0x00000000, 0xffffffff, 0xffccffff, 0xff99ffff, 0xff66ffff, 0xff33ffff, 0xff00ffff, 0xffffccff, 0xffccccff, 0xff99ccff, 0xff66ccff, 0xff33ccff, 0xff00ccff, 0xffff99ff, 0xffcc99ff, 0xff9999ff,
0xff6699ff, 0xff3399ff, 0xff0099ff, 0xffff66ff, 0xffcc66ff, 0xff9966ff, 0xff6666ff, 0xff3366ff, 0xff0066ff, 0xffff33ff, 0xffcc33ff, 0xff9933ff, 0xff6633ff, 0xff3333ff, 0xff0033ff, 0xffff00ff,
Expand Down
2 changes: 1 addition & 1 deletion Assets/Plugins/MagicaUnity/Scripts/VoxMesher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ public Mesh MeshVoxelData(VoxModel data, int frame, Mesh result)
new Vector3(xp[0] + du[0] + dv[0], xp[1] + du[1] + dv[1], xp[2] + du[2] + dv[2]) - originOffset,
new Vector3(xp[0] + dv[0], xp[1] + dv[1], xp[2] + dv[2]) - originOffset,
new Vector3(q[0], q[1], q[2]),
colorList[colorValue - 1], maskValue < 0, verts, normals, colors, indices);
colorList[colorValue], maskValue < 0, verts, normals, colors, indices);
//Increment counters and continue
//Zero-out mask
for (l = 0; l < h; ++l)
Expand Down
3 changes: 3 additions & 0 deletions Assets/Plugins/MagicaUnity/Scripts/VoxModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,8 @@ public class VoxModel : ScriptableObject
public float modelScale = 1f;
[Header("Origin of model scale .5 center 0 left 1 right for each axis")]
public Vector3 origin = new Vector3(.5f, .5f, .5f);
#if UNITY_EDITOR
public string modelSource = "";
#endif
}
}
Binary file not shown.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file modified Assets/Plugins/MagicaUnity/VoxelModels/Door.vox
Binary file not shown.
Binary file not shown.
8 changes: 8 additions & 0 deletions Assets/Plugins/MagicaUnity/VoxelModels/PalleteTest.vox.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit fe8c4cc

Please sign in to comment.