Skip to content

Commit 7ef71af

Browse files
committed
Enable webxr with html argument.
1 parent aa3c85c commit 7ef71af

File tree

3 files changed

+90
-0
lines changed

3 files changed

+90
-0
lines changed

geometry/meshcat.h

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,37 @@ in the visualizer.
129129
Delete(). You are welcome to use absolute paths to organize your data, but the
130130
burden on tracking and cleaning them up lie on you.
131131
132+
@section meshcat_url_parameters Parameters for the hosted Meshcat page
133+
134+
%Meshcat has an *experimental* AR/VR option (using WebXR). It can be enabled
135+
through url parameters. For example, for a meshcat url `http://localhost:7000`,
136+
the following will enable the VR mode:
137+
138+
http://localhost:7000?webxr=vr
139+
140+
To to use augmented reality (where the meshcat background is replaced with your
141+
device's camera image), use:
142+
143+
http://localhost:7000?webxr=ar
144+
145+
If augmented reality is not available, it will fallback to VR mode.
146+
147+
Some notes on using the AR/VR modes:
148+
149+
- Before starting the WebXR session, position the interactive camera to be
150+
approximately where you want the origin of the head set's origin to be.
151+
- The meshcat scene controls are disabled while the WebXR session is active.
152+
- WebXR sessions can only be run with *perspective* cameras.
153+
- The controllers can be *visualized* but currently can't interact with the
154+
Drake simulation physically. To visualize the controllers append the
155+
additional url parameter `controller=on` as in
156+
`http://localhost:7000?webxr=vr&controller=on`.
157+
158+
If you do not have AR/VR hardware, you can use an emulator in your browser to
159+
experiment with the mode. Use an browser plugin like WebXR API Emulator (i.e.,
160+
for [Chrome](https://chrome.google.com/webstore/detail/webxr-api-emulator/mjddjgeghkdijejnciaefnkjmkafnnje)
161+
or [Firefox](https://addons.mozilla.org/en-US/firefox/addon/webxr-api-emulator/)).
162+
132163
@section network_access Network access
133164
134165
See MeshcatParams for options to control the hostname and port to bind to.

geometry/meshcat.html

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,30 @@
190190
make_connection(url, reconnect_ms);
191191
<!-- CONNECTION BLOCK END -->
192192

193+
const webxrMode = urlParams.get('webxr');
194+
if (webxrMode) {
195+
if (webxrMode === "vr") {
196+
viewer.handle_command({
197+
type: "enable_webxr",
198+
mode: "vr"
199+
});
200+
} else if (webxrMode === "ar") {
201+
viewer.handle_command({
202+
type: "enable_webxr",
203+
mode: "ar"
204+
});
205+
}
206+
}
207+
208+
const controllerMode = urlParams.get('controller');
209+
if (controllerMode === "on") {
210+
viewer.handle_command({
211+
type: "visualize_vr_controller"
212+
});
213+
}
214+
215+
216+
193217
</script>
194218

195219
<style>

geometry/test/meshcat_manual_test.cc

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -508,6 +508,41 @@ Open up your browser to the URL above.
508508
}
509509
}
510510

511+
std::cout << "\n";
512+
std::cout << "Now we'll test the WebXR functionality.\n";
513+
std::cout << "In a new browser window, open the URL:\n "
514+
<< meshcat->web_url() << "?webxr=vr&controller=on\n";
515+
std::cout << "If you don't have VR hardware installed on your machine, "
516+
"you'll have to install the WebXR API emulator appropriate to "
517+
"your browser. E.g., for Google Chrome "
518+
"see:\n https://chrome.google.com/webstore/detail/"
519+
"webxr-api-emulator/mjddjgeghkdijejnciaefnkjmkafnnje\n";
520+
std::cout << "\nIf the emulator is installed properly, you should see a "
521+
"button at the bottom that says \"Enter VR\".\n";
522+
std::cout << "Open the window showing the WebXR emulator and select a VR "
523+
"device with controllers (e.g., Oculus Quest). Click the "
524+
"\"Enter VR\" button. You should see the following:\n"
525+
<< " - The rendering screen is now split into two images.\n"
526+
<< " - The meshcat controls are gone (there is a message in the "
527+
"console informing you of this).\n"
528+
<< " - You should be able to manipulate the view in the WebXR "
529+
"emulator to affect what you see."
530+
<< "When you're done, close the browser window.\n\n";
531+
532+
MaybePauseForUser();
533+
534+
std::cout << "\nNow we'll try it again with *augmented* reality.\n"
535+
<< "In yet another browser window, open:\n"
536+
<< meshcat->web_url() << "?webxr=ar&controller=on\n"
537+
<< "This should be the same as before but with two differences:\n"
538+
<< " - The button reads \"Enter XR\"\n"
539+
<< " - When you click the button, the background becomes white. "
540+
"If you have an actual AR device, you should see the camera's "
541+
"image as the background.\n"
542+
<< "When you're done, close the browser window.\n\n";
543+
544+
MaybePauseForUser();
545+
511546
std::cout << "Exiting..." << std::endl;
512547
return 0;
513548
}

0 commit comments

Comments
 (0)