[英]How can I prevent an object to be destroyed after I used the function: .load() in javascript (three.js)
I am trying to prevent the destroying of my object after I loaded it. 我试图防止在加载对象后破坏它。
var loader = new THREE.ColladaLoader();
var rescue;
loader.load(
'Improved_Person_Maker_better_quality.dae',
function(collada) {
scene.add(collada.scene);
collada.scene.position.set(-25, 1, 25);
collada.scene.rotation.x = -Math.PI / 2;
rescue = collada.scene;
},
function(xhr) {
console.log((xhr.loaded / xhr.total * 100) + '% loaded');
}
);
rescue.position.set(-20, 1, 25);
The last statement is not possible, because the loaded mesh does not exist any more. 最后一条语句是不可能的,因为已加载的网格不再存在。 Is there a way to rescue collada.scene? 有办法挽救collada.scene吗?
The loader is asynchronous. 加载程序是异步的。
That means rescue.position.set()
is being called before rescue
is defined in the loader callback. 这意味着在加载程序回调中定义rescue
之前,将先调用rescue
rescue.position.set()
。
Just set the position in the callback, which you have done. 只需在回调中设置位置即可。
If you must refer to rescue
later in your code, you can use this pattern 如果您稍后必须在代码中引用rescue
,则可以使用此模式
if ( rescue !== undefined ) {
// your code
}
three.js r.75 three.js r.75
You can try this: 您可以尝试以下方法:
var rescue;
var manager = new THREE.LoadingManager();
manager.onProgress = function(item, loaded, total) {
console.log(item, loaded, total);
};
manager.onLoad = function(){
rescue.position.set(-20, 1, 25);
};
var loader = new THREE.ColladaLoader(manager);
loader.options.convertUpAxis = true;
loader.load("<url path for the file>", function(collada) {
scene.add(collada.scene);
collada.scene.position.set(-25, 1, 25);
collada.scene.rotation.x = -Math.PI / 2;
rescue = collada.scene;
}, function(xhr) {
console.log((xhr.loaded / xhr.total * 100) + '% loaded');
});
}
Loading Manager provide you flexibility of the async manage loading so that later point you can have multiple files. 加载管理器为您提供了异步管理加载的灵活性,以便以后您可以拥有多个文件。
A really great thank goes here to Stallion and Westlangley for giving me the right hint with the asynchronous loading. 非常感谢Stallion和Westlangley在异步加载方面给了我正确的提示。
I found a solution here but I will reproduce it, too. 我在这里找到了一个解决方案,但我也会复制它。
The solution is to create a normal THREE.Object3d() object. 解决方案是创建一个普通的THREE.Object3d()对象。 It is possible to add every object as a child of another. 可以将每个对象作为另一个对象添加。 So I did the following: 所以我做了以下事情:
loadObject: function(){
var loader = new THREE.ColladaLoader();
var container = new THREE.Object3D();
loader.load(
'Improved_Person_Maker_better_quality.dae',
function ( collada ) {
container.add(collada.scene);
},
function ( xhr ) {
console.log( (xhr.loaded / xhr.total * 100) + '% loaded' );
});
return container;
}
For flexibilty reasons I created a new function. 出于灵活性的原因,我创建了一个新功能。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.