How to embed a 3D model in a website

Google's <model-viewer> is considered the industry standard for showcasing 3D models online, particularly for e-commerce. It allows developers to easily embed 3D models on websites using a small code snippet.

In this article we cover the steps for embedding a 3D model in a website using <model-viewer>:

  1. Convert your 3D model to GLB format
  2. Optimise your GLB file for the web
  3. Host your GLB model online
  4. Add the embed script to your website

Step 1: Model Format

glTF is widely regarded as the best format for displaying 3D models on a website, and is often described as the “JPEG of 3D”. GLB is the binary version of glTF where all assets such as textures are combined into a single file. This makes it convenient to use and generally more efficient to load on the web.

<model-viewer> only supports glTF/GLB formats. If your model is in another format such as OBJ, FBX or USD we would recommend using Blender to convert it to GLB. To do this load the model into Blender and go to File > Export and select glTF 2.0 (.glb/.gltf), and ensure the format is set to glTF Binary (.glb) before exporting.

If you need assistance converting your models, including bulk conversion, please contact us to discuss our 3D model conversion services.

Step 2: Model Optimisation

Your model may need to be optimised to ensure it loads in a reasonable time and performs well across all device types, especially mobile devices. Optimisation is often a balancing act to reduce model complexity while preserving overall quality. The two highest impact changes you can make are:

  • Apply compression such as Draco. This can reduce file sizes by up to 90% depending on the model.
  • Reduce the size of any large textures that may cause the model to crash on lower powered mobile devices.

Shopify have a free, web-based tool for doing this:
https://gltf-compressor.com

If you need help optimising a model further, please contact us to discuss our GLB optimisation services.

Step 3: Model Hosting

If your website runs on a platform such as Wix, Webflow, Squarespace, Framer or Wordpress there can be various limitations trying to host a GLB model on there:

  • Lack of support for the GLB file type
  • File size limits
  • Bandwidth limits

Self-hosting via Cloudflare R2

Cloudflare R2 is a cloud storage service (similar to AWS S3), that provides unlimited bandwidth and generous storage and read allowances on the free tier, making it particularly suited to hosting 3D models. Hosting on R2 requires a few technical configurations:

  1. Connect a domain you own to the R2 bucket (e.g. models.mysite.com)
    This allows for full CDN caching and removes the rate-limits of the R2 development URL.
  2. Enable CORS on the R2 bucket
    This allows your website to load the model from a separate domain or sub-domain.
  3. Configure a Cloudflare cache rule
    This is required since GLB files aren't cached by default. The rule should cache the model for long durations in both the browser cache and edge cache.
  4. Set the MIME type
    Ensure the GLB is uploaded to R2 with the correct MIME type: model/gltf-binary.

Step 4: Embed Model

The final step is to add the <model-viewer> embed script to your website and set the src property to point at your hosted GLB model.

<script type="module" src="https://ajax.googleapis.com/ajax/libs/model-viewer/4.4.0/model-viewer.min.js"></script>
<model-viewer src="https://cdn.tinyglb.com/models/2af91e2c093b45a78865dd330845dd0e.glb" 
              style="width: 100%; height: 300px" camera-controls>
</model-viewer>

<model-viewer> provides an Editor to make it easier to generate the code snippet for controlling various viewer options such as:

  • Poster generation: The poster is a static image of the model displayed to the user while the model is still loading
  • Auto load: You may wish to have the model only load when the user explicitly requests to view it
  • Initial camera position: You may wish to change the default position the camera starts in

Alternative Solution: Embed using TinyGLB

TinyGLB aims to simplify this entire process allowing you to upload a GLB model and within seconds get a hosted link for sharing and embedding. TinyGLB also provides the following:

  • All of your 3D models managed in one place.
  • Automatic compression and texture resizing on upload.
  • Multiple embed options: inline, pop-up and custom <model-viewer>.
  • Hosted viewer page to easily share a link to your model.
  • Easy control over standard <model-viewer> options such as poster generation, auto load, lighting, camera controls etc.
  • Extended viewer options such as control buttons (full screen, recentre, zoom) and custom backgrounds.
  • Unlimited bandwidth and model views.
  • Global CDN and serverless architecture for high performance and high availability.

Try TinyGLB for free →