简体   繁体   English

通过 XHR 加载 GLTF model 时的 Infinity% 加载百分比

[英]Infinity% loading percentage when loading GLTF model via XHR

I am trying to load a GLTF model of a piano using XHR, and display the loading percentage on a webpage.我正在尝试使用 XHR 加载钢琴的 GLTF model,并在网页上显示加载百分比。 The model is loaded using the Three.js library. model 是使用 Three.js 库加载的。 When I run the code on a local server, the loading percentage is displayed correctly and the model is rendered without any issues.当我在本地服务器上运行代码时,加载百分比显示正确并且 model 呈现没有任何问题。 However, when I host the code on a website, the loading percentage is displayed as "Infinity%".但是,当我在网站上托管代码时,加载百分比显示为“Infinity%”。 I am getting an error "Uncaught TypeError: Failed to set the 'value' property on 'HTMLProgressElement': The provided double value is non-finite" in my console.我在控制台中收到错误“未捕获的类型错误:无法在‘HTMLProgressElement’上设置‘值’属性:提供的双精度值是非有限的”。 I have tried to debug the issue by changing the code but I am not able to figure out what is causing the problem.我试图通过更改代码来调试问题,但我无法找出导致问题的原因。 I need help in understanding why this is happening and how to fix it.我需要帮助来理解为什么会发生这种情况以及如何解决它。

Heres the code (i highlighted the line with an arrow (i added parseInt and it changed to NaN%, so it did nothing useful)):这是代码(我用箭头突出显示了该行(我添加了 parseInt 并将其更改为 NaN%,因此它没有任何用处)):

loader.load(
  "models/piano/piano.gltf",
  (gltf) => {
    piano = gltf.scene;
    scene.add(piano);
    piano.traverse((child) => {
      if (child.isMesh) {
        child.castShadow = true;
        child.receiveShadow = true;

        if (child.material.map) {
          child.material.map.anisotropy = 4;
        }
        if (child.material.roughness) {
          child.material.roughness = 0.2;
        }
        if (child.name.startsWith("key")) {
          child.userData = {
            position: child.position.y,
            rotation: child.rotation.x,
          };
          pianoKeys.push(child);
        }
      }
    });

    Promise.all(
      notes.map((note, index) => {
        loadingProgress.value = parseInt((index / 88) * 100);
        currentFile.innerHTML = `sounds/${note}.mp3<span>${
          index + 1
        } / 88</span>`;
        if (index + 1 == 88) {
          currentFile.innerHTML = `Finishing loading...`;
        }
        return new Promise((resolve) => {
          audioSources[note] = new Howl({
            src: [`sounds/${note}.mp3`],
            volume: 1,
            onload: resolve,
          });
        });
      })
    ).then(() => {
      filesLoaded();
    });
  },
  (xhr) => {
    const percentComplete = parseInt((xhr.loaded / xhr.total) * 100);
    currentFile.innerHTML = `models/piano/piano.gltf<span>${percentComplete}%</div>`; //  <--
    loadingProgress.value = percentComplete;
  }
);

Okay i think i found a solution, i added console.log(xhr) to xhr function and checked loaded value on local server and assigned it as a variable, in my case 289907, because xhr.total returns 0 on website.好的,我想我找到了解决方案,我将 console.log(xhr) 添加到 xhr function 并检查了本地服务器上的加载值并将其分配为变量,在我的例子中是 289907,因为 xhr.total 在网站上返回 0。 and now it works perfectly现在它完美地工作

From the documentation文档

onProgress: A function to be called while the loading is in progress. onProgress:在加载过程中调用 function。 The argument will be the XMLHttpRequest instance, that contains.total and.loaded bytes.参数将是 XMLHttpRequest 实例,它包含总字节数和加载字节数。 If the server does not set the Content-Length header;如果服务器没有设置Content-Length header; .total will be 0. .total 将为 0。

It seems like your server is not setting the appropriate header, so you're getting: xhr.loaded / 0 = infinity看起来你的服务器没有设置适当的 header,所以你得到: xhr.loaded / 0 = infinity

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM