Browse Source

Make primitive type more robust, remove unnecessary group creation

Garrett Johnson 6 years ago
parent
commit
b08bdc14a8
1 changed files with 26 additions and 21 deletions
  1. 26 21
      examples/js/loaders/LDrawLoader.js

+ 26 - 21
examples/js/loaders/LDrawLoader.js

@@ -7,6 +7,12 @@
 
 THREE.LDrawLoader = ( function () {
 
+	function isPrimitiveType( type ) {
+
+		return /primitive/i.test( type ) || type === 'Subpart';
+
+	}
+
 	function LineParser( line, lineNumber ) {
 
 		this.line = line;
@@ -334,7 +340,7 @@ THREE.LDrawLoader = ( function () {
 
 
 					// TODO: Handle smoothing
-					if ( scope.separateObjects && parseScope.type === 'Part' || ! parentParseScope.isFromParse ) {
+					if ( scope.separateObjects && ! isPrimitiveType( parseScope.type ) || ! parentParseScope.isFromParse ) {
 
 						const objGroup = parseScope.groupObject;
 						if ( parseScope.triangles.length > 0 ) {
@@ -355,6 +361,17 @@ THREE.LDrawLoader = ( function () {
 
 						}
 
+						if ( parentParseScope.groupObject ) {
+
+							objGroup.name = parseScope.fileName;
+							objGroup.matrix.copy( parseScope.matrix );
+							objGroup.matrix.decompose( objGroup.position, objGroup.quaternion, objGroup.scale );
+							objGroup.matrixAutoUpdate = false;
+
+							parentParseScope.groupObject.add( objGroup );
+
+						}
+
 					} else {
 
 						if ( scope.separateObjects ) {
@@ -415,18 +432,6 @@ THREE.LDrawLoader = ( function () {
 
 					}
 
-					if ( parentParseScope.groupObject && parseScope.groupObject.children.length ) {
-
-						const objGroup = parseScope.groupObject;
-						objGroup.name = parseScope.fileName;
-						objGroup.matrix.copy( parseScope.matrix );
-						objGroup.matrix.decompose( objGroup.position, objGroup.quaternion, objGroup.scale );
-						objGroup.matrixAutoUpdate = false;
-
-						parentParseScope.groupObject.add( objGroup );
-
-					}
-
 					scope.removeScopeLevel();
 
 					if ( onProcessed ) {
@@ -1118,9 +1123,14 @@ THREE.LDrawLoader = ( function () {
 										currentParseScope.triangles = [];
 										currentParseScope.lineSegments = [];
 										currentParseScope.optionalSegments = [];
-										currentParseScope.groupObject = new THREE.Group();
 										currentParseScope.type = type;
 
+										if ( parentParseScope.isFromParse === false || scope.separateObjects && isPrimitiveType( type ) === false ) {
+
+											currentParseScope.groupObject = new THREE.Group();
+
+										}
+
 										triangles = currentParseScope.triangles;
 										lineSegments = currentParseScope.lineSegments;
 										optionalSegments = currentParseScope.optionalSegments;
@@ -1454,17 +1464,12 @@ THREE.LDrawLoader = ( function () {
 
 			}
 
-			const groupObject = currentParseScope.groupObject;
-			groupObject.userData.category = category;
-			groupObject.userData.keywords = keywords;
-			groupObject.userData.subobjects = subobjects;
-
+			currentParseScope.category = category;
+			currentParseScope.keywords = keywords;
 			currentParseScope.subobjects = subobjects;
 			currentParseScope.numSubobjects = subobjects.length;
 			currentParseScope.subobjectIndex = 0;
 
-			return groupObject;
-
 		}
 
 	};