| | 1 | | using System.Collections; |
| | 2 | | using System.Collections.Generic; |
| | 3 | | using UnityEngine; |
| | 4 | |
|
| | 5 | | public class BIWScaleGizmos : BIWGizmos |
| | 6 | | { |
| | 7 | | const float MINIMUN_SCALE_ALLOWED = 0.01f; |
| | 8 | |
|
| | 9 | | [SerializeField] internal BIWGizmosAxis axisProportionalScale = null; |
| | 10 | |
|
| | 11 | | internal Vector2 lastMousePosition; |
| | 12 | | private Vector2 initialMousePosition; |
| | 13 | | private Vector3 initialHitPoint; |
| | 14 | | internal Vector3 lastHitPoint; |
| 183 | 15 | | private Dictionary<Transform, Vector3> entitiesOriginalPositions = new Dictionary<Transform, Vector3>(); |
| | 16 | |
|
| | 17 | | public override void Initialize(Camera camera, Transform cameraTransform) |
| | 18 | | { |
| 72 | 19 | | base.Initialize(camera, cameraTransform); |
| 72 | 20 | | axisProportionalScale.SetGizmo(this); |
| 72 | 21 | | } |
| | 22 | |
|
| 8 | 23 | | public override void SetSnapFactor(BIWGizmosController.SnapInfo snapInfo) { snapFactor = snapInfo.scale; } |
| | 24 | |
|
| | 25 | | public override float TransformEntity(Transform entityTransform, BIWGizmosAxis axis, float axisValue) |
| | 26 | | { |
| 2 | 27 | | Vector3 scaleDirection = GetScaleDirection(entityTransform, axis); |
| | 28 | |
|
| | 29 | | // In order to avoid to make the scale of each selected entity dependent of the 'entityTransform' parent, |
| | 30 | | // we temporally move all entities to the same position as 'entityTransform' before calculate the new scale. |
| 4 | 31 | | foreach (Transform entity in entityTransform) |
| | 32 | | { |
| 0 | 33 | | entitiesOriginalPositions.Add(entity, entity.transform.position); |
| 0 | 34 | | entity.transform.position = entityTransform.position; |
| | 35 | | } |
| | 36 | |
|
| 2 | 37 | | if (axis == axisProportionalScale) |
| | 38 | | { |
| | 39 | | // New scale calculation (for proportional scale gizmo) |
| 1 | 40 | | entityTransform.localScale = GetNewScale(entityTransform, axisValue, scaleDirection, false); |
| 1 | 41 | | } |
| | 42 | | else |
| | 43 | | { |
| | 44 | | // New scale calculation (for XYZ-axis scale gizmo) |
| 2 | 45 | | foreach (var originalEntity in entitiesOriginalPositions) |
| | 46 | | { |
| 0 | 47 | | Transform entity = originalEntity.Key; |
| 0 | 48 | | entity.transform.SetParent(null); |
| 0 | 49 | | entity.localScale = GetNewScale(entity.transform, axisValue, scaleDirection, true); |
| 0 | 50 | | entity.SetParent(entityTransform); |
| | 51 | | } |
| | 52 | | } |
| | 53 | |
|
| | 54 | | // Once the new scale has been calculated, we restore the original positions of all the selected entities. |
| 4 | 55 | | foreach (var originalEntity in entitiesOriginalPositions) |
| | 56 | | { |
| 0 | 57 | | Transform entity = originalEntity.Key; |
| 0 | 58 | | Vector3 originalPosition = originalEntity.Value; |
| | 59 | |
|
| 0 | 60 | | entity.position = originalPosition; |
| | 61 | | } |
| | 62 | |
|
| 2 | 63 | | entitiesOriginalPositions.Clear(); |
| | 64 | |
|
| 2 | 65 | | return axisValue; |
| | 66 | | } |
| | 67 | |
|
| | 68 | | private Vector3 GetScaleDirection(Transform entityTransform, BIWGizmosAxis axis) |
| | 69 | | { |
| 2 | 70 | | Vector3 scaleDirection = activeAxis.transform.forward; |
| 2 | 71 | | if (axis == axisProportionalScale) |
| | 72 | | { |
| 1 | 73 | | scaleDirection = Vector3.one; |
| 1 | 74 | | float inputDirection = (lastMousePosition.x - initialMousePosition.x) + (lastMousePosition.y - initialMouseP |
| 1 | 75 | | if (inputDirection < 0) |
| 0 | 76 | | scaleDirection = -Vector3.one; |
| | 77 | |
|
| 1 | 78 | | initialMousePosition = lastMousePosition; |
| 1 | 79 | | initialHitPoint = lastHitPoint; |
| 1 | 80 | | } |
| 1 | 81 | | else if (worldOrientedGizmos) |
| | 82 | | { |
| 1 | 83 | | scaleDirection = entityTransform.rotation * activeAxis.transform.forward; |
| 1 | 84 | | scaleDirection.x = Mathf.Abs(scaleDirection.x); |
| 1 | 85 | | scaleDirection.y = Mathf.Abs(scaleDirection.y); |
| 1 | 86 | | scaleDirection.z = Mathf.Abs(scaleDirection.z); |
| | 87 | | } |
| | 88 | |
|
| 2 | 89 | | return scaleDirection; |
| | 90 | | } |
| | 91 | |
|
| | 92 | | private Vector3 GetNewScale(Transform entityTransform, float axisValue, Vector3 scaleDirection, bool applyRounding) |
| | 93 | | { |
| 1 | 94 | | Vector3 initialEntityScale = entityTransform.localScale; |
| 1 | 95 | | if (applyRounding && snapFactor > 0) |
| 0 | 96 | | initialEntityScale = GetScaleRoundedToSnapFactor(entityTransform.localScale, axisValue); |
| | 97 | |
|
| 1 | 98 | | Vector3 newScale = initialEntityScale + scaleDirection * axisValue; |
| | 99 | |
|
| 1 | 100 | | if (Mathf.Abs(newScale.x) < MINIMUN_SCALE_ALLOWED || Mathf.Abs(newScale.y) < MINIMUN_SCALE_ALLOWED) |
| 0 | 101 | | newScale += scaleDirection * MINIMUN_SCALE_ALLOWED; |
| | 102 | |
|
| 1 | 103 | | return newScale; |
| | 104 | | } |
| | 105 | |
|
| | 106 | | private Vector3 GetScaleRoundedToSnapFactor(Vector3 scale, float axisValue) |
| | 107 | | { |
| 0 | 108 | | Vector3 activeAxisVector = GetActiveAxisVector(); |
| | 109 | |
|
| 0 | 110 | | scale = new Vector3( |
| | 111 | | activeAxisVector == Vector3.right ? (axisValue >= 0 ? Mathf.Floor(scale.x / snapFactor) : Mathf.Ceil(scale.x |
| | 112 | | activeAxisVector == Vector3.up ? (axisValue >= 0 ? Mathf.Floor(scale.y / snapFactor) : Mathf.Ceil(scale.y / |
| | 113 | | activeAxisVector == Vector3.back ? (axisValue >= 0 ? Mathf.Floor(scale.z / snapFactor) : Mathf.Ceil(scale.z |
| | 114 | |
|
| 0 | 115 | | return scale; |
| | 116 | | } |
| | 117 | |
|
| | 118 | | internal override void SetPreviousAxisValue(float axisValue, float transformValue) |
| | 119 | | { |
| 1 | 120 | | if (activeAxis == axisProportionalScale) |
| 0 | 121 | | previousAxisValue = 0; |
| | 122 | | else |
| 1 | 123 | | previousAxisValue = axisValue; |
| 1 | 124 | | } |
| | 125 | |
|
| | 126 | | internal override float GetHitPointToAxisValue(BIWGizmosAxis axis, Vector3 hitPoint, Vector2 mousePosition) |
| | 127 | | { |
| 1 | 128 | | if (axis == axisProportionalScale) |
| | 129 | | { |
| 1 | 130 | | if (startDragging) |
| | 131 | | { |
| 0 | 132 | | initialMousePosition = mousePosition; |
| 0 | 133 | | initialHitPoint = hitPoint; |
| | 134 | | } |
| | 135 | |
|
| 1 | 136 | | lastMousePosition = mousePosition; |
| 1 | 137 | | lastHitPoint = hitPoint; |
| 1 | 138 | | return Vector3.Distance(initialHitPoint, hitPoint); |
| | 139 | | } |
| | 140 | |
|
| 0 | 141 | | return axis.transform.InverseTransformPoint(hitPoint).z; |
| | 142 | | } |
| | 143 | | } |