Babylon JS Day 14
Today I learned how to add VR support to my scene. Until now I’ve been using a template that Babylon JS provided in their Getting Started guide. I had to replace most of that code with an async version to be able to use the WebXR features that Babylon JS offers.
I’m using the createDefaultXRExperienceAsync
helper. Over time I need to learn how to customize the VR features a bit, but for now I’m going to use all their default settings.
Once I got the scene loaded in VR on my Oculus Quest 2, the first obvious change that I needed to make was scaling the cards down in size. When I built the card earlier this week, I sized them with “full unit” values to keep things simple. While that looks OK on the desktop browser, in VR these cards were huge. It was just a matter of scaling the entire card (with all child objects) at the end of my createCard
function.
card.scaling = new BABYLON.Vector3(0.15, 0.15, 0.15);
Then I turned my attention to the event for the buttons on the cards. When a user presses this button, I want to show an expanded detail UI on another object in the scene. The scene will only have one “detail card” but may contain any number of the smaller “info cards”. Think of this as a master-detail interface, but in 3D space. First, I added a placeholder object for the detail card. I made this much larger than the item cards and positioned it behind them for now. Assigning a name in the constructor is important. I’ll need that to query this item in the scene graph.
const detailCard = BABYLON.MeshBuilder.CreateBox("detailCard", { height: 4, width: 6, depth: 0.4 });
detailCard.position = new BABYLON.Vector3(5, 4, 6);
Then I updated the button event. I used a method on the scene called getMeshByName()
to get a reference to the detail card in the scene, then toggle the isEnabled
property on the detail card. Later, this button will also send data related to the info card to the detail card for display. While the info card contains a few preview items, the detail card will contain full text, images, etc.
button1.onPointerUpObservable.add(function () {
let detailCard = scene.getMeshByName("detailCard");
detailCard.setEnabled(detailCard.isEnabled() ? false : true);
console.log("card button was clicked");
});
Tomorrow I’ll get to work connecting all these features together. I want the user to be able to grab and move the info cards. I’d like to replace the button the card with a controller interaction. For example, say the user picks up a card and presses the A button to show the detail card. I also want to the detail card to show up in a position relative to the users’ position, not a fixed spot like it is now. I’m not sure how much I’ll be able to get done during the time I’ve allotted for the Friday Project, but I’m sure I’ll have something interesting to write about when I’m done.
Here are a couple of rough previews of the info and detail cards, recorded in the Oculus Quest 2.