diff --git a/Assets/Extensions.cs b/Assets/Extensions.cs old mode 100644 new mode 100755 index 26c3cab..6fb510f --- a/Assets/Extensions.cs +++ b/Assets/Extensions.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using System.Linq; using System.Text; +using UnityEngine; public static class Extensions { @@ -9,4 +10,49 @@ { yield return item; } + + public static Vector2 OverX(this Vector2 v, Func func) + { + return new Vector2(func(v.x), v.y); + } + + public static Vector3 OverX(this Vector3 v, Func func) + { + return new Vector3(func(v.x), v.y, v.z); + } + + public static Vector4 OverX(this Vector4 v, Func func) + { + return new Vector4(func(v.x), v.y, v.z, v.w); + } + + public static Vector2 OverY(this Vector2 v, Func func) + { + return new Vector2(v.x, func(v.y)); + } + + public static Vector3 OverY(this Vector3 v, Func func) + { + return new Vector3(v.x, func(v.y), v.z); + } + + public static Vector4 OverY(this Vector4 v, Func func) + { + return new Vector4(v.x, func(v.y), v.z, v.w); + } + + public static Vector3 OverZ(this Vector3 v, Func func) + { + return new Vector3(v.x, v.y, func(v.z)); + } + + public static Vector4 OverZ(this Vector4 v, Func func) + { + return new Vector4(v.x, v.y, func(v.z), v.w); + } + + public static Vector4 OverW(this Vector4 v, Func func) + { + return new Vector4(v.x, v.y, v.z, func(v.w)); + } } diff --git a/Assets/Extensions.cs b/Assets/Extensions.cs old mode 100644 new mode 100755 index 26c3cab..6fb510f --- a/Assets/Extensions.cs +++ b/Assets/Extensions.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using System.Linq; using System.Text; +using UnityEngine; public static class Extensions { @@ -9,4 +10,49 @@ { yield return item; } + + public static Vector2 OverX(this Vector2 v, Func func) + { + return new Vector2(func(v.x), v.y); + } + + public static Vector3 OverX(this Vector3 v, Func func) + { + return new Vector3(func(v.x), v.y, v.z); + } + + public static Vector4 OverX(this Vector4 v, Func func) + { + return new Vector4(func(v.x), v.y, v.z, v.w); + } + + public static Vector2 OverY(this Vector2 v, Func func) + { + return new Vector2(v.x, func(v.y)); + } + + public static Vector3 OverY(this Vector3 v, Func func) + { + return new Vector3(v.x, func(v.y), v.z); + } + + public static Vector4 OverY(this Vector4 v, Func func) + { + return new Vector4(v.x, func(v.y), v.z, v.w); + } + + public static Vector3 OverZ(this Vector3 v, Func func) + { + return new Vector3(v.x, v.y, func(v.z)); + } + + public static Vector4 OverZ(this Vector4 v, Func func) + { + return new Vector4(v.x, v.y, func(v.z), v.w); + } + + public static Vector4 OverW(this Vector4 v, Func func) + { + return new Vector4(v.x, v.y, v.z, func(v.w)); + } } diff --git a/Assets/IsoEditor.cs b/Assets/IsoEditor.cs index 0895cdb..65c4470 100755 --- a/Assets/IsoEditor.cs +++ b/Assets/IsoEditor.cs @@ -17,6 +17,29 @@ EnableButtons(); } + public float IsoAngle + { + get + { + return IsoCamera.transform.localEulerAngles.x; + } + } + + public float IsoAspect + { + get + { + return Mathf.Cos(Mathf.PI / 2.0f - Mathf.Deg2Rad * IsoAngle); + } + } + + public static readonly float HSqrt2 = Mathf.Sqrt(2) / 2.0f; + + public Vector2 IsoProject(Vector3 pos) + { + return new Vector2((pos.x + pos.z) * HSqrt2, ((-pos.x + pos.z) * IsoAspect + pos.y) * HSqrt2); + } + private void OnDisable() { DisableButtons(); diff --git a/Assets/Extensions.cs b/Assets/Extensions.cs old mode 100644 new mode 100755 index 26c3cab..6fb510f --- a/Assets/Extensions.cs +++ b/Assets/Extensions.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using System.Linq; using System.Text; +using UnityEngine; public static class Extensions { @@ -9,4 +10,49 @@ { yield return item; } + + public static Vector2 OverX(this Vector2 v, Func func) + { + return new Vector2(func(v.x), v.y); + } + + public static Vector3 OverX(this Vector3 v, Func func) + { + return new Vector3(func(v.x), v.y, v.z); + } + + public static Vector4 OverX(this Vector4 v, Func func) + { + return new Vector4(func(v.x), v.y, v.z, v.w); + } + + public static Vector2 OverY(this Vector2 v, Func func) + { + return new Vector2(v.x, func(v.y)); + } + + public static Vector3 OverY(this Vector3 v, Func func) + { + return new Vector3(v.x, func(v.y), v.z); + } + + public static Vector4 OverY(this Vector4 v, Func func) + { + return new Vector4(v.x, func(v.y), v.z, v.w); + } + + public static Vector3 OverZ(this Vector3 v, Func func) + { + return new Vector3(v.x, v.y, func(v.z)); + } + + public static Vector4 OverZ(this Vector4 v, Func func) + { + return new Vector4(v.x, v.y, func(v.z), v.w); + } + + public static Vector4 OverW(this Vector4 v, Func func) + { + return new Vector4(v.x, v.y, v.z, func(v.w)); + } } diff --git a/Assets/IsoEditor.cs b/Assets/IsoEditor.cs index 0895cdb..65c4470 100755 --- a/Assets/IsoEditor.cs +++ b/Assets/IsoEditor.cs @@ -17,6 +17,29 @@ EnableButtons(); } + public float IsoAngle + { + get + { + return IsoCamera.transform.localEulerAngles.x; + } + } + + public float IsoAspect + { + get + { + return Mathf.Cos(Mathf.PI / 2.0f - Mathf.Deg2Rad * IsoAngle); + } + } + + public static readonly float HSqrt2 = Mathf.Sqrt(2) / 2.0f; + + public Vector2 IsoProject(Vector3 pos) + { + return new Vector2((pos.x + pos.z) * HSqrt2, ((-pos.x + pos.z) * IsoAspect + pos.y) * HSqrt2); + } + private void OnDisable() { DisableButtons(); diff --git a/Assets/IsoObject.cs b/Assets/IsoObject.cs index 9782b6f..ee4db18 100755 --- a/Assets/IsoObject.cs +++ b/Assets/IsoObject.cs @@ -9,7 +9,7 @@ public class IsoObjectEditor : Editor { private int textureSelected = 0; - private List textures = new List(); + private List textures = new List(); public override void OnInspectorGUI() { DrawDefaultInspector(); @@ -22,15 +22,18 @@ { if (mr.sharedMaterial.shader.name == "Unlit/IsoShader") { - textures.Add(mr.sharedMaterial.GetTexture("_MainTex")); + textures.Add(mr.sharedMaterial); } } textureSelected = EditorGUILayout.Popup(Math.Min(textures.Count, textureSelected), new GUIContent("Select Texture").ToSingle().Concat(textures.Select(t => new GUIContent(t.name))).ToArray()); if (textureSelected != 0) { + Material textureMat = textures[textureSelected - 1]; + IsoTextureEditor editor = EditorWindow.GetWindow(); - editor.Init(textures[textureSelected - 1]); + Vector4 offset = textureMat.GetVector("_TextureOffset"); + editor.Init(textureMat.GetTexture("_MainTex"), new Vector2(offset.x, offset.y), textureMat.GetFloat("_PixelsPerUnit")); editor.Show(); textureSelected = 0; } diff --git a/Assets/Extensions.cs b/Assets/Extensions.cs old mode 100644 new mode 100755 index 26c3cab..6fb510f --- a/Assets/Extensions.cs +++ b/Assets/Extensions.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using System.Linq; using System.Text; +using UnityEngine; public static class Extensions { @@ -9,4 +10,49 @@ { yield return item; } + + public static Vector2 OverX(this Vector2 v, Func func) + { + return new Vector2(func(v.x), v.y); + } + + public static Vector3 OverX(this Vector3 v, Func func) + { + return new Vector3(func(v.x), v.y, v.z); + } + + public static Vector4 OverX(this Vector4 v, Func func) + { + return new Vector4(func(v.x), v.y, v.z, v.w); + } + + public static Vector2 OverY(this Vector2 v, Func func) + { + return new Vector2(v.x, func(v.y)); + } + + public static Vector3 OverY(this Vector3 v, Func func) + { + return new Vector3(v.x, func(v.y), v.z); + } + + public static Vector4 OverY(this Vector4 v, Func func) + { + return new Vector4(v.x, func(v.y), v.z, v.w); + } + + public static Vector3 OverZ(this Vector3 v, Func func) + { + return new Vector3(v.x, v.y, func(v.z)); + } + + public static Vector4 OverZ(this Vector4 v, Func func) + { + return new Vector4(v.x, v.y, func(v.z), v.w); + } + + public static Vector4 OverW(this Vector4 v, Func func) + { + return new Vector4(v.x, v.y, v.z, func(v.w)); + } } diff --git a/Assets/IsoEditor.cs b/Assets/IsoEditor.cs index 0895cdb..65c4470 100755 --- a/Assets/IsoEditor.cs +++ b/Assets/IsoEditor.cs @@ -17,6 +17,29 @@ EnableButtons(); } + public float IsoAngle + { + get + { + return IsoCamera.transform.localEulerAngles.x; + } + } + + public float IsoAspect + { + get + { + return Mathf.Cos(Mathf.PI / 2.0f - Mathf.Deg2Rad * IsoAngle); + } + } + + public static readonly float HSqrt2 = Mathf.Sqrt(2) / 2.0f; + + public Vector2 IsoProject(Vector3 pos) + { + return new Vector2((pos.x + pos.z) * HSqrt2, ((-pos.x + pos.z) * IsoAspect + pos.y) * HSqrt2); + } + private void OnDisable() { DisableButtons(); diff --git a/Assets/IsoObject.cs b/Assets/IsoObject.cs index 9782b6f..ee4db18 100755 --- a/Assets/IsoObject.cs +++ b/Assets/IsoObject.cs @@ -9,7 +9,7 @@ public class IsoObjectEditor : Editor { private int textureSelected = 0; - private List textures = new List(); + private List textures = new List(); public override void OnInspectorGUI() { DrawDefaultInspector(); @@ -22,15 +22,18 @@ { if (mr.sharedMaterial.shader.name == "Unlit/IsoShader") { - textures.Add(mr.sharedMaterial.GetTexture("_MainTex")); + textures.Add(mr.sharedMaterial); } } textureSelected = EditorGUILayout.Popup(Math.Min(textures.Count, textureSelected), new GUIContent("Select Texture").ToSingle().Concat(textures.Select(t => new GUIContent(t.name))).ToArray()); if (textureSelected != 0) { + Material textureMat = textures[textureSelected - 1]; + IsoTextureEditor editor = EditorWindow.GetWindow(); - editor.Init(textures[textureSelected - 1]); + Vector4 offset = textureMat.GetVector("_TextureOffset"); + editor.Init(textureMat.GetTexture("_MainTex"), new Vector2(offset.x, offset.y), textureMat.GetFloat("_PixelsPerUnit")); editor.Show(); textureSelected = 0; } diff --git a/Assets/IsoTextureEditor.cs b/Assets/IsoTextureEditor.cs old mode 100644 new mode 100755 index a5f5037..b282235 --- a/Assets/IsoTextureEditor.cs +++ b/Assets/IsoTextureEditor.cs @@ -7,34 +7,44 @@ public class IsoTextureEditor : EditorWindow { - private Camera cam; private Texture texture; - private RenderTexture handleTex; + private Vector2 textureOffset; + private IsoEditor editor; + private float pixelsPerUnit; private void Awake() { - cam = EditorUtility.CreateGameObjectWithHideFlags("testCam", HideFlags.HideAndDontSave, typeof(Camera)).GetComponent(); - cam.clearFlags = CameraClearFlags.SolidColor; - cam.backgroundColor = new Color(0, 0, 0, 0); - cam.cullingMask = 0; - cam.orthographic = true; + } private void OnDestroy() { - DestroyImmediate(cam); } - public void Init(Texture texture) + public void Init(Texture texture, Vector2 textureOffset, float pixelsPerUnit) { this.texture = texture; titleContent = new GUIContent(texture.name + ": Iso Boxes"); minSize = new Vector2(texture.width, texture.height + 30); - - handleTex = new RenderTexture(texture.width, texture.height, 0); - cam.targetTexture = handleTex; - - cam.orthographicSize = texture.height * 0.5f; - cam.transform.position = new Vector3(texture.width * 0.5f, texture.height * 0.5f, 0); - cam.transform.rotation = Quaternion.identity; + editor = FindObjectOfType(); + this.textureOffset = textureOffset; + this.pixelsPerUnit = pixelsPerUnit; } + + private static float ControlPointSize = 6f; + private void DrawControlPoint(Color color, Vector2 pos) + { + EditorGUI.DrawRect(new Rect(pos.x - ControlPointSize * 0.5f, pos.y - ControlPointSize * 0.5f, ControlPointSize, ControlPointSize), color); + } + + private void RenderCube(Color color, Vector2 origin, Vector3 size) + { + DrawControlPoint(color, origin); + DrawControlPoint(color, origin + (editor.IsoProject(new Vector3(-size.x, 0, 0)) * pixelsPerUnit).OverY(y => -y)); + DrawControlPoint(color, origin + (editor.IsoProject(new Vector3(0, 0, size.z)) * pixelsPerUnit).OverY(y => -y)); + + DrawControlPoint(color, origin + (editor.IsoProject(new Vector3(0, size.y, 0)) * pixelsPerUnit).OverY(y => -y)); + DrawControlPoint(color, origin + (editor.IsoProject(new Vector3(-size.x, size.y, 0)) * pixelsPerUnit).OverY(y => -y)); + DrawControlPoint(color, origin + (editor.IsoProject(new Vector3(0, size.y, size.z)) * pixelsPerUnit).OverY(y => -y)); + } + private void OnGUI() { if (texture != null) @@ -45,9 +55,7 @@ } - Handles.SetCamera(cam); - Handles.PositionHandle(new Vector3(), Quaternion.identity); - Handles.DrawCamera(new Rect(0, 0, texture.width, texture.height), cam); + RenderCube(Color.red, -textureOffset, new Vector3(1f, 1.5f, 2.1f)); } } } \ No newline at end of file