Simple VR app
After setting up GearVR Framework, let's create our first VR app and learn a few very important concepts in the process.
Create a GearVR Framework project by copying the template project
Perform the following steps to make sure your project runs correctly
- (if developing for Gear VR) Copy your Oculus signature file to
- Change the
build.gradleto a unique name to avoid naming conflict when you test the app later
- Change the
res/values/strings.xmlto avoid confusion when you debug the app.
Before we start, let's take a look at some essential parts of a GearVR Framework app
The template project contains two classes,
MainActivityis the entry point of the app, like
android.app.Activityit handles the initialization and life cycle of a VR app.
MainSceneis the container of a scene, just like a scene in the movie, it contains things like camera, characters, visual effects etc, it is the place for all your VR content.
In the assets folder, there are two files:
gvr.xmlis where you config various behavior of GearVR framework, which we'll get into detail in future tutorials
oculussigis the Oculus signing file which allows you to deploy debug apps to VR devices, so always make sure this you have a signing file in your project
Usually a VR app/game consists of one or more scenes. The template project already created one Scene called
MainScene and it should be the starting point for your VR project.
MainScene extends from
GVRMain, if you're creating your own entry point class, make sure to extend
There are two functions in
MainScene; both are important for the scene to work
- onInit() is called when the scene is being loaded, and can be used to perform actions like object creation, assets loading.
- onStep() called once per frame, can be used to perform things like animation, AI or user interactions
Adding an object to the scene is simple, Just create the object and specify the material and add it to the scene
First, let's add a new member variable for the Cube to the
Then add the cube to our scene with following code in
//Create a cube mCube = new GVRCubeSceneObject(gvrContext, true, new GVRMaterial(gvrContext, GVRMaterial.GVRShaderType.Phong.ID); //Set position of the cube at (0, -2, -3) mCube.getTransform().setPosition(0, -2, -3); //Add cube to the scene gvrContext.getMainScene().addSceneObject(mCube);
Build and run the app, you should be able to see a white cube on the screen
If you're using "VR developer mode" without headset the orientation might be different, you might need to turn around to see the cube
Make it move¶
Now let's make the cube rotate. Because we want to see the cube rotate continuously, we need to update its rotation every frame. So instead of the
onInit() function we need to add the rotation logic into
Add the following code to the
//Rotate the cube along the Y axis mCube.getTransform().rotateByAxis(1, 0, 1, 0);
rotateByAxis() has 4 parameters, the first specifies the rotation angle while the rest 3 defines the rotation axis.
Build and run the app, you should be able to see a rotating cube.
Now that you have a rotating cube in VR, feel free to try different things: change its color, make it scale up and down or move it around.
Complete Source Code for this sample