1.0, gitignore

This commit is contained in:
max
2018-08-27 11:55:21 +02:00
parent 88e5537d9b
commit 13b1344a23
30 changed files with 2092 additions and 0 deletions

View File

@ -0,0 +1,61 @@
//Maxartz15
//Version 1.0
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();
}
}
}

View File

@ -0,0 +1,70 @@
//Maxartz15
//Version 1.0
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;
}
}
}

View File

@ -0,0 +1,39 @@
//Maxartz15
//Version 1.0
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));
}
}
}

View File

@ -0,0 +1,187 @@
//Maxartz15
//Version 1.0
using System;
using System.IO;
using System.Text;
using System.Collections;
using System.Collections.Generic;
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();
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)
{
List<Vector2> uvs = new List<Vector2>();
//Get UV's
mesh.GetUVs(uvChannel, uvs);
for (int i = 0; i < uvs.Count; i++)
{
if(flipY)
{
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 * (atlasSize.y - textureRect.height - textureRect.y)));
Debug.Log("02" + uvs[i].x);
}
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);
}
}
mesh.SetUVs(uvChannel, uvs);
return mesh;
}
//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));
Debug.Log(savePath + filename);
}
}
//End
}
struct ObjMaterial
{
public string name;
public string textureName;
}
}

View File

@ -0,0 +1,203 @@
//Maxartz15
//Version 1.0
//Part of MA_TextureUtils
//https://github.com/maxartz15/MA_TextureUtils
using UnityEngine;
using UnityEditor;
using System.IO;
using System.Collections;
using System.Collections.Generic;
//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-
namespace MA_Texture
{
public static class MA_TextureUtils
{
/// <summary>
/// Some base converters and texture settings setters.
/// </summary>
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();
// 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();
Debug.Log("Saved texture: " + texture.name);
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 static Texture2D MA_Scale2D(this Texture2D texture, int newWidth, int newHeight)
{
Texture2D texture2D = new Texture2D(newWidth, newHeight);
float ratioWidth = texture.width / newWidth;
float ratioHeight = texture.height / newHeight;
for (int x = 0; x < texture.width; x++)
{
for (int y = 0; y < texture.height; y++)
{
Color pixel = texture.GetPixel(x, y);
int posX = Mathf.FloorToInt(x / ratioWidth);
int posY = Mathf.FloorToInt(y / ratioHeight);
texture2D.SetPixel(posX, posY, new Color(pixel.r, pixel.g, pixel.b, pixel.a));
}
}
texture2D.Apply();
return texture2D;
}
public static Texture MA_Scale(this Texture texture, int newWidth, int newHeight)
{
Texture2D texture2D = (Texture2D)MA_TextureUtils.ConvertToReadableTexture(texture);
texture2D.MA_Scale2D(newWidth, newHeight);
texture = texture2D;
return texture;
}
public static Texture2D MA_Scale22D(this Texture2D texture, float width, float height)
{
float ratioWidth = width / texture.width;
float ratioHeight = height / texture.height;
int newWidth = Mathf.RoundToInt(texture.width * ratioWidth);
int newHeight = Mathf.RoundToInt(texture.height * ratioHeight);
Texture2D newTexture = new Texture2D(newWidth, newHeight);
for (int x = 0; x < texture.width; x++)
{
for (int y = 0; y < texture.height; y++)
{
Color pixel = texture.GetPixel(x, y);
int posX = Mathf.RoundToInt(x * ratioWidth);
int posY = Mathf.RoundToInt(y * ratioHeight);
newTexture.SetPixel(posX, posY, new Color(pixel.r, pixel.g, pixel.b, pixel.a));
}
}
newTexture.name = texture.name;
newTexture.Apply();
return newTexture;
}
public static Texture MA_Scale2(this Texture texture, float newWidth, float newHeight)
{
Texture2D texture2D = (Texture2D)MA_TextureUtils.ConvertToReadableTexture(texture);
texture = texture2D.MA_Scale22D(newWidth, newHeight);
return texture;
}
#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, texture.height - y - offsetY, combineTexture.GetPixel(x, texture.height - 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(texture);
texture = texture2D.MA_Combine2D(combineTexture2D, offsetX, offsetY);
return texture;
}
#endregion
}
}