使用 <model-viewer> 的增强现实
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
<model-viewer>
Web 组件可用于在 Web 上查看和与 3D 模型互动,并且可以无缝过渡到在 Web 上以增强现实方式放置和与这些 3D 模型互动。
在支持 ARCore 的 Android 设备上查看时,具有 ar
属性的 <model-viewer>
组件会显示一个按钮,如以下示例所示:
![模型查看器屏幕截图]()
在支持 ARCore 的设备上点按此按钮,系统会使用 Chrome 的 WebXR 功能或 Scene Viewer 应用显示模型,具体取决于 ar-modes
属性的值。
您可以使用 Web 组件槽来自定义 AR 按钮的外观。如需查看示例,请参阅 <model-viewer> documentation
。如需详细了解如何通过 Android 应用或浏览器在 AR 中显示互动式 3D 模型,请参阅场景查看器。
<model-viewer>
使用入门
以下步骤展示了如何在任何网页上开始使用 <model-viewer>
。
添加 <model-viewer>
依赖项
从 unpkg CDN 添加
<script type="module" src="https://unpkg.com/@google/model-viewer/dist/model-viewer.min.js"></script>
通过 npm
npm install @google/model-viewer
然后,使用首选捆绑器在 node_modules/model-viewer/dist/model-viewer.min.js
中添加 <model-viewer>
,
添加 <model-viewer>
标记
将 <model-viewer>
Web 组件添加到 HTML 文档中:
<model-viewer src="https://modelviewer.dev/shared-assets/models/Astronaut.glb"
ios-src="https://modelviewer.dev/shared-assets/models/Astronaut.usdz"
alt="A 3D model of an astronaut"
ar
auto-rotate
camera-controls></model-viewer>
您可以通过更改 <model-viewer>
标记上的属性来配置模型显示。这些属性会显示镜头移动、模型动画和环境信息等选项。<model-viewer>
文档列出了可设置的所有属性。模型编辑器实用程序可以生成 <model-viewer>
HTML 标记,并配置相机位置和光照等属性。
![模型编辑器屏幕截图]()
模型兼容性
<model-viewer>
支持 gltf
和 glb
文件格式的模型。
如需查看受支持的 glTF 扩展列表,请参阅 three.js GLTFLoader documentation
。
为确保模型能够正常显示,请在模型编辑器中检查您的模型。
浏览器和设备支持
所有常青浏览器都支持使用 <model-viewer>
在网页中显示 3D 模型。我们建议您使用 :focus-visible
polyfill 在元素获得焦点时隐藏焦点圈。
若要使用 AR 功能,您需要使用受 ARCore 支持的设备,并安装面向 AR 的 Google Play 服务。如需了解 webxr
AR 模式,请参阅 WebXR 浏览器支持。
默认情况下,如果不支持 WebXR,系统会改用场景查看器。
后续步骤
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2024-11-23。
[null,null,["最后更新时间 (UTC):2024-11-23。"],[],[],null,["# Augmented reality with <model-viewer>\n\nThe `\u003cmodel-viewer\u003e` web component can be used to view and interact with 3D models on the web, and it seamlessly transitions to placing and interacting with those 3D models in Augmented Reality on the web.\n\nWhen viewed on an ARCore-supported Android device, a `\u003cmodel-viewer\u003e` component\nwith the `ar` attribute displays a button as shown in the following example:\n\nTapping this button on ARCore-supported devices will display the model using Chrome's WebXR functionality or\n[the Scene Viewer app](/ar/develop/java/scene-viewer), depending on the value of\nthe [`ar-modes`](https://modelviewer.dev/docs/index.html#entrydocs-augmentedreality-attributes-arModes)\nattribute.\n\nThe appearance of the AR button can be customized by using web component slots.\nRefer to the [`\u003cmodel-viewer\u003e documentation`](https://modelviewer.dev/examples/augmentedreality/#customButton) for an example. See also, [Scene Viewer](/ar/develop/scene-viewer) for more\nabout displaying interactive 3D models in AR from an Android app or browser.\n\nGetting started with `\u003cmodel-viewer\u003e`\n-------------------------------------\n\nThe following steps show how to get started with `\u003cmodel-viewer\u003e` on any webpage.\n\n### Add the `\u003cmodel-viewer\u003e` dependency\n\n### From unpkg CDN\n\n \u003cscript type=\"module\" src=\"https://unpkg.com/@google/model-viewer/dist/model-viewer.min.js\"\u003e\u003c/script\u003e\n\n### From npm\n\n npm install @google/model-viewer\n\nThen, add `\u003cmodel-viewer\u003e` at `node_modules/model-viewer/dist/model-viewer.min.js` using your preferred bundler,\n\n### Include the `\u003cmodel-viewer\u003e` tag\n\nAdd the `\u003cmodel-viewer\u003e` web component to your HTML document: \n\n \u003cmodel-viewer src=\"https://modelviewer.dev/shared-assets/models/Astronaut.glb\"\n ios-src=\"https://modelviewer.dev/shared-assets/models/Astronaut.usdz\"\n alt=\"A 3D model of an astronaut\"\n ar\n auto-rotate\n camera-controls\u003e\u003c/model-viewer\u003e\n\n### Configure the `\u003cmodel-viewer\u003e` tag\n\nThe model display can be configured by changing the attributes on the `\u003cmodel-viewer\u003e` tag.\nThese properties expose options such as camera movement, model animations, and environment information.\nThe [`\u003cmodel-viewer\u003e` documentation](https://modelviewer.dev/docs/) lists all attributes that can be set.\nThe [Model Editor](https://modelviewer.dev/editor/) utility can generate a `\u003cmodel-viewer\u003e` HTML tag and configure properties such as camera position and lighting.\n\nModel compatibility\n-------------------\n\nModels in the [`gltf` and `glb` file format](https://www.khronos.org/gltf/) are supported by `\u003cmodel-viewer\u003e`.\nRefer to the [`three.js GLTFLoader documentation`](https://threejs.org/docs/#examples/en/loaders/GLTFLoader) for a list of\nsupported glTF extensions.\n\nTo ensure that your model will display properly, check your model in [Model Editor](https://modelviewer.dev/editor/).\n\nBrowser and device support\n--------------------------\n\nUsing `\u003cmodel-viewer\u003e` to display 3D models in a web page is supported in all evergreen browsers.\nWe recommend using [a `:focus-visible` polyfill](https://modelviewer.dev/#section-browser-support) to hide focus rings when the element is focused.\n\nAR features require an [ARCore-supported device](https://developers.google.com/ar/devices) and an installation of [Google Play Services for AR](https://play.google.com/store/apps/details?id=com.google.ar.core). For the `webxr` [AR mode](https://modelviewer.dev/docs/#entrydocs-augmentedreality-attributes-arModes), refer to [WebXR browser support](https://caniuse.com/webxr).\nBy default, if WebXR is not supported, [Scene Viewer](https://developers.google.com/ar/develop/java/scene-viewer#scene_viewer_runtime_requirements) will be used instead.\n\nNext steps\n----------\n\n- Review the [`\u003cmodel-viewer\u003e` reference documentation](https://modelviewer.dev/docs/index.html)\n- Check out [`\u003cmodel-viewer\u003e` samples on modelviewer.dev](https://modelviewer.dev/examples/)\n- See also, [Scene Viewer](/ar/develop/scene-viewer) for more about displaying interactive 3D models in AR from an Android app or browser."]]