Capítulo 178 de 859
A loader for the LDraw format.
[LDraw](https://ldraw.org/} (LEGO Draw) is an 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 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.
LDrawLoader supports the following extensions:
const loader = new LDrawLoader();
loader.setConditionalLineMaterial( LDrawConditionalLineMaterial ); // the type of line material depends on the used renderer
const object = await loader.loadAsync( 'models/ldraw/officialLibrary/models/car.ldr_Packed.mpd' );
scene.add( object );
Constructor
| Signature |
|---|
new LDrawLoader( manager : LoadingManager ) |
Methods
| Method | Description |
|---|---|
.addDefaultMaterials() : LDrawLoader | Initializes the loader with default materials. |
.addMaterial( material : Material ) : LDrawLoader | Adds a single material to the loader's material library. |
.addMaterials( materials : Array.<Material> ) : LDrawLoader | Adds a list of materials to the loader's material library. |
.clearMaterials() : LDrawLoader | Clears the loader's material library. |
.getMainEdgeMaterial() : Material | Returns the material for the edges main LDraw color. |
.getMainMaterial() : Material | Returns the Material for the main LDraw color. |
.getMaterial( colorCode : string ) : Material | Returns a material for the given color code. |
.load( url : string, onLoad : function, onProgress : onProgressCallback, onError : onErrorCallback ) | Starts loading from the given URL and passes the loaded LDraw asset to the onLoad() callback. |
.parse( text : string, onLoad : function, onError : onErrorCallback ) | Parses the given LDraw data and returns the resulting group. |
.preloadMaterials( url : string ) : Promise (async) | This async method preloads materials from a single LDraw file. In the official parts library there is a special file which is loaded always the first (LDConfig.ldr) and contains all the standard colo… |
| `.setConditionalLineMaterial( type : LDrawConditionalLineMaterial.constructor | LDrawConditionalLineNodeMaterial.constructor ) : LDrawLoader` |
.setFileMap( fileMap : Object.<string, string> ) : LDrawLoader | Sets a map which maps referenced library filenames to new filenames. If a fileMap is not specified (the default), library parts will be accessed by trial and error in subfolders 'parts', 'p' and 'mod… |
.setMaterials( materials : Array.<Material> ) : LDrawLoader | Sets the loader's material library. This method clears existing material definitions. |
.setPartsLibraryPath( path : string ) : LDrawLoader | This method must be called prior to load() unless the model to load does not reference library parts (usually it will be a model with all its parts packed in a single file). |