GL ERROR :GL_INVALID_OPERATION : glDrawArrays: attempt to access out of range vertices in attribute 1, three.js

GL ERROR :GL_INVALID_OPERATION : glDrawArrays: attempt to access out of range vertices in attribute 1, three.js

这让我受挫了一段时间。我在做一个游戏,主图是使用obj格式的模型。我这样加载它:

var objLoader = new THREE.OBJLoader();
objLoader.setPath('Assets/');

objLoader.load('prison.obj', function(prison){
    prison.rotation.x = Math.PI / 2;
    prison.position.z += 0.1;
    prison.scale.set(15, 15, 15)
    scene.add(prison);
});

所以当我加载相同的模型但更小时,它工作正常。但是,现在我在模型中添加了更多,而且它更大了。 WebGL 开始给我这个警告: [.WebGL-0x7fb8de02fe00]GL ERROR :GL_INVALID_OPERATION : glDrawArrays: attempt to access out of range vertices in attribute 1. 这个警告发生了 256 次在 WebGL 说 WebGL: 错误太多之前,不会再向控制台报告此上下文的错误。

有了这个警告,我的模型没有完全加载。在预览中,我看到的模型是这样的,如预期:

但在 Three.js 中,我看到了一些不同的东西:

嗯,我不太确定这里出了什么问题:

  1. 可能是因为我使用的是 OBJLoader CDN
  2. 可能是我的模型尺寸太大了
  3. 也许我不知道自己在做什么

感谢任何帮助,谢谢。如果您需要更多详细信息,请告诉我。

此错误告诉您几何属性计数不匹配。例如,您的几何图形可能有:

  • 100 个顶点位置
  • 99 个顶点法线
  • 99 个顶点 UV

...或类似的东西。在第 100 个顶点上查找信息时,它显示 "attempt to access out-of-range vertices"

理想情况下,您希望重新导出 OBJ 资产,这样您就不必手动查找导致问题的几何体。但是,如果您无法获得新的 OBJ,您可以:

  • 防止使用 mesh.visibility = false
  • 渲染问题几何体
  • 修复几何属性计数。要修复它,您必须找到哪个属性是短的:
// We assume you already found the mesh with the problem.
const problemGeometry = mesh.geometry;

// Now we dig through the console to see each attribute.
// Look up each attribute.count property to see which one is short. 
console.log(problemGeometry.attributes);

// Then you'll have to set the draw range to the lowest of these counts
// Here I assume the lowest is 99
problemGeometry.setDrawRange(0, 99);

如果您的几何图形具有 geometry.index 属性,请不要忘记也查看它。这应该修复您的几何图形以使用最少的公共属性数进行渲染。有关 setDrawRange

的信息,请参阅此处