利用擴增實境 (模型觀眾)
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
<model-viewer>
網路元件可用於在網路上查看及與 3D 模型互動,並可在網路上透過擴增實境放置及與這些 3D 模型互動。
在支援 ARCore 的 Android 裝置上查看時,具有 ar
屬性的 <model-viewer>
元件會顯示按鈕,如以下範例所示:
![Model Viewer 螢幕截圖]()
在支援 ARCore 的裝置上輕觸這個按鈕時,系統會根據 ar-modes
屬性的值,使用 Chrome 的 WebXR 功能或Scene Viewer 應用程式顯示模型。
您可以使用網頁元件版位自訂 AR 按鈕的外觀。如需範例,請參閱 <model-viewer> documentation
。如要進一步瞭解如何透過 Android 應用程式或瀏覽器,在 AR 中顯示互動式 3D 模型,請參閱Scene Viewer。
開始使用 <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
接著,使用偏好的 bundler 在 node_modules/model-viewer/dist/model-viewer.min.js
中新增 <model-viewer>
。
加入 <model-viewer>
標記
將 <model-viewer>
網頁元件新增至 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 Editor 公用程式可產生 <model-viewer>
HTML 標記,並設定相機位置和燈光等屬性。
![模型編輯器螢幕截圖]()
模型相容性
<model-viewer>
支援 gltf
和 glb
檔案格式的模型。如需支援的 glTF 擴充功能清單,請參閱 three.js GLTFLoader documentation
。
為確保模型能正確顯示,請在模型編輯器中檢查模型。
瀏覽器和裝置支援
所有常青瀏覽器都支援使用 <model-viewer>
在網頁中顯示 3D 模型。建議您在元素獲得焦點時,使用:focus-visible
polyfill 隱藏焦點環。
如要使用 AR 功能,您必須使用 ARCore 支援的裝置,並安裝 Google Play 服務 - AR 適用。如要瞭解 webxr
AR 模式,請參閱「WebXR 瀏覽器支援」。根據預設,如果不支援 WebXR,系統會改用 Scene Viewer。
後續步驟
除非另有註明,否則本頁面中的內容是採用創用 CC 姓名標示 4.0 授權,程式碼範例則為阿帕契 2.0 授權。詳情請參閱《Google Developers 網站政策》。Java 是 Oracle 和/或其關聯企業的註冊商標。
上次更新時間:2024-11-23 (世界標準時間)。
[null,null,["上次更新時間: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."]]