mirror of
https://github.com/BlackMATov/unity-iso-tools.git
synced 2025-12-14 17:09:31 +07:00
fix alignment. fix child renderer. physics wip
This commit is contained in:
@@ -1,59 +0,0 @@
|
||||
using UnityEngine;
|
||||
using UnityEditor;
|
||||
using System.Linq;
|
||||
|
||||
namespace IsoTools {
|
||||
public class IsoEditorWindow : EditorWindow {
|
||||
|
||||
public static bool Alignment { get; private set; }
|
||||
public static bool ShowBounds { get; private set; }
|
||||
|
||||
static string _alignmentPropName = "IsoTools.IsoEditorWindow.Alignment";
|
||||
static string _showBoundsPropName = "IsoTools.IsoEditorWindow.ShowBounds";
|
||||
|
||||
void AlignmentSelection() {
|
||||
var iso_objects = Selection.gameObjects
|
||||
.Where(p => p.GetComponent<IsoObject>())
|
||||
.Select(p => p.GetComponent<IsoObject>());
|
||||
foreach ( var iso_object in iso_objects ) {
|
||||
iso_object.Position = iso_object.TilePosition;
|
||||
iso_object.FixTransform();
|
||||
}
|
||||
}
|
||||
|
||||
[MenuItem("IsoTools/IsoEditor")]
|
||||
static void Init() {
|
||||
var window = EditorWindow.GetWindow<IsoEditorWindow>();
|
||||
window.title = "IsoEditor";
|
||||
window.Show();
|
||||
}
|
||||
|
||||
void OnGUI() {
|
||||
GUILayout.Space(5);
|
||||
ShowBounds = EditorGUILayout.Toggle("Show bounds", ShowBounds);
|
||||
Alignment = EditorGUILayout.Toggle("Auto alignment", Alignment);
|
||||
if ( GUILayout.Button("Alignment selection objects") || Alignment ) {
|
||||
AlignmentSelection();
|
||||
}
|
||||
}
|
||||
|
||||
void OnFocus() {
|
||||
if ( EditorPrefs.HasKey(_alignmentPropName) ) {
|
||||
Alignment = EditorPrefs.GetBool(_alignmentPropName);
|
||||
}
|
||||
if ( EditorPrefs.HasKey(_showBoundsPropName) ) {
|
||||
ShowBounds = EditorPrefs.GetBool(_showBoundsPropName);
|
||||
}
|
||||
}
|
||||
|
||||
void OnLostFocus() {
|
||||
EditorPrefs.SetBool(_alignmentPropName, Alignment);
|
||||
EditorPrefs.SetBool(_showBoundsPropName, ShowBounds);
|
||||
}
|
||||
|
||||
void OnDestroy() {
|
||||
EditorPrefs.SetBool(_alignmentPropName, Alignment);
|
||||
EditorPrefs.SetBool(_showBoundsPropName, ShowBounds);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,12 +0,0 @@
|
||||
fileFormatVersion: 2
|
||||
guid: bc6645583f19a4c99b26459ef98d0d9c
|
||||
timeCreated: 1431871954
|
||||
licenseType: Free
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -26,21 +26,33 @@ namespace IsoTools {
|
||||
}) / _positions.Count;
|
||||
}
|
||||
|
||||
void AlignmentIsoObject(IsoObject iso_object) {
|
||||
if ( IsoEditorWindow.Alignment ) {
|
||||
iso_object.Position = iso_object.TilePosition;
|
||||
iso_object.FixTransform();
|
||||
bool IsAnyAlignment {
|
||||
get { return _positions.Keys.Any(p => p.Alignment); }
|
||||
}
|
||||
|
||||
void AlignmentSelection() {
|
||||
foreach ( var iso_object in _positions.Keys ) {
|
||||
AlignmentIsoObject(iso_object);
|
||||
}
|
||||
GrabPositions();
|
||||
}
|
||||
|
||||
void AlignmentIsoObject(IsoObject iso_object) {
|
||||
iso_object.Position = iso_object.TilePosition;
|
||||
iso_object.FixTransform();
|
||||
}
|
||||
|
||||
float ZMoveIsoObjects(float delta) {
|
||||
Undo.RecordObjects(_iso_zpositions.Keys.ToArray(), "Move");
|
||||
var is_any_alignment = IsAnyAlignment;
|
||||
return _iso_zpositions.Aggregate(0.0f, (AccIn, pair) => {
|
||||
var iso_object = pair.Key;
|
||||
var iso_orig_z = pair.Value;
|
||||
iso_object.PositionZ = iso_orig_z + delta;
|
||||
iso_object.FixTransform();
|
||||
AlignmentIsoObject(iso_object);
|
||||
if ( is_any_alignment ) {
|
||||
AlignmentIsoObject(iso_object);
|
||||
}
|
||||
var z_delta = iso_object.Position.z - iso_orig_z;
|
||||
return Mathf.Abs(z_delta) > Mathf.Abs(AccIn) ? z_delta : AccIn;
|
||||
});
|
||||
@@ -48,12 +60,15 @@ namespace IsoTools {
|
||||
|
||||
Vector3 XYMoveIsoObjects(Vector3 delta) {
|
||||
Undo.RecordObjects(_positions.Keys.ToArray(), "Move");
|
||||
var is_any_alignment = IsAnyAlignment;
|
||||
return _positions.Aggregate(Vector3.zero, (AccIn, pair) => {
|
||||
var iso_object = pair.Key;
|
||||
var iso_orig_p = pair.Value;
|
||||
iso_object.transform.position = iso_orig_p + delta;
|
||||
iso_object.FixIsoPosition();
|
||||
AlignmentIsoObject(iso_object);
|
||||
if ( is_any_alignment ) {
|
||||
AlignmentIsoObject(iso_object);
|
||||
}
|
||||
var pos_delta = iso_object.transform.position - iso_orig_p;
|
||||
return pos_delta.magnitude > AccIn.magnitude ? pos_delta : AccIn;
|
||||
});
|
||||
@@ -111,47 +126,6 @@ namespace IsoTools {
|
||||
}
|
||||
}
|
||||
|
||||
void DrawTop(Vector3 pos, Vector3 size) {
|
||||
var iso_world = GameObject.FindObjectOfType<IsoWorld>();
|
||||
if ( iso_world ) {
|
||||
var points = new Vector3[]{
|
||||
iso_world.IsoToScreen(pos),
|
||||
iso_world.IsoToScreen(pos + IsoUtils.Vec3FromX(size.x)),
|
||||
iso_world.IsoToScreen(pos + IsoUtils.Vec3FromXY(size.x, size.y)),
|
||||
iso_world.IsoToScreen(pos + IsoUtils.Vec3FromY(size.y)),
|
||||
iso_world.IsoToScreen(pos)
|
||||
};
|
||||
Handles.DrawPolyLine(points);
|
||||
}
|
||||
}
|
||||
|
||||
void DrawVert(Vector3 pos, Vector3 size) {
|
||||
var iso_world = GameObject.FindObjectOfType<IsoWorld>();
|
||||
if ( iso_world ) {
|
||||
Handles.DrawLine(
|
||||
iso_world.IsoToScreen(pos),
|
||||
iso_world.IsoToScreen(pos + IsoUtils.Vec3FromZ(size.z)));
|
||||
}
|
||||
}
|
||||
|
||||
void DrawCube(Vector3 pos, Vector3 size) {
|
||||
Handles.color = Color.green;
|
||||
DrawTop (pos - IsoUtils.Vec3FromZ(0.5f), size);
|
||||
DrawTop (pos + IsoUtils.Vec3FromZ(size.z - 0.5f), size);
|
||||
DrawVert(pos - IsoUtils.Vec3FromZ(0.5f), size);
|
||||
DrawVert(pos - IsoUtils.Vec3FromZ(0.5f) + IsoUtils.Vec3FromX(size.x), size);
|
||||
DrawVert(pos - IsoUtils.Vec3FromZ(0.5f) + IsoUtils.Vec3FromY(size.y), size);
|
||||
}
|
||||
|
||||
void DrawTargetBounds() {
|
||||
if ( IsoEditorWindow.ShowBounds ) {
|
||||
var iso_object = target as IsoObject;
|
||||
if ( iso_object ) {
|
||||
DrawCube(iso_object.Position, iso_object.Size);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void OnEnable() {
|
||||
GrabPositions();
|
||||
}
|
||||
@@ -170,12 +144,14 @@ namespace IsoTools {
|
||||
} else {
|
||||
Tools.hidden = false;
|
||||
}
|
||||
DrawTargetBounds();
|
||||
}
|
||||
|
||||
public override void OnInspectorGUI() {
|
||||
DrawDefaultInspector();
|
||||
GrabPositions();
|
||||
if ( GUILayout.Button("Alignment selection") ) {
|
||||
AlignmentSelection();
|
||||
}
|
||||
}
|
||||
}
|
||||
} // namespace IsoTools
|
||||
@@ -8,7 +8,7 @@ PhysicMaterial:
|
||||
m_Name: BoxPhysicMaterial
|
||||
dynamicFriction: .600000024
|
||||
staticFriction: .600000024
|
||||
bounciness: .400000006
|
||||
bounciness: .800000012
|
||||
frictionCombine: 0
|
||||
bounceCombine: 0
|
||||
frictionDirection2: {x: 0, y: 0, z: 0}
|
||||
|
||||
@@ -104,7 +104,7 @@ Prefab:
|
||||
objectReference: {fileID: 0}
|
||||
- target: {fileID: 417528, guid: c1b9b2ad3a021c549aa9190df3101248, type: 2}
|
||||
propertyPath: m_LocalPosition.z
|
||||
value: 37.5
|
||||
value: 50
|
||||
objectReference: {fileID: 0}
|
||||
- target: {fileID: 417528, guid: c1b9b2ad3a021c549aa9190df3101248, type: 2}
|
||||
propertyPath: m_LocalRotation.x
|
||||
@@ -308,7 +308,7 @@ Prefab:
|
||||
objectReference: {fileID: 0}
|
||||
- target: {fileID: 453404, guid: d10e5320df9f84d4186bda0ba98db9ff, type: 2}
|
||||
propertyPath: m_LocalPosition.z
|
||||
value: 87.5
|
||||
value: 90
|
||||
objectReference: {fileID: 0}
|
||||
- target: {fileID: 453404, guid: d10e5320df9f84d4186bda0ba98db9ff, type: 2}
|
||||
propertyPath: m_LocalRotation.x
|
||||
@@ -390,7 +390,7 @@ Prefab:
|
||||
objectReference: {fileID: 0}
|
||||
- target: {fileID: 453404, guid: d10e5320df9f84d4186bda0ba98db9ff, type: 2}
|
||||
propertyPath: m_LocalPosition.z
|
||||
value: 75
|
||||
value: 80
|
||||
objectReference: {fileID: 0}
|
||||
- target: {fileID: 453404, guid: d10e5320df9f84d4186bda0ba98db9ff, type: 2}
|
||||
propertyPath: m_LocalRotation.x
|
||||
@@ -472,7 +472,7 @@ Prefab:
|
||||
objectReference: {fileID: 0}
|
||||
- target: {fileID: 453404, guid: d10e5320df9f84d4186bda0ba98db9ff, type: 2}
|
||||
propertyPath: m_LocalPosition.z
|
||||
value: 62.5
|
||||
value: 70
|
||||
objectReference: {fileID: 0}
|
||||
- target: {fileID: 453404, guid: d10e5320df9f84d4186bda0ba98db9ff, type: 2}
|
||||
propertyPath: m_LocalRotation.x
|
||||
@@ -550,7 +550,7 @@ Prefab:
|
||||
objectReference: {fileID: 0}
|
||||
- target: {fileID: 417528, guid: c1b9b2ad3a021c549aa9190df3101248, type: 2}
|
||||
propertyPath: m_LocalPosition.z
|
||||
value: 50
|
||||
value: 60
|
||||
objectReference: {fileID: 0}
|
||||
- target: {fileID: 417528, guid: c1b9b2ad3a021c549aa9190df3101248, type: 2}
|
||||
propertyPath: m_LocalRotation.x
|
||||
@@ -738,7 +738,7 @@ Prefab:
|
||||
objectReference: {fileID: 0}
|
||||
- target: {fileID: 417528, guid: c1b9b2ad3a021c549aa9190df3101248, type: 2}
|
||||
propertyPath: m_LocalPosition.z
|
||||
value: 25
|
||||
value: 40
|
||||
objectReference: {fileID: 0}
|
||||
- target: {fileID: 417528, guid: c1b9b2ad3a021c549aa9190df3101248, type: 2}
|
||||
propertyPath: m_LocalRotation.x
|
||||
@@ -832,7 +832,7 @@ Prefab:
|
||||
objectReference: {fileID: 0}
|
||||
- target: {fileID: 417528, guid: c1b9b2ad3a021c549aa9190df3101248, type: 2}
|
||||
propertyPath: m_LocalPosition.z
|
||||
value: 22.2222214
|
||||
value: 30
|
||||
objectReference: {fileID: 0}
|
||||
- target: {fileID: 417528, guid: c1b9b2ad3a021c549aa9190df3101248, type: 2}
|
||||
propertyPath: m_LocalRotation.x
|
||||
@@ -926,7 +926,7 @@ Prefab:
|
||||
objectReference: {fileID: 0}
|
||||
- target: {fileID: 417528, guid: c1b9b2ad3a021c549aa9190df3101248, type: 2}
|
||||
propertyPath: m_LocalPosition.z
|
||||
value: 0
|
||||
value: 20
|
||||
objectReference: {fileID: 0}
|
||||
- target: {fileID: 417528, guid: c1b9b2ad3a021c549aa9190df3101248, type: 2}
|
||||
propertyPath: m_LocalRotation.x
|
||||
@@ -946,7 +946,7 @@ Prefab:
|
||||
objectReference: {fileID: 0}
|
||||
- target: {fileID: 417528, guid: c1b9b2ad3a021c549aa9190df3101248, type: 2}
|
||||
propertyPath: m_RootOrder
|
||||
value: 0
|
||||
value: 1
|
||||
objectReference: {fileID: 0}
|
||||
- target: {fileID: 11417528, guid: c1b9b2ad3a021c549aa9190df3101248, type: 2}
|
||||
propertyPath: _position.x
|
||||
@@ -1020,7 +1020,7 @@ Prefab:
|
||||
objectReference: {fileID: 0}
|
||||
- target: {fileID: 453404, guid: d10e5320df9f84d4186bda0ba98db9ff, type: 2}
|
||||
propertyPath: m_LocalPosition.z
|
||||
value: 12.5
|
||||
value: 10
|
||||
objectReference: {fileID: 0}
|
||||
- target: {fileID: 453404, guid: d10e5320df9f84d4186bda0ba98db9ff, type: 2}
|
||||
propertyPath: m_LocalRotation.x
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -8,12 +8,6 @@ namespace IsoTools {
|
||||
[ExecuteInEditMode]
|
||||
public class IsoObject : MonoBehaviour {
|
||||
|
||||
#if UNITY_EDITOR
|
||||
Vector3 _lastSize = Vector3.zero;
|
||||
Vector3 _lastPosition = Vector3.zero;
|
||||
Vector2 _lastTransform = Vector2.zero;
|
||||
#endif
|
||||
|
||||
// ------------------------------------------------------------------------
|
||||
//
|
||||
// Size
|
||||
@@ -161,6 +155,29 @@ namespace IsoTools {
|
||||
get { return new Vector2(TilePositionX, TilePositionZ); }
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------
|
||||
//
|
||||
// For editor
|
||||
//
|
||||
// ------------------------------------------------------------------------
|
||||
|
||||
#if UNITY_EDITOR
|
||||
Vector3 _lastSize = Vector3.zero;
|
||||
Vector3 _lastPosition = Vector3.zero;
|
||||
Vector2 _lastTransform = Vector2.zero;
|
||||
|
||||
[SerializeField] bool _alignment = true;
|
||||
[SerializeField] bool _showBounds = false;
|
||||
|
||||
public bool Alignment {
|
||||
get { return _alignment; }
|
||||
}
|
||||
|
||||
public bool ShowBounds {
|
||||
get { return _showBounds; }
|
||||
}
|
||||
#endif
|
||||
|
||||
// ------------------------------------------------------------------------
|
||||
//
|
||||
// Functions
|
||||
@@ -185,6 +202,11 @@ namespace IsoTools {
|
||||
}
|
||||
|
||||
public void FixTransform() {
|
||||
#if UNITY_EDITOR
|
||||
if ( Application.isEditor && !Application.isPlaying && Alignment ) {
|
||||
_position = TilePosition;
|
||||
}
|
||||
#endif
|
||||
transform.position = IsoUtils.Vec3ChangeZ(
|
||||
IsoWorld.IsoToScreen(Position),
|
||||
transform.position.z);
|
||||
@@ -228,22 +250,23 @@ namespace IsoTools {
|
||||
MartDirtyIsoWorld();
|
||||
}
|
||||
|
||||
//TODO: now working for child sprites
|
||||
void OnBecameVisible() {
|
||||
MartDirtyIsoWorld();
|
||||
#if UNITY_EDITOR
|
||||
void OnDrawGizmos() {
|
||||
if ( ShowBounds ) {
|
||||
IsoUtils.DrawCube(Position, Size, Color.red);
|
||||
}
|
||||
}
|
||||
|
||||
#if UNITY_EDITOR
|
||||
void Update() {
|
||||
if ( Application.isEditor ) {
|
||||
if ( !IsoUtils.Vec2Approximately(_lastTransform, transform.position) ) {
|
||||
FixIsoPosition();
|
||||
if ( !IsoUtils.Vec3Approximately(_lastSize, _size) ) {
|
||||
Size = _size;
|
||||
}
|
||||
if ( !IsoUtils.Vec3Approximately(_lastPosition, _position) ) {
|
||||
Position = _position;
|
||||
}
|
||||
if ( !IsoUtils.Vec3Approximately(_lastSize, _size) ) {
|
||||
Size = _size;
|
||||
if ( !IsoUtils.Vec2Approximately(_lastTransform, transform.position) ) {
|
||||
FixIsoPosition();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -73,7 +73,7 @@ namespace IsoTools {
|
||||
void Awake() {
|
||||
_fakeObject = new GameObject();
|
||||
FakeGameObject.name = "_Fake" + gameObject.name;
|
||||
FakeGameObject.hideFlags = HideFlags.HideInHierarchy;
|
||||
//FakeGameObject.hideFlags = HideFlags.HideInHierarchy;
|
||||
|
||||
var rigidbody = FakeGameObject.AddComponent<Rigidbody>();
|
||||
rigidbody.freezeRotation = true;
|
||||
@@ -82,7 +82,7 @@ namespace IsoTools {
|
||||
rigidbody.collisionDetectionMode = CollisionMode;
|
||||
|
||||
AddBoxCollider();
|
||||
AddHelperSpheres();
|
||||
//AddHelperSpheres();
|
||||
|
||||
_lastPosition = IsoObject.Position;
|
||||
FakeGameObject.transform.position = IsoObject.Position;
|
||||
|
||||
@@ -4,11 +4,11 @@ using System;
|
||||
namespace IsoTools {
|
||||
public static class IsoUtils {
|
||||
|
||||
// ------------------------------------------------------------------------
|
||||
// ---------------------------------------------------------------------
|
||||
//
|
||||
// Consts
|
||||
//
|
||||
// ------------------------------------------------------------------------
|
||||
// ---------------------------------------------------------------------
|
||||
|
||||
public static Vector3 Vec2OneX { get { return new Vector2(1.0f, 0.0f); } }
|
||||
public static Vector3 Vec2OneY { get { return new Vector2(0.0f, 1.0f); } }
|
||||
@@ -21,24 +21,22 @@ namespace IsoTools {
|
||||
public static Vector3 Vec3OneYZ { get { return new Vector3(0.0f, 1.0f, 1.0f); } }
|
||||
public static Vector3 Vec3OneXZ { get { return new Vector3(1.0f, 0.0f, 1.0f); } }
|
||||
|
||||
// ------------------------------------------------------------------------
|
||||
// ---------------------------------------------------------------------
|
||||
//
|
||||
// Abs/Min/Max
|
||||
//
|
||||
// ------------------------------------------------------------------------
|
||||
// ---------------------------------------------------------------------
|
||||
|
||||
// -----------------------------
|
||||
// Abs
|
||||
// -----------------------------
|
||||
|
||||
public static Vector2 Vec2Abs(Vector2 v) {
|
||||
return new Vector2(
|
||||
Mathf.Abs(v.x),
|
||||
Mathf.Abs(v.y));
|
||||
}
|
||||
|
||||
public static Vector2 Vec2Min(Vector2 a, Vector2 b) {
|
||||
return new Vector2(
|
||||
Mathf.Min(a.x, b.x),
|
||||
Mathf.Min(a.y, b.y));
|
||||
}
|
||||
|
||||
|
||||
public static Vector3 Vec3Abs(Vector3 v) {
|
||||
return new Vector3(
|
||||
Mathf.Abs(v.x),
|
||||
@@ -46,6 +44,24 @@ namespace IsoTools {
|
||||
Mathf.Abs(v.z));
|
||||
}
|
||||
|
||||
// -----------------------------
|
||||
// Min
|
||||
// -----------------------------
|
||||
|
||||
public static float Vec2MinF(Vector2 v) {
|
||||
return Mathf.Min(v.x, v.y);
|
||||
}
|
||||
|
||||
public static float Vec3MinF(Vector3 v) {
|
||||
return Mathf.Min(v.x, v.y, v.z);
|
||||
}
|
||||
|
||||
public static Vector2 Vec2Min(Vector2 a, Vector2 b) {
|
||||
return new Vector2(
|
||||
Mathf.Min(a.x, b.x),
|
||||
Mathf.Min(a.y, b.y));
|
||||
}
|
||||
|
||||
public static Vector3 Vec3Min(Vector3 a, Vector3 b) {
|
||||
return new Vector3(
|
||||
Mathf.Min(a.x, b.x),
|
||||
@@ -53,6 +69,18 @@ namespace IsoTools {
|
||||
Mathf.Min(a.z, b.z));
|
||||
}
|
||||
|
||||
// -----------------------------
|
||||
// Max
|
||||
// -----------------------------
|
||||
|
||||
public static float Vec2MaxF(Vector2 v) {
|
||||
return Mathf.Max(v.x, v.y);
|
||||
}
|
||||
|
||||
public static float Vec3MaxF(Vector3 v) {
|
||||
return Mathf.Max(v.x, v.y, v.z);
|
||||
}
|
||||
|
||||
public static Vector2 Vec2Max(Vector2 a, Vector2 b) {
|
||||
return new Vector2(
|
||||
Mathf.Max(a.x, b.x),
|
||||
@@ -66,11 +94,21 @@ namespace IsoTools {
|
||||
Mathf.Max(a.z, b.z));
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------
|
||||
// ---------------------------------------------------------------------
|
||||
//
|
||||
// Ceil/Floor/Round
|
||||
//
|
||||
// ------------------------------------------------------------------------
|
||||
// ---------------------------------------------------------------------
|
||||
|
||||
// -----------------------------
|
||||
// Ceil
|
||||
// -----------------------------
|
||||
|
||||
public static Vector2 Vec2Ceil(Vector2 v) {
|
||||
return new Vector2(
|
||||
Mathf.Ceil(v.x),
|
||||
Mathf.Ceil(v.y));
|
||||
}
|
||||
|
||||
public static Vector3 Vec3Ceil(Vector3 v) {
|
||||
return new Vector3(
|
||||
@@ -79,6 +117,16 @@ namespace IsoTools {
|
||||
Mathf.Ceil(v.z));
|
||||
}
|
||||
|
||||
// -----------------------------
|
||||
// Floor
|
||||
// -----------------------------
|
||||
|
||||
public static Vector2 Vec2Floor(Vector2 v) {
|
||||
return new Vector2(
|
||||
Mathf.Floor(v.x),
|
||||
Mathf.Floor(v.y));
|
||||
}
|
||||
|
||||
public static Vector3 Vec3Floor(Vector3 v) {
|
||||
return new Vector3(
|
||||
Mathf.Floor(v.x),
|
||||
@@ -86,6 +134,16 @@ namespace IsoTools {
|
||||
Mathf.Floor(v.z));
|
||||
}
|
||||
|
||||
// -----------------------------
|
||||
// Round
|
||||
// -----------------------------
|
||||
|
||||
public static Vector2 Vec2Round(Vector2 v) {
|
||||
return new Vector2(
|
||||
Mathf.Round(v.x),
|
||||
Mathf.Round(v.y));
|
||||
}
|
||||
|
||||
public static Vector3 Vec3Round(Vector3 v) {
|
||||
return new Vector3(
|
||||
Mathf.Round(v.x),
|
||||
@@ -93,11 +151,15 @@ namespace IsoTools {
|
||||
Mathf.Round(v.z));
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------
|
||||
// ---------------------------------------------------------------------
|
||||
//
|
||||
// Div/DivCeil/DivFloor/DivRound
|
||||
//
|
||||
// ------------------------------------------------------------------------
|
||||
// ---------------------------------------------------------------------
|
||||
|
||||
// -----------------------------
|
||||
// Div
|
||||
// -----------------------------
|
||||
|
||||
public static Vector3 Vec3Div(Vector3 a, float b) {
|
||||
return new Vector3(
|
||||
@@ -113,6 +175,10 @@ namespace IsoTools {
|
||||
a.z / b.z);
|
||||
}
|
||||
|
||||
// -----------------------------
|
||||
// DivCeil
|
||||
// -----------------------------
|
||||
|
||||
public static Vector3 Vec3DivCeil(Vector3 a, float b) {
|
||||
return Vec3Ceil(Vec3Div(a, b));
|
||||
}
|
||||
@@ -120,6 +186,10 @@ namespace IsoTools {
|
||||
public static Vector3 Vec3DivCeil(Vector3 a, Vector3 b) {
|
||||
return Vec3Ceil(Vec3Div(a, b));
|
||||
}
|
||||
|
||||
// -----------------------------
|
||||
// DivFloor
|
||||
// -----------------------------
|
||||
|
||||
public static Vector3 Vec3DivFloor(Vector3 a, float b) {
|
||||
return Vec3Floor(Vec3Div(a, b));
|
||||
@@ -128,6 +198,10 @@ namespace IsoTools {
|
||||
public static Vector3 Vec3DivFloor(Vector3 a, Vector3 b) {
|
||||
return Vec3Floor(Vec3Div(a, b));
|
||||
}
|
||||
|
||||
// -----------------------------
|
||||
// DivRound
|
||||
// -----------------------------
|
||||
|
||||
public static Vector3 Vec3DivRound(Vector3 a, float b) {
|
||||
return Vec3Round(Vec3Div(a, b));
|
||||
@@ -137,11 +211,15 @@ namespace IsoTools {
|
||||
return Vec3Round(Vec3Div(a, b));
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------
|
||||
// ---------------------------------------------------------------------
|
||||
//
|
||||
// FromX
|
||||
// Vec2From/Vec3From
|
||||
//
|
||||
// ------------------------------------------------------------------------
|
||||
// ---------------------------------------------------------------------
|
||||
|
||||
// -----------------------------
|
||||
// Vec2From
|
||||
// -----------------------------
|
||||
|
||||
public static Vector2 Vec2FromX(float x) {
|
||||
return new Vector2(x, 0.0f);
|
||||
@@ -154,6 +232,10 @@ namespace IsoTools {
|
||||
public static Vector2 Vec2FromXY(float x, float y) {
|
||||
return new Vector2(x, y);
|
||||
}
|
||||
|
||||
// -----------------------------
|
||||
// Vec3From
|
||||
// -----------------------------
|
||||
|
||||
public static Vector3 Vec3FromX(float x) {
|
||||
return new Vector3(x, 0.0f, 0.0f);
|
||||
@@ -179,11 +261,15 @@ namespace IsoTools {
|
||||
return new Vector3(x, 0.0f, z);
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------
|
||||
// ---------------------------------------------------------------------
|
||||
//
|
||||
// ChangeX
|
||||
//
|
||||
// ------------------------------------------------------------------------
|
||||
// ---------------------------------------------------------------------
|
||||
|
||||
// -----------------------------
|
||||
// Vec2Change
|
||||
// -----------------------------
|
||||
|
||||
public static Vector2 Vec2ChangeX(Vector2 v, float x) {
|
||||
return new Vector2(x, v.y);
|
||||
@@ -192,6 +278,10 @@ namespace IsoTools {
|
||||
public static Vector2 Vec2ChangeY(Vector2 v, float y) {
|
||||
return new Vector2(v.x, y);
|
||||
}
|
||||
|
||||
// -----------------------------
|
||||
// Vec3Change
|
||||
// -----------------------------
|
||||
|
||||
public static Vector3 Vec3ChangeX(Vector3 v, float x) {
|
||||
return new Vector3(x, v.y, v.z);
|
||||
@@ -217,11 +307,11 @@ namespace IsoTools {
|
||||
return new Vector3(x, v.y, z);
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------
|
||||
// ---------------------------------------------------------------------
|
||||
//
|
||||
// Approximately
|
||||
//
|
||||
// -----------------------------------------------------------------------
|
||||
// ---------------------------------------------------------------------
|
||||
|
||||
public static bool Vec2Approximately(Vector2 a, Vector2 b) {
|
||||
return
|
||||
@@ -236,11 +326,18 @@ namespace IsoTools {
|
||||
Mathf.Approximately(a.z, b.z);
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------
|
||||
// ---------------------------------------------------------------------
|
||||
//
|
||||
// LookUpCube
|
||||
//
|
||||
// ------------------------------------------------------------------------
|
||||
// ---------------------------------------------------------------------
|
||||
|
||||
public static void LookUpCube(Vector2 min, Vector2 max, Action<Vector2> act) {
|
||||
for ( var y = min.y; y < max.y; ++y ) {
|
||||
for ( var x = min.x; x < max.x; ++x ) {
|
||||
act(new Vector2(x, y));
|
||||
}}
|
||||
}
|
||||
|
||||
public static void LookUpCube(Vector3 min, Vector3 max, Action<Vector3> act) {
|
||||
for ( var z = min.z; z < max.z; ++z ) {
|
||||
@@ -249,5 +346,49 @@ namespace IsoTools {
|
||||
act(new Vector3(x, y, z));
|
||||
}}}
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------
|
||||
//
|
||||
// Debug draw
|
||||
//
|
||||
// ---------------------------------------------------------------------
|
||||
|
||||
#if UNITY_EDITOR
|
||||
static void DrawTop(Vector3 pos, Vector3 size) {
|
||||
var iso_world = GameObject.FindObjectOfType<IsoWorld>();
|
||||
if ( iso_world ) {
|
||||
var points = new Vector3[]{
|
||||
iso_world.IsoToScreen(pos),
|
||||
iso_world.IsoToScreen(pos + IsoUtils.Vec3FromX(size.x)),
|
||||
iso_world.IsoToScreen(pos + IsoUtils.Vec3FromXY(size.x, size.y)),
|
||||
iso_world.IsoToScreen(pos + IsoUtils.Vec3FromY(size.y)),
|
||||
iso_world.IsoToScreen(pos)
|
||||
};
|
||||
Gizmos.DrawLine(points[0], points[1]);
|
||||
Gizmos.DrawLine(points[1], points[2]);
|
||||
Gizmos.DrawLine(points[2], points[3]);
|
||||
Gizmos.DrawLine(points[3], points[0]);
|
||||
}
|
||||
}
|
||||
|
||||
static void DrawVert(Vector3 pos, Vector3 size) {
|
||||
var iso_world = GameObject.FindObjectOfType<IsoWorld>();
|
||||
if ( iso_world ) {
|
||||
Gizmos.DrawLine(
|
||||
iso_world.IsoToScreen(pos),
|
||||
iso_world.IsoToScreen(pos + IsoUtils.Vec3FromZ(size.z)));
|
||||
}
|
||||
}
|
||||
|
||||
public static void DrawCube(Vector3 pos, Vector3 size, Color color) {
|
||||
Gizmos.color = color;
|
||||
DrawTop (pos - IsoUtils.Vec3FromZ(0.5f), size);
|
||||
DrawTop (pos + IsoUtils.Vec3FromZ(size.z - 0.5f), size);
|
||||
DrawVert(pos - IsoUtils.Vec3FromZ(0.5f), size);
|
||||
DrawVert(pos + IsoUtils.Vec3FromZ(0.5f), size);
|
||||
DrawVert(pos - IsoUtils.Vec3FromZ(0.5f) + IsoUtils.Vec3FromX(size.x), size);
|
||||
DrawVert(pos - IsoUtils.Vec3FromZ(0.5f) + IsoUtils.Vec3FromY(size.y), size);
|
||||
}
|
||||
#endif
|
||||
}
|
||||
}
|
||||
@@ -39,18 +39,19 @@ namespace IsoTools {
|
||||
public List<int> Objects = new List<int>();
|
||||
}
|
||||
|
||||
bool _dirty = true;
|
||||
float _lastTileSize = 0.0f;
|
||||
float _lastMinDepth = 0.0f;
|
||||
float _lastMaxDepth = 0.0f;
|
||||
bool _dirty = true;
|
||||
float _lastTileSize = 0.0f;
|
||||
float _lastMinDepth = 0.0f;
|
||||
float _lastMaxDepth = 0.0f;
|
||||
|
||||
List<SectorInfo> _sectors = new List<SectorInfo>();
|
||||
List<ObjectInfo> _objects = new List<ObjectInfo>();
|
||||
List<int> _depends = new List<int>();
|
||||
float _objsSectorSize = 0.0f;
|
||||
Vector3 _objsMinNumPos = Vector3.zero;
|
||||
Vector3 _objsMaxNumPos = Vector3.zero;
|
||||
Vector3 _objsNumPosCount = Vector3.zero;
|
||||
List<SectorInfo> _sectors = new List<SectorInfo>();
|
||||
List<ObjectInfo> _objects = new List<ObjectInfo>();
|
||||
List<int> _depends = new List<int>();
|
||||
HashSet<IsoObject> _visibles = new HashSet<IsoObject>();
|
||||
float _objsSectorSize = 0.0f;
|
||||
Vector3 _objsMinNumPos = Vector3.zero;
|
||||
Vector3 _objsMaxNumPos = Vector3.zero;
|
||||
Vector3 _objsNumPosCount = Vector3.zero;
|
||||
|
||||
[SerializeField]
|
||||
public float _tileSize = 32.0f;
|
||||
@@ -263,31 +264,36 @@ namespace IsoTools {
|
||||
return a_yesno;
|
||||
}
|
||||
|
||||
void SetupSectorSize(IsoObject[] iso_objects) {
|
||||
void SetupVisibles(IsoObject[] iso_objects) {
|
||||
var new_visibles = new HashSet<IsoObject>(
|
||||
iso_objects.Where(p => IsIsoObjectVisible(p)));
|
||||
if ( !_visibles.IsSupersetOf(new_visibles) ) {
|
||||
MarkDirty();
|
||||
}
|
||||
_visibles = new_visibles;
|
||||
}
|
||||
|
||||
void SetupSectorSize() {
|
||||
_objsSectorSize = 0.0f;
|
||||
var objsSum = 0;
|
||||
foreach ( var obj in iso_objects ) {
|
||||
if ( IsIsoObjectVisible(obj) ) {
|
||||
++objsSum;
|
||||
_objsSectorSize += Mathf.Max(obj.Size.x, obj.Size.y, obj.Size.z);
|
||||
}
|
||||
foreach ( var obj in _visibles ) {
|
||||
++objsSum;
|
||||
_objsSectorSize += Mathf.Max(obj.Size.x, obj.Size.y, obj.Size.z);
|
||||
}
|
||||
_objsSectorSize = Mathf.Round(Mathf.Max(3.0f, _objsSectorSize / objsSum));
|
||||
}
|
||||
|
||||
void SetupObjects(IsoObject[] iso_objects) {
|
||||
void SetupObjects() {
|
||||
_objects.Clear();
|
||||
_objsMinNumPos = Vector3.zero;
|
||||
_objsMaxNumPos = Vector3.one;
|
||||
foreach ( var obj in iso_objects ) {
|
||||
if ( IsIsoObjectVisible(obj) ) {
|
||||
var max_size = IsoUtils.Vec3Max(Vector3.one, obj.Size);
|
||||
var min_npos = IsoUtils.Vec3DivFloor(obj.Position, _objsSectorSize);
|
||||
var max_npos = IsoUtils.Vec3DivCeil(obj.Position + max_size, _objsSectorSize);
|
||||
_objsMinNumPos = IsoUtils.Vec3Min(_objsMinNumPos, min_npos);
|
||||
_objsMaxNumPos = IsoUtils.Vec3Max(_objsMaxNumPos, max_npos);
|
||||
_objects.Add(new ObjectInfo(_objects.Count, obj, min_npos, max_npos));
|
||||
}
|
||||
foreach ( var obj in _visibles ) {
|
||||
var max_size = IsoUtils.Vec3Max(Vector3.one, obj.Size);
|
||||
var min_npos = IsoUtils.Vec3DivFloor(obj.Position, _objsSectorSize);
|
||||
var max_npos = IsoUtils.Vec3DivCeil(obj.Position + max_size, _objsSectorSize);
|
||||
_objsMinNumPos = IsoUtils.Vec3Min(_objsMinNumPos, min_npos);
|
||||
_objsMaxNumPos = IsoUtils.Vec3Max(_objsMaxNumPos, max_npos);
|
||||
_objects.Add(new ObjectInfo(_objects.Count, obj, min_npos, max_npos));
|
||||
}
|
||||
_objsNumPosCount = _objsMaxNumPos - _objsMinNumPos;
|
||||
}
|
||||
@@ -359,10 +365,11 @@ namespace IsoTools {
|
||||
}
|
||||
|
||||
void StepSort() {
|
||||
var iso_objects = GameObject.FindObjectsOfType<IsoObject>();
|
||||
SetupVisibles(iso_objects);
|
||||
if ( _dirty ) {
|
||||
var iso_objects = GameObject.FindObjectsOfType<IsoObject>();
|
||||
SetupSectorSize(iso_objects);
|
||||
SetupObjects(iso_objects);
|
||||
SetupSectorSize();
|
||||
SetupObjects();
|
||||
SetupSectors();
|
||||
SetupObjectDepends();
|
||||
PlaceAllObjects();
|
||||
|
||||
Reference in New Issue
Block a user