mirror of
https://github.com/maxartz15/MA_TextureAtlasser.git
synced 2025-06-14 15:26:10 +02:00
Restructured files and folders.
This commit is contained in:
9
Assets/MA_ToolBox/MA_Utilities/EditorUtils.meta
Normal file
9
Assets/MA_ToolBox/MA_Utilities/EditorUtils.meta
Normal file
@ -0,0 +1,9 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 6aa9d38bd8e54d0448095604d7d62fd1
|
||||
folderAsset: yes
|
||||
timeCreated: 1522672244
|
||||
licenseType: Free
|
||||
DefaultImporter:
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
@ -0,0 +1,62 @@
|
||||
//https://github.com/maxartz15/MA_EditorUtils
|
||||
|
||||
#if UNITY_EDITOR
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using UnityEditor;
|
||||
|
||||
namespace MA_Editor.Grid
|
||||
{
|
||||
public static class Grid
|
||||
{
|
||||
public static void DrawGrid(Rect editorWorkRect, float gridSpacing, Color gridColor)
|
||||
{
|
||||
//Process grid spacing values
|
||||
int widthDivs = Mathf.CeilToInt(editorWorkRect.width / gridSpacing);
|
||||
int heightDivs = Mathf.CeilToInt(editorWorkRect.height / gridSpacing);
|
||||
|
||||
//Using handles
|
||||
Handles.BeginGUI();
|
||||
Handles.color = gridColor;
|
||||
|
||||
for (int x = 0; x < widthDivs; x++)
|
||||
{
|
||||
Handles.DrawLine(new Vector3(gridSpacing * x, 0, 0), new Vector3(gridSpacing * x, editorWorkRect.height, 0));
|
||||
}
|
||||
|
||||
for (int y = 0; y < heightDivs; y++)
|
||||
{
|
||||
Handles.DrawLine(new Vector3(0, gridSpacing * y, 0), new Vector3(editorWorkRect.width, gridSpacing * y, 0));
|
||||
}
|
||||
|
||||
Handles.color = Color.white;
|
||||
Handles.EndGUI();
|
||||
}
|
||||
|
||||
public static void DrawZoomableGrid(Rect editorWorkRect, float gridSpacing, Color gridColor, Vector2 zoomCoordsOrigin)
|
||||
{
|
||||
//Process grid spacing values
|
||||
int widthDivs = Mathf.CeilToInt(editorWorkRect.width / gridSpacing);
|
||||
int heightDivs = Mathf.CeilToInt(editorWorkRect.height / gridSpacing);
|
||||
|
||||
//Using handles
|
||||
Handles.BeginGUI();
|
||||
Handles.color = gridColor;
|
||||
|
||||
for (int x = 1; x < widthDivs; x++)
|
||||
{
|
||||
Handles.DrawLine(new Vector3(gridSpacing * x - zoomCoordsOrigin.x, -zoomCoordsOrigin.y, 0), new Vector3(gridSpacing * x - zoomCoordsOrigin.x, editorWorkRect.height - zoomCoordsOrigin.y, 0));
|
||||
}
|
||||
|
||||
for (int y = 1; y < heightDivs; y++)
|
||||
{
|
||||
Handles.DrawLine(new Vector3(-zoomCoordsOrigin.x, gridSpacing * y - zoomCoordsOrigin.y, 0), new Vector3(editorWorkRect.width - zoomCoordsOrigin.x, gridSpacing * y - zoomCoordsOrigin.y, 0));
|
||||
}
|
||||
|
||||
Handles.color = Color.white;
|
||||
Handles.EndGUI();
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
@ -0,0 +1,12 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 64df885d83caddf48acfa78533a3e76f
|
||||
timeCreated: 1522672827
|
||||
licenseType: Free
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
@ -0,0 +1,74 @@
|
||||
//https://github.com/maxartz15/MA_EditorUtils
|
||||
|
||||
//References:
|
||||
//http://martinecker.com/martincodes/unity-editor-window-zooming/
|
||||
|
||||
#if UNITY_EDITOR
|
||||
using UnityEngine;
|
||||
using MA_Editor;
|
||||
|
||||
namespace MA_Editor.RectUtils
|
||||
{
|
||||
public static class RectUtils
|
||||
{
|
||||
//Start http://martinecker.com/martincodes/unity-editor-window-zooming/
|
||||
public static Vector2 TopLeft(this Rect rect)
|
||||
{
|
||||
return new Vector2(rect.xMin, rect.yMin);
|
||||
}
|
||||
|
||||
public static Rect ScaleSizeBy(this Rect rect, float scale)
|
||||
{
|
||||
return rect.ScaleSizeBy(scale, rect.center);
|
||||
}
|
||||
|
||||
public static Rect ScaleSizeBy(this Rect rect, float scale, Vector2 pivotPoint)
|
||||
{
|
||||
Rect result = rect;
|
||||
result.x -= pivotPoint.x;
|
||||
result.y -= pivotPoint.y;
|
||||
result.xMin *= scale;
|
||||
result.xMax *= scale;
|
||||
result.yMin *= scale;
|
||||
result.yMax *= scale;
|
||||
result.x += pivotPoint.x;
|
||||
result.y += pivotPoint.y;
|
||||
return result;
|
||||
}
|
||||
|
||||
public static Rect ScaleSizeBy(this Rect rect, Vector2 scale)
|
||||
{
|
||||
return rect.ScaleSizeBy(scale, rect.center);
|
||||
}
|
||||
|
||||
public static Rect ScaleSizeBy(this Rect rect, Vector2 scale, Vector2 pivotPoint)
|
||||
{
|
||||
Rect result = rect;
|
||||
result.x -= pivotPoint.x;
|
||||
result.y -= pivotPoint.y;
|
||||
result.xMin *= scale.x;
|
||||
result.xMax *= scale.x;
|
||||
result.yMin *= scale.y;
|
||||
result.yMax *= scale.y;
|
||||
result.x += pivotPoint.x;
|
||||
result.y += pivotPoint.y;
|
||||
return result;
|
||||
}
|
||||
//End http://martinecker.com/martincodes/unity-editor-window-zooming/
|
||||
|
||||
public static Rect MultiplyRectSize(Rect rect, float amount)
|
||||
{
|
||||
Rect multipliedRect = new Rect(rect.x, rect.y, rect.width * amount, rect.height * amount);
|
||||
return multipliedRect;
|
||||
}
|
||||
|
||||
public static Rect MultiplyRectSizeAndCenter(Rect rect, float amount)
|
||||
{
|
||||
Rect multipliedRect = new Rect(rect.x, rect.y, rect.width * amount, rect.height * amount);
|
||||
multipliedRect.x = -(multipliedRect.width / 2);
|
||||
multipliedRect.y = -(multipliedRect.height / 2);
|
||||
return multipliedRect;
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
@ -0,0 +1,12 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 1ec1c2403bd92a84798e0ad68ebf4bad
|
||||
timeCreated: 1522671138
|
||||
licenseType: Free
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
@ -0,0 +1,43 @@
|
||||
//https://github.com/maxartz15/MA_EditorUtils
|
||||
|
||||
//References:
|
||||
//http://martinecker.com/martincodes/unity-editor-window-zooming/
|
||||
|
||||
#if UNITY_EDITOR
|
||||
using UnityEngine;
|
||||
using MA_Editor;
|
||||
using MA_Editor.RectUtils;
|
||||
|
||||
namespace MA_Editor.GUILayoutZoom
|
||||
{
|
||||
//http://martinecker.com/martincodes/unity-editor-window-zooming/
|
||||
public class GUILayoutZoom
|
||||
{
|
||||
private const float EditorWindowTabHeight = 21.0f; //The height of the editor window top bar. (were the name, zoom and exit buttons are)
|
||||
private static Matrix4x4 prevGuiMatrix;
|
||||
|
||||
public static Rect BeginArea(float zoomScale, Rect screenCoordsArea)
|
||||
{
|
||||
GUI.EndGroup(); // End the group Unity begins automatically for an EditorWindow to clip out the window tab. This allows us to draw outside of the size of the EditorWindow.
|
||||
|
||||
Rect clippedArea = screenCoordsArea.ScaleSizeBy(1.0f / zoomScale, screenCoordsArea.TopLeft());
|
||||
clippedArea.y += EditorWindowTabHeight;
|
||||
GUI.BeginGroup(clippedArea);
|
||||
|
||||
prevGuiMatrix = GUI.matrix;
|
||||
Matrix4x4 translation = Matrix4x4.TRS(clippedArea.TopLeft(), Quaternion.identity, Vector3.one);
|
||||
Matrix4x4 scale = Matrix4x4.Scale(new Vector3(zoomScale, zoomScale, 1.0f));
|
||||
GUI.matrix = translation * scale * translation.inverse * GUI.matrix;
|
||||
|
||||
return clippedArea;
|
||||
}
|
||||
|
||||
public static void EndArea()
|
||||
{
|
||||
GUI.matrix = prevGuiMatrix;
|
||||
GUI.EndGroup();
|
||||
GUI.BeginGroup(new Rect(0.0f, EditorWindowTabHeight, Screen.width, Screen.height));
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
@ -0,0 +1,12 @@
|
||||
fileFormatVersion: 2
|
||||
guid: ea365137c05cbd64489983567390af51
|
||||
timeCreated: 1522669939
|
||||
licenseType: Free
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
9
Assets/MA_ToolBox/MA_Utilities/MeshUtils.meta
Normal file
9
Assets/MA_ToolBox/MA_Utilities/MeshUtils.meta
Normal file
@ -0,0 +1,9 @@
|
||||
fileFormatVersion: 2
|
||||
guid: ac03fa4896f5ed74c80155bbbfccd958
|
||||
folderAsset: yes
|
||||
timeCreated: 1522672266
|
||||
licenseType: Free
|
||||
DefaultImporter:
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
218
Assets/MA_ToolBox/MA_Utilities/MeshUtils/MA_MeshUtils.cs
Normal file
218
Assets/MA_ToolBox/MA_Utilities/MeshUtils/MA_MeshUtils.cs
Normal file
@ -0,0 +1,218 @@
|
||||
//https://github.com/maxartz15/MA_MeshUtils
|
||||
|
||||
//References:
|
||||
//http://wiki.unity3d.com/index.php?title=ObjExporter
|
||||
|
||||
#if UNITY_EDITOR
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Text;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using UnityEngine;
|
||||
using UnityEditor;
|
||||
|
||||
namespace MA_Mesh
|
||||
{
|
||||
public static class MA_MeshUtils
|
||||
{
|
||||
public static void MA_SaveMeshAsset(Mesh mesh, string savePath, string meshName = "")
|
||||
{
|
||||
Mesh newMesh = new Mesh();
|
||||
newMesh.SetVertices(new List<Vector3>(mesh.vertices));
|
||||
newMesh.SetTriangles(mesh.triangles, 0);
|
||||
newMesh.SetUVs(0, new List<Vector2>(mesh.uv));
|
||||
|
||||
if(meshName == "")
|
||||
{
|
||||
newMesh.name = mesh.name;
|
||||
}
|
||||
else
|
||||
{
|
||||
newMesh.name = meshName;
|
||||
}
|
||||
|
||||
AssetDatabase.CreateAsset(newMesh, savePath);
|
||||
AssetDatabase.SaveAssets();
|
||||
}
|
||||
|
||||
public static Mesh MA_DuplicateMesh(Mesh mesh)
|
||||
{
|
||||
Mesh newMesh = new Mesh
|
||||
{
|
||||
name = mesh.name,
|
||||
bounds = mesh.bounds,
|
||||
colors = mesh.colors,
|
||||
subMeshCount = mesh.subMeshCount
|
||||
};
|
||||
|
||||
newMesh.SetVertices(new List<Vector3>(mesh.vertices));
|
||||
for (int i = 0; i < mesh.subMeshCount; i++)
|
||||
{
|
||||
newMesh.SetTriangles(mesh.GetTriangles(i), i);
|
||||
}
|
||||
newMesh.SetNormals(new List<Vector3>(mesh.normals));
|
||||
newMesh.SetUVs(0, new List<Vector2>(mesh.uv));
|
||||
newMesh.SetTangents(new List<Vector4>(mesh.tangents));
|
||||
|
||||
return newMesh;
|
||||
}
|
||||
|
||||
public static Mesh MA_ReMapUV(this Mesh mesh, Vector2 atlasSize, Vector2 textureSize, Vector2 texturePosition, int uvChannel = 0)
|
||||
{
|
||||
/*
|
||||
0 1
|
||||
512 x 512
|
||||
|
||||
0 .5 = 1 / 512 * 256
|
||||
256 x 256
|
||||
|
||||
+ pos
|
||||
*/
|
||||
|
||||
List<Vector2> uvs = new List<Vector2>();
|
||||
|
||||
//Get UV's
|
||||
mesh.GetUVs(uvChannel, uvs);
|
||||
|
||||
foreach (Vector2 uvCordinate in uvs)
|
||||
{
|
||||
float x = (uvCordinate.x / atlasSize.x * textureSize.x) + texturePosition.x;
|
||||
float y = (uvCordinate.y / atlasSize.y * textureSize.y) + texturePosition.y;
|
||||
uvCordinate.Set(x, y);
|
||||
}
|
||||
|
||||
mesh.SetUVs(uvChannel, uvs);
|
||||
|
||||
return mesh;
|
||||
}
|
||||
|
||||
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++)
|
||||
{
|
||||
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)
|
||||
{
|
||||
newX = Wrap(newX, xMax, xMin + xMax);
|
||||
newY = Wrap(newY, yMax, yMin + yMax);
|
||||
}
|
||||
|
||||
uvs[i] = new Vector2(newX, newY);
|
||||
}
|
||||
|
||||
mesh.SetUVs(uvChannel, uvs);
|
||||
|
||||
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)
|
||||
{
|
||||
int vertexOffset = 0;
|
||||
int normalOffset = 0;
|
||||
int uvOffset = 0;
|
||||
|
||||
Material material = new Material(Shader.Find("Standard"));
|
||||
|
||||
StringBuilder sb = new StringBuilder();
|
||||
|
||||
sb.Append("g ").Append(mesh.name).Append("\n");
|
||||
|
||||
foreach(Vector3 v in mesh.vertices)
|
||||
{
|
||||
//This is sort of ugly - inverting x-component since we're in
|
||||
//a different coordinate system than "everyone" is "used to".
|
||||
sb.Append(string.Format("v {0} {1} {2}\n", -v.x, v.y, v.z));
|
||||
}
|
||||
|
||||
sb.Append("\n");
|
||||
|
||||
foreach(Vector3 v in mesh.normals)
|
||||
{
|
||||
sb.Append(string.Format("vn {0} {1} {2}\n", -v.x, v.y, v.z));
|
||||
}
|
||||
|
||||
sb.Append("\n");
|
||||
|
||||
foreach(Vector3 v in mesh.uv)
|
||||
{
|
||||
sb.Append(string.Format("vt {0} {1}\n", v.x, v.y));
|
||||
}
|
||||
|
||||
for (int m = 0 ; m < mesh.subMeshCount; m++)
|
||||
{
|
||||
sb.Append("\n");
|
||||
sb.Append("usemtl ").Append(material.name + m).Append("\n");
|
||||
sb.Append("usemap ").Append(material.name + m).Append("\n");
|
||||
|
||||
// int[] triangles = mesh.GetTriangles(m);
|
||||
// for (int i = 0; i < triangles.Length; i += 3)
|
||||
// {
|
||||
// sb.Append(string.Format("f {0}/{0}/{0} {1}/{1}/{1} {2}/{2}/{2}\n", triangles[i]+1, triangles[i+1]+1, triangles[i+2]+1));
|
||||
// }
|
||||
|
||||
int[] triangles = mesh.GetTriangles(m);
|
||||
for (int i = 0; i < triangles.Length; i += 3)
|
||||
{
|
||||
//Because we inverted the x-component, we also needed to alter the triangle winding.
|
||||
sb.Append(string.Format("f {1}/{1}/{1} {0}/{0}/{0} {2}/{2}/{2}\n", triangles[i]+ 1 + vertexOffset, triangles[i + 1] + 1 + normalOffset, triangles[i +2 ] + 1 + uvOffset));
|
||||
}
|
||||
}
|
||||
|
||||
//vertexOffset += mesh.vertices.Length;
|
||||
//normalOffset += mesh.normals.Length;
|
||||
//uvOffset += mesh.uv.Length;
|
||||
|
||||
return sb.ToString();
|
||||
}
|
||||
|
||||
public static void MeshToFile(Mesh mesh, string filename, string savePath)
|
||||
{
|
||||
using (StreamWriter sw = new StreamWriter(savePath + filename + ".obj"))
|
||||
{
|
||||
sw.Write(MeshToString(mesh));
|
||||
}
|
||||
}
|
||||
//End
|
||||
}
|
||||
|
||||
//struct ObjMaterial
|
||||
//{
|
||||
// public string name;
|
||||
// public string textureName;
|
||||
//}
|
||||
}
|
||||
#endif
|
@ -0,0 +1,12 @@
|
||||
fileFormatVersion: 2
|
||||
guid: b8292e68eba0ff5488f218a303e60a5b
|
||||
timeCreated: 1518370877
|
||||
licenseType: Free
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
9
Assets/MA_ToolBox/MA_Utilities/TextureUtils.meta
Normal file
9
Assets/MA_ToolBox/MA_Utilities/TextureUtils.meta
Normal file
@ -0,0 +1,9 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 9727f759199d13e4e814f5fb3b8a41de
|
||||
folderAsset: yes
|
||||
timeCreated: 1522672279
|
||||
licenseType: Free
|
||||
DefaultImporter:
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
230
Assets/MA_ToolBox/MA_Utilities/TextureUtils/MA_TextureUtils.cs
Normal file
230
Assets/MA_ToolBox/MA_Utilities/TextureUtils/MA_TextureUtils.cs
Normal file
@ -0,0 +1,230 @@
|
||||
//https://github.com/maxartz15/MA_TextureUtils
|
||||
|
||||
//References:
|
||||
//http://www.gamasutra.com/blogs/JoshSutphin/20131007/201829/Adding_to_Unitys_BuiltIn_Classes_Using_Extension_Methods.php
|
||||
//https://forum.unity3d.com/threads/contribution-texture2d-blur-in-c.185694/
|
||||
//http://orbcreation.com/orbcreation/page.orb?1180
|
||||
//https://support.unity3d.com/hc/en-us/articles/206486626-How-can-I-get-pixels-from-unreadable-textures-
|
||||
//https://github.com/maxartz15/MA_TextureAtlasser/commit/9f5240967a51692fa2a17a6b3c8d124dd5dc60f9
|
||||
|
||||
#if UNITY_EDITOR
|
||||
using UnityEngine;
|
||||
using UnityEditor;
|
||||
using System.IO;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System;
|
||||
|
||||
namespace MA_Texture
|
||||
{
|
||||
public static class MA_TextureUtils
|
||||
{
|
||||
public static Texture ConvertToReadableTexture(Texture texture)
|
||||
{
|
||||
if (texture == null)
|
||||
return texture;
|
||||
// Create a temporary RenderTexture of the same size as the texture
|
||||
RenderTexture tmp = RenderTexture.GetTemporary(
|
||||
texture.width,
|
||||
texture.height,
|
||||
0,
|
||||
RenderTextureFormat.Default,
|
||||
RenderTextureReadWrite.Linear);
|
||||
|
||||
// Blit the pixels on texture to the RenderTexture
|
||||
Graphics.Blit(texture, tmp);
|
||||
|
||||
// Backup the currently set RenderTexture
|
||||
RenderTexture previous = RenderTexture.active;
|
||||
|
||||
// Set the current RenderTexture to the temporary one we created
|
||||
RenderTexture.active = tmp;
|
||||
|
||||
// Create a new readable Texture2D to copy the pixels to it
|
||||
Texture2D myTexture2D = new Texture2D(texture.width, texture.width);
|
||||
|
||||
// Copy the pixels from the RenderTexture to the new Texture
|
||||
myTexture2D.ReadPixels(new Rect(0, 0, tmp.width, tmp.height), 0, 0);
|
||||
myTexture2D.Apply();
|
||||
myTexture2D.name = texture.name;
|
||||
|
||||
// Reset the active RenderTexture
|
||||
RenderTexture.active = previous;
|
||||
|
||||
// Release the temporary RenderTexture
|
||||
RenderTexture.ReleaseTemporary(tmp);
|
||||
// "myTexture2D" now has the same pixels from "texture" and it's readable.
|
||||
|
||||
return myTexture2D;
|
||||
}
|
||||
|
||||
#region Save
|
||||
public static Texture2D MA_Save2D(this Texture2D texture, string textureName, string savePath)
|
||||
{
|
||||
if (!Directory.Exists(savePath))
|
||||
Directory.CreateDirectory(savePath);
|
||||
|
||||
FileStream fs = new FileStream(savePath + "/" + textureName + ".png", FileMode.Create);
|
||||
BinaryWriter bw = new BinaryWriter(fs);
|
||||
|
||||
bw.Write(texture.EncodeToPNG());
|
||||
bw.Close();
|
||||
fs.Close();
|
||||
|
||||
AssetDatabase.Refresh();
|
||||
|
||||
return texture;
|
||||
}
|
||||
|
||||
public static Texture MA_Save(this Texture texture, string name, string savePath)
|
||||
{
|
||||
Texture2D texture2D = (Texture2D)MA_TextureUtils.ConvertToReadableTexture(texture);
|
||||
|
||||
texture2D.MA_Save2D(name, savePath);
|
||||
|
||||
texture = texture2D;
|
||||
|
||||
return texture;
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region Scale
|
||||
public enum TextureScaleMode
|
||||
{
|
||||
Bilinear,
|
||||
Point
|
||||
}
|
||||
|
||||
public static Texture MA_Scale(this Texture texture, int width, int height, TextureScaleMode scaleMode)
|
||||
{
|
||||
Texture2D texture2D = (Texture2D)MA_TextureUtils.ConvertToReadableTexture(texture);
|
||||
|
||||
texture2D.MA_Scale2D(width, height, scaleMode);
|
||||
|
||||
texture = texture2D;
|
||||
|
||||
return texture;
|
||||
}
|
||||
|
||||
public static Texture2D MA_Scale2D(this Texture2D texture, int newWidth, int newHeight, TextureScaleMode scaleMode)
|
||||
{
|
||||
Color[] curColors = texture.GetPixels();
|
||||
Color[] newColors = new Color[newWidth * newHeight];
|
||||
|
||||
switch (scaleMode)
|
||||
{
|
||||
case TextureScaleMode.Bilinear:
|
||||
newColors = MA_BilinearScale(curColors, texture.width, texture.height, newWidth, newHeight);
|
||||
break;
|
||||
case TextureScaleMode.Point:
|
||||
newColors = MA_PointScale(curColors, texture.width, texture.height, newWidth, newHeight);
|
||||
break;
|
||||
|
||||
}
|
||||
|
||||
texture.Resize(newWidth, newHeight);
|
||||
texture.SetPixels(newColors);
|
||||
texture.Apply();
|
||||
|
||||
return texture;
|
||||
}
|
||||
|
||||
private static Color[] MA_BilinearScale(Color[] curColors, int curWidth, int curHeight, int newWidth, int newHeight)
|
||||
{
|
||||
Color[] newColors = new Color[newWidth * newHeight];
|
||||
|
||||
float ratioX = 1.0f / ((float)newWidth / (curWidth - 1));
|
||||
float ratioY = 1.0f / ((float)newHeight / (curHeight - 1));
|
||||
|
||||
for (int y = 0; y < newHeight; y++)
|
||||
{
|
||||
int yFloor = Mathf.FloorToInt(y * ratioY);
|
||||
var y1 = yFloor * curWidth;
|
||||
var y2 = (yFloor + 1) * curWidth;
|
||||
var yw = y * newWidth;
|
||||
|
||||
for (int x = 0; x < newWidth; x++)
|
||||
{
|
||||
int xFloor = Mathf.FloorToInt(x * ratioX);
|
||||
var xLerp = x * ratioX - xFloor;
|
||||
|
||||
newColors[yw + x] = ColorLerpUnclamped(ColorLerpUnclamped(curColors[y1 + xFloor], curColors[y1 + xFloor + 1], xLerp),
|
||||
ColorLerpUnclamped(curColors[y2 + xFloor], curColors[y2 + xFloor + 1], xLerp),
|
||||
y * ratioY - yFloor);
|
||||
}
|
||||
}
|
||||
|
||||
return newColors;
|
||||
}
|
||||
|
||||
private static Color[] MA_PointScale(Color[] curColors, int curWidth, int curHeight, int newWidth, int newHeight)
|
||||
{
|
||||
Color[] newColors = new Color[newWidth * newHeight];
|
||||
|
||||
float ratioX = ((float)curWidth) / newWidth;
|
||||
float ratioY = ((float)curHeight) / newHeight;
|
||||
|
||||
for (int y = 0; y < newHeight; y++)
|
||||
{
|
||||
var thisY = Mathf.RoundToInt((ratioY * y) * curWidth);
|
||||
var yw = y * newWidth;
|
||||
|
||||
for (int x = 0; x < newWidth; x++)
|
||||
{
|
||||
newColors[yw + x] = curColors[Mathf.RoundToInt(thisY + ratioX * x)];
|
||||
}
|
||||
}
|
||||
|
||||
return newColors;
|
||||
}
|
||||
|
||||
private static Color ColorLerpUnclamped(Color c1, Color c2, float value)
|
||||
{
|
||||
return new Color(c1.r + (c2.r - c1.r) * value,
|
||||
c1.g + (c2.g - c1.g) * value,
|
||||
c1.b + (c2.b - c1.b) * value,
|
||||
c1.a + (c2.a - c1.a) * value);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region combine
|
||||
public static Texture2D MA_Combine2D(this Texture2D texture, Texture2D combineTexture, int offsetX, int offsetY, bool flipY = true)
|
||||
{
|
||||
for (int x = 0; x < combineTexture.width; x++)
|
||||
{
|
||||
if(flipY)
|
||||
{
|
||||
//Y is 'flipped' because textures are made from left to right, bottom to top. We want to draw from left to right and top to bottom.
|
||||
for (int y = combineTexture.height; y > 0; y--)
|
||||
{
|
||||
texture.SetPixel(x + offsetX, y + (texture.height - offsetY - combineTexture.height), combineTexture.GetPixel(x, y));
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
for (int y = 0; y < combineTexture.height; y++)
|
||||
{
|
||||
texture.SetPixel(x + offsetX, y + offsetY, combineTexture.GetPixel(x, y));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
texture.Apply();
|
||||
|
||||
return 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(combineTexture);
|
||||
|
||||
texture = texture2D.MA_Combine2D(combineTexture2D, offsetX, offsetY);
|
||||
|
||||
return texture;
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
#endif
|
@ -0,0 +1,12 @@
|
||||
fileFormatVersion: 2
|
||||
guid: a156d40c509995b4bbc8c259cdac9519
|
||||
timeCreated: 1518369341
|
||||
licenseType: Free
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
Reference in New Issue
Block a user