In this tutorial, you’ll continue building a Spatial SDK app with Spatial Editor by learning about common processes like importing assets, adding existing components, and creating custom components. By the end of this tutorial, you’ll have a grabbable basketball in your scene that a robot will constantly look at.
For best practices when creating with Spatial SDK, see the design guidelines to further enhance your app’s user experience and broaden its audience.
Import an asset
In the Spatial Editor, imported assets are stored in the asset panel. To learn more about importing assets, see Import and manage assets.
Open the subfolder basketballHoop. In it, you’ll see the file basketballHoop.glb.
In Spatial Editor, click File > Import, then navigate to basketballHoop.glb, and click Open to import the model into your project.
If you’re creating a new Spatial SDK app, skip to the next section. If you’re adding Spatial SDK to an existing app, follow the remaining steps to add some additional assets.
In the toolbar, click the books icon (the Open Asset Library button).
The asset library window appears.
In the asset library window, double-click the robot and basketBall models to add them to your assets. You’ll use them in the next section.
Once the models finish downloading, they will appear in the Project Assets panel.
The robot, basketball, and basketball hoop assets in the Assets panel.
Add and position objects in the Viewport
In the Spatial Editor, the Viewport is the main area where you interact with and build your spatial environment. To learn more about positioning objects, see Position and size objects.
Drag and drop the robot, basketBall, and basketballHoop models from the Assets panel to the Composition panel.
The models appear in the viewport at the world origin, which is 0 on the X, Y, and Z axes.
In the Composition panel, select basketballHoop.
In the basketballHoop properties panel on the right, update the Transformation values to match these.
Translation
X: -2
Y: 0.15
Z: 1
Rotation
Y: 45
Scale
X: 0.25
Y: 0.25
Z: 0.25
Position the basketball model using the following values.
Translation
Y: 0.25
Z: 0.9
Position the robot model using the following values.
Translation
X: 1
Y: 1
Z: 0.6
Here’s what the scene looks like in Spatial Editor once you’ve positioned the models.
Assign components
Components are an essential part of the Entity-Component-System (ECS) architecture used in Spatial SDK. Components are containers for app data that provide specific functionalities to scene objects. For example, a grabbable component makes any object it’s attached to grabbable with raycasting. Some components are provided by the SDK, but you can also write your own custom components.
To assign components to entities with the Spatial Editor, follow these steps.
In the Properties panel, add the Grabbable component to the basketball by clicking the + next to Meta Spatial SDK Components, searching for Grabbable, and then clicking the search result.
Note: If you add custom components to a project, like the LookAt component you create later in this tutorial, they won’t appear in the search results until you click the Reload button in the Meta Spatial SDK Components popup.
Save the project by clicking File > Save.
Explore the main activity
In Android, the main activity file contains the core activity lifecycle of a Spatial SDK app. The activity lifecycle consists of several boilerplate functions you need for Spatial SDK apps.
Some boilerplate functions are provided by Android, and some are specific to Spatial SDK. Here’s a partial list of functions you’ll see in a main activity file.
registerFeatures(): Registers the VR feature with the framework.
onCreate(): Initializes the activity and sets up the scene.
onSceneReady(): Prepares the scene for rendering.
createUI(): Creates the UI panel scene object.
loadGLXF(): Loads the GLXF file with the robot and ball models. Callback code is called once the scene is ready.
The main activity file also sets up the app’s environment, including lighting and a skybox, loads 3D models from the corresponding .glxf file, and handles registering new panels.
The main activity file is named either ImmersiveActivity.kt or CustomComponentsStarterActivity.kt depending on which option you chose in Build your first Spatial SDK app.
Create a custom component
A component consists of two files, an XML file that lists the component’s attributes, and a Kotlin file that defines the component’s system. To learn how to create your own components, see Write a new component and Write a new system.
The sample app already includes the required files for the LookAt component, which causes an object to continually track a target object. The component uses two files, LookAtSystem.kt and LookAt.xml.
LookAtSystem.kt
LookAtSystem.kt updates the entity’s position and rotation to achieve the desired behavior. It executes every frame and updates entities that the LookAt component is attached to.
If you’re adding Spatial SDK to a 2D app, you’ll need to uncomment this file.
Select the contents of app/src/quest/java/com/meta/media/templateLookAtSystem.kt and then uncomment them by clicking Code > Comment with Block Comment in the toolbar.
Save the file.
LookAt.xml
LookAt.xml defines the reusable component with the following customizable attributes:
target: The entity that this entity should look at.
lookAtHead: A boolean value indicating whether the entity should look at the head of the target entity or its center.
axis: An enumeration value indicating which axis the entity should look at. It can be set to ALL, Y, or other values.
speed: A float value indicating how fast the entity should rotate to look at the target.
offset: A Vector3 value indicating the offset from the target position that the entity should look at.
If you’re adding Spatial SDK to a 2D app, you’ll need to uncomment this file.
Select the contents of app/src/main/components/LookAt.xml and then uncomment them by clicking Code > Comment with Block Comment in the toolbar.
Save the file.
Register component in the main activity
Now that your component is ready, you’ll register it with both the component manager and the system manager so you can generate it as needed.
Open the main activity file for your app (the file is named either ImmersiveActivity.kt or CustomComponentsStarterActivity.kt).
Inside the onCreate function, underneath // TODO: register the LookAt system and component, add the following code to register the LookAt component and the LookAtSystem.
If you haven’t reloaded your project in Spatial Editor since you added the LookAt.xml file, then reload the Meta Spatial SDK Components list.
In the Composition panel, select robot.
In the robot properties panel on the right, search for LookAt, and then select the LookAt search result from the list. Now the LookAt component is added to the robot.
In the LookAt component’s properties, set the target entity to the basketball entity.
Change the second field of offset to 180.
Add component programmatically
Underneath // TODO add the LookAt component to the robot so it points at the basketBall, add the following code.
val robot = composition.getNodeByName("robot").entity
val basketBall = composition.getNodeByName("basketBall").entity
robot.setComponent(LookAt(basketBall, offset = Vector3(x = 0f, y = 180f, z = 0f)))
Here’s what the code does:
Creates variables for the robot and basketball using the getNodeByName function on the composition.
Uses the setComponent function on the robot entity to add the LookAt component and set the basketball as the target entity.
Adds an offset to ensure the robot points correctly at the basketball.
Preview the component
In Android Studio, click the green Run ‘app’ button.
Move the ball by hovering over it with your controller, pressing the trigger, and then moving your hand.
As you grab and move the ball, the LookAt component rotates the robot to face the ball.
(Optional) Create, modify, and register a panel
This section briefly explains how to add a new panel to your scene via Spatial Editor and customize the panel’s content. For a detailed explanation of this process, see Register 2D panels.
Open the app’s Main.metaspatial file in Meta Spatial Editor.
In the upper left toolbar next to the cylinder button, click the Add 2D Panel button.
A panel appears at the world origin of the scene.
In the Properties panel, set ID to the following:
@id/ui_example2
The ID should reference an ID resource (R.id.ui_example2) rather than a layout resource, as the layout is specified separately in the registration. The ui_example2 refers to the file ui_example2.xml, which you’ll create in the next section. That file will add content to the panel.
Set the panel’s Translation values as follows.
X: 2
Y: 0.8
Z: 1.75
In the Composition panel, right-click on Panel1, select Rename..., and enter Panel 2.
In the top toolbar, select File > Save.
Open Android Studio.
In the file directory panel, find the file app/src/main/res/layout/ui_example.xml.
Duplicate the ui_example.xml file and name the copy ui_example2.xml.
In app/src/main/res/values/ids.xml, inside the <resources> block, add a line for the new panel.
<item type="id" name="ui_example2" />
In your main activity file, below the comment TODO: add a second panel in the registerPanels() method, add this code to register your new panel.