"See the music, hear the dance," said George Balanchine, famed Russian-born choreographer and father of the American ballet.
We won't attempt to raise the level of the art form, but still, maybe it'd be fun to visualize the playlists on our phones. In this project, we will create 3D animated abstract graphics that dance to the beat of your music. You might be familiar with music visualizations in 2D, but what would it look like in VR? To get inspired, try Googling for images using the phrase geometry wars, the classic game for XBox, for example!
A visualizer app takes input from the Android audio system and displays visualizations. In this project, we will take advantage of the Android Visualizer
class, which lets an app capture part of the currently playing audio, not the full fidelity music details but a lower quality audio content sufficient for visualizations.
In this project, we will:
The source code for this project can be found on the Packt Publishing website and on GitHub at https://github.com/cardbookvr/visualizevr (with each topic as a separate commit).
To build this project, we're going to use our RenderBox library created in Chapter 5, RenderBox Engine. You can use yours, or grab a copy from the downloadable files provided with this book or our GitHub repo (use the commit tagged after-ch8
—https://github.com/cardbookvr/renderboxlib/releases/tag/after-ch8). For a more detailed description of how to import the RenderBox
library, refer to the final section, Using RenderBox in future projects, of Chapter 5, RenderBox Engine. To create a new project, perform the following steps:
VisualizeVR
and target Android 4.4 KitKat (API 19) with an Empty Activity.renderbox
, common
, and core
packages, using File | New Module | Import .JAR/.AAR Package.build.gradle
file as explained in Chapter 2, The Skeleton Cardboard Project, to compile against SDK 22./res/layout/activity_main.xml
and AndroidManifest.xml
, as explained in the previous chapters.MainActivity
as class MainActivity extends CardboardActivity implements IRenderBox
, and implement the interface method stubs (Ctrl + I).We can go ahead and define the onCreate
method in MainActivity
. The class now has the following code:
public class MainActivity extends CardboardActivity implements IRenderBox { private static final String TAG = "MainActivity";CardboardView cardboardView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); cardboardView = (CardboardView) findViewById(R.id.cardboard_view); cardboardView.setRenderer(new RenderBox(this, this)); setCardboardView(cardboardView); } @Override public void setup() { } @Override public void preDraw() { // code run beginning each frame } @Override public void postDraw() { // code run end of each frame } }
You can add a cube to the scene, temporarily, to ensure that everything is set up properly. Add it to the setup
method as follows:
public void setup() { new Transform() .setLocalPosition(0,0,-7) .setLocalRotation(45,60,0) .addComponent(new Cube(true)); }
If you remember, a Cube
is a Component
that's added to a Transform
. The Cube
defines its geometry (for example, vertices). The Transform
defines its position, rotation, and scale in 3D space.
You should be able to click on Run 'app' with no compile errors, and see the cube and Cardboard split screen view on your Android device.