瀏覽代碼

Removed code changes except for the part that adds user-defined vertex attributes

Alan Millman 7 年之前
父節點
當前提交
5bc5b8af64
共有 3 個文件被更改,包括 66 次插入98 次删除
  1. 1 0
      editor/index.html
  2. 52 1
      editor/js/Loader.js
  3. 13 97
      examples/js/loaders/deprecated/LegacyGLTFLoader.js

+ 1 - 0
editor/index.html

@@ -24,6 +24,7 @@
 		<script src="../examples/js/loaders/ColladaLoader.js"></script>
 		<script src="../examples/js/loaders/FBXLoader.js"></script>
 		<script src="../examples/js/loaders/GLTFLoader.js"></script>
+		<script src="../examples/js/loaders/deprecated/LegacyGLTFLoader.js"></script>
 		<script src="../examples/js/loaders/KMZLoader.js"></script>
 		<script src="../examples/js/loaders/MD2Loader.js"></script>
 		<script src="../examples/js/loaders/OBJLoader.js"></script>

+ 52 - 1
editor/js/Loader.js

@@ -175,8 +175,18 @@ var Loader = function ( editor ) {
 				reader.addEventListener( 'load', function ( event ) {
 
 					var contents = event.target.result;
+					var loader;
+
+					if ( isGltf1( contents ) ) {
+
+						loader = new THREE.LegacyGLTFLoader();
+
+					} else {
+
+						loader = new THREE.GLTFLoader();
+
+					}
 
-					var loader = new THREE.GLTFLoader();
 					loader.parse( contents, '', function ( result ) {
 
 						result.scene.name = filename;
@@ -572,4 +582,45 @@ var Loader = function ( editor ) {
 
 	}
 
+	function isGltf1( contents ) {
+
+		var resultContent;
+
+		if ( typeof contents === 'string' ) {
+
+			resultContent = contents;
+
+		} else {
+
+			var magic = THREE.LoaderUtils.decodeText( new Uint8Array( contents, 0, 4 ) );
+
+			if ( magic === 'glTF' ) {
+
+				try {
+
+					extensions[ EXTENSIONS.KHR_BINARY_GLTF ] = new GLTFBinaryExtension( contents );
+
+				} catch ( error ) {
+
+					// dunno what it is, but it's definitely not OK
+					return false;
+
+				}
+
+				resultContent = extensions[ EXTENSIONS.KHR_BINARY_GLTF ].content;
+
+			} else {
+
+				resultContent = THREE.LoaderUtils.decodeText( new Uint8Array( contents ) );
+
+			}
+
+		}
+
+		var json = JSON.parse( resultContent );
+
+		return ( json.asset != undefined && json.asset.version[ 0 ] < 2 );
+
+	}
+
 };

+ 13 - 97
examples/js/loaders/deprecated/LegacyGLTFLoader.js

@@ -714,15 +714,11 @@ 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' );
@@ -735,6 +731,11 @@ 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':
@@ -742,57 +743,16 @@ 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;
-
-						}
-
-					}
-
 			}
 
 		}
@@ -1644,14 +1604,10 @@ 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;
@@ -1662,70 +1618,30 @@ 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 material = json.materials[ primitive.material ];
+									var attributeNames = json.techniques[ material.technique ].attributes;
+									var attributeName = Object.keys( attributeNames )[ attributeIndex ];
 
-									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 );
-
-									}
-
+									geometry.addAttribute( attributeName, bufferAttribute );
 							}
 
 							++attributeIndex;