Develop
Develop
Select your platform

Add a cube

Updated: Oct 2, 2024
In this tutorial, you will learn how to add a cube to your Spatial SDK app and then change its appearance.

Step 1: Modify the main activity

The main activity includes an onSceneReady() function. The system calls onSceneReady() when the spatial scene is ready to be populated. To add a cube, you’ll modify the onSceneReady() function.
  1. In Android Studio, open the StarterSampleActivity.kt file, which has a file path of /Meta-Spatial-SDK-Samples/StarterSample/app/src/main/java/com/meta/spatial/samples/startersample. This file is the starting point for your app.
    The location of the StartSampleActivity.kt file
  2. In the list of imports at the top of the file, add this code to import Spatial SDK dependencies.
     import com.meta.spatial.core.Quaternion
     import com.meta.spatial.toolkit.Box
     import com.meta.spatial.toolkit.Color4
    
  3. At the end of the onSceneReady() function, add this code. It passes components to the Entity.create() method to create a red cube at the world origin and add it to your scene in a single call.
     Entity.create(
         listOf(
             Mesh(Uri.parse("mesh://box")),
             Box(Vector3(-0.25f, -0.25f, -0.25f), Vector3(0.25f, 0.25f, 0.25f)),
             Material().apply {
               baseColor = Color4(red = 1.0f, green = 0.1f, blue = 0.1f, alpha = 1.0f)
             },
             Transform(
               Pose(
                 Vector3(0.0f, 0.0f, 0.0f),
                 Quaternion(0.0f, 0.0f, 0.0f))),
     ))
    
    For more information about entities and components, see Entities, components, and systems.
    Note: In Spatial SDK, values like size and position are measured in meters.

Step 2: Build and run

  1. Plug your Meta Quest device into your computer.
  2. In Android Studio, click the green play button to launch the app on your Meta Quest device.
  3. Put on your headset to see what the changes look like. In the scene, there’s now a red cube in front of the panel.

Step 3: Change the cube’s appearance

In the code sample you added in Step 1, change some of the component numbers to adjust the cube’s appearance. For example, change green = 0.1f to green = 1.0f to adjust the color.
These are the components you can adjust.
  • Box: Changes the size of the cube.
  • Material: Changes the color of the cube.
  • Transform: Changes the position and rotation of the cube.
Here’s an example of how changing those values affects the cube’s appearance.

Step 4: Make the cube grabbable

To grab and move the cube, you add the Grabbable component from the Spatial SDK toolkit package.
  1. In the list of imports at the top of the file, add this code to import Spatial SDK dependencies.
     import com.meta.spatial.toolkit.Grabbable
    
  2. In the code block you pasted earlier, add Grabbable() as an argument at the end of listOf().
    Here’s what the code block should now look like.
     Entity.create(
         listOf(
             Mesh(Uri.parse("mesh://box")),
             Box(Vector3(-0.25f, -0.25f, -0.25f), Vector3(0.25f, 0.25f, 0.25f)),
             Material().apply {
               baseColor = Color4(red = 1.0f, green = 0.1f, blue = 0.1f, alpha = 1.0f)
             },
             Transform(
               Pose(
                 Vector3(0.0f, 0.0f, 0.0f),
                 Quaternion(0.0f, 0.0f, 0.0f))),
             Grabbable(),
     ))
    
    When you build and run your app, you can now grab the cube.

Next steps

Did you find this page helpful?
Thumbs up icon
Thumbs down icon