Skip to content
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 38 additions & 2 deletions docs/examples/en/loaders/LDrawLoader.html
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ <h2>Extensions</h2>
</p>

<ul>
<li>!COLOUR: Colour and surface finish declarations.</li>
<li>!COLOUR: Color and surface finish declarations.</li>
<li>BFC: Back Face Culling specification.</li>
<li>!CATEGORY: Model/part category declarations.</li>
<li>!KEYWORDS: Model/part keywords declarations.</li>
Expand Down Expand Up @@ -176,9 +176,45 @@ <h3>[method:undefined parse]( [param:String text], [param:String path], [param:F

<h3>[method:Material getMaterial]( [param:String colourCode] )</h3>
<p>
[page:String colourCode] — For an already loaded LDraw asset, returns the [page:Material] associated with the parameter colour code.
[page:String colourCode] — Color code to get the associated [page:Material].
</p>

<p>For an already loaded LDraw asset, returns the [page:Material] associated with the parameter color code.
This method can be useful to modify the main color code material of a model or part that exposes it.
To get the main material this code can be used after the asset has been loaded:

<code>
const mainMaterial = loader.getMaterial( LDrawLoader.getMainColorCode() );
const mainEdgeMaterial = loader.getMaterial( LDrawLoader.getMainEdgeColorCode() );
// ... Modify materials colors, etc
</code>

The main color code is the standard way to color an LDraw part. It is '16' for triangles and '24' for edges. Usually
a complete model will not expose the main color (that is, no part uses the code '16' at the top level, because they
are assigned other specific colors) An LDraw part file on the other hand will expose the code '16' to be colored, and
can have additional fixed colors.

<h3>[method:String getMainColorCode]()</h3>
<p>
Returns the standard code for the main LDraw color.
</p>

<h3>[method:String getMainEdgeColorCode]()</h3>
<p>
Returns the standard code for the edges main LDraw color.
</p>

<h3>[method:void preloadMaterials]( [param:String path] )</h3>
<p>
[page:String path] — Path of the LDraw materials asset.
</p>

<p>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 color codes. This method is
intended to be used with not packed files, for example in an editor where materials are preloaded and parts are
loaded on demand.</p>


<h2>Source</h2>

<p>
Expand Down
36 changes: 21 additions & 15 deletions examples/jsm/loaders/LDrawLoader.js
Original file line number Diff line number Diff line change
Expand Up @@ -1486,6 +1486,9 @@ function createObject( elements, elementSize, isConditionalSegments = false, tot

//

const MAIN_COLOUR_CODE = '16';
const MAIN_EDGE_COLOUR_CODE = '24';

class LDrawLoader extends Loader {

constructor( manager ) {
Expand Down Expand Up @@ -1631,16 +1634,14 @@ class LDrawLoader extends Loader {

// Subobjects
subobjects: null,
numSubobjects: 0,
subobjectIndex: 0,
inverted: false,
category: null,
keywords: null,

// Current subobject
currentFileName: null,
mainColorCode: parentScope ? parentScope.mainColorCode : '16',
mainEdgeColorCode: parentScope ? parentScope.mainEdgeColorCode : '24',
mainColorCode: parentScope ? parentScope.mainColorCode : MAIN_COLOUR_CODE,
mainEdgeColorCode: parentScope ? parentScope.mainEdgeColorCode : MAIN_EDGE_COLOUR_CODE,
matrix: new Matrix4(),
type: 'Model',
groupObject: null,
Expand Down Expand Up @@ -1715,6 +1716,16 @@ class LDrawLoader extends Loader {

}

getMainColorCode() {

return MAIN_COLOUR_CODE;
}

getMainEdgeColorCode() {

return MAIN_EDGE_COLOUR_CODE;
}
Comment thread
yomboprime marked this conversation as resolved.
Outdated

parseColorMetaDirective( lineParser ) {

// Parses a color definition and returns a THREE.Material
Expand Down Expand Up @@ -1975,13 +1986,13 @@ class LDrawLoader extends Loader {

// Parses next color code and returns a THREE.Material

if ( ! forEdge && colorCode === '16' ) {
if ( ! forEdge && colorCode === MAIN_COLOUR_CODE ) {

colorCode = mainColorCode;

}

if ( forEdge && colorCode === '24' ) {
if ( forEdge && colorCode === MAIN_EDGE_COLOUR_CODE ) {

colorCode = mainEdgeColorCode;

Expand Down Expand Up @@ -2054,8 +2065,6 @@ class LDrawLoader extends Loader {
currentParseScope.category = info.category;
currentParseScope.keywords = info.keywords;
currentParseScope.subobjects = info.subobjects;
currentParseScope.numSubobjects = info.subobjects.length;
currentParseScope.subobjectIndex = 0;
currentParseScope.type = info.type;
currentParseScope.totalFaces = info.totalFaces;

Expand Down Expand Up @@ -2236,22 +2245,19 @@ class LDrawLoader extends Loader {
const parseScope = this.newParseScopeLevel( null, parentScope );
parseScope.url = url;

const parentParseScope = parseScope.parentScope;

// Set current matrix
if ( subobject ) {

parseScope.matrix.copy( subobject.matrix );
parseScope.inverted = subobject.inverted;
parseScope.startingConstructionStep = subobject.startingConstructionStep;
parseScope.fileName = subobject.fileName;
if ( subobject.colorCode === '16' && parseScope.parentScope ) {
if ( subobject.colorCode === MAIN_COLOUR_CODE && parentScope ) {

const parentScope = parseScope.parentScope;
parseScope.mainColorCode = parentScope.mainColorCode;
parseScope.mainEdgeColorCode = parentScope.mainEdgeColorCode;

} else if ( subobject.colorCode !== '16' ) {
} else if ( subobject.colorCode !== MAIN_COLOUR_CODE ) {

parseScope.mainColorCode = subobject.colorCode;
parseScope.mainEdgeColorCode = subobject.colorCode;
Expand All @@ -2267,7 +2273,7 @@ class LDrawLoader extends Loader {
const promises = [];
for ( let i = 0, l = subobjects.length; i < l; i ++ ) {

promises.push( loadSubobject( parseScope.subobjects[ i ] ) );
promises.push( loadSubobject( subobjects[ i ] ) );

}

Expand All @@ -2281,7 +2287,7 @@ class LDrawLoader extends Loader {
}

// If it is root object then finalize this object and compute construction steps
if ( ! parentParseScope.isFromParse ) {
if ( ! parentScope.isFromParse ) {

this.finalizeObject( parseScope );
this.computeConstructionSteps( parseScope.groupObject );
Expand Down