简体   繁体   English

等待加载完成

[英]Wait until loading is completed

Hi guys iam kind of stuck right now. 大家好,我现在陷入困境。

My Problem is that iam back at my starting point before all of my Objects are loaded. 我的问题是,我在加载所有对象之前回到了我的起点。

After the creation of my shelf i calculate a boundingBox that i need to calculate the Position and the translation of my Object. 创建完架子后,我需要计算一个boundingBox,我需要计算Position和Object的平移。 But because iam back to early i cant calculate my bounding box and it says everything is 0 or infinity. 但是由于iam可以追溯到很早,所以我无法计算边界框,它说的都是0或无穷大。 So i kind of need a way to wait until everything is loaded. 因此,我有点需要一种方法,直到所有内容加载完毕。 i Thought that the onload part on the loader does that but it doesnt work. 我以为装载机上的onload部分可以做到这一点,但它不起作用。

Here are the relevant parts of my code: 这是我的代码的相关部分:

in my init i call one function 在我的初始化我叫一个功能

let shelf = this.creators.createShelf(parts);

My Creator Class builds a Object 我的创作者类构建一个对象

createShelf(parts) {

  let shelf= new THREE.Mesh();
  let mesh: THREE.Mesh;
  let props;
  for (let part of parts) {
    if (part.model == true) {
      let loadObject = this.helpers.loadObject(part);
      shelf.add(loadObject);
      // do stuff
      return shelf;

Now i load a 3d Object i created with the ObjectLoader of ThreeJS 现在,我加载使用ThreeJS的ObjectLoader创建的3d对象

 loadObject(props: Object3D) {

    let object = new THREE.Mesh();
    let modelLoader = new THREE.ObjectLoader();
    modelLoader.load(part.modelObjPath,
        // onload
        function (obj) {
            // do stuff to the Object
            object.add(obj);  
        },
        function (xhr) {
            console.log( (xhr.loaded / xhr.total * 100) + '% ' + part.name + ' loaded' );
        },
        function (xhr) {
            console.error( 'An error happened' );
        }
    );
    return object;
}

my bounding box createt either with Box3 or a BoxHelper returns this: 我的Box3或BoxHelper创建的边界框返回以下内容:

let box = new THREE.Box3().setFromObject(shelf);

bb object 
Ta {min: p, max: p}
max:
p {x: -Infinity, y: -Infinity, z: -Infinity}
min:
p {x: Infinity, y: -Infinity, z: -Infinity}

i hope i could explain where iam stuck. 我希望我能解释我困在哪里。

Thanks for looking into it. 感谢您调查。

The docs have the answer already: https://threejs.org/docs/#api/loaders/managers/LoadingManager 该文档已经有了答案: https : //threejs.org/docs/#api/loaders/managers/LoadingManager

You can pass in the manager to as many items as you need making this a handy class for waiting for all your objects and items to load before manipulating your scene and assets. 您可以将管理器传递给您所需的任意数量的项目,这使它成为方便的类,可以在操作场景和资产之前等待所有对象和项目加载。 You then specify actions you'd like to take during start, progress, and onLoad. 然后,您可以指定要在开始,进度和onLoad期间执行的操作。 onLoad fires when all the items that have the manager passed in have loaded. 当所有已传入管理器的项目均已加载时,将触发onLoad。

var manager = new THREE.LoadingManager();
manager.onStart = function ( url, itemsLoaded, itemsTotal ) {

    console.log( 'Started loading file: ' + url + '.\nLoaded ' + itemsLoaded + ' of ' + itemsTotal + ' files.' );

};

manager.onLoad = function ( ) {

    console.log( 'Loading complete!');

};


manager.onProgress = function ( url, itemsLoaded, itemsTotal ) {

    console.log( 'Loading file: ' + url + '.\nLoaded ' + itemsLoaded + ' of ' + itemsTotal + ' files.' );

};

manager.onError = function ( url ) {

    console.log( 'There was an error loading ' + url );

};

var loader = new THREE.OBJLoader( manager );
loader.load( 'file.obj', function ( object ) {

    //

} );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM