[page:Loader] →

[name]

A loader for LDraw resources.

[link:https://ldraw.org LDraw] (LEGO Draw) is an [link:https://ldraw.org/article/218.html open format specification] for describing LEGO and other construction set 3D models.

An LDraw asset (a text file usually with extension .ldr, .dat or .txt) can describe just a single construction piece, or an entire model. In the case of a model the LDraw file can reference other LDraw files, which are loaded from a library path set with [page:Function setPartsLibraryPath]. You usually download the LDraw official parts library, extract to a folder and point setPartsLibraryPath to it.

Library parts will be loaded by trial and error in subfolders 'parts', 'p' and 'models'. These file accesses are not optimal for web environment, so a script tool has been made to pack an LDraw file with all its dependencies into a single file, which loads much faster. See section 'Packing LDraw models'. The LDrawLoader example loads several packed files. The official parts library is not included due to its large size.

Extensions

LDrawLoader supports the following extensions:

Code Example

// Instantiate a loader const loader = new LDrawLoader(); // Optionally set library parts path // loader.setPartsLibraryPath( path to library ); // Load a LDraw resource loader.load( // resource URL 'models/car.ldr_Packed.mpd', // called when the resource is loaded function ( group ) { // Optionally, use LDrawUtils.mergeObject() from // 'examples/jsm/utils/LDrawUtils.js' to merge all // geometries by material (it gives better runtime // performance, but construction steps are lost) // group = LDrawUtils.mergeObject( group ); scene.add( group ); }, // called while loading is progressing function ( xhr ) { console.log( ( xhr.loaded / xhr.total * 100 ) + '% loaded' ); }, // called when loading has errors function ( error ) { console.log( 'An error happened' ); } );

Examples

[example:webgl_loader_ldraw]

Packing LDraw models

To pack a model with all its referenced files, download the [link:https://www.ldraw.org/parts/latest-parts.html Official LDraw parts library] and use the following Node script: [link:https://github.com/mrdoob/three.js/blob/master/utils/packLDrawModel.js utils/packLDrawModel.js] It contains instructions on how to setup the files and execute it.

Metadata in .userData

LDrawLoader returns a [page:Group] object which contains an object hierarchy. Depending of each subobject type, its .userData member will contain the following members:
In a [page:Group], the userData member will contain:

In a [page:Material], the userData member will contain: