Skip to content

Commit b2731ec

Browse files
author
Jared Bienz
committed
Progress on Nudge direction being based on controller direction. Added SpatialFloorQuad to enable spatial mapping in test scenes.
1 parent c94faa3 commit b2731ec

File tree

14 files changed

+285
-321
lines changed

14 files changed

+285
-321
lines changed

SpatialAlignment-Unity/Assembly-CSharp-Editor.csproj

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -138,10 +138,6 @@
138138
<Compile Include="Assets\HoloToolkit\UX\Scripts\Collections\Editor\CollectionEditor.cs" />
139139
<Compile Include="Assets\HoloToolkit\UX\Scripts\Editor\CompoundButtonSaveInterceptor.cs" />
140140
<Compile Include="Assets\HoloToolkit\UX\Scripts\Editor\KeyboardInputFieldEditor.cs" />
141-
<Compile Include="Assets\HoloToolkit-Examples\Medical\Editor\VolumeInformationEditor.cs" />
142-
<Compile Include="Assets\HoloToolkit-Preview\SpectatorView\Scripts\SpatialSync\MarkerGeneration3D\Editor\MarkerGeneration3DEditor.cs" />
143-
<Compile Include="Assets\HoloToolkit-Preview\SpectatorView\Scripts\Utilities\Editor\PlatformSwitcherEditor.cs" />
144-
<Compile Include="Assets\HoloToolkit-Preview\SpectatorView\UnityARKitPlugin\Plugins\iOS\UnityARKit\Editor\UnityARBuildPostprocessor.cs" />
145141
<Reference Include="Unity.TextMeshPro.Editor">
146142
<HintPath>C:/Users/jbienz/Code/JBienzMS/SpatialAlignment/SpatialAlignment-Unity/Library/ScriptAssemblies/Unity.TextMeshPro.Editor.dll</HintPath>
147143
</Reference>
@@ -415,9 +411,6 @@
415411
<Reference Include="Newtonsoft.Json">
416412
<HintPath>C:/Users/jbienz/Code/JBienzMS/SpatialAlignment/SpatialAlignment-Unity/Assets/HoloToolkit/Utilities/Scripts/GLTF/Plugins/JsonNet/Newtonsoft.Json.dll</HintPath>
417413
</Reference>
418-
<Reference Include="QRCodesTrackerPlugin.Placeholder">
419-
<HintPath>C:/Users/jbienz/Code/JBienzMS/SpatialAlignment/SpatialAlignment-Unity/Assets/HoloToolkit-Preview/QRTracker/Plugins/QRCodesTrackerPlugin.Placeholder.dll</HintPath>
420-
</Reference>
421414
<Reference Include="UnityEditor.Advertisements">
422415
<HintPath>C:/Users/jbienz/Code/JBienzMS/SpatialAlignment/SpatialAlignment-Unity/Library/PackageCache/[email protected]/Editor/UnityEditor.Advertisements.dll</HintPath>
423416
</Reference>

SpatialAlignment-Unity/Assembly-CSharp.csproj

Lines changed: 0 additions & 271 deletions
Large diffs are not rendered by default.

SpatialAlignment-Unity/Assets/HoloToolkit-Preview.meta

Lines changed: 0 additions & 8 deletions
This file was deleted.

SpatialAlignment-Unity/Assets/SpatialAlignment-Examples/Refinement/Scenes/SA-Refinement.unity

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -336,6 +336,12 @@ PrefabInstance:
336336
propertyPath: m_LocalEulerAnglesHint.z
337337
value: 0
338338
objectReference: {fileID: 0}
339+
- target: {fileID: 114129378875773704, guid: d29bc40b7f3df26479d6a0aac211c355,
340+
type: 3}
341+
propertyPath: FloorQuad
342+
value:
343+
objectReference: {fileID: 1520281459093612, guid: 7272c8e73bd10ec44b3bd7cd4451165c,
344+
type: 3}
339345
m_RemovedComponents: []
340346
m_SourcePrefab: {fileID: 100100000, guid: d29bc40b7f3df26479d6a0aac211c355, type: 3}
341347
--- !u!1001 &278732485
@@ -493,6 +499,7 @@ MonoBehaviour:
493499
m_Name:
494500
m_EditorClassIdentifier:
495501
refineOnStart: 0
502+
targetTransform: {fileID: 0}
496503
controllerPrefab: {fileID: 1469530028792937992, guid: f16944cb4f51ca045bc297eb5098241a,
497504
type: 3}
498505
directionAmount: 0.01
@@ -575,6 +582,7 @@ MonoBehaviour:
575582
m_Name:
576583
m_EditorClassIdentifier:
577584
refineOnStart: 0
585+
targetTransform: {fileID: 0}
578586
autoHideMeshes: 1
579587
directionPrefab: {fileID: 3882863017087187430, guid: 8e43969e520e5d149b6ca084446340d7,
580588
type: 3}

SpatialAlignment-Unity/Assets/SpatialAlignment-Examples/Refinement/Scenes/SA-RefinementBuilding.unity

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -405,6 +405,12 @@ PrefabInstance:
405405
propertyPath: m_LocalEulerAnglesHint.z
406406
value: 0
407407
objectReference: {fileID: 0}
408+
- target: {fileID: 114129378875773704, guid: d29bc40b7f3df26479d6a0aac211c355,
409+
type: 3}
410+
propertyPath: FloorQuad
411+
value:
412+
objectReference: {fileID: 1520281459093612, guid: 7272c8e73bd10ec44b3bd7cd4451165c,
413+
type: 3}
408414
m_RemovedComponents: []
409415
m_SourcePrefab: {fileID: 100100000, guid: d29bc40b7f3df26479d6a0aac211c355, type: 3}
410416
--- !u!1001 &278732485
@@ -956,6 +962,7 @@ MonoBehaviour:
956962
m_Name:
957963
m_EditorClassIdentifier:
958964
refineOnStart: 0
965+
targetTransform: {fileID: 0}
959966
autoHideMeshes: 1
960967
directionPrefab: {fileID: 3882863017087187430, guid: 8e43969e520e5d149b6ca084446340d7,
961968
type: 3}
@@ -1011,6 +1018,7 @@ MonoBehaviour:
10111018
m_Name:
10121019
m_EditorClassIdentifier:
10131020
refineOnStart: 0
1021+
targetTransform: {fileID: 0}
10141022
controllerPrefab: {fileID: 1469530028792937992, guid: f16944cb4f51ca045bc297eb5098241a,
10151023
type: 3}
10161024
directionAmount: 0.01

SpatialAlignment-Unity/Assets/SpatialAlignment/Refinement/Prefabs/NudgeController.prefab

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@ MonoBehaviour:
131131
Targets: []
132132
lockFocus: 0
133133
refinement: {fileID: 0}
134+
useLookDirection: 1
134135
--- !u!65 &1848810891968837930
135136
BoxCollider:
136137
m_ObjectHideFlags: 0
@@ -142,7 +143,7 @@ BoxCollider:
142143
m_IsTrigger: 0
143144
m_Enabled: 1
144145
serializedVersion: 2
145-
m_Size: {x: 0.5, y: 0.38, z: 0.02}
146+
m_Size: {x: 0.5, y: 0.45, z: 0.02}
146147
m_Center: {x: 0, y: 0, z: 0.17}
147148
--- !u!114 &6313569961156761970
148149
MonoBehaviour:

SpatialAlignment-Unity/Assets/SpatialAlignment/Refinement/Scripts/NudgeController.cs

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
2424
//
2525

26+
using HoloToolkit.Unity;
2627
using HoloToolkit.Unity.InputModule;
2728
using HoloToolkit.Unity.Receivers;
2829
using System;
@@ -43,8 +44,45 @@ public class NudgeController : InteractionReceiver
4344
[SerializeField]
4445
[Tooltip("The nudge refinement instance to control.")]
4546
private NudgeRefinement refinement;
47+
48+
[SerializeField]
49+
[Tooltip("Whether the look direction of the controller should be used as the forward direction for nudge operations.")]
50+
private bool useLookDirection = true;
4651
#endregion // Unity Inspector Variables
4752

53+
protected virtual RefinementDirection GetLookDirection()
54+
{
55+
// Which "forward" are we using
56+
Vector3 forward;
57+
if (refinement.Space == Space.World)
58+
{
59+
// Just use controller forward
60+
forward = transform.forward;
61+
}
62+
else
63+
{
64+
// Use controller forward but in target local space
65+
forward = refinement.TargetTransform.InverseTransformDirection(transform.forward);
66+
}
67+
68+
// Normalize the forward direction
69+
Vector3 normForward = forward.normalized;
70+
71+
// Round the normalized vector to 0 decimal places
72+
Vector3 roundedForward = normForward.Round();
73+
74+
// Try to convert the vector to a direction
75+
RefinementDirection direction;
76+
if (roundedForward.TryGetDirection(out direction))
77+
{
78+
return direction;
79+
}
80+
else
81+
{
82+
return RefinementDirection.Forward;
83+
}
84+
}
85+
4886
#region Overrides / Event Handlers
4987
protected override void InputUp(GameObject obj, InputEventData eventData)
5088
{
@@ -54,6 +92,12 @@ protected override void InputUp(GameObject obj, InputEventData eventData)
5492
return;
5593
}
5694

95+
// Update forward direction to match look direction?
96+
if (useLookDirection)
97+
{
98+
refinement.ForwardDirection = GetLookDirection();
99+
}
100+
57101
// Execute action and direction based on name
58102
switch (obj.name)
59103
{
@@ -88,6 +132,15 @@ protected override void InputUp(GameObject obj, InputEventData eventData)
88132
/// Gets or sets the nudge refinement instance to control.
89133
/// </summary>
90134
public NudgeRefinement Refinement { get { return refinement; } set { refinement = value; } }
135+
136+
/// <summary>
137+
/// Gets or sets whether the look direction of the controller should be
138+
/// used as the forward direction for nudge operations.
139+
/// </summary>
140+
/// <remarks>
141+
/// The default is <c>true</c>.
142+
/// </remarks>
143+
public bool UseLookDirection { get { return useLookDirection; } set { useLookDirection = value; } }
91144
#endregion // Public Properties
92145
}
93146
}

SpatialAlignment-Unity/Assets/SpatialAlignment/Refinement/Scripts/NudgeRefinement.cs

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,6 @@ protected override void OnEnable()
212212
// Pass to base
213213
base.OnEnable();
214214
}
215-
216215
#endregion // Unity Overrides
217216

218217
#region Public Methods
@@ -248,15 +247,8 @@ public void Nudge(RefinementDirection direction)
248247
// Create the offset
249248
Vector3 offset = actualDireciton.ToVector() * directionAmount;
250249

251-
// Update the position
252-
if (space == Space.World)
253-
{
254-
gameObject.transform.position += offset;
255-
}
256-
else
257-
{
258-
gameObject.transform.localPosition += offset;
259-
}
250+
// Update the position using the correct coordinate space (local vs world)
251+
TargetTransform.Translate(offset, space);
260252
}
261253

262254
/// <summary>
@@ -271,7 +263,7 @@ public void Nudge(NudgeRotation rotation)
271263
float angle = (rotation == NudgeRotation.Left ? -rotationAmount : rotationAmount);
272264

273265
// Update the rotation
274-
gameObject.transform.Rotate(upDirection.ToVector(), angle, space);
266+
TargetTransform.Rotate(upDirection.ToVector(), angle, space);
275267
}
276268

277269
/// <summary>

SpatialAlignment-Unity/Assets/SpatialAlignment/Refinement/Scripts/RayRefinement.cs

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ private void NextStep()
222222
CreateTarget(originPrefab, ref modelOrigin, currentStep.ToString());
223223

224224
// Parent the target
225-
modelOrigin.transform.SetParent(this.transform, worldPositionStays: true);
225+
modelOrigin.transform.SetParent(TargetTransform, worldPositionStays: true);
226226

227227
break;
228228

@@ -233,7 +233,7 @@ private void NextStep()
233233
CreateTarget(directionPrefab, ref modelDirection, currentStep.ToString());
234234

235235
// Parent the target
236-
modelDirection.transform.SetParent(this.transform, worldPositionStays: true);
236+
modelDirection.transform.SetParent(TargetTransform, worldPositionStays: true);
237237

238238
// Add line renderer
239239
modelLine = AddLine(modelDirection);
@@ -278,9 +278,7 @@ private void NextStep()
278278

279279
// Get transform positions
280280
Vector3 modelOriginWorld = modelOrigin.transform.position;
281-
// Vector3 modelOriginLocal = modelOrigin.transform.localPosition;
282281
Vector3 modelDirectionWorld = modelDirection.transform.position;
283-
// Vector3 modelDirectionLocal = modelDirection.transform.localPosition;
284282
Vector3 placementOriginWorld = placementOrigin.transform.position;
285283
Vector3 placementDirectionWorld = placementDirection.transform.position;
286284

@@ -297,10 +295,10 @@ private void NextStep()
297295
float rotation = (placementAngle - modelAngle);
298296

299297
// Update parent position to align origins
300-
gameObject.transform.position += offset;
298+
TargetTransform.position += offset;
301299

302300
// Update parent rotation, but around placement origin
303-
gameObject.transform.RotateAround(placementOriginWorld, Vector3.up, rotation);
301+
TargetTransform.RotateAround(placementOriginWorld, Vector3.up, rotation);
304302

305303
// Finish refinement
306304
FinishRefinement();
@@ -433,15 +431,15 @@ protected override void Start()
433431
if (originPrefab == null)
434432
{
435433
originPrefab = GameObject.CreatePrimitive(PrimitiveType.Sphere);
436-
originPrefab.transform.SetParent(transform, worldPositionStays: true);
434+
originPrefab.transform.SetParent(TargetTransform, worldPositionStays: true);
437435
originPrefab.GetComponent<Collider>().enabled = false;
438436
originPrefab.transform.localScale = new Vector3(DEF_SCALE, DEF_SCALE, DEF_SCALE);
439437
originPrefab.SetActive(false);
440438
}
441439
if (directionPrefab == null)
442440
{
443441
directionPrefab = GameObject.CreatePrimitive(PrimitiveType.Capsule);
444-
directionPrefab.transform.SetParent(transform, worldPositionStays: true);
442+
directionPrefab.transform.SetParent(TargetTransform, worldPositionStays: true);
445443
directionPrefab.GetComponent<Collider>().enabled = false;
446444
directionPrefab.transform.localScale = new Vector3(DEF_SCALE, DEF_SCALE, DEF_SCALE);
447445
directionPrefab.SetActive(false);
@@ -500,7 +498,7 @@ protected override void Update()
500498
Vector3 relativePos = direction.position - origin.position;
501499

502500
// Rotate the target direction to point away from the origin
503-
targetInterpolator.SetTargetRotation(Quaternion.FromToRotation(transform.up, relativePos));
501+
targetInterpolator.SetTargetRotation(Quaternion.FromToRotation(TargetTransform.up, relativePos));
504502

505503
// Get the line renderer
506504
LineRenderer line = (isModel ? modelLine : placementLine);

SpatialAlignment-Unity/Assets/SpatialAlignment/Refinement/Scripts/RefinementBase.cs

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,10 @@ public class RefinementBase : MonoBehaviour
8282
[SerializeField]
8383
[Tooltip("Whether to begin refining when the behavior starts.")]
8484
private bool refineOnStart;
85+
86+
[SerializeField]
87+
[Tooltip("Optional transform where nudge operations will be applied. If none is specified, the transform of the applied GameObject will be used.")]
88+
private Transform targetTransform;
8589
#endregion // Unity Inspector Variables
8690

8791
#region Overrides / Event Handlers
@@ -122,21 +126,33 @@ protected virtual void OnRefinementStarted()
122126
/// </summary>
123127
protected virtual void RestoreLastTransform()
124128
{
125-
transform.position = lastPosition;
126-
transform.rotation = lastRotation;
129+
targetTransform.position = lastPosition;
130+
targetTransform.rotation = lastRotation;
127131
}
128132

129133
/// <summary>
130134
/// Saves the current transform as the last transform.
131135
/// </summary>
132136
protected virtual void SaveLastTransform()
133137
{
134-
lastPosition = transform.position;
135-
lastRotation = transform.rotation;
138+
lastPosition = targetTransform.position;
139+
lastRotation = targetTransform.rotation;
136140
}
137141
#endregion // Overrides / Event Handlers
138142

139143
#region Unity Overrides
144+
/// <summary>
145+
/// Awake is called when the script instance is being loaded.
146+
/// </summary>
147+
protected virtual void Awake()
148+
{
149+
// If no transform is specified, use the GameObjects transform
150+
if (targetTransform == null)
151+
{
152+
targetTransform = gameObject.transform;
153+
}
154+
}
155+
140156
/// <summary>
141157
/// This function is called after all frame updates for the last frame of the object’s existence.
142158
/// </summary>
@@ -260,6 +276,11 @@ public void StartRefinement()
260276
/// Gets or sets whether to begin refining when the behavior starts.
261277
/// </summary>
262278
public bool RefineOnStart { get => refineOnStart; set => refineOnStart = value; }
279+
280+
/// <summary>
281+
/// Gets or sets an optional transform where nudge operations will be applied. If none is specified, the transform of the applied GameObject will be used.
282+
/// </summary>
283+
public Transform TargetTransform { get { return targetTransform; } set { targetTransform = value; } }
263284
#endregion // Public Properties
264285

265286
#region Public Events

0 commit comments

Comments
 (0)