Capítulo 178 de 859

Chapter 178: LDrawLoader

Core Idea

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:

  • !COLOUR: Color and surface finish declarations.
  • BFC: Back Face Culling specification.
  • !CATEGORY: Model/part category declarations.
  • !KEYWORDS: Model/part keywords declarations.

Code Examples

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 );
  • What it demonstrates: Typical usage of LDrawLoader.

Reference Tables

Constructor

Signature
new LDrawLoader( manager : LoadingManager )

Methods

MethodDescription
.addDefaultMaterials() : LDrawLoaderInitializes the loader with default materials.
.addMaterial( material : Material ) : LDrawLoaderAdds a single material to the loader's material library.
.addMaterials( materials : Array.<Material> ) : LDrawLoaderAdds a list of materials to the loader's material library.
.clearMaterials() : LDrawLoaderClears the loader's material library.
.getMainEdgeMaterial() : MaterialReturns the material for the edges main LDraw color.
.getMainMaterial() : MaterialReturns the Material for the main LDraw color.
.getMaterial( colorCode : string ) : MaterialReturns 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.constructorLDrawConditionalLineNodeMaterial.constructor ) : LDrawLoader`
.setFileMap( fileMap : Object.<string, string> ) : LDrawLoaderSets 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> ) : LDrawLoaderSets the loader's material library. This method clears existing material definitions.
.setPartsLibraryPath( path : string ) : LDrawLoaderThis 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).

Key Takeaways

  1. LDrawLoader extends: Loader
  2. Key methods: addDefaultMaterials, addMaterial, addMaterials, clearMaterials.

Connects To