-
Notifications
You must be signed in to change notification settings - Fork 0
/
RotateTool.cs
98 lines (73 loc) · 3.59 KB
/
RotateTool.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
//
// Cuboid (http://github.com/arjonagelhout/cuboid)
// Copyright (c) 2023 Arjo Nagelhout
//
using System.Collections;
using System.Collections.Generic;
using Cuboid.Models;
using UnityEngine;
using Cuboid.Utils;
using Cuboid.Input;
using UnityEngine.UIElements;
namespace Cuboid
{
public class RotateTool : AxisHandleTool<RotateHandle, AxisHandleData>
{
[SerializeField] private GameObject _rotateHandlePrefab;
private RotateHandle[] _rotateHandles = new RotateHandle[3];
protected override void InstantiateHandles()
{
_rotateHandles = InstantiateHandles(_rotateHandlePrefab);
}
protected override void UpdateHandlesRotation()
{
RotatePlaneHandles(_rotateHandles);
}
protected override void OnHandleBeginDrag()
{
base.OnHandleBeginDrag();
// show rotation visual
}
protected override void OnHandleEndDrag()
{
base.OnHandleEndDrag();
// hide rotation visual
}
protected override void OnHandleDrag(SpatialPointerEventData eventData, AxisHandleData data)
{
TransformData initialTransformData = _selectionController.InitialSelectionTransformData;
TransformData newTransformData = initialTransformData;
// there are two approaches for rotation, one being the raycast with plane approach
// and the other being using the world position, and simply projecting it to the rotation plane
//
// the RotateHandle could be changed so that it sets its Configuration.customGetSpatialPointerInputPosition
// delegate, but it will probably feel inconsistent with the rest of the interactions.
// we can convert the position to the local coordinate space of the initialTransformData
Axis axis = data.axis;
(Axis, Axis) otherAxes = axis.GetOtherAxes();
Plane plane = new Plane(axis.ToVector3(), Vector3.zero);
// pressed position
Vector3 pressedPosition = eventData.spatialPressPosition;
Vector3 position = eventData.spatialPosition;
initialTransformData.SetScaleMutating(Vector3.one);
Vector3 localPressedPosition = initialTransformData.WorldToLocalMatrix.MultiplyPoint3x4(pressedPosition);
Vector3 localPosition = initialTransformData.WorldToLocalMatrix.MultiplyPoint3x4(position);
Vector3 localProjectedPressedPosition = plane.ClosestPointOnPlane(localPressedPosition);
Vector3 localProjectedPosition = plane.ClosestPointOnPlane(localPosition);
float deltaAngle = Vector3.SignedAngle(localProjectedPressedPosition, localProjectedPosition, axis.ToVector3());
if (ModifiersController.Instance.ShiftModifier.Value)
{
deltaAngle = deltaAngle.Snap(360, 8);
}
Quaternion initialRotation = initialTransformData.Rotation;
Vector3 rotatedAxis = initialRotation * axis.ToVector3();
Quaternion deltaRotation = Quaternion.AngleAxis(deltaAngle, rotatedAxis);
// we should multiply the rotations, because setting the rotation of a specific axis (x, y or z)
// doesn't actually rotate it around that axis.
Quaternion rotation = deltaRotation * initialRotation;
newTransformData.SetRotationMutating(rotation);
_selectionController.SetCurrentSelectionTransformData(newTransformData);
_selectionController.UpdateRealityObjectInstanceTransforms();
}
}
}