2

I am trying to add my own 3D GLB models to the google scene viewer generic code. However when I replace the link to the chair model with a downloaded GLB model the model does not show up on the webpage.

I have used my own models that I downloaded. I also tried using the same chair from the generic code by downloading it. I looked at the inspector and while the placeholder is present, it seems that the space is empty and the alt text does not display either

This is the code I'm working with:

<!DOCTYPE html>

<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="utf-8" />
    <title>3DModel</title>
    <script type="module" src="https://unpkg.com/@google/model-viewer/dist/model-viewer.js"></script>
    <script nomodule src="https://unpkg.com/@google/model-viewer/dist/model-viewer-legacy.js"></script>
</head>
<body>
    <model-viewer src="chair.glb" auto-rotate camera-controls alt="cube" background-color="#455A64"></model-viewer>
</body>
</html>

All I have changed is the src from a link to the chair.glb to the location of the downloaded model (which is in the same folder as the index code)

How can I fix this?

Arto Bendiken
  • 2,293
  • 1
  • 22
  • 26

3 Answers3

2

If you take a look at the console you'll probably spot an error like this:

model-viewer.js:36006 Access to XMLHttpRequest at 'file:///chair.glb' from origin 'null' has been blocked by CORS policy: Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, https.

This means the browser locks access to the chair.glb file because it is served via the file:/// protocol.

To get around you can either upload the html and the .glb file to a 'real' webserver online or you run a simple webserver on your computer and preview the file there.

For quick results I'd recommend the Mongoose Webserver

Get a a binary for your platform at https://cesanta.com/binary.html and start it inside the folder where your html & glb files are. This will open a browser window where you can select the html file you want to to open.

obscure
  • 8,071
  • 2
  • 9
  • 30
1

As far as I can help you is:

  1. Specify the correct path of the model.
  2. My model is stored in models folder which is inside shared-assets folder.(use "./chair.glb")
  3. Try giving relative path.

<!DOCTYPE html>

<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="utf-8" />
    <title>3DModel</title>
    <script type="module" src="https://unpkg.com/@google/model-viewer/dist/model-viewer.js"></script>
    <script nomodule src="https://unpkg.com/@google/model-viewer/dist/model-viewer-legacy.js"></script>
</head>
<body>
    <model-viewer src="./shared-assets/models/cube.gltf" auto-rotate camera-controls alt="cube" background-color="#455A64"></model-viewer>
</body>
</html>
0

<!DOCTYPE html>
<html lang="en">
  <head>
    <title>model-viewer</title>
    <script
      type="module"
      src="https://unpkg.com/@google/model-viewer/dist/model-viewer.min.js"
    ></script>
  </head>
  <body>
    <div>
      <model-viewer
   src="https://rawcdn.githack.com/BabylonJS/Exporters/422493778d6ffbc2980e83e46eb94729bbeada0c/Maya/Samples/glTF%202.0/T-Rex/trex_running.gltf"
        alt="dragon"
        auto-rotate
        camera-controls
      ></model-viewer>
    </div>
    </body>
</html>

Here is my example in this model is loading you can check this.

Iron Man
  • 19
  • 1