【发布时间】:2015-08-23 07:50:58
【问题描述】:
以下情况:
我在搅拌机中创建了一个场景并使用 io_three 插件将其导出。
如果我勾选导出器中的场景选项,则生成的 json 文件包含所有对象。
如果我不打勾,它将只导出当前选定的对象(而不是所有对象)。
问题:
我找不到用threejs显示整个场景的方法。我只看到我的背景,没有场景。
我的加载器看起来像这样:
var loader = new THREE.JSONLoader();
loader.load('test.json', function(geometry, material){
var materials = new THREE.MeshFaceMaterial(material);
mesh = new THREE.Mesh(geometry, materials);
scene.add(mesh);
});
我收到一个错误:
Uncaught TypeError: Cannot read property 'length' of undefined(anonymous function) @ three.min.js:268THREE.JSONLoader.parse @ three.min.js:271f.onreadystatechange @ three.min.js:266
生成的 json 文件(2 个带纹理的基本立方体):
{
"textures": [{
"image": "E30E1CC2-2152-3E22-8467-D5845ECA8529",
"anisotropy": 1,
"repeat": [1,1],
"magFilter": "LinearFilter",
"mapping": "UVMapping",
"uuid": "F7F25B99-8709-3776-8D32-B0774203CF6B",
"type": "Geometry",
"name": "Tex",
"wrap": ["RepeatWrapping","RepeatWrapping"],
"minFilter": "LinearMipMapLinearFilter"
}],
"images": [{
"uuid": "E30E1CC2-2152-3E22-8467-D5845ECA8529",
"type": "Geometry",
"name": "brick01.jpg",
"url": "brick01.jpg"
}],
"object": {
"matrix": [1,0,0,0,0,1,0,0,0,0,1,0,0,0,0,1],
"uuid": "048B5D01-9785-45B0-AA12-2E95F912E9B2",
"type": "Scene",
"children": [{
"type": "Mesh",
"name": "Cube",
"uuid": "F29D1AC4-D789-3D8E-BAB5-59DC1B758FE2",
"matrix": [-1.1386,0,0,0,0,0,1.1386,0,0,1.1386,0,0,0.460104,-0.303026,0.456013,1],
"visible": true,
"material": "FC9C3C1C-87E7-3748-8148-105003E5F3D5",
"castShadow": true,
"receiveShadow": true,
"geometry": "FE46F7EB-A263-3807-8987-EB40ABE18EAA"
},{
"type": "Mesh",
"name": "Cube.001",
"uuid": "7E8B5B6C-C1E8-35F8-8CAD-F6CC57432E76",
"matrix": [-3.01607,0,0,0,0,0,3.01607,0,0,3.01607,0,0,-2.49361,4.47873,-4.95054,1],
"visible": true,
"material": "FC9C3C1C-87E7-3748-8148-105003E5F3D5",
"castShadow": true,
"receiveShadow": true,
"geometry": "177A9EDE-9E89-3179-879D-52C39352875C"
}]
},
"geometries": [{
"uuid": "FE46F7EB-A263-3807-8987-EB40ABE18EAA",
"type": "Geometry",
"materials": [{
"mapDiffuse": "brick01.jpg",
"DbgName": "Material",
"colorAmbient": [0.8,0.8,0.8],
"shading": "lambert",
"opacity": 1,
"colorDiffuse": [0.8,0.8,0.8],
"wireframe": false,
"colorEmissive": [0,0,0],
"DbgIndex": 0,
"mapDiffuseRepeat": [1,1],
"mapDiffuseAnisotropy": 1,
"depthWrite": true,
"depthTest": true,
"blending": "NormalBlending",
"DbgColor": 15658734,
"mapDiffuseWrap": ["RepeatWrapping","RepeatWrapping"],
"visible": true,
"transparent": false
}],
"data": {
"faces": [43,0,1,2,3,0,0,1,2,3,0,1,2,3,43,4,7,6,5,0,4,1,5,6,4,5,6,7,43,0,4,5,1,0,7,8,9,10,0,4,7,1,43,1,5,6,2,0,11,12,13,14,1,7,6,2,43,2,6,7,3,0,15,16,17,18,2,6,5,3,43,4,0,3,7,0,4,19,20,21,4,0,3,5],
"uvs": [[0,0.666667,0.333333,0.666667,0.333333,1,0,1,0.333333,0.333333,0,0.666667,0,0.333333,0.666667,0.333333,0.333333,0.333333,0.333333,0,0.666667,0,0,0.333333,0,0,0.333333,0,0.333333,0.333333,1,0.333333,0.666667,0.333333,0.666667,0,1,0,0.666667,0.333333,0.666667,0.666667,0.333333,0.666667]],
"vertices": [1,1,-1,1,-1,-1,-1,-1,-1,-1,1,-1,1,0.999999,1,0.999999,-1,1,-1,-1,1,-1,1,1],
"name": "CubeGeometry",
"normals": [0.577349,0.577349,-0.577349,0.577349,-0.577349,-0.577349,-0.577349,-0.577349,-0.577349,-0.577349,0.577349,-0.577349,0.577349,0.577349,0.577349,-0.577349,0.577349,0.577349,-0.577349,-0.577349,0.577349,0.577349,-0.577349,0.577349],
"metadata": {
"faces": 6,
"generator": "io_three",
"normals": 8,
"vertices": 8,
"version": 3,
"materials": 1,
"uvs": 1
}
}
},{
"uuid": "177A9EDE-9E89-3179-879D-52C39352875C",
"type": "Geometry",
"materials": [{
"mapDiffuse": "brick01.jpg",
"DbgName": "Material",
"colorAmbient": [0.8,0.8,0.8],
"shading": "lambert",
"opacity": 1,
"colorDiffuse": [0.8,0.8,0.8],
"wireframe": false,
"colorEmissive": [0,0,0],
"DbgIndex": 0,
"mapDiffuseRepeat": [1,1],
"mapDiffuseAnisotropy": 1,
"depthWrite": true,
"depthTest": true,
"blending": "NormalBlending",
"DbgColor": 15658734,
"mapDiffuseWrap": ["RepeatWrapping","RepeatWrapping"],
"visible": true,
"transparent": false
}],
"data": {
"faces": [43,0,1,2,3,0,0,1,2,3,0,1,2,3,43,4,7,6,5,0,4,1,5,6,4,5,6,7,43,0,4,5,1,0,7,8,9,10,0,4,7,1,43,1,5,6,2,0,11,12,13,14,1,7,6,2,43,2,6,7,3,0,15,16,17,18,2,6,5,3,43,4,0,3,7,0,4,19,20,21,4,0,3,5],
"uvs": [[0,0.666667,0.333333,0.666667,0.333333,1,0,1,0.333333,0.333333,0,0.666667,0,0.333333,0.666667,0.333333,0.333333,0.333333,0.333333,0,0.666667,0,0,0.333333,0,0,0.333333,0,0.333333,0.333333,1,0.333333,0.666667,0.333333,0.666667,0,1,0,0.666667,0.333333,0.666667,0.666667,0.333333,0.666667]],
"vertices": [1,1,-1,1,-1,-1,-1,-1,-1,-1,1,-1,1,0.999999,1,0.999999,-1,1,-1,-1,1,-1,1,1],
"name": "Cube.001Geometry.1",
"normals": [0.577349,0.577349,-0.577349,0.577349,-0.577349,-0.577349,-0.577349,-0.577349,-0.577349,-0.577349,0.577349,-0.577349,0.577349,0.577349,0.577349,-0.577349,0.577349,0.577349,-0.577349,-0.577349,0.577349,0.577349,-0.577349,0.577349],
"metadata": {
"faces": 6,
"generator": "io_three",
"normals": 8,
"vertices": 8,
"version": 3,
"materials": 1,
"uvs": 1
}
}
}],
"materials": [{
"map": "F7F25B99-8709-3776-8D32-B0774203CF6B",
"depthWrite": true,
"depthTest": true,
"blending": "NormalBlending",
"emissive": 0,
"color": 13421772,
"uuid": "FC9C3C1C-87E7-3748-8148-105003E5F3D5",
"type": "MeshLambertMaterial",
"name": "Material",
"vertexColors": false,
"ambient": 13421772
}],
"metadata": {
"generator": "io_three",
"version": 4.3,
"type": "Object",
"sourceFile": "test.blend"
}
}
我还尝试了“test.js”、OBJLoader、OBJMTLLoader 并寻找其他具有相同问题的问题,但没有任何帮助。 有人可以告诉我我做错了什么和/或给我一个基本的例子,它可以工作或应该工作。 当我使用一个立方体而不是两个并且不使用场景选项时,它可以工作。 但是如何用json加载我的场景(多个对象)并用threejs显示呢?
【问题讨论】:
-
对于调试,您可以使用three.js 而不是three.min.js。 JSONLoader 定义了
vertices = json.vertices。由于 JSON 结构与此场景选项不同,因此错误来自几行之后的vertices.length。据我所知,JSONLoader 的输出仍然是一个网格(实际的搅拌机 json 导出器是最近的)。 ColladaLoader 可以导入场景。 -
@Astrak 我也试过 ColladaLoader,效果很好。我只是想知道搅拌机中的那个场景选项以及为什么它在三个.js 中不起作用。
标签: javascript json three.js blender