@@ -16,6 +16,7 @@ public class ScaleHandles : HandlesBase
16
16
protected override HandlesBaseConfiguration BaseConfig => config ;
17
17
private ScaleHandlesConfiguration config ;
18
18
private bool areHandlesFlattened = false ;
19
+ private FlattenModeType currentFlattenAxis = FlattenModeType . DoNotFlatten ;
19
20
20
21
/// <summary>
21
22
/// Cached handle positions - we keep track of handle positions in this array
@@ -171,16 +172,9 @@ private Bounds CreateVisual(int handleIndex, GameObject parent, bool isFlattened
171
172
handleVisual . transform . localPosition = Vector3 . zero ;
172
173
handleVisual . transform . localRotation = Quaternion . identity ;
173
174
174
- if ( isFlattened )
175
- {
176
- // Rotate 2D slate handle asset for proper orientation
177
- parent . transform . Rotate ( 0 , 0 , - 90 ) ;
178
- }
179
- else
180
- {
181
- Quaternion realignment = GetRotationRealignment ( handleIndex ) ;
182
- parent . transform . localRotation = realignment ;
183
- }
175
+
176
+ Quaternion realignment = GetRotationRealignment ( handleIndex , isFlattened ) ;
177
+ parent . transform . localRotation = realignment ;
184
178
185
179
if ( config . HandleMaterial != null )
186
180
{
@@ -190,7 +184,7 @@ private Bounds CreateVisual(int handleIndex, GameObject parent, bool isFlattened
190
184
return handleVisualBounds ;
191
185
}
192
186
193
- protected Quaternion GetRotationRealignment ( int handleIndex )
187
+ protected Quaternion GetRotationRealignment ( int handleIndex , bool isFlattened )
194
188
{
195
189
// Helper lambda to sign a vector.
196
190
Vector3 signVector ( Vector3 i ) => new Vector3 ( Mathf . Sign ( i . x ) , Mathf . Sign ( i . y ) , Mathf . Sign ( i . z ) ) ;
@@ -200,21 +194,52 @@ protected Quaternion GetRotationRealignment(int handleIndex)
200
194
Vector3 neutralHandle = signVector ( HandlePositions [ 6 ] ) ;
201
195
Vector3 handlePos = signVector ( HandlePositions [ handleIndex ] ) ;
202
196
203
- // Flip the handle if it's on the underside of the bounds.
204
- Quaternion flip = Quaternion . Euler ( 0 , 0 , handlePos . y > 0 ? 0 : 90 ) ;
197
+ if ( isFlattened )
198
+ {
199
+ Vector3 axis = Vector3 . forward ;
200
+ switch ( currentFlattenAxis )
201
+ {
202
+ case FlattenModeType . FlattenAuto :
203
+ Debug . LogError ( "ScaleHandles should never receive FlattenAuto. BoundsControl should pass actualFlattenAxis" ) ;
204
+ break ;
205
+ case FlattenModeType . FlattenX :
206
+ axis = Vector3 . right ;
207
+ break ;
208
+ case FlattenModeType . FlattenY :
209
+ axis = - Vector3 . up ;
210
+ break ;
211
+ case FlattenModeType . FlattenZ :
212
+ axis = Vector3 . forward ;
213
+ break ;
214
+ }
215
+
216
+ Vector3 neutralProjected = Vector3 . ProjectOnPlane ( neutralHandle , axis ) ;
217
+ Vector3 handleProjected = Vector3 . ProjectOnPlane ( handlePos , axis ) ;
205
218
206
- float angleAroundVertical = Vector3 . SignedAngle ( new Vector3 ( neutralHandle . x , 0 , neutralHandle . z ) ,
207
- new Vector3 ( handlePos . x , 0 , handlePos . z ) ,
208
- Vector3 . up ) ;
219
+ float angleAroundAxis = Vector3 . SignedAngle ( neutralProjected ,
220
+ handleProjected ,
221
+ axis ) ;
209
222
210
- return Quaternion . Euler ( 0 , angleAroundVertical , 0 ) * flip ;
223
+ return Quaternion . AngleAxis ( angleAroundAxis , axis ) * Quaternion . LookRotation ( axis , Vector3 . up ) ;
224
+ }
225
+ else
226
+ {
227
+ // Flip the handle if it's on the underside of the bounds.
228
+ Quaternion flip = Quaternion . Euler ( 0 , 0 , handlePos . y > 0 ? 0 : 90 ) ;
229
+
230
+ float angleAroundVertical = Vector3 . SignedAngle ( new Vector3 ( neutralHandle . x , 0 , neutralHandle . z ) ,
231
+ new Vector3 ( handlePos . x , 0 , handlePos . z ) ,
232
+ Vector3 . up ) ;
233
+ return Quaternion . Euler ( 0 , angleAroundVertical , 0 ) * flip ;
234
+ }
211
235
}
212
236
213
237
internal void Reset ( bool areHandlesActive , FlattenModeType flattenAxis )
214
238
{
215
239
IsActive = areHandlesActive ;
216
240
ResetHandles ( ) ;
217
241
bool isFlattened = flattenAxis != FlattenModeType . DoNotFlatten ;
242
+ currentFlattenAxis = flattenAxis ;
218
243
if ( areHandlesFlattened != isFlattened )
219
244
{
220
245
areHandlesFlattened = isFlattened ;
0 commit comments