Project

webxr

0.0
No release in over 3 years
Complete Ruby bindings for the WebXR Device API, enabling VR/AR application development in Ruby via ruby.wasm
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
 Dependencies

Runtime

~> 2.0
 Project Readme

WebXR Ruby

CI Gem Version

WebXR Device API bindings for Ruby. Build VR/AR applications in Ruby using ruby.wasm.

Installation

Add this line to your application's Gemfile:

gem "webxr"

Or install it directly:

gem install webxr

Requirements

  • Ruby 3.2+
  • ruby.wasm runtime (for browser execution)
  • WebXR-compatible browser (Chrome, Edge, Firefox, etc.)

Usage

Basic VR Session

require "webxr"

# Check WebXR availability
if WebXR::System.available?
  system = WebXR::System.instance

  # Check VR support
  if system.session_supported?("immersive-vr")
    # Request VR session
    session = system.request_session("immersive-vr")

    # Get reference space
    space = session.request_reference_space("local-floor")

    # Animation loop
    session.request_animation_frame do |time, frame|
      pose = frame.viewer_pose(space)
      # Render your scene using pose.views
    end
  end
end

Using Helpers

require "webxr"

# SessionManager for simplified session handling
manager = WebXR::Helpers::SessionManager.new

manager.start_vr do |session, space|
  # Automatically handles setup and cleanup
  session.request_animation_frame do |time, frame|
    # Your render loop
  end
end

Input Handling

# Access controllers
session.input_sources.each do |source|
  puts "Controller: #{source.handedness}"  # "left", "right", "none"

  if source.gamepad
    source.gamepad.buttons.each_with_index do |button, i|
      puts "Button #{i} pressed" if button.pressed?
    end
  end
end

# Input events
session.on(:select) do |event|
  source = event.input_source
  puts "Select from #{source.handedness} controller"
end

AR with Hit Testing

session = system.request_session("immersive-ar", required_features: ["hit-test"])
space = session.request_reference_space("local")
viewer_space = session.request_reference_space("viewer")

hit_test_source = session.request_hit_test_source(space: viewer_space)

session.request_animation_frame do |time, frame|
  results = frame.get_hit_test_results(hit_test_source)

  results.each do |result|
    pose = result.get_pose(space)
    # Place objects at hit position
  end
end

Development

# Install dependencies
bundle install

# Run tests
bundle exec rake spec

# Generate documentation
bundle exec rake doc

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/ydah/webxr-ruby.

License

The gem is available as open source under the terms of the MIT License.

Resources