三个 Js BufferGeometry Loader 给了我一个"Uncaught TypeError: Cannot read property 'index' of undefined"为什



我正在尝试在三个中加载一个 json 几何.js但我不断得到一个three.js:35740 Uncaught TypeError: Cannot read property 'index' of undefined at BufferGeometryLoader.parse (three.js:35740) at Object.onLoad (three.js:35730) at XMLHttpRequest.<anonymous> (three.js:30798)

这分别是我的代码和我的 JSON 文件:

var loader = new THREE.BufferGeometryLoader();
loader.load(
'models/box2.json',
function (geometry) {
var material = new THREE.MeshBasicMaterial( { color: 0xF5F5F5 } );
var object = new THREE.Mesh( geometry, material );
// object.name = 'shirt';
mesheto = object;
scene.add(object);
GameLoop();
}
);

{
"uvs":[],
"normals":[-5.32907e-15,-1,2.98023e-08,1.06581e-14,1,-2.98023e-08,1,4.47034e-08,2.83122e-07,-2.83122e-07,-7.45059e-08,1,-1,-1.3411e-07,-2.23517e-07,2.38419e-07,1.78814e-07,-1],
"metadata":{
"type":"Geometry",
"version":3,
"vertices":8,
"generator":"io_three",
"uvs":0,
"normals":6,
"faces":6
},
"vertices":[1,-1,-1,1,-1,1,-1,-1,1,-1,-1,-1,1,1,-1,0.999999,1,1,-1,1,1,-1,1,-1],
"faces":[33,0,1,2,3,0,0,0,0,33,4,7,6,5,1,1,1,1,33,0,4,5,1,2,2,2,2,33,1,5,6,2,3,3,3,3,33,2,6,7,3,4,4,4,4,33,4,0,3,7,5,5,5,5]

}

请有人帮我解释导致此错误的原因吗?

看起来您的 JSON 数据格式不正确,无法BufferGeometry.BufferGeometryLoader至少需要以下格式的 JSON 数据:

{
data: {
index: [],
attributes: {
position: {
array: [],
itemSize: 0,
normalized: 0
}
}
}
}

您拥有的似乎是来自Geometry对象的数据,可以通过JSONLoader解析。

您可以使用以下方法将Geometry转换为BufferGeometry

// someGeometry is a THREE.Geometry object
var bg = new THREE.BufferGeometry().fromGeometry(someGeometry);

最新更新