Export presets, wrap uv's, bugfixes and formatting.

[MA_TextureAtlas] Updated export window to use scriptable asset export presets. The option to wrap uv's (default = true). Bigfixes and code formatting.
This commit is contained in:
max
2019-11-06 18:08:01 +01:00
parent e81a4ec119
commit a728366035
12 changed files with 507 additions and 261 deletions

View File

@ -39,17 +39,19 @@ namespace MA_Mesh
public static Mesh MA_DuplicateMesh(Mesh mesh)
{
Mesh newMesh = new Mesh();
newMesh.name = mesh.name;
Mesh newMesh = new Mesh
{
name = mesh.name,
bounds = mesh.bounds,
colors = mesh.colors,
subMeshCount = mesh.subMeshCount
};
newMesh.SetVertices(new List<Vector3>(mesh.vertices));
newMesh.bounds = mesh.bounds;
newMesh.colors = mesh.colors.ToArray();
newMesh.subMeshCount = mesh.subMeshCount;
for (int i = 0; i < mesh.subMeshCount; i++)
{
newMesh.SetTriangles(mesh.GetTriangles(i), i);
}
newMesh.subMeshCount = mesh.subMeshCount;
newMesh.SetNormals(new List<Vector3>(mesh.normals));
newMesh.SetUVs(0, new List<Vector2>(mesh.uv));
newMesh.SetTangents(new List<Vector4>(mesh.tangents));
@ -86,27 +88,41 @@ namespace MA_Mesh
return mesh;
}
public static Mesh MA_UVReMap(this Mesh mesh, Vector2 atlasSize, Rect textureRect, int uvChannel = 0, bool flipY = true)
{
List<Vector2> uvs = new List<Vector2>();
public static Mesh MA_UVReMap(this Mesh mesh, Vector2 atlasSize, Rect textureRect, int uvChannel = 0, bool flipY = true, bool wrap = true)
{
//Get UV's
List<Vector2> uvs = new List<Vector2>();
mesh.GetUVs(uvChannel, uvs);
//Min and max bounds in 0-1 space.
float xMin, xMax, yMin, yMax;
xMin = (1f / atlasSize.x * textureRect.width);
xMax = (1f / atlasSize.x * textureRect.x);
yMin = (1f / atlasSize.y * textureRect.height);
//Flip uv's if needed.
if (flipY)
{
yMax = (1f / atlasSize.y * (atlasSize.y - textureRect.height - textureRect.y));
}
else
{
yMax = (1f / atlasSize.y * textureRect.y);
}
for (int i = 0; i < uvs.Count; i++)
{
if(flipY)
float newX = uvs[i].x * xMin + xMax;
float newY = uvs[i].y * yMin + yMax;
//Wrap the verts outside of the uv space around back into the uv space.
if (wrap)
{
uvs[i] = new Vector2((uvs[i].x / atlasSize.x * textureRect.width) + (1 / atlasSize.x * textureRect.x),
(uvs[i].y / atlasSize.y * textureRect.height) + (1 / atlasSize.y * (atlasSize.y - textureRect.height - textureRect.y)));
}
else
{
//Debug.Log("01" + uvs[i].x);
uvs[i] = new Vector2((uvs[i].x / atlasSize.x * textureRect.width) + (1 / atlasSize.x * textureRect.x),
(uvs[i].y / atlasSize.y * textureRect.height) + (1 / atlasSize.y * textureRect.y));
//Debug.Log("02" + uvs[i].x);
newX = Wrap(newX, xMax, xMin + xMax);
newY = Wrap(newY, yMax, yMin + yMax);
}
uvs[i] = new Vector2(newX, newY);
}
mesh.SetUVs(uvChannel, uvs);
@ -114,6 +130,14 @@ namespace MA_Mesh
return mesh;
}
public static float Wrap(float val, float min, float max)
{
val -= (float)Math.Round((val - min) / (max - min)) * (max - min);
if (val < min)
val = val + max - min;
return val;
}
//Start http://wiki.unity3d.com/index.php?title=ObjExporter
public static string MeshToString(Mesh mesh)
{
@ -121,7 +145,6 @@ namespace MA_Mesh
int normalOffset = 0;
int uvOffset = 0;
Material material = new Material(Shader.Find("Standard"));
StringBuilder sb = new StringBuilder();
@ -169,9 +192,9 @@ namespace MA_Mesh
}
}
vertexOffset += mesh.vertices.Length;
normalOffset += mesh.normals.Length;
uvOffset += mesh.uv.Length;
//vertexOffset += mesh.vertices.Length;
//normalOffset += mesh.normals.Length;
//uvOffset += mesh.uv.Length;
return sb.ToString();
}
@ -186,10 +209,10 @@ namespace MA_Mesh
//End
}
struct ObjMaterial
{
public string name;
public string textureName;
}
//struct ObjMaterial
//{
// public string name;
// public string textureName;
//}
}
#endif

View File

@ -218,7 +218,7 @@ namespace MA_Texture
public static Texture MA_Combine(this Texture texture, Texture combineTexture, int offsetX, int offsetY)
{
Texture2D texture2D = (Texture2D)MA_TextureUtils.ConvertToReadableTexture(texture);
Texture2D combineTexture2D = (Texture2D)MA_TextureUtils.ConvertToReadableTexture(texture);
Texture2D combineTexture2D = (Texture2D)MA_TextureUtils.ConvertToReadableTexture(combineTexture);
texture = texture2D.MA_Combine2D(combineTexture2D, offsetX, offsetY);