|
| 1 | +# Generic WebXR Experience |
| 2 | + |
| 3 | +A flexible and reusable WebXR boilerplate that can be easily forked and customized for various VR projects. |
| 4 | + |
| 5 | +## Features |
| 6 | + |
| 7 | +- **VR Scene Setup**: Basic responsive scene using Three.js and WebXR API |
| 8 | +- **Navigation and Interaction**: Teleportation, controller input handling, and object interaction |
| 9 | +- **Responsive UI System**: VR-compatible UI panels that adapt to different devices |
| 10 | +- **Controller Input Handling**: Support for various VR controllers with standardized input mapping |
| 11 | +- **Environment Loader**: Support for loading custom 3D models and skyboxes |
| 12 | +- **Spatial Audio**: Immersive audio system with 3D positioning |
| 13 | +- **Performance Monitoring**: Built-in FPS counter and performance tools |
| 14 | +- **Multiplayer Support**: Basic networking infrastructure for multi-user experiences |
| 15 | +- **Fallback Mechanisms**: Graceful fallback to non-VR mode when WebXR is unavailable |
| 16 | + |
| 17 | +## Project Structure |
| 18 | + |
| 19 | +``` |
| 20 | +webxr/ |
| 21 | +├── index.html # Main entry point |
| 22 | +├── src/ |
| 23 | +│ ├── core/ # Core WebXR functionality |
| 24 | +│ │ ├── main.js # Main application entry point |
| 25 | +│ │ ├── xr-session.js # WebXR session management |
| 26 | +│ │ ├── environment.js # 3D environment setup |
| 27 | +│ │ ├── controllers.js # VR controller handling |
| 28 | +│ ├── ui/ # User interface components |
| 29 | +│ │ ├── styles.css # CSS styles |
| 30 | +│ │ ├── ui-panel.js # UI management |
| 31 | +│ ├── utils/ # Utility functions |
| 32 | +│ │ ├── input-manager.js # Input handling |
| 33 | +│ │ ├── performance-monitor.js # Performance tracking |
| 34 | +│ ├── audio/ # Audio system |
| 35 | +│ │ ├── audio-manager.js # Spatial audio management |
| 36 | +│ ├── network/ # Networking components |
| 37 | +│ │ ├── multiplayer.js # Multiplayer functionality |
| 38 | +│ ├── assets/ # Assets (models, textures, audio) |
| 39 | +│ ├── models/ # 3D models |
| 40 | +│ ├── textures/ # Textures |
| 41 | +│ ├── audio/ # Audio files |
| 42 | +├── packaging/ # Distribution packaging guides |
| 43 | + ├── pwa/ # Progressive Web App packaging |
| 44 | + ├── quest/ # Meta Quest Store packaging |
| 45 | + ├── steamvr/ # SteamVR/PCVR packaging |
| 46 | +``` |
| 47 | + |
| 48 | +## Getting Started |
| 49 | + |
| 50 | +### Prerequisites |
| 51 | + |
| 52 | +- A WebXR-compatible browser (Chrome, Firefox, Edge with WebXR support) |
| 53 | +- A VR headset (Oculus Quest, HTC Vive, Valve Index, etc.) for full VR experience |
| 54 | +- Basic knowledge of HTML, CSS, and JavaScript |
| 55 | + |
| 56 | +### Running the Experience |
| 57 | + |
| 58 | +1. Clone this repository |
| 59 | +2. Serve the files using a local web server: |
| 60 | + - Using Python: `python -m http.server` |
| 61 | + - Using Node.js: `npx serve` |
| 62 | +3. Open the browser and navigate to the local server (typically `http://localhost:8000` or `http://localhost:3000`) |
| 63 | +4. Click the "Enter VR" button to start the VR experience |
| 64 | + |
| 65 | +### Desktop Controls |
| 66 | + |
| 67 | +When not in VR mode, you can navigate the scene using: |
| 68 | + |
| 69 | +- **WASD** or **Arrow Keys**: Move around |
| 70 | +- **Mouse**: Look around |
| 71 | +- **Space**: Move up |
| 72 | +- **Shift**: Move down |
| 73 | +- **F**: Toggle fullscreen |
| 74 | +- **R**: Reset position |
| 75 | +- **Escape**: Exit pointer lock |
| 76 | + |
| 77 | +## VR Controls |
| 78 | + |
| 79 | +In VR mode, you can interact with the environment using: |
| 80 | + |
| 81 | +- **Right Controller Trigger**: Teleport |
| 82 | +- **Left Controller Trigger**: Interact with objects |
| 83 | +- **Left Controller Grip**: Toggle settings panel |
| 84 | +- **Right Controller Thumbstick**: Rotate (snap or smooth turning based on settings) |
| 85 | +- **Left Controller Thumbstick**: Move (if smooth locomotion is enabled) |
| 86 | + |
| 87 | +## Distribution Options |
| 88 | + |
| 89 | +This WebXR experience can be distributed in several ways: |
| 90 | + |
| 91 | +### 1. Browser-based WebXR |
| 92 | + |
| 93 | +The simplest approach is to host the files on a web server with HTTPS support. Users can access the experience directly in a WebXR-compatible browser. |
| 94 | + |
| 95 | +### 2. Progressive Web App (PWA) |
| 96 | + |
| 97 | +You can enhance the experience as a Progressive Web App that can be installed from the browser: |
| 98 | + |
| 99 | +- Provides offline support |
| 100 | +- Installable on the home screen |
| 101 | +- More app-like experience |
| 102 | + |
| 103 | +See the [PWA Packaging Guide](./packaging/pwa/README.md) for details. |
| 104 | + |
| 105 | +### 3. Meta Quest Store App |
| 106 | + |
| 107 | +For distribution on the Meta Quest Store, you can package the experience as a native Android app: |
| 108 | + |
| 109 | +- Full app store presence |
| 110 | +- Better integration with Quest features |
| 111 | +- Access to Quest-specific APIs |
| 112 | + |
| 113 | +See the [Meta Quest Packaging Guide](./packaging/quest/README.md) for details. |
| 114 | + |
| 115 | +### 4. SteamVR/PCVR App |
| 116 | + |
| 117 | +For distribution on Steam and other PCVR platforms, you can package the experience as an Electron app: |
| 118 | + |
| 119 | +- Distribution on Steam |
| 120 | +- Better integration with SteamVR |
| 121 | +- Access to desktop features |
| 122 | + |
| 123 | +See the [SteamVR/PCVR Packaging Guide](./packaging/steamvr/README.md) for details. |
| 124 | + |
| 125 | +For a comprehensive overview of all packaging options, see the [Packaging Guide](./packaging/README.md). |
| 126 | + |
| 127 | +## Customization |
| 128 | + |
| 129 | +### Changing the Environment |
| 130 | + |
| 131 | +To customize the environment, modify the `environment.js` file: |
| 132 | + |
| 133 | +```javascript |
| 134 | +// Add custom environment objects |
| 135 | +addEnvironmentObjects() { |
| 136 | + // Add your custom objects here |
| 137 | +} |
| 138 | + |
| 139 | +// Load a custom environment model |
| 140 | +this.loadEnvironmentModel('path/to/your/model.glb'); |
| 141 | + |
| 142 | +// Load a custom skybox |
| 143 | +this.loadPanoramicSkybox('path/to/your/skybox.jpg'); |
| 144 | +``` |
| 145 | + |
| 146 | +### Adding Interaction |
| 147 | + |
| 148 | +To make objects interactive, register them with the controller manager: |
| 149 | + |
| 150 | +```javascript |
| 151 | +// Create an object |
| 152 | +const myObject = new THREE.Mesh( |
| 153 | + new THREE.BoxGeometry(1, 1, 1), |
| 154 | + new THREE.MeshStandardMaterial({ color: 0xff0000 }) |
| 155 | +); |
| 156 | +myObject.position.set(0, 1, -2); |
| 157 | +scene.add(myObject); |
| 158 | + |
| 159 | +// Make it interactive |
| 160 | +controllerManager.registerInteractable(myObject); |
| 161 | +``` |
| 162 | + |
| 163 | +### Adding Sounds |
| 164 | + |
| 165 | +To add spatial audio: |
| 166 | + |
| 167 | +```javascript |
| 168 | +// Load and play a sound at a specific position |
| 169 | +audioManager.loadSound('mySound', 'path/to/sound.mp3', { |
| 170 | + spatial: true, |
| 171 | + volume: 0.8, |
| 172 | + loop: false |
| 173 | +}).then(() => { |
| 174 | + audioManager.playSound('mySound', new THREE.Vector3(0, 1, -2)); |
| 175 | +}); |
| 176 | +``` |
| 177 | + |
| 178 | +## Performance Considerations |
| 179 | + |
| 180 | +- Use the built-in performance monitor to track FPS and optimize as needed |
| 181 | +- Adjust the quality settings for different devices |
| 182 | +- Use the `updateQuality` method in `environment.js` to adjust visual quality based on performance |
| 183 | + |
| 184 | +## Browser Compatibility |
| 185 | + |
| 186 | +This experience works best in browsers with full WebXR support: |
| 187 | +- Chrome 79+ |
| 188 | +- Firefox 76+ |
| 189 | +- Edge 79+ |
| 190 | +- Oculus Browser 7.0+ |
| 191 | + |
| 192 | +## Integration with Existing Applications |
| 193 | + |
| 194 | +The WebXR experience is designed as a standalone module that can be integrated with existing applications in various ways. For detailed instructions on how to integrate the WebXR experience with your main application, see the [Integration Guide](./integration.md). |
| 195 | + |
| 196 | +### Hono.js Integration |
| 197 | + |
| 198 | +The WebXR experience is primarily designed to be integrated with the main application using Hono.js and served at the `/webxr` URL path. A ready-to-use integration file is provided in [hono-integration.js](./hono-integration.js). |
| 199 | + |
| 200 | +```javascript |
| 201 | +// In your main application |
| 202 | +import { integrateWebXR } from './webxr/hono-integration.js'; |
| 203 | + |
| 204 | +// Integrate WebXR experience at /webxr path |
| 205 | +integrateWebXR(app); |
| 206 | +``` |
| 207 | + |
| 208 | +### Other Integration Options |
| 209 | + |
| 210 | +Alternative integration options include: |
| 211 | +- Serving as a separate experience |
| 212 | +- Linking from your main application |
| 213 | +- Embedding within an iframe |
| 214 | +- Direct code integration |
| 215 | + |
| 216 | +## License |
| 217 | + |
| 218 | +This project is available for use under the MIT License. |
| 219 | + |
| 220 | +## Acknowledgments |
| 221 | + |
| 222 | +- Three.js for 3D rendering |
| 223 | +- WebXR API for VR functionality |
0 commit comments