Last Updated: Apr 29, 2017
- Create a Manager that knows about all the Holograms to instantiate
- Update the Object Placer to instantiate Holograms based on the Manager
This section of the tutorial will focus on using the techniques for placing and scaling a hologram from the previous tutorial and create a manager to instantiate many objects of different sizes. At the end of this tutorial you will have an entire scene calculated with Spatial Understanding and the holograms placed and scaled in the real world.
Create the Object Collection Manger
Open the project in Unity. Create a new script called ObjectCollectionManager.cs. Add the Manager to your placement GameObject. Your scene should look like this:
Open the script in Visual Studio. Add the following code to the ObjectCollectionManager.cs:
We start by establishing a few properties that are used to manage the desired size of buildings and a place to hold all the models. The ScaleFactor is used to track (or force) the scaling that will happen to the holograms. Finally, the ActiveHolograms list is used to track all the holograms after they are instantiated, this makes it easy to add other scripts that manipulate the holograms after they are instantiated.
We add the CreateWideBuilding method which is a refactored version of the same method from the previous tutorial. This method calls CreateBuilding with parameters telling the method how to instantiate the building. The only difference from the last tutorial is that we add the instantiated gameobject to the ActiveHolograms list. We also replaced the call to RescaleDesiredSizeProportional with a call to the helper RescaleToSameScaleFactor. The reason for this change is that we need all the holograms to get the same scale factor applied to them so that they all look the same size relative to one another.
The new RescaleToSameScaleFactor is made of a series of utility functions that looks at every hologram in the manager and calculates the largest scale factor that would allow every hologram to fit within the bounding box that is established by WideBuildingSize. Once this is calculated it is reused in every future call and does not need to be recalculated.
Switch back to Unity 3D. The object collection manager now has some properties, we need to populate. Use the following settings:
The Object Collection Manager is now configured to display three different buildings, scaled to fit within a bounding box of 1 x 0.5 x 0.5 meters.
Some of the functionality of the Object Placer Script has moved into the Object Collection Manager. Next we will update Object Placer to remove that redundant functionality. Open ObjectPlacer.cs in Visual Studio. Edit the Contents to:
These changes will remove some of the properties in Unity 3D. Switch back to Unity 3D. Verify that your Placement GameObject properties match these:
Build and Deploy to your device. Make sure you map a lot of floor space, and you should now see three buildings. Notice that if you map a small amount of floor space the ObjectPlacer handles it gracefully and only displays as many holograms that it can fit in the available floor space.
Add other Types of Holograms
Next we will extend this to generate many different types of buildings, and create an entire town. Update ObjectCollectionManager.cs with these changes:
These changes add properties to allow you to specify other types of building and trees. These work just like the methods we created earlier to create wide buildings.
Modify ObjectPlacer.cs to contain these contents:
These changes add an extra property that is used to tell the ObjectPlacer whether or not to draw the trees. We also added some logic to find locations for these trees. Notice that in the CreateScene method we are creating a work list of all the queries for every object we hope to place in the scene and sending all that work to the solver at once. Because the Object Placement Solver is computationally intensive it is better to do all of this work at once while the scene is loading. For this example I place the result of the query for each object in a work queue called _results which is serviced every frame. This causes the objects to pop up as each object is found, this probably is not desirable for a real application, but helps demonstrate the speed of the solver. When the solver is unable to place an item we continue without any errors to the user. The goal for this demo is to fill all the available floor space with holograms, not to display every hologram.
Switch back to Unity 3D. The Placement GameObject now has additional properties to configure. Set them with the following settings:
Build and Deploy to your device. You should now see a complete town generated. Because the spatial map is slightly different each time, the solver will place items differently in the same room. ObjectPlacer and ObjectCollectionManager can easily be customized to manage holograms for a different application, or even generalized as a component to use across different applications.
Target Versions: Unity 5.6.1f1 | HoloToolkit Jun-04-2017 | Visual Studio 2017 15.2
|Unity 3D Project Creation||How to create a HoloLens project in Unity 3D|
|Source Control||Configure git for HoloLens / Unity work|
|Spatial Mapping||How to spatial map a Room|
|Object Surface Observer||Set up fake spatial map data for the Unity editor|
|TagAlongs and Billboarding||Tag along instructions to the user to force text on screen|
|Spatial Understanding||Add spatial understanding to get play space detail|
|Finalizing Spatial Understanding||Complete Spatial Understanding and Input Manager|
|Object Placement and Scaling||Find valid locations for holograms in the play space|
|Hologram Management||Manage the holograms you want to place in the world|
|Cursor and Voice||Add a cursor and voice commands|
|Occlusion||Add occlusion with the real world to your scene|
|Physics||Add Colliders and RigidBodys to your holograms|