Share

engineering

9min read

Have Fun with Nature—Polidea's VR Experience

Have Fun with Nature—Polidea's VR Experience

We already had a chance to try some VR and AR technologies in a form of Polidea Labs projects—check Augmenting Reality on the web and Augmenting reality with an iPhone. The aim was to check the level of advancement of available tools and do some basic research. It was a great introduction to preparing a better demo. With the gathered knowledge we were able to create an interactive web VR experience in which we combined a beautiful design with a thoroughly-planned development.

About the project

It took us some time to finalize the concept of this app. We did a couple of prototypes on the way, since imagining how something should work in VR and actually making it happen are two different things. As WebVR is not suitable for high-quality rendering we decided to create a low-poly world, but improve the experience with a colorful landscape. By placing rocks from the menu on correct shapes on the stone in the middle, a user is able to change the scenery around them. You can try and play around with it yourself here. It works both in a desktop browser as a 360-degree mode and on mobile as a VR mode. You should definitely try it out on your VR headset.

image.png

Development and technology stack

You can check full source code on our github.

Even though VR and AR are still developing, there are a lot of various tools to choose from. Creating an app which will be available on as many platforms as possible was a fundamental argument in favor of choosing WebVR. On top of that, we decided to also use the A-Frame framework, which is very well documented. It easily integrates with a regular HTML, but you have to use dedicated markup tags. You might also use built-in JS functions to build more complex logic around simple HTML parameters.

For instance, we used registerComponent to create an item parameter which will invoke specific behavior when it’s hovered in 3D. It has to define a schema aka a list of properties which will be interpreted by A-frame in the markup. Here’s the source code:

import * as AFRAME from "aframe";
AFRAME.registerComponent('hovered_menu_item', {
schema: {
position_down: {default: {x: 0, y: 0, z: 0}},
position_up: {default: {x: 0, y: 0, z: 0.1}},
color_down: {default: "#b4d1ff"},
color_up: {default: "#FFFFFF"},
},
init: function () {
let el = this.el;
this.el.addEventListener('raycaster-intersected', function () {
el.emit("run_down");
});
this.el.addEventListener('raycaster-intersected-cleared', function () {
el.emit("run_up");
});
el.setAttribute("animation__position_down", {
property: "position",
dur: 400,
easing: "easeInSine",
to: this.data.position_down,
startEvents: "run_down",
restartEvents: "run_down",
pauseEvents: "run_up"
});
el.setAttribute("animation__position_up", {
property: "position",
dur: 400,
easing: "easeInSine",
to: this.data.position_up,
startEvents: "run_up",
restartEvents: "run_up",
pauseEvents: "run_down"
});
},
play: function () {
let el = this.el;
el.setAttribute("animation__color_down", {
property: "material.color",
dur: 400,
easing: "easeInSine",
from: this.data.color_up,
to: this.data.color_down,
startEvents: "run_down",
restartEvents: "run_down"
});
el.setAttribute("animation__color_up", {
property: "material.color",
dur: 400,
easing: "easeInSine",
from: this.data.color_down,
to: this.data.color_up,
startEvents: "run_up",
restartEvents: "run_up"
});
}
})
view raw hovered_menu_item.js hosted with ❤ by GitHub
class MenuItem extends React.Component {
render() {
return (
<Entity
className="item intersectable"
controller-clickable
hovered_menu_item={{
position_down: this.state.field.position_down,
position_up: this.state.field.position_up
}}
geometry={{primitive: 'plane', width: 1.0, height: 1.0}}
material={{shader: "flat", color: 'white', opacity: 1.0}}
rotation={{x: 0, y: 0, z: 0}}
position={this.state.field.position_up}
shadow="cast: false; receive: false"
events={{
click: (evt) => {
let el = evt.target;
this.onMenuItemClicked(el, this.state.field.texture)
}
}}>
<a-image
position="0.0 -0.05 0.005"
scale="0.8 0.8 0.8"
src={this.state.field.textureId}/>
</Entity>
);
}
}
view raw menu_disks.js hosted with ❤ by GitHub

As you might have noticed, we are really fond of React in Polidea. Since the app was supposed to smoothly transition from 2D Landing Page to VR and also dynamically adapt to the gameplay state we’ve decided to use aframe-react library and keep most of the code as React Components. It made a project more readable and well-organized. It’s wasn’t all roses, React DOM substitution mechanism doesn’t always play well along A-frame animations. We made it work, but we had to compromise between React and A-frame syntaxes.

One of the helpful tools we used during development was ngrok. It gives remote access to the app when you are developing it locally on your machine. We were able to instantly share and test each other’s versions of the code and check them on various devices. It proved to be very useful as it turned out we had to debug code on Daydream and Gear VR devices. Unfortunately, the free version of ngrok is quite limited, but once you go premium it might boost your web development process significantly.

Challenges and tips

First of all, WebVR ecosystem still hasn’t matured enough. At the early stage of the project we used an A-frame redux library, which one week later was removed from NPM to make space for its successor. Fortunately, the newer version of the library turned out to be more intuitive and easy-to-use.

Still…be careful. If you are tempted to use experimental libraries—which utilize a well-known web development pattern—think it through; evaluate if you know it well enough to transition to a newer version or if you’re capable of fixing it yourself, when necessary.

There’s no good advice for a situation like this, but at this point it’s better to use the most stable A-frame libraries, even though they require you to write additional code. Remember to use a specific version in package.json or yarn file, because even a small patch can break your project.

image.png

Commercial VR development practices are still developing and so is the VR design process. It was a challenge to build a process, where both designers and developers could work hand-in-hand, including rapid prototyping and 3D modeling. VR/AR designers needed to check if their vision “feels” good in VR. Developers also didn’t know if it’s possible to easily transform their concepts into a working application.

However, we transformed this ‘don’t know’ circle into a successful close agile collaboration. Instead of receiving all designs and UI project at the beginning, we were incrementally adding small components and testing. Some visual attributes were changed in the code, some required only 3D models improvements. At some point, it became as pleasant as assembling a Lego set. So don’t feel discouraged by frequent code changes, when developing a VR—try to use the most out of React and A-frame by smartly dividing your code into smaller components.

image.png

It was a challenge to make Daydream and GearVR controllers work properly, mostly due to a testing inconvenience. We were still developing and testing it mostly in a desktop browser, but as often as possible we were running it using NGROK on both Daydream and Samsung Gear VR devices.

Summary

Web VR and AR is still growing and is bound to rapidly evolve in the near future. Obviously, hardware ecosystem grows the fastest, so it will take some time for development technologies to catch up until they are reliable enough and easy to use in various situations. A-frame is definitely a framework you should choose if you are into web development and try to acquire new skills in VR and AR areas. There’s still a lot of room for discovering best design patterns, creating good development practices and building interesting VR or AR applications. In Polidea, we are not slowing down as we’re constantly improving the process of creating VR & AR applications.

Share

Katarzyna

Senior Software Engineer

Did you enjoy the read?

If you have any questions, don’t hesitate to ask!

Did you enjoy the read?

If you have any questions, don’t hesitate to ask!