Game Development Reference
In-Depth Information
Like ColladaLoader , the JSONLoader uses a load method with a callback. However,
the callback receives a THREE.Geometry object as its first parameter. Not all 3D
models have associated materials, but if the object does have materials, they will be
passed to the callback in an array as the second parameter:
var loader = new THREE.JSONLoader();
loader.load('model.js', function(geometry, materials) {
var material = materials && materials.length ?
new THREE.MeshFaceMaterial(materials) :
new THREE.MeshBasicMaterial({ color: 0x000000 });
var mesh = new THREE.Mesh(geometry, material);
scene.add(mesh);
});
As explained in Chapter 2 , Building a World , MeshFaceMaterial is a container that
maps multiple materials to different faces of the mesh.
The SceneLoader is a little different from other loaders because it can use other
loaders to handle specific parts of the scene:
var loader = new THREE.SceneLoader();
loader.addGeometryHandler('ctm', THREE.CTMLoader);
loader.addHierarchyHandler('dae', THREE.ColladaLoader);
loader.load('scene.js', function(result) {
scene.add(result.scene);
});
If a scene includes an external model, the SceneLoader will try to import it using
the appropriate handler. Use the addGeometryHandler method to add loaders for
file formats that only support single meshes, and use addHierarchyHandler to add
loaders for file formats that support multimesh scenes (DAE, OBJ, and UTF8). In this
example, CTM and DAE files will be loaded correctly.
Exporting to Three.js
The Three.js project includes extensions for the 3ds Max, Maya, and Blender 3D
modeling programs to make exporting models to the Three.js JSON format easier.
These extensions have some limitations; for example, some modifiers such as
smoothing groups are not supported. There are two common alternatives to avoid
these issues. The first is to export models to a format such as DAE and use the
corresponding Three.js importer. Another approach is to export models to OBJ
format and then run the Python converter script in the utils/converters folder
to transform the model to Three.js JSON format. Choosing a file format is mostly
a trade-off in file size (how long the file will take to retrieve) and initialization
(how long the file will take to parse). You may need to test different formats for
performance-sensitive projects.
 
Search WWH ::




Custom Search