DEV Community

Kingswillwariboko
Kingswillwariboko

Posted on

A Guide to Combining React and Google Model Viewer

Have you ever wondered how to embed 3D models into a web page or website? In this article, we will explore the simple steps for integrating Google Model Viewer into a React app.

Model Viewer is an open source web component created by Google and managed through GitHub. Its purpose is to make it easy to display 3D content on the web by using just a few lines of HTML code.

To integrate Model Viewer into your React app, you will need to install the NPM package by running the following command:

Image description

An alternative method (recommended method) is to add the following link in the body of your index.html file, located in the public directory, as shown in the code snippet below:

Image description

Next, you will need to use the component in any React component of your choice. In this example, we will add it to our App.js file. Before doing so, you will need to import your 3D model in either glb or gltf format, as only these formats are compatible with Google Model Viewer.

Image description

In the code above, we imported our glb file and referenced it in the src attribute of the tag. We also added additional attributes, such as ar, which enables an augmented reality experience, and set the ar-modes attribute.

The webxr mode allows the use of AR in browsers that support the WebXR API, such as Chrome for Android or Safari for iOS.

The quick-look mode enables AR on iOS devices using the Quick Look feature, which allows users to view AR content directly from the Safari browser without needing to download a separate app.

The scene-viewer mode enables AR in the Google Scene Viewer app, which is available on Android devices.

By including all three modes in the ar-modes attribute, the element becomes compatible with various AR platforms and devices, providing a more seamless experience for users.

We added camera controls that allow users to interact with and control the camera view of the 3D model displayed in the element. This feature enhances the user experience by allowing them to examine the 3D model more closely and from different angles, making the model feel more like a real object that the user can manipulate and explore.

Finally, the xr-environment is an attribute that can be added to the element in order to specify the environment in which the 3D model will be displayed.

These steps cover the basic process of integrating Google Model Viewer with React. By following these steps, you can embed 3D models into your React app and allow users to interact with them in a variety of ways, including augmented reality.

Top comments (0)