Three.js THREE.ObjectLoader 错误
Three.js Error with THREE.ObjectLoader
我似乎对 Three.ObjectLoader 有疑问。我正在以 JSON 格式 4.3 导出场景。这个场景包括几何体、材质和灯光。场景在 Three.js 编辑器中打开,没有任何错误。
我正在使用 Three.js r70 master 开发 firefox。
这是生成的json的link:https://gist.github.com/fraguada/d86637f7987096b361ea
在我尝试编写的查看器中,我使用以下代码进行加载:
var manager = new THREE.LoadingManager();
manager.onProgress = function ( item, loaded, total ) {
console.log( item, loaded, total );
};
// instantiate a loader
var loader = new THREE.ObjectLoader(manager);
loader.load(
// resource URL coming from other file
Name,
// Function when resource is loaded
function ( result )
{ scene.add( result.scene ); },
// Function called when download progresses
function ( xhr )
{ console.log( (xhr.loaded / xhr.total * 100) + '% loaded' ); },
// Function called when download errors
function ( xhr )
{ console.log( 'An error happened' ); }
);
在控制台中,我看到以下内容:
THREE.WebGLRenderer 70 three.min.js (line 513)
100% loaded content.js (line 117)
THREE.Object3D.add: undefined is not an instance of THREE.Object3D. three.min.js (line 164)
js/Test83.js 1 1 content.js (line 86)
错误也出现在第7674行的unminified three.js
如果我在 Three.js 编辑器中创建几何体和其他对象并将其导出为场景,也会出现此问题。
问题似乎出在这里:scene.add( result.scene );
假设 THREE.ObjectLoader 可以从文件中使用 JSON 是不正确的?在我 post 的代码中,如果我删除 scene.add( result.scene );
似乎文件至少加载了(数据加载,没有可视化的几何图形),因为没有出现错误。如果我有很多网格的场景,进度会输出到控制台(10% 加载,20% 加载等)。
如有任何见解,我们将不胜感激。
经过更多挖掘,我意识到我需要使用 THREE.XHRLoader 来引入 json,然后使用 THREE.ObjectLoader 来解析结果。这样的事情应该有效:
var loaderObj = new THREE.ObjectLoader();
var loader = new THREE.XHRLoader();
loader.load( 'js/data.json', function ( text ) {
text = "{ \"scene\" : " + text + " }";
var json = JSON.parse( text );
var scene = loaderObj.parse( json.scene );
},
// Function called when download progresses
function ( xhr )
{ console.log( (xhr.loaded / xhr.total * 100) + '% loaded' ); },
// Function called when download errors
function ( xhr )
{ console.log( 'An error happened' ); } );
这个方法很好用,是看ThreeJS编辑器发布场景时生成的代码学习的。
我认为你应该改为 scene.add( result )
。
我似乎对 Three.ObjectLoader 有疑问。我正在以 JSON 格式 4.3 导出场景。这个场景包括几何体、材质和灯光。场景在 Three.js 编辑器中打开,没有任何错误。
我正在使用 Three.js r70 master 开发 firefox。 这是生成的json的link:https://gist.github.com/fraguada/d86637f7987096b361ea
在我尝试编写的查看器中,我使用以下代码进行加载:
var manager = new THREE.LoadingManager();
manager.onProgress = function ( item, loaded, total ) {
console.log( item, loaded, total );
};
// instantiate a loader
var loader = new THREE.ObjectLoader(manager);
loader.load(
// resource URL coming from other file
Name,
// Function when resource is loaded
function ( result )
{ scene.add( result.scene ); },
// Function called when download progresses
function ( xhr )
{ console.log( (xhr.loaded / xhr.total * 100) + '% loaded' ); },
// Function called when download errors
function ( xhr )
{ console.log( 'An error happened' ); }
);
在控制台中,我看到以下内容:
THREE.WebGLRenderer 70 three.min.js (line 513)
100% loaded content.js (line 117)
THREE.Object3D.add: undefined is not an instance of THREE.Object3D. three.min.js (line 164)
js/Test83.js 1 1 content.js (line 86)
错误也出现在第7674行的unminified three.js
如果我在 Three.js 编辑器中创建几何体和其他对象并将其导出为场景,也会出现此问题。
问题似乎出在这里:scene.add( result.scene );
假设 THREE.ObjectLoader 可以从文件中使用 JSON 是不正确的?在我 post 的代码中,如果我删除 scene.add( result.scene );
似乎文件至少加载了(数据加载,没有可视化的几何图形),因为没有出现错误。如果我有很多网格的场景,进度会输出到控制台(10% 加载,20% 加载等)。
如有任何见解,我们将不胜感激。
经过更多挖掘,我意识到我需要使用 THREE.XHRLoader 来引入 json,然后使用 THREE.ObjectLoader 来解析结果。这样的事情应该有效:
var loaderObj = new THREE.ObjectLoader();
var loader = new THREE.XHRLoader();
loader.load( 'js/data.json', function ( text ) {
text = "{ \"scene\" : " + text + " }";
var json = JSON.parse( text );
var scene = loaderObj.parse( json.scene );
},
// Function called when download progresses
function ( xhr )
{ console.log( (xhr.loaded / xhr.total * 100) + '% loaded' ); },
// Function called when download errors
function ( xhr )
{ console.log( 'An error happened' ); } );
这个方法很好用,是看ThreeJS编辑器发布场景时生成的代码学习的。
我认为你应该改为 scene.add( result )
。