WebXR 要求
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
如需设置能够使用 WebXR 进行 AR 开发的环境,您需要:
使用安全的上下文
安全上下文是一种安全机制,可确保网络资源在传输时提供特定级别的安全性。
您需要一个安全上下文才能通过新功能提供可信 XR 体验。
为了有资格在安全上下文中提供,您的资源必须至少满足以下条件之一:
- 资源必须通过 TLS (
https://
) 递送
- 必须通过
localhost
或 *.localhost
请求资源。
设置端口转发
如果您没有可通过 TLS 访问的开发服务器,仍然可以使用 Chrome 开发者工具的端口转发在安全的上下文中进行开发。通过端口转发,Android 设备可以访问开发机器网络服务器上托管的内容。它会在 Android 设备上创建一个映射到开发机器上的 TCP 端口的监听 TCP 端口。然后,流量便可通过这两个端口之间的 USB 连接传输。
请按照以下步骤设置端口转发:
- 按照 Chrome 开发者工具说明中的设置端口转发中的步骤操作。
- 确保开发工作站上的 Web 服务器正在运行。
- 验证您的连接是否正常运行。在 Android 设备上打开 Google Chrome,然后通过端口转发设置中指定的端口号找到
localhost
,例如 localhost:8000
。
现在,Web 服务器上的资源应该可以在 Android 设备上可见。这些资源是在安全上下文中提供,可以使用 WebXR 功能。
设置受支持的设备
Android 上的 AR 体验只能在支持 ARCore 的设备上运行。确保设备已安装并启用了面向 AR 的 Google Play 服务。
如需验证设备是否已正确配置为运行 WebXR,请在兼容的浏览器中浏览到示例 WebXR 页面。
后续步骤
现在,您已经设置了开发环境以使用 WebXR 打造 AR 体验,接下来您可以:
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2023-12-08。
[null,null,["最后更新时间 (UTC):2023-12-08。"],[[["\u003cp\u003eWebXR AR development requires a secure context (HTTPS, localhost, or *.localhost), a compatible browser, and an ARCore supported device.\u003c/p\u003e\n"],["\u003cp\u003ePort forwarding through Chrome DevTools can be used to establish a secure context if a TLS-enabled server is unavailable.\u003c/p\u003e\n"],["\u003cp\u003eAR experiences on Android are limited to ARCore supported devices with Google Play Services for AR installed and enabled.\u003c/p\u003e\n"],["\u003cp\u003eTo verify device and browser compatibility, visit a sample WebXR page like the one linked in the document.\u003c/p\u003e\n"],["\u003cp\u003eAfter setup, you can proceed to create immersive AR sessions and explore further using provided resources.\u003c/p\u003e\n"]]],["To enable WebXR AR development, use a web server in a secure context, either via TLS (`https://`) or `localhost`. If TLS is unavailable, use Chrome DevTools' port forwarding to map a port on your development machine to your Android device, ensuring the server is running. Confirm your Android device is ARCore-supported with Google Play Services for AR. Verify the setup by browsing to a sample WebXR page using a compatible browser.\n"],null,["# WebXR requirements\n\nTo setup an environment capable of AR development using WebXR, you'll need:\n\n- A web server that serves resources in a [secure context](https://developer.mozilla.org/en-US/docs/Web/Security/Secure_Contexts)\n- A [WebXR compatible browser](https://developer.mozilla.org/en-US/docs/Web/API/WebXR_Device_API#browser_compatibility) on an [ARCore supported device](/ar/devices)\n\n | **Note:** Android Emulator is not yet supported by WebXR.\n\nUse a secure context\n--------------------\n\nA [secure context](https://developer.mozilla.org/en-US/docs/Web/Security/Secure_Contexts) is a security mechanism that ensures that web resources have been transferred with a certain level of security.\nYou'll need a secure context to provide a [trusted XR experience](https://www.chromium.org/Home/chromium-security/prefer-secure-origins-for-powerful-new-features) with your new features.\n\nIn other to qualify as being served in a secure context, your resource must meet at least one of the following conditions:\n\n- The resource must be delivered over TLS (`https://`)\n- The resource must be requested through `localhost` or `*.localhost`.\n\n### Set up port forwarding\n\nIf you don't have a development server accessible over TLS, you can still develop in a secure context by using Chrome DevTools' [Port forwarding](https://developers.google.com/web/tools/chrome-devtools/remote-debugging/local-server). Port forwarding enables an Android device to access content hosted on the development machine's web server. It creates a listening TCP port on the Android device that maps to a TCP port on the development machine. Traffic can then travel through the USB connection between these two ports.\n\nFollow these steps to set up port forwarding:\n\n1. Follow the steps for **Set up port forwarding** in the [Chrome DevTools instructions](https://developers.google.com/web/tools/chrome-devtools/remote-debugging/local-server).\n2. Ensure that the web server on the development workstation is running.\n3. Verify that your connection works. Open Google Chrome on the Android device and browse to `localhost` on the port number specified in *Port forwarding settings* , for example, `localhost:8000`.\n\nThe resources on the web server should now be visible from the Android device. These resources are served in a Secure Context and can use WebXR capabilities.\n\nSet up a supported device\n-------------------------\n\nAR experiences on Android can only be run on an [ARCore supported device](https://developers.google.com/ar/devices).\nEnsure that the device has **Google Play Services for AR** installed and enabled.\n\nTo verify that the device is correctly configured to run WebXR, browse to [a sample WebXR page](https://immersive-web.github.io/webxr-samples/immersive-ar-session.html) in [a compatible browser](https://developer.mozilla.org/en-US/docs/Web/API/WebXR_Device_API#browser_compatibility).\n\nNext steps\n----------\n\nNow that you have set up your development environment to create AR experiences using WebXR, you can:\n\n- Learn how to [create an immersive AR session using WebXR](/ar/develop/webxr/hello-webxr).\n- Try [WebXR Samples](https://immersive-web.github.io/webxr-samples/)."]]