My First “Real” Magic Leap Project: Part 1
Exploring building a more complex Magic Leap (Unity) application; in this case a simple table game.
Having written a number of simple Magic Leap applications, thought to take a stab at a more complex application that utilizes a number of the Magic Leap features; documenting my experience along the way.
note: The completed application is available for download; throughout the series the incremental changes in the code is available through various branches in a Git repository. The room used in the series is available in the Virtual Room Generator folder in the download.
The application, will assume (will use the simulator) the user is in a room with a table in the center. The table will have three long blocks on it.
Using the application…
- The user first will define the area to build a mesh; surrounding the table
- Next the user will walk around the table to build a complete mesh of the table and the items on it
- The user finally will place a virtual ball on the table and launch it in a particular direction. The goal will be to have the ball bounce as many times off the blocks as possible.
Prerequisites
Before one can begin building a more substantial Magic Leap project, one needs to first understand Hello Cube; wrote a short article on that topic.
One additional step is to import the Magic Leap Unity Package into your Unity project; wrote a series of articles that described this and using a number of the Magic Leap features.
Virtual Room Generator
As many of us currently do not have the Magic Leap One device yet, we need to use the Virtual Room Generator to setup our room; in this case we want a table with some blocks.
note: I later removed the “bean bag” in the middle of the table.
Observations:
- It will take some trial and error to generate a suitable random room to build off from; couldn’t figure out how to add the items on the table without randomly generating a room with the maximum number of Random Clutter
Meshing
Many of the other Magic Leap features, e.g., hand gestures, plane detection, etc., are simply exposed as straightforward classes in the namespace UnityEngine.Experimental.XR.MagicLeap. Having tried, now twice, to deconstruct Magic Leaps examples, Meshing appears to be more complicated.
Luckily, Magic Leap has provided a tutorial on the topic: Introduction to Meshing — Unity Edition.
Observations:
- The tutorial heavily depends on using a prefab, Assets / MagicLeap / Examples / Prefabs / MLSpatialMapper; likewise we will follow this approach (even though it feels uncomfortable using example code as a dependency of our application)
- Under-the-hood, the prefab depends on a complicated script that is a low-level Unity class UnityEngine.XR.MagicLeap.MLSpatialMapper
- One important thing to understand with Magic Leap is that a color’s brightness is it’s transparency; Unity’s transparency is not used (explained below)
Magic Leap uses a light-based additive color space. Since black is the absence of light, it renders as transparent. If a black object is viewed in front of other digital content, it is perceived as masking out the content behind it. If you want to create the illusion of showing black or darker colors, you will have to surround the black area with light. In small areas, the transparent effect will be less noticeable, however, in larger areas you will notice the transparency.
— Magic Leap — Color
The Missing MLSpatialMapper API Reference
While the MLSpatialMapper prefab is delivered as example code (and as such is not documented in the Magic Leap API Reference), it distinctly feels like it should be treated as an API as:
- Both Magic Leap’s Introduction to Meshing — Unity Edition and Meshing Example depend on it
- Its underlying script UnityEngine.XR.MagicLeap.MLSpatialMapper is strangely complicated (and is delivered as a hidden library)
The prefab can be configured by modifying its Transform and ML Spatial Mapper (Script) components.
The most relevant component settings are:
- The Transform component defines a cubic area (units are in meters) in which the mapper finds (real world) objects to map into mesh objects. Because the Magic Leap One hardware, presumably, can only map what is visible to it (and the cameras are in front); it make sense to set this cubic area to be in front of the Magic Leap One hardware
- Enabling the ML Spatial Mapper component enables the creation of the mesh objects; likewise disabling it disables the creation
- The ML Spatial Mapper > Mesh Prefab property defines the prefab to use when creating mesh objects; e.g., might want to set the physics material on the prefab
- The ML Spatial Mapper > Mesh Parent property defines the game object in which to add mesh objects as children to
Using MLSpatialMapper prefab amounts to instantiating it and creating an empty game object (and assigning it to the instance’s ML Spatial Mapper component’s Mesh Parent property).
Next Steps
In the next article, My First “Real” Magic Leap Project: Part 2, we will control the meshing process.