浏览代码

Merge pull request #14522 from erasta/dev

STLLoader: Improve classification as ASCII or binary
Mr.doob 7 年之前
父节点
当前提交
ec58f2d97a
共有 1 个文件被更改,包括 24 次插入7 次删除
  1. 24 7
      examples/js/loaders/STLLoader.js

+ 24 - 7
examples/js/loaders/STLLoader.js

@@ -86,22 +86,39 @@ THREE.STLLoader.prototype = {
 			// However, ASCII STLs lacking the SPACE after the 'd' are known to be
 			// plentiful.  So, check the first 5 bytes for 'solid'.
 
+			// Several encodings, such as UTF-8, precede the text with up to 5 bytes:
+			// https://en.wikipedia.org/wiki/Byte_order_mark#Byte_order_marks_by_encoding
+			// Search for "solid" to start anywhere after those prefixes.
+
 			// US-ASCII ordinal values for 's', 'o', 'l', 'i', 'd'
 
 			var solid = [ 115, 111, 108, 105, 100 ];
 
-			for ( var i = 0; i < 5; i ++ ) {
+			for ( var off = 0; off < 5; off ++ ) {
+
+				// If "solid" text is matched to the current offset, declare it to be an ASCII STL.
+
+				if ( matchDataViewAt ( solid, reader, off ) ) return false;
 
-				// If solid[ i ] does not match the i-th byte, then it is not an
-				// ASCII STL; hence, it is binary and return true.
+			}
+
+			// Couldn't find "solid" text at the beginning; it is binary STL.
 
-				if ( solid[ i ] != reader.getUint8( i, false ) ) return true;
+			return true;
 
- 			}
+		}
 
-			// First 5 bytes read "solid"; declare it to be an ASCII STL
+		function matchDataViewAt( query, reader, offset ) {
+
+			// Check if each byte in query matches the corresponding byte from the current offset
+
+			for ( var i = 0, il = query.length; i < il; i ++ ) {
+
+				if ( query[ i ] !== reader.getUint8( offset + i, false ) ) return false;
+
+			}
 
-			return false;
+			return true;
 
 		}