← Back to all posts

Creating Immersive VR Video Experiences in Drupal CMS

George Bonnici
Creating Immersive VR Video Experiences in Drupal CMS

Creating Immersive VR Video Experiences in Drupal CMS

Virtual reality has been something boiling in the background of the techworld and has struggled to make the leap to web development. There are many technical limitations around devices (IOS especially) but with A-Frame.js and Drupal, you can now create immersive 360° video experiences that work right in the browser. No VR headset required (though it's even better with one). Let's dive into how to bring this exciting tech to your Drupal site.

The Technical Foundation: A-Frame.js and Drupal Field Formatters

At the heart of our VR implementation is A-Frame.js, a powerful WebVR framework that makes creating VR experiences as simple as writing HTML. But how do we integrate this with Drupal CMS? The answer lies in custom field formatters.

Creating the VR Field Formatter

I have chosen a basic implementation of at embedded link - to create a fuller solution, a file field would be more efficient - that is a custom field formatter that transforms a simple video URL into an immersive VR experience. Here's how it works:

namespace Drupal\vr_embed\Plugin\Field\FieldFormatter;

use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Field\FormatterBase;

/**
 * Plugin implementation of the 'vr_iframe_formatter' formatter.
 *
 * @FieldFormatter(
 *   id = "vr_iframe_formatter",
 *   label = @Translation("VR iFrame Embed"),
 *   field_types = {
 *     "link",
 *     "string"
 *   }
 * )
 */
class VRIframeFormatter extends FormatterBase {
  // Implementation details...
}

This formatter accepts both link and string field types, making it versatile for different content structures. The real power comes from the A-Frame.js integration. This is my template where I pull the url from my input:

<a-scene id="vr-scene" embedded allowfullscreen allowvr>
  <a-assets class="display-block-important" timeout="10000">
    <video
      class="display-block-important"
      id="video360"
      autoplay
      loop
      muted
      crossorigin="anonymous"
      playsinline
      webkit-playsinline
      src="$url">
    </video>
  </a-assets>
  <a-videosphere src="#video360" rotation="0 -90 0"></a-videosphere>
  <a-camera look-controls></a-camera>
</a-scene>

Key Technical Features

1. Responsive Design

The VR container is set to be fully responsive:

<div style="width: 100%; height: 400px" id="vr-container">

This ensures your VR experience looks great on any device, from mobile phones to desktop screens (This is limited as displayed further below).

2. Video Optimization

The video element includes several important attributes:

  • autoplay: Starts the video automatically
  • loop: Continuously plays the video
  • muted: Ensures autoplay works across browsers
  • crossorigin="anonymous": Handles CORS requirements
  • playsinline: Enables inline playback on iOS devices

3. A-Frame Components

  • a-videosphere: Creates a 360° video sphere
  • a-camera: Provides user control for looking around
  • look-controls: Enables mouse/touch interaction

Implementation Steps

  1. Install the Module Create a new custom module in your Drupal installation:

    mkdir -p modules/custom/vr_embed
    
  2. Add A-Frame.js Include A-Frame.js in your theme or module. I ending up using a CDN but downloading the library properly would be far more efficient:

    $page['#attached']['library'][] = 'vr_embed/aframe';
    
  3. Create the Field Formatter Place the formatter code in:

    modules/custom/vr_embed/src/Plugin/Field/FieldFormatter/VRIframeFormatter.php
    
  4. Configure the Field

    • Add a new field to your content type
    • Select the "VR iFrame Embed" formatter
    • Upload or link to your 360° video

Best Practices and Tips

  1. Video Format

    • Use MP4/Webp format for maximum compatibility
    • Keep file sizes reasonable (compress when possible)
    • Consider offering multiple quality options
  2. Performance

    • Implement lazy loading for VR content
    • Consider using a CDN for video delivery
    • Monitor mobile performance
  3. User Experience

    • Add loading indicators
    • Provide fallback content for non-VR browsers
    • Include clear instructions for VR navigation

iOS Limitations: No Fullscreen in Safari

When developing VR experiences for the web, it's important to be aware of platform-specific limitations. One such limitation is the inability to programmatically enter fullscreen mode in Safari on iOS devices. This affects elements like <video>, <canvas>, <a-scene>, and any calls to requestFullscreen(), which do not function as they do on other platforms.

The Limitation

On iOS, web content cannot enter fullscreen programmatically inside Safari. This means that users cannot experience VR content in fullscreen mode by default, which can impact the immersive quality of the experience. I had a quick looksie into possible solutions and found there would need a bit of work.

The Exception: Progressive Web Apps (PWAs)

There is, however, an exception to this limitation. When a web page is added to the Home Screen and launched as a Progressive Web App (PWA), the fullscreen API is allowed. This means that if you want to provide a fullscreen VR experience on iOS, you can encourage users to add your site to their Home Screen as a PWA.

Workarounds and Considerations

  1. Encourage PWA Usage: Provide instructions for users on how to add your site to their Home Screen to enable fullscreen capabilities.

  2. Design for Non-Fullscreen: Ensure that your VR content is still engaging and functional without fullscreen mode. This might involve optimizing the layout and controls for smaller, non-fullscreen displays.

  3. User Experience: Clearly communicate to users why fullscreen might not be available and how they can enhance their experience by using the PWA option.

In Closing

Creating VR experiences in Drupal CMS is surprisingly straightforward, thanks to the power of A-Frame.js and Drupal's flexible field system. Whether you're showcasing real estate, creating virtual tours, or offering immersive product demonstrations, the technical foundations are all there. Just need to keep in mind a few gotchas.

Tags

Drupal CMSVRWeb DevelopmentA-Frame.js