2
0
Эх сурвалжийг харах

Updated LegacyGLTFLoader.js shader parsing

Alan Millman 7 жил өмнө
parent
commit
70572a2521

+ 105 - 11
examples/js/loaders/deprecated/LegacyGLTFLoader.js

@@ -31,7 +31,7 @@ THREE.LegacyGLTFLoader = ( function () {
 
 			loader.load( url, function ( data ) {
 
-				scope.parse( data, onLoad, path );
+				scope.parse( data, path, onLoad );
 
 			}, onProgress, onError );
 
@@ -49,7 +49,7 @@ THREE.LegacyGLTFLoader = ( function () {
 
 		},
 
-		parse: function ( data, callback, path ) {
+		parse: function ( data, path, callback ) {
 
 			var content;
 			var extensions = {};
@@ -714,11 +714,15 @@ THREE.LegacyGLTFLoader = ( function () {
 
 			switch ( semantic ) {
 
+				case "POSITION_0":
+				case "POSITION0":
 				case "POSITION":
 
 					shaderText = shaderText.replace( regEx, 'position' );
 					break;
 
+				case "NORMAL_0":
+				case "NORMAL0":
 				case "NORMAL":
 
 					shaderText = shaderText.replace( regEx, 'normal' );
@@ -731,11 +735,6 @@ THREE.LegacyGLTFLoader = ( function () {
 					shaderText = shaderText.replace( regEx, 'uv' );
 					break;
 
-				case 'TEXCOORD_1':
-
-					shaderText = shaderText.replace( regEx, 'uv2' );
-					break;
-
 				case 'COLOR_0':
 				case 'COLOR0':
 				case 'COLOR':
@@ -743,16 +742,57 @@ THREE.LegacyGLTFLoader = ( function () {
 					shaderText = shaderText.replace( regEx, 'color' );
 					break;
 
+				case "WEIGHT_0":
+				case "WEIGHT0":
 				case "WEIGHT":
 
 					shaderText = shaderText.replace( regEx, 'skinWeight' );
 					break;
 
+				case "JOINT_0":
+				case "JOINT0":
 				case "JOINT":
 
 					shaderText = shaderText.replace( regEx, 'skinIndex' );
 					break;
 
+				default:
+
+					var underscoreIndex = semantic.indexOf( '_' );
+
+					if ( underscoreIndex !== - 1 ) {
+
+						var attrName = semantic.slice( 0, underscoreIndex ).toLowerCase();
+						var attrNum = parseInt( semantic.slice( underscoreIndex + 1 ) ) + 1;
+
+						switch ( attrName ) {
+
+							case 'position':
+							case 'normal':
+							case 'color':
+
+								shaderText = shaderText.replace( regEx, attrName + attrNum );
+								break;
+
+							case 'texcoord':
+
+								shaderText = shaderText.replace( regEx, 'uv' + attrNum );
+								break;
+
+							case 'weight':
+
+								shaderText = shaderText.replace( regEx, 'skinWeight' + attrNum );
+								break;
+
+							case 'joint':
+
+								shaderText = shaderText.replace( regEx, 'skinIndex' + attrNum );
+								break;
+
+						}
+
+					}
+
 			}
 
 		}
@@ -1592,6 +1632,8 @@ THREE.LegacyGLTFLoader = ( function () {
 
 						var attributes = primitive.attributes;
 
+						var attributeIndex = 0;
+
 						for ( var attributeId in attributes ) {
 
 							var attributeEntry = attributes[ attributeId ];
@@ -1602,10 +1644,14 @@ THREE.LegacyGLTFLoader = ( function () {
 
 							switch ( attributeId ) {
 
+								case 'POSITION_0':
+								case 'POSITION0':
 								case 'POSITION':
 									geometry.addAttribute( 'position', bufferAttribute );
 									break;
 
+								case 'NORMAL_0':
+								case 'NORMAL0':
 								case 'NORMAL':
 									geometry.addAttribute( 'normal', bufferAttribute );
 									break;
@@ -1616,26 +1662,74 @@ THREE.LegacyGLTFLoader = ( function () {
 									geometry.addAttribute( 'uv', bufferAttribute );
 									break;
 
-								case 'TEXCOORD_1':
-									geometry.addAttribute( 'uv2', bufferAttribute );
-									break;
-
 								case 'COLOR_0':
 								case 'COLOR0':
 								case 'COLOR':
 									geometry.addAttribute( 'color', bufferAttribute );
 									break;
 
+								case 'WEIGHT_0':
+								case 'WEIGHT0':
 								case 'WEIGHT':
 									geometry.addAttribute( 'skinWeight', bufferAttribute );
 									break;
 
+								case 'JOINT_0':
+								case 'JOINT0':
 								case 'JOINT':
 									geometry.addAttribute( 'skinIndex', bufferAttribute );
 									break;
 
+								default:
+
+									var addAttributeWithItsNameUnchanged = false;
+
+									var underscoreIndex = attributeId.indexOf( '_' );
+									if ( underscoreIndex !== - 1 ) {
+
+										var attrName = attributeId.slice( 0, underscoreIndex ).toLowerCase();
+										var attrNum = parseInt( attributeId.slice( underscoreIndex + 1 ) ) + 1;
+
+										switch ( attrName ) {
+
+											case 'position':
+											case 'normal':
+											case 'color':
+												geometry.addAttribute( attrName + attrNum, bufferAttribute );
+												break;
+
+											case 'texcoord':
+												geometry.addAttribute( 'uv' + attrNum, bufferAttribute );
+												break;
+
+											case 'weight':
+												geometry.addAttribute( 'skinWeight' + attrNum, bufferAttribute );
+												break;
+
+											case 'joint':
+												geometry.addAttribute( 'skinIndex' + attrNum, bufferAttribute );
+												break;
+
+											default:
+												addAttributeWithItsNameUnchanged = true;
+										}
+
+									}
+
+									if ( addAttributeWithItsNameUnchanged || underscoreIndex === - 1 ) {
+
+										var material = json.materials[ primitive.material ];
+										var attributeNames = json.techniques[ material.technique ].attributes;
+										var attributeName = Object.keys( attributeNames )[ attributeIndex ];
+
+										geometry.addAttribute( attributeName, bufferAttribute );
+
+									}
+
 							}
 
+							++attributeIndex;
+
 						}
 
 						if ( primitive.indices ) {