200 lines
4.8 KiB
C#
200 lines
4.8 KiB
C#
using System;
|
|
using UnityEngine;
|
|
|
|
namespace ProCore.Decals
|
|
{
|
|
[Serializable]
|
|
public class Decal
|
|
{
|
|
private static Vector3 DefaultRotation = new Vector3(-45f, 45f, 0f);
|
|
|
|
private static Vector3 DefaultScale = new Vector3(0.8f, 1.2f, 1f);
|
|
|
|
public string name;
|
|
|
|
public string id;
|
|
|
|
public bool isPacked;
|
|
|
|
public string materialId;
|
|
|
|
public Vector3 rotation;
|
|
|
|
public Vector3 scale;
|
|
|
|
public Rect atlasRect;
|
|
|
|
public int orgGroup;
|
|
|
|
public int orgIndex;
|
|
|
|
public int atlasGroup;
|
|
|
|
public int atlasIndex;
|
|
|
|
public Placement rotationPlacement;
|
|
|
|
public Placement scalePlacement;
|
|
|
|
public Texture2D texture;
|
|
|
|
public Decal()
|
|
{
|
|
}
|
|
|
|
public Decal(Texture2D img)
|
|
{
|
|
name = img.name;
|
|
texture = img;
|
|
materialId = string.Empty;
|
|
isPacked = false;
|
|
rotation = new Vector3(-45f, 45f, 0f);
|
|
scale = new Vector3(0.8f, 1.2f, 1f);
|
|
atlasRect = new Rect(0f, 0f, 0f, 0f);
|
|
}
|
|
|
|
public override string ToString()
|
|
{
|
|
return name + "(Org: " + orgIndex + " Atlas: " + atlasIndex + " Packed: " + isPacked + ")";
|
|
}
|
|
|
|
public static bool Deserialize(string txt, out Decal decal)
|
|
{
|
|
decal = new Decal();
|
|
string[] array = txt.Replace("{", string.Empty).Replace("}", string.Empty).Trim()
|
|
.Split('\n');
|
|
if (array.Length < 11)
|
|
{
|
|
return false;
|
|
}
|
|
decal.name = array[0];
|
|
decal.id = array[1];
|
|
decal.rotation = DefaultRotation;
|
|
if (!Vec3WithString(array[2], ref decal.rotation))
|
|
{
|
|
Debug.LogWarning("Failed parsing default rotation values. Using defaults.");
|
|
}
|
|
decal.scale = DefaultScale;
|
|
if (!Vec3WithString(array[3], ref decal.scale))
|
|
{
|
|
Debug.LogWarning("Failed parsing default scale values. Using defaults.");
|
|
}
|
|
Vector4 vec = Vector4.one;
|
|
if (!Vec4WithString(array[4], ref vec))
|
|
{
|
|
Debug.LogWarning("Failed parsing atlas rect. Using default.");
|
|
}
|
|
decal.atlasRect = Vec4ToRect(vec);
|
|
decal.orgGroup = 0;
|
|
if (!int.TryParse(array[5], out decal.orgGroup))
|
|
{
|
|
Debug.LogWarning("Failed parsing organizational group. Setting to group 0");
|
|
}
|
|
decal.atlasGroup = 0;
|
|
if (!int.TryParse(array[6], out decal.atlasGroup))
|
|
{
|
|
Debug.LogWarning("Failed parsing atlas group. Setting to group 0");
|
|
}
|
|
decal.orgIndex = 0;
|
|
if (!int.TryParse(array[7], out decal.orgIndex))
|
|
{
|
|
Debug.LogWarning("Failed parsing organizational group. Setting to group 0");
|
|
}
|
|
decal.atlasIndex = 0;
|
|
if (!int.TryParse(array[8], out decal.atlasIndex))
|
|
{
|
|
Debug.LogWarning("Failed parsing atlas group. Setting to group 0");
|
|
}
|
|
decal.rotationPlacement = Placement.Fixed;
|
|
int result;
|
|
if (!int.TryParse(array[9], out result))
|
|
{
|
|
Debug.LogWarning("Failed parsing rotationPlacement. Setting to \"Fixed\"");
|
|
}
|
|
else
|
|
{
|
|
decal.rotationPlacement = (Placement)result;
|
|
}
|
|
decal.scalePlacement = Placement.Fixed;
|
|
if (!int.TryParse(array[10], out result))
|
|
{
|
|
Debug.LogWarning("Failed parsing scalePlacement. Setting to \"Fixed\"");
|
|
}
|
|
else
|
|
{
|
|
decal.scalePlacement = (Placement)result;
|
|
}
|
|
bool result2 = false;
|
|
if (array.Length < 12 || !bool.TryParse(array[11], out result2))
|
|
{
|
|
result2 = false;
|
|
Debug.LogWarning("Failed parsing packed. Setting to \"false\"");
|
|
}
|
|
else
|
|
{
|
|
decal.isPacked = result2;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
public string Serialize()
|
|
{
|
|
return "{\n" + name.Replace(",", "\\,") + "\n" + id + "\n" + rotation.ToString() + "\n" + scale.ToString() + "\n(" + atlasRect.xMin + ", " + atlasRect.yMin + ", " + atlasRect.width + ", " + atlasRect.height + ")\n" + orgGroup + "\n" + atlasGroup + "\n" + orgIndex + "\n" + atlasIndex + "\n" + (int)rotationPlacement + "\n" + (int)scalePlacement + "\n" + isPacked + "\n}";
|
|
}
|
|
|
|
private static bool Vec3WithString(string str, ref Vector3 vec3)
|
|
{
|
|
string[] array = str.Replace("(", string.Empty).Replace(")", string.Empty).Split(',');
|
|
float result;
|
|
if (!float.TryParse(array[0], out result))
|
|
{
|
|
return false;
|
|
}
|
|
float result2;
|
|
if (!float.TryParse(array[1], out result2))
|
|
{
|
|
return false;
|
|
}
|
|
float result3;
|
|
if (!float.TryParse(array[2], out result3))
|
|
{
|
|
return false;
|
|
}
|
|
vec3 = new Vector3(result, result2, result3);
|
|
return true;
|
|
}
|
|
|
|
private static bool Vec4WithString(string str, ref Vector4 vec4)
|
|
{
|
|
string[] array = str.Replace("(", string.Empty).Replace(")", string.Empty).Split(',');
|
|
float result;
|
|
if (!float.TryParse(array[0], out result))
|
|
{
|
|
return false;
|
|
}
|
|
float result2;
|
|
if (!float.TryParse(array[1], out result2))
|
|
{
|
|
return false;
|
|
}
|
|
float result3;
|
|
if (!float.TryParse(array[2], out result3))
|
|
{
|
|
return false;
|
|
}
|
|
float result4;
|
|
if (!float.TryParse(array[3], out result4))
|
|
{
|
|
return false;
|
|
}
|
|
vec4 = new Vector4(result, result2, result3, result4);
|
|
return true;
|
|
}
|
|
|
|
private static Rect Vec4ToRect(Vector4 v)
|
|
{
|
|
return new Rect(v.x, v.y, v.z, v.w);
|
|
}
|
|
}
|
|
}
|