Introduction: Use Your HTC Vive With Processing!
This guide will show you how to add a library Processing that will enable you to use your HTC Vive with Processing so you can view and interact with your sketches in Virtual Reality! It will also cover how to implement the library in a basic sketch.
There are many great avenues for creating immersive experiences with the Vive and other VR headsets (Unity, Unreal, InstaVR), and through creating the ViveP5 library I hope to extend the potential to create VR experiences to Processing so that creators are able to work across an even wider range of software to create weird and wonderful experiences. If you're just starting out, or even if you're a Processing-wizard and are having issues with a particular problem, Processing has an extremely helpful community across its Forum and Stack Overflow that might be able to help you, or have already solved your problem.
In order to undertake this instructable you will need:
- Processing 3 installed (if you don't have it installed you can download it here)
- HTC Vive Headset (controllers are supported, but optional for this tutorial)
- SteamVR installed (this is included with Steam, but if you dont have Steam you can download it here)
- About 20-30 minutes of your time
Step 1: Installing the ViveP5 Library
To support the Vive headset in Processing we first need to add a library that will do the heavy work behind the scenes and help us to interface the headset and controllers with Processing in a simplified way.
- To start, click this link to download the download the .zip of the most recent build - "ViveP5.zip"
Now that we have the zip we need to extract and move it to the Sketchbook location that Processing references to find your downloaded libraries.
- To be certain of the referenced library location let's quickly open Processing and go to File > Preferences. The top of this window shows the location of our "Sketchbook".
- Copy ViveP5.zip. Navigate to your sketchbook folder > libraries, paste the .zip and extract the contents of ViveP5.zip here. Once the ViveP5 folder is extracted feel free to delete the .zip file.
Our library is now in the right location! Now lets check that Processing finds it so we can use it in our sketches.
- Restart processing and go to Sketch > Import Library and if ViveP5 is visible you're good to go!
______
If you had any issues please refer to these instructions to make sure your installation is correct and that you're running Processing 3.3
If it still doesn't show please feel free to leave a comment / message me or raise an issue on the ViveP5 Bitbucket
Step 2: Understanding the Basics of ViveP5
The following shows how a normal sketch runs in processing. The comments on the right explain each part of the typical sketch (don't copy the following code into processing - it doesn't work yet).
void setup(){ // We setup our sketch window and create size(1028,1028, P3D); // any objects }
void draw(){ // Process stuff // The draw loop is used to run processes calculation1(); // on our objects and make calculations // eg. (move a box or add two numbers) // The draw loop is used to then draw all // Draw stuff // of our objects drawStuff(); // eg. (draw the moved box) }
When we are using ViveP5 we need to apply a slightly different logic when we're using Processing because we need to draw everything twice (once for each eye as they see from two different perspectives). So the way we have to use the draw() function is different. We want to use draw() function to still do all of our calculations for our objects, but we want to draw everything in a separate function - VRdraw(). Our code now needs to look like this:
void setup(){ // Still we setup our sketch normally size(1028,1028, P3D); } void draw(){ // Process stuff // We still run our processes and calculations calculation1(); // in the draw loop } // We do not draw in the draw loop anymore void VRdraw(){ //We do all of our drawing in the VRdraw now! // Draw stuff drawstuff(); }
Now that we've covered this we can move on and begin to create a working sketch. With a few lines of code we can easily setup a Vive in our scenes.
Step 3: Creating a Vive in Your Sketch
Lets look at a basic sketch that puts a grid of pink spinning boxes in our scene.
void setup() { <br> size(1028, 1028, P3D); } void draw() { background(200,100,150); // Note: Draw loop is filled with camera(1500+(millis()/50), 1500, // all drawing related functions 1500, 0, 0, 0, 0, 1, 0); perspective(PI/2, PI/3.0, 1, 100000); translate(500, 500, 0); drawBoxes(); } void drawBoxes() { // function that draws 8x8x8 grid for (int i = -1000; i < 1000; i+=200) { // of rotating boxes for (int j = -1000; j < 1000; j += 200) { for (int k = 0; k < 1000; k += 200) { pushMatrix(); translate(i, j, k); fill(255, 120, 140); rotateX(millis() / 1000.0f); rotateY(millis() / 900.0f); box(50); popMatrix(); } } } }
Now lets make some changes to our sketch to add a vive into the scene through implementing ViveP5.
To first import ViveP5 go to Sketch > Import Library > ViveP5 which will add a line to the top of your sketch
import core.viveP5.*;
We can now create an instance of our Vive class and initialize it in the setup loop.
Vive vive;<br>void setup() { size(1028, 1028, P3D); vive = new Vive(this); }
If we look at the previous draw loop - all of the functions are either related to setting up the camera, or drawing the boxes.
The only tricky thing we need to do is remove the background command (this ruins the Vive drawing for some strange reason) and replace it with vive.setBackground(200,100,150).
We no longer need to setup the camera now that we're using the vive so we can remove all of the camera related stuff.
Anything that is drawn needs to be moved to VRdraw() so the draw loop is essentially empty, which in this case means drawBoxes needs to be moved to VRdraw()
We finally need to use the draw loop to tell the vive to run its draw function every frame - vive.draw();
void draw() {<br> vive.draw(); vive.SetBackground(200,100,150); } void VRdraw(){ drawBoxes(); } void drawBoxes() { for (int i = -1000; i < 1000; i+=200) { for (int j = -1000; j < 1000; j += 200) { for (int k = 0; k < 1000; k += 200) { pushMatrix(); translate(i, j, k); fill(255, 120, 140); rotateX(millis() / 1000.0f); rotateY(millis() / 900.0f); box(50); popMatrix(); } } } }
The .pde of each of these scripts is attached below.
Step 4: Get Creative!
Now that you have the basis for a Processing VR sketch and hopefully everything worked - get creative and create some immersive experiences!
If you want to know more about the code and its capabilities refer to the source code in the ViveP5 folder in your library or the Javadoc which is
Step 5: Troubleshooting Issues
"What other functions are included in this package?"
Refer to the Javadoc which is inside your sketchbook folder > libraries > ViveP5 > reference > index.html
"My sketch crashes the first time I run it"
This is normal because usually processing is waiting for SteamVR to launch and if SteamVR takes longer than 5 seconds to launch the sketch will automatically timeout.
"I can think of a faster way to implement some of these functions"
Please contribute to the BitBucket and make your own fork of the repository if you have some crazy ideas you want to test.
"I followed everything and nothing works"
Please leave a comment, message me or raise an issue on BitBucket
"The Vive has eyes the wrong way around"
Write this line into your setup loop after you have initialized the vive:
vive.swapEyes();
"One eye draws, but the other doesnt / is black"
Look at the Javadoc (inside your sketchbook folder > libraries > ViveP5 > reference > index.html) for the command "setEyes" (try different combinations of numbers between 1 and 5 for each eye int). Write this line into your setup loop after you have initialized the vive with different values for int left and int right:
vive.setEyes(int left, int right);
15 Comments
5 years ago
just so that everyone knows it works with the oculus rift! Make sure you have the Vive software installed, SteamVR and the Oculus app and it should work. I was able to make a pretty nicr code with it for a school project with functions such as ''Controller cR = vive.controllers.get(1);".
Reply 4 years ago
Thank you, Gabriel! I am also trying to make it work with Oculus but I have an issue. I can successfully lunch the vive_tutorial.pde and see the window on my desktop with the cubes, and moving the headset, the direction of view correctly change; I can also see my controllers moving. But inside the headset, I see the SteamVR loading ambient and nothing else happen. I get an error on my desktop that says that the HDMI output is not correctly set, but I cannot find where the change the setting. Did you have this issue? I am using an Alienware laptop and the Oculus with the last software/firmware updates. Thank you!
4 years ago
Hi nice Lib :) question (sorry i'm noobby) i look in to the reference can't find how to get rotation data from controller.
Thanks K
4 years ago
Thanks for creating this! Is there a way of creating a barrel distortion in the back code of vivep5?
Question 5 years ago on Introduction
Does this require PC and a high performance GPU to work?
5 years ago
the console told me: java.lang.UnsatisfiedLinkError: Unable to load library 'openvr_api': Native library (darwin/libopenvr_api.dylib) not found in resource path
Reply 5 years ago
I think this error occurs because you are using Processing for OS X. The viveP5 Library only works with Windows and Needs a path as in vive.java:
public String steamVRPath = "C:/Program Files (x86)/Steam/steamapps/common/SteamVR";
Reply 5 years ago
There is a function "setPath(String path)" in the vive class which should be able to be used to override the existing path when creating the class. Unfortunately I don't have a mac so am unable to test. Let me know if you had any progress with resolving this issue!
5 years ago
Dear Vive-Wizzard,
unfortunately an error appears since the Windows 10 Fall Creators Update.
If I try to run this simple program, an error occurs. (see picture).
import core.viveP5.*;
Vive vive;
void setup() {
size(300, 300, P3D);
vive = new Vive(this);
}
Do you have encountered this problem as well?
Reply 5 years ago
Hi Reiner,
I was able to recreate this error after updating. Problem seemed to be related to a reference to a now outdated part of SteamVR. I updated the reference and everything seems to be working. Pull the fresh .dll from the bitbucket and it should be working from you.
5 years ago
Thanks a lot for the great work. Unfortunately, I have difficulties querying the controller. I've tried it that way:
import core.viveP5.*;
Vive vive;
Controller cont;
void setup() {
size(1028, 1028, P3D);
vive = new Vive(this);
cont = new Controller(this, vive, 1, 1);
}
void draw() {
vive.draw();
vive.setBackground(100, 100, 150);
if (cont.getGrip()) {
println (cont.getGrip());
exit();
}
}
Could you please help me?
Reply 5 years ago
Hey Reiner - Just had a look at your issue. The controllers exist in an arraylist inside the vive class. So to access them you have to loop through the arraylist vive.controllers (this is done in the draw loop because you want to query once per frame). The loop looks like this -
for(Controller c: vive.controllers){
if(c.getGrip() == true){
println("grip pressed");
}
}
Implemented inside one of the examples:
import core.viveP5.*;
Vive vive;
Controller cont;
void setup() {
size(1028, 1028, P3D);
vive = new Vive(this);
vive.setPath("D:/Program Files (x86)/Steam/steamapps/common/SteamVR");
cont = new Controller(this, vive, 1, 1);
}
void draw() {
vive.draw();
vive.setBackground(200,100,150);
for(Controller c: vive.controllers){
if(c.getGrip() == true){
println("grip pressed");
}
}
}
void VRdraw(){
drawBoxes();
}
void drawBoxes() {
for (int i = -1000; i < 1000; i+=200) {
for (int j = -1000; j < 1000; j += 200) {
for (int k = 0; k < 1000; k += 200) {
pushMatrix();
translate(i, j, k);
fill(255, 120, 140);
rotateX(millis() / 1000.0f);
rotateY(millis() / 900.0f);
box(50);
popMatrix();
}
}
}
}
Reply 5 years ago
Hey Reiner - I will be able to look at this problem in detail this weekend and will let you know how to solve this issue very soon!
6 years ago
That's a really fun way to make vr environments :) Neat idea!
Reply 6 years ago
Thanks, Swansong!