XR Unity Tutorial: Create a VR Meditation app

November 03 2021

meditationunity tutorialvirtual reality

Virtual reality is here and there are new ways of using it. It’s quickly making its way into many aspects of our lives, and now it is reaching meditation as well. Have you ever think of having a guided meditation vr application?

Meditation has become very popular in recent years as more and more people are becoming aware of its benefits and the need to slow down and take some time for relaxation. But with the popularity of meditation came the realization that most people don't have the time and money to travel to a remote mountain and live in a hut for 20 days just to meditate. Mental health is just as important as our physical health. This is where virtual meditation comes into play.

Anapanasathi meditation includes focusing on breathing. This type of meditation involves concentrating on cycles of breathing and also observing the both full and empty phase of breathing. Using the breath as the focal point of awareness during physical, mental and emotional turmoil allows one to quickly resume a state of peace. This technique can be practiced anywhere at any time. In this tutorial, we will be working on building a meditation experience that resembles anapanasati meditation in virtual reality.

vr meditation by circuit stream

Download the file projects: Github link

Access On-Demand Video Walkthrough Workshop 

What Are the Steps of Making a VR Meditation App Experience?

There are many ways to create a meditation virtual experience. In general, there are 3 main components to a virtual reality experience:

  1. Visual experience
  2. Audio experience
  3. Interactivity

Our intention here is to create a quite simple experience that can soothe the user along with a calming audio combination. It is better to get a simple sketch of the experience that we will be building. Therefore, I made a quick draft which explains what we will be designing.

vr relaxation

It is important to have an idea sketched before trying to build it, this saves time and gives a better overview of what you are planning to design. In this scenario, my idea is to build a night scene next to a lake with a waterfall in front. The night scene will be combined with some fireflies and lights around you to make it feel alive.

What are the steps of making a VR Meditation experience?

Table of Contents:

Prerequisites

We are going to build the virtual meditation experience from the ground up, and we are going to use the help of Unity’s awesome asset store to design the visual experience. In this tutorial, we will be using Unity 2020.8 LTS version and our target is to run our experience on an Oculus Quest. We are going to use Unity’s integrated XR plugin management with Oculus VR plugin support.

If you are new to Unity and its packages, let’s get things started by installing the Unity engine. First download the Unity Hub from the Unity website and create a Unity account. Once Unity Hub is installed, login with your Unity account and install the 2020.3 editor version along with Android Build support.

Step 1 - Downloading the required assets from the asset store

Our intention is to build a soothing visual using the assets from the asset store. And in this tutorial, we will only be using the free assets. Also, we are going to download 3D models from Sketchfab, which is a great platform to host and purchase 3D models from various artists around the world.

Go to Unity's Asset Store and login with your Unity Account

Go to following assets and add them to your account

  1. Terrain Tools Sample Asset Pack | 2D Nature | Unity Asset Store
  2. Conifers [BOTD] | 3D Trees | Unity Asset Store
  3. Simple Water Shader URP | 2D Water | Unity Asset Store
  4. Rock and Boulders 2 | 3D Exterior | Unity Asset Store
  5. Terrain Textures Pack Free | 2D Nature | Unity Asset Store
  6. Grass Flowers Pack Free | 2D Nature | Unity Asset Store
  7. Fantasy Skybox FREE | 2D Sky | Unity Asset Store
  8. Fantasy Effect | VFX Particles | Unity Asset Store

In addition to that, click this link and download the “fbx” format 3D model of the following:

Hand Painted Lotus Plant - Download Free 3D model by Sarath K (@sarath.irn.kat005) [aeceb2d] (sketchfab.com)

Step 2 - Setting up the project

Open Unity Hub and create a new project. Select Universal Render Pipeline (URP) Template, give a name and a location for your project and press “Create Project”

We are now going to switch the build platform to Android, since Oculus Quest is an Android device. Check the following instructions to see how to change the build platform. Once we switch the platform, it is time to get things started by creating a new Scene. Go to File > New Scene to create a new empty scene.

Step 3 - Downloading and importing all the assets

Go to Window > Package manager to open the package manager. This is from where we download all the packages that we added to the library.

Select “Packages : My Assets” from the top dropdown menu

Scroll down to find or search the asset name that you added and select it

Download the package using the button below, and click the Import button again to import the asset files to your project.

Step 4 - Designing the base terrain

It is time to design the world. Right click an empty space in your “Hierarchy” window and choose 3D Object > Terrain to create a basic terrain.

Now you will be able to see a huge terrain in the scene view. Go to the inspector window, click the terrain settings button (while the terrain is selected), and reduce the size of the terrain to 200 x 200.

Now, create a simple cube and change the scale of the cube to x=0.5, y=1.3, z=0.5. This is done to keep a reference as a human, which will make it easier for us to get the scaling of the terrain correct. Place the cube in a location where you intended the user to be seated. I placed it just in the middle of the center and an edge of the terrain.

Now, select the terrain, click the brush tool in the Inspector window and select “Raise or Lower Terrain”. You will also be able to select a brush, set its strength, rotation and the size. Once a proper brush is selected, drag on the terrain to draw mountains that resemble a lake hole in the middle.

Tip: We are using Unity Terrain tools. With that we have the option to hold the “A” key and move your mouse left and right to change the width, “S” key to change the strength and the “D” key to change the rotation. Also, you will be able to paint with the Control/Cmd key to lower the terrain.

Drawing a proper terrain might take some time. Experiment with different brush shapes and tools from the drop down menu and design the terrain as you please. Also, make sure to position the reference in a place where you want the user to be located.

This is what I build, taking around 10-15 minutes.

Step 5 - Painting the terrain and adding trees and grass

It is time to paint the terrain with proper colours. Select the terrain and choose the paint texture option from the drop down menu. Scroll down to the Layers section and Click the “Add Layer” button. You will see a list of options to choose from. Click a layer you like and press okay. I would recommend the “grass” layer since it looks like grass and covers the total terrain. Also, choose “sand”, “stones” layers and paint the terrain using the brush tools. Make sure that you keep it close to a realistic environment with proper textures.

Adding Trees : Click the “Paint Trees” button in the terrain component window, Click “Edit Trees” and choose “Add Tree”. Click the dot next to Tree Prefab and Choose the Conifer tree prefab you got from the asset store.

Once done, click and drag on the tree to paint trees.

Similarly, choose the grass from the “Paint Details” button in the Terrain component. You can choose the Grass Flowers assets we downloaded from the asset store.

Use your imagination and draw trees and grass on the terrain, but not on the lake area. My Scene would look like the following.

Step 6 - Adding water and a waterfall

In your project window, you will see a folder called “Ignite Coders”, go to the “Prefabs” folder in the Simple Water Shader Folder and drag and drop “Waterblock_50m” prefab into the scene. Place the waterblock prefab in the scene such that it looks like the lake. You might need to use the transform tools extensively from here onwards.

Also, open the Rocks and Boulders folder and drag and drop different rocks and cliffs from the prefabs folder and place them around the lake, including a bigger cliff by the side of the lake to place a waterfall.

In your project window, search for the “Waterfall” prefab we downloaded with the fantasy Effect particle package. Drag and drop the prefab near the cliff, and place it properly to look like a proper waterfall.

waterfall in unity

Step 7 - Adding the sky and lighting

Now our scene is almost over, it is time to change the sky and set up lighting to make it look like a gorgeous night scene.

Go to Window Menu > Rendering > Lighting to open the lighting window.

Select the Environment tab in the lighting window and click the small dot next to “Skybox Material” to open the list of materials. You can choose a skybox material that you downloaded from the fantasy skybox asset pack. I am using the Night_01_moonless as my skybox for the scene.

Now, click the “Directional Light” in your hierarchy and change the following properties in the Light component within the inspector window.

Color: Dark Blue color that goes with the skybox and the lake color
Strength: Around 0.9
Intensity: A lower value around 0.3

Finally, add some point lights using the GameObject menu or right clicking the hierarchy and choosing Light > Point Light

Color: Fire orange, yellow or some similar color that you like. Play with different settings.
Mode: Mixed
Shadow Type: No Shadows

Place the point light next to the reference cube, also duplicate the point light with “Ctrl+D” or from the hierarchy window and place point light around the scene. This will make the night scene more alive. Place some lights next to the waterfall as well. My scene would look like this after adding the sky and lights.

Step 8 - Adding post-processing

Post processing in Unity helps increase the visual quality of the scene adding features such as colour grading, bloom and various other features. We are going to add some post processing to our scene to make it more fantasy-like.

Right click the hierarchy and select Volume > Global Volume

Go to the inspector window and click the “New” button to create a post processing profile.

Now, click the “Add Override” button and add “Bloom”, “Color Adjustments” and “Color Curves” and change the properties as follows to enhance the scene.

Finally, drag and drop 2, 3 lotus flower 3D models on the lake to make it prettier. Our final scene would now look like this.

Step 9 - Making the Scene VR

We came so far, and now it's the time to convert into a VR scene.
Go to the package manager and select “Unity Registry” from the dropdown above, and choose “XR Plugin Manager”.

Now go to Edit > Project Settings > XR Plugin Management and tick “Oculus” to install oculus support.

Right click an empty space in the hierarchy and select XR > Convert Main Camera to XR Rig

This will take your main camera into a game object named “XRRig”

Place the XR Rig on the reference cube place, and remove the reference cube from the scene. It’s job is now done.

Click the Main Camera, and tick “Post Processing” and set Clipping planes Near to 0.01

Step 10 - Adding Sound effects

Sounds and music, also a meditative narration is very important in a meditation application. We are now going to add sounds to our scene. In order to make the experience more intuitive, I combine a set of sounds together such as follows. I added some youtube links as reference for the sounds I have used in my project

  1. Forest ambient sound: Forest Sounds | Woodland Ambience, Bird Song, Nearby Village | 3 Hours - YouTube
  2. Water wave sound : Peaceful Lake Sounds in Nature | Relax, Meditate, Focus | 10 Hours Water Sounds White Noise - YouTube
  3. Meditation narrative: Guided Mindfulness Meditation Female Voice Only No Music 10 Minutes - YouTube
  4. Calm music : 15 Minute Meditation Music, Relaxing Music, Calming Music, Stress Relief Music, Study Music, ☯3293B - YouTube

Import the files as mp3 or wav into your project.

Create an empty game object in the hierarchy and name it “Sound”

Create 4 empty gameobjects inside naming each “ambience, water, voice and music”.

Select all 4 objects and add the “Audio Source” component.

Select each gameobject and choose the respective sound for the Audio Clip section in the inspector window. Also, change the volume as necessary to give proper priority to the sound mix.

Step 11 - Adding interactivity

As per the introduction, the “Anapanasati” meditation is all about breathing. It is about maintaining the focus on breathing in and out. In this project, we are going to use our controller input to provide an interaction that mimics breathing in and out.

The idea: Breathing in will be portrayed by a set of white light particles going into your body through the nose, and breathing out by a set of red particles moving out your body which represents letting go of your negative energy.

Currently, detecting the breathing patterns automatically through an oculus quest is not possible with the sensors we are left with. Therefore, we will be using the controller input to mimic the breathing pattern. Breathing in, the user will grip both ends and breathing out, the user will slowly release the grips. Capturing this input, we are able to show the particles going in and out.

We will need two particle systems.

Right click your Main Camera (which is now your head) and go to Effects > Particle System

Rename the particle “Particle_In” and change the properties of the particle system as follows

The colour of the particle should start with transparent, gradient to white and transparent again at the end. Change the shape properties as follows.

Also, create a new material for the particle and apply the material to the “renderer” section of the particle system. The properties of the particle shader will be as follows. Having an emissive particle will glow with the bloom post processing effect.

Similarly, create a new particle system within the Main Camera and rename it “Particle_Out”. Change the settings of the particle system as follows.

Once the particle systems are added separately, it is time to write some coding to get the interactivity enabled. Before writing any code, go to Assets Menu > Seed XR Input Bindings

This will open the unity Input settings and will also create a list of new inputs that binds all the VR controller inputs.

Create a new C# script in your project folder and name it “Particle Manager”. Select your XR Rig and click the “Add Component” button in the inspector. Search and add the Particle manager script as a component.

Double click and open the script and write the code as follows.

using System.Collections;

using System.Collections.Generic;

using UnityEngine;

public class ParticleManager : MonoBehaviour

{

    public ParticleSystem particleIn;

    public ParticleSystem particlOut;

    ParticleSystem.EmissionModule emitIn;

    ParticleSystem.EmissionModule emitOut;

    private int breathingState;

    // Start is called before the fist frame update

    void Start()

    {

        emitIn = particleIn.emission;

        emitOut = particlOut.emission;

        emitIn.enabled = false;

        emitOut.enabled = false;

        breathingState = 0; // 0-idle, 1 - Breath in, 2- BreathOut

    }

    // Update is called once per frame

    void Update()

    {

        if (Input.GetButton("XRI_Right_GripButton") && Input.GetButton("XRI_Left_GripButton"))

        {

            breathingState = 1;

        }else if (!Input.GetButton("XRI_Right_GripButton") && !Input.GetButton("XRI_Left_GripButton"))

        {

            breathingState = 2;

        }

        else 

        {

            breathingState = 0;

        }

        //states

        switch (breathingState)

        {

            case 0:

                emitIn.enabled = false;

                emitOut.enabled = false;

                break;

            case 1:

                emitIn.enabled = true;

                emitOut.enabled = false;

                break;

            case 2:

                emitIn.enabled = false;

                emitOut.enabled = true;

                break;

            default:

                break;

        }

        

    }

}

This will check for your controller grip input (both controllers) and turn the particle systems on and off. It will show the breathing in particle when holding both buttons, and will show the breathing out particle when the button is released.

Now save your script and go back to unity. Select the XR rig, drag and drop both particle systems with respective particleIn and particleOut properties as follows.

Step 12 - Build and Enjoy

Connect your oculus quest to your computer. Go to build settings in the File menu. Make sure you have added your VR scene by clicking “Add Open Scenes” and in Android mode. Click build and Run and wait for the gorgeous scene to be played on your device.

What's Next?

This is a simple implementation of a VR meditation experience. You will be able to extend this to a full featured meditation application integrating more features and interactivity, along with more scenes to meditate. You can also extend this into a therapeutic app which helps people with anxiety and stress.

Checkout our blog and our workshops page regularly to know more about AR (and VR - virtual reality) application development.

If you are interested in getting into the field of XR, make sure to check out our 10-week XR development course and our 10-week XR interaction and design course.

🔙   Back to the Beginner Unity XR Tutorials

Interested in XR Unity development or learning about AR and VR?

Take a look at our course content and syllabus.

Download the XR Development with Unity Syllabus

WRITTEN BY

Indika Wijesooriya

Circuit Stream Instructor

Receive our newsletter to stay on top of the latest virtual reality and augmented reality info.