Three.js load multiple separated objects / JSONLoader

余生长醉 提交于 2019-11-29 02:32:37
Jaime

Yes, it is possible to load an entire scene from a json file exported from Blender!

I achieved that with the following process: (Using three.js r80)

  1. First you have to name your different objects in Blender like in the following image Outliner.
  2. Then you can export the file using Three.js json exporter add-on for Blender, but taking care of marking the Scene checkbox like in the following:

  1. Using this option your json file now contains all the meshes on the Blender's Outliner, as you can verify using any text editor. It doesn't matter if the meshes were selected or not.
  2. It is important to know that the root (or parent) object isn't a Geometry anymore. It is labeled with the Object type by now. To access the children objects (of Mesh type) you can use the getObjectByName method on the root object like in the following code:

    jsonloader.load( "obj/Books.json", function ( loadedObj ) {
        var surface = loadedObj.getObjectByName("Surface");
        var outline = loadedObj.getObjectByName("Outline");
        var mask = loadedObj.getObjectByName("Mask");
        // Watch the objects properties on console:
        console.log(loadedObj);
        console.log(surface);
        console.log(outline);
        console.log(mask);
    } );
    

    If we check the browser's console, we can see the proper objects assigned. And from now, you can manipulate the children objects independently (move, rotate, change materials, etc.)

Each object loaded has an associated .id. So you can use the Object3D.getObjectById() to find it and apply transforms on it.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!