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