Spectacles Attach Lines Sample Project
Lines Sample Project builds on Simple Plane Tracking (SPT) to create lines in your space. The target displays the location and orientation in front of your gaze. Tap the touchpad to place pointA of the line. Tap the touchpad again to place pointB of the line.
This project shows how to:
- Display text to the screen, including debug logs
- Add a smoothing function to the single plane tracking target (SPT), which will approximate the target when the SPT has lost tracking in front of the player’s fov so that the player always sees something
- Make lines
- Make a simple state machine
Please download the project here.
Your camera should always be set to “Camera Type: Perspective” because we are working in 3D.
In order for SPT to work, it needs to know your camera’s rotation and movement in space relative to the images seen through the stereo cameras. Therefore, you must attach a “Device Tracking” component to the camera and set “Tracking Mode: World.”
In Lens Studio, the z axis of the camera points backwards, behind the camera. To place an object in front of the camera, you must set that object’s local z position relative to the camera to be in the negative direction. For example, the “Text Object” scene object is parented to the camera, so it is in camera space, and its local position relative to the camera is (0, 0, -80).
Sometimes, the camera pointing backwards confuses me during matrix operations. Therefore, I make a “CameraPivot” scene object parented to the camera, and I point the pivot so its forward points in front of the camera. Please use whatever math or approach works for you.
The SmoothFollowTarget script uses the target generated by single plane tracking and applies a function to generate a new target, which will always stay within the player’s field of view (fov). The intention is to create a more smooth effect for players who care more about always having feedback than always having tracking. When the SPT target loses tracking in front of the player, getting “stuck” off the player’s fov, the Smooth Follow Target will use the last known depth for the last tracked plane and simply lerp to within the player’s current fov. The debug sphere at the center of the screen is green if we have tracking in our fov and red if we have lost tracking in our fov, and the SmoothFollowTarget is taking over.
When the player taps the touchpad, LineController generates lines using the line prefab and passes whatever target it’s using (SPT or SmoothFollowTarget will work) to the Line script on the line prefab.
LineController also plays a particle effect and audio effect when the player taps the touchpad. These effects are controlled by the ParticleBurstController. In parenting one particle effect to the target and simply replaying it rather than spawning new ones, the intention is a more lightweight way to use particle effects on Spectacles.
Line uses the reference to its target and a simple state machine to make a line. It’s first state upon initializing is to set its position A. Then it increments its state to its second state, which is to set its position B. Position B updates as the player moves to find the next place they’d like to attach the line. Once the player taps the touchpad again, the line is placed and is now static in the scene.
Line uses two points to rotate and stretch a cylinder so that it makes a line. The position of the cylinder is actually halfway between the two points. The rotation and scale are such that one side of the cylinder reaches point A and the other side reaches point B. This is a unique way of making a line, but it works. The script SimpleLine, which is also parented to the main camera, is a more simple example of how to make a static line with two points set in the editor. I recommend starting with the script first for your project.
In closing, this project is heavily annotated and commented, therefore this post is short. Please post any questions below and enjoy!