Updated: 25th April 2018

HoloLens Dev 101: Building a Dynamic User Interface, Part 11 (Rotating Objects) ” HoloLens : Next Reality

Thus far, we’ve assembled a system which incorporates the consumer’s gaze and what the user is considering to ascertain the UI components they’re seeing. This type of context-dependent UI is all about streamlining the way we operate in augmented reality. My hopes for this particular series is that individuals will use it as a jumping-off point. Although we’re now adding items and changing them across the distance, this system may be a lot more elaborate, having lots of other means to interact with items.

Move along with Scale tools in place, we need to receive our Rotate instrument to work together. Fortunately, following the work of the past two classes, this is a fairly simple matter to achieve.

Step 1: Create a RotateToolInteraction Class

As there’s just a small difference in the code to transfer our items versus rotating our items, we’ll once again utilize the MoveTool.cs from lesson 9 in this series since the template for our RotationToolInteraction course. So, as we’ve done a number of occasions before, pick the “Rotate” subobject of the TransformUITool, then click on the”Add Component” button again.

Click on the “New Script” option at the bottom of the components list, also when the New Script box appears, then form RotateToolInteraction to the Name area. Then click on “Create and Insert.”

Once it finishes its own creation and compiling procedure, double-click the “RotateToolInteraction.cs” script file to open it in Visual Studio. It will load Visual Studio in case you don’t already have it opened.

Step 2: Duplicate the MoveTool.Cs Code

Now double-click on the “MoveTool.cs” file to make sure it’s opened in Visual Studio too.

Once Visual Studio is opened, search for your “MoveTool.cs” tab, and click on it to bring it forward. Select all of the code using the keyboard shortcut Ctrl + A, hit on Ctrl + C to copy the entire class.

In Visual Studio, select the “RotateToolInteraction.cs” tab to make it the most active course. Again, use the keyboard command Ctrl + A to select everything in the course, then utilize Ctrl + V to glue the contents we’ve got from the clipboard into the new class.

In the end, on line, replace the category name announcement “MoveTool” using “RotateToolInteraction,” and click on “File,” then “Save” With course creation out of the way, we’ve got the code adjustments we will need to make.

Step 3: Combine a Vector3 into a Quaternion

Back in Unity, object manipulation which involves rotation, position, and scale has been handled in the Change component. With quick and easy access to such components of our transform, together with data structures like Vector3s and Quaternions ( more about those 2 terms soon) we could make sweeping changes, often with one line of code.

A Vector3 Is For Moving & Scaling

As we’ve talked about formerly, the information type that we use for scale and position is known as a Vector3. This information type is made up of three float values: X, Y, along with Z. While we often wind up using high quality Vector3s for rotations, under-the-hood rotations utilize something quite distinct, which we’ll address in a moment. First, here’s a quick refresher on the principal line of code we’re working with.

Since we simply copied and glued move code, the following line of code (line 57 in the event you are following along) is the line which handles our motion updates every frame.

HostTransform.position = Vector3.Lerp (HostTransform.position, HostTransform.position + manipulationDelta, PositionLerpSpeed);

Here we’re upgrading the job of the HostTransform item’s transform using Linear interpolation, or Lerp, for brief. Linear interpolation is a way to smooth out the motion from frame to frame. From the Lerp procedure, we’re incorporating the current position of the object (HostTransform.position) into the manipulationDelta (or even the distance that our hands has moved between frames).

If we proceed hands in 1 way, we add a positive number. Moving in the other course adds a negative number, or, in case you remember your math, that is subtracting. Ultimately, when we don’t move our hands in any respect, we’re adding zero, and so causing no change.

Quaternions Are for Rotating

A quaternion is a complicated, four-element structure that does not endure the trappings that include using Vector3s along with Euler angles (a construction that is synonymous with a Vector3 at Unity) for rotations like gimbal lock. While the math behind a quaternion is complicated, as we don’t need to change them straight or, they are not so bad. On a high degree, quaternions are extremely simple to work with. Oftentimes, we’ll use Euler angles, which also utilize a Vector3 information structure, but we’re converting the Vector3 into a quaternion as required.

To see that in action, change line 57 we’re just talking about on this line:

HostTransform.rotation = Quaternion.Lerp(currentRot ,currentRot * Quaternion.Euler(manipulationDelta * DistanceScale), RotationLerpSpeed);

Should you examine the code, you might see how similar it looks to the Move code. Again, we’re using the current rotation of the object and, even in this circumstance, multiplying it by a quaternion. As manipulationDelta is a Vector 3 and DistanceScale is a float (a top precision floating point number), we cannot directly multiply into a quaternion. Rather we utilize the Quaternion.Euler system, which requires the product of multiplying manipulationDelta and DistanceScale and returns a quaternion, converting our Vector3 into the output we need.

In case you are wondering, the two quaternion and Vector3 include a way for Lerp.

Step 4: Compile, Test & Adjust as Needed

With that change, we now have the ability to rotate our items with our UI system. As we’ve done several times before, build and compile the program and give it a run in your HoloLens.

Look at among those items, and rotate it with our latest tool. If it spins uncontrollably, it’s possible to easily adjust the rotation rate by shifting the Rotation Lerp Rate slider in the “RotationToolInteraction” component.

Congrats, this is a significant landmark. While I’m not calling this conclusion, since there are some obvious things still missing, we need to take a breath. I’m going to go along with my puppy.

See you in the next semester, where we all make it easy to tell all those tools are being chosen by having a highlight color — we are not going to deceive by making it a feel swap. Following that, we’ll make our Scale modes selectable from the program itself.

Cover image and screenshots by Jason Odom/Next Truth