So s want to make virtual reality applications?
Great!
Let's get started.
In this quick tutorial, I'll teach you
how to use the Java SDK to make your application truly 3D.
You may have noticed that rendering for virtual reality
is different from normal Android applications.
I'm guessing you're using some sort of a graphics API,
like OpenGL, or maybe a higher-level framework.
If you haven't thought about that yet,
then take your time to decide.
It is really important to get it right.
Our own treasure hunt demo is using
OpenGL ES 2.0 for rendering the graphics in 3D.
Now that you have decided on your renderer,
it is time to download the SDK.
Got it?
Let's go!
The first thing you need to do is add some tags
to your manifest file.
Cardboard requires the use of API Level 16 or higher.
The Cardboard SDK requires read and write permissions.
So it compared the user's phone to the Cardboard viewer.
The NFC permission is required to access the NFC tag present
in some Cardboard viewers.
And the device should always be in landscape mode.
This is because the user is going to be moving their head,
and the orientation should not change as they do so.
Therefore, it is important for you
to lock the screen orientation to landscape.
It is recommended for you to handle orientation and keyboard
configuration changes to avoid having your application being
reset as these changes occur.
And finally, if you want your application
to show inside a Cardboard app listing,
then you should add the Cardboard intent-filter.
This is highly recommended, and can help people
discover your application.
So make sure you add it.
Now let's get to programming.
Start by extending your activity from Cardboard activity.
The Cardboard activity exposes Cardboard events,
such as the trigger, and handles common things required
for virtual reality rendering, like hiding the system UI
by using Android immersive mode.
Your activity should also implement the CardboardView
StereoRenderer interface.
This interface handles stereoscopic rendering
and simplifies your work by initializing open [INAUDIBLE]
for you, and giving you the correct matrices for drawing
the scene fully [INAUDIBLE].
Normally, an Android user interface elements
are built using views.
In virtual reality, we have just one view element,
which we are using as a surface to render to.
This is the CardboardView.
Add the CardboardView to your layout XML
in the following way.
Then, in the activity class, initialize the CardboardView
by setting the renderer and the view like this.
It is time to implement the StereoRenderer.
The only frame callback is called once every new frame,
before it is drawn.
This is a good time to update any render data,
such as animation and physics.
The callback comes with the head transform data.
This class contains the position and orientation
of the user's head.
You can use this for calculating where the user is looking at,
and what the user is interacting with in the environment,
such as objects, and user interface.
You can also use this for first time calling, introducing
the [INAUDIBLE] when [INAUDIBLE].
Drawing should be done in the onDrawEye callback.
This callback is called twice every frame,
as it is required to draw the scene for both eyes.
And the call contains the eye class.
Inside is the view matrix, offsets, and orientation
for the eye being rendered.
This data correctly offsets the camera's position for the eye,
creating the stereoscopic effect.
Note that a view [INAUDIBLE] is also
set for you, dividing the screen in two halves.
Again, one half for an eye.
Simply combine the eye matrix with your own view
matrix for correct rendering.
Now that we have rendering working,
let's talk about input.
While looking around can be fun, it
is more fun if you can interact with the environment.
That is why Cardboard has the trigger.
To use the trigger, simply implement
the onCardboardTrigger callback.
This would be called as the user activates the trigger.
Remember that you only have one button, so
make sure to use it wisely.
Make everything work by context.
Looking on a button, and using the trigger,
should activate the button.
However, if the user is looking somewhere else,
you'll want to make something else happen, instead, or maybe
nothing.
Sometimes it is enough to activate things
only by looking at them.
For example, a button can animate
as the user looks at it, and activate its action
after a second or two.
This also applies to parts of the scene.
You can highlight objects, and activate animations,
as the user looks around.
For example, if a butterfly is sitting on a tree,
you can make it animate and fly around,
as the user looks at it.
This way, you know the user isn't missing any of the scene,
and you are not forcing them to try and interact
with everything manually.
Now that you know how to make everything work,
it is important to keep in mind some best practices in order
to make a compelling virtual reality experience.
The three most important rules to remember
are, always keep tracking on; keep stable
60 frames per second, or higher; and avoid unexpected motion.
One of the things that makes virtual reality compelling
is the ability to look around.
In contrast, it would feel extremely unnatural
if the camera stopped responding to your head.
Therefore, you should always take into account
the user's orientation, and never freeze the camera
or force the user to look somewhere specific.
If you want to grab the user's attention,
use cues, such as light and sound,
to direct them to look where you want.
You can also delay activating an event in your scene
until you know the user had turned
their head in that direction.
That way, they have time to take things in, and enjoy the scene.
You must always keep to 60 FPS or higher.
Not only does it contribute to a good user experience,
but it is even more crucial in virtual reality.
Think about it this way-- the screen
is the only thing the user can see.
Rendering at 60 FPS means the user sees the same frame
for 16.6 milliseconds.
If you miss 60 FPS, vsync drops you to 30 FPS,
and its frame is shown for 33 milliseconds.
That means, as the user moves their head,
they are getting an incorrect image for a very long time.
This is why it is very important for virtual reality
applications to be fast and responsive.
Movement can be tricky, because the user does not
feel like they are in motion.
If the world starts moving around,
it can contribute to an odd feeling
if there is discrepancy between one's actual lack of movement
and what the user is seeing.
There are ways to convey movement safely.
For example, by keeping motion constant,
and avoiding acceleration.
Or by using another object and making it move first,
or creating a path for the user to see.
This signals to the user they're about to be moved,
and subconsciously prepares them.
There are many more ways to ensure good user experience.
I recommend you to check out the Cardboard Design Lab
application, to learn more about good and bad design patterns,
so you can create the best user experience in your game
or application.
Good luck with making your own virtual reality experience.
And make sure to post about it in our Cardboard community.
[MUSIC PLAYING]