Browse Source

Merge pull request #13339 from 1d2d3d/LegacyGLTFLoaderMinimalShaderUpdate

Updated LegacyGLTFLoader.js shader parsing
Mr.doob 7 years ago
parent
commit
f66fc35c69
3 changed files with 69 additions and 3 deletions
  1. 1 0
      editor/index.html
  2. 46 1
      editor/js/Loader.js
  3. 22 2
      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>

+ 46 - 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,39 @@ var Loader = function ( editor ) {
 
 	}
 
+	function isGltf1( contents ) {
+
+		var resultContent;
+
+		if ( typeof contents === 'string' ) {
+
+			// contents is a JSON string
+			resultContent = contents;
+
+		} else {
+
+			var magic = THREE.LoaderUtils.decodeText( new Uint8Array( contents, 0, 4 ) );
+
+			if ( magic === 'glTF' ) {
+
+				// contents is a .glb file; extract the version
+				var version = new DataView( contents ).getUint32( 4, true );
+
+				return version < 2;
+
+			} else {
+
+				// contents is a .gltf file
+				resultContent = THREE.LoaderUtils.decodeText( new Uint8Array( contents ) );
+
+			}
+
+		}
+
+		var json = JSON.parse( resultContent );
+
+		return ( json.asset != undefined && json.asset.version[ 0 ] < 2 );
+
+	}
+
 };

+ 22 - 2
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 = {};
@@ -1634,6 +1634,26 @@ THREE.LegacyGLTFLoader = ( function () {
 									geometry.addAttribute( 'skinIndex', bufferAttribute );
 									break;
 
+								default:
+
+									if ( ! primitive.material ) break;
+
+									var material = json.materials[ primitive.material ];
+
+									if ( ! material.technique ) break;
+
+									var parameters = json.techniques[ material.technique ].parameters || {};
+
+									for( var attributeName in parameters ) {
+
+										if ( parameters [ attributeName ][ 'semantic' ] === attributeId ) {
+
+											geometry.addAttribute( attributeName, bufferAttribute );
+
+										}
+
+									}
+
 							}
 
 						}