Browse Source

PLYLoader: Fix handling binary files with \n\r line endings in header. (#26232)

* add support for meshPhongNodeMaterial

* detect files with \r\n line endings

* handle files with \n\r line endings in ascii section correctly

* make \r\n specific.

* Update PLYLoader.js

---------

Co-authored-by: aardgoose <[email protected]>
Co-authored-by: Michael Herzog <[email protected]>
aardgoose 2 năm trước cách đây
mục cha
commit
f71e2eee03
1 tập tin đã thay đổi với 10 bổ sung6 xóa
  1. 10 6
      examples/jsm/loaders/PLYLoader.js

+ 10 - 6
examples/jsm/loaders/PLYLoader.js

@@ -104,17 +104,15 @@ class PLYLoader extends Loader {
 
 	parse( data ) {
 
-		function parseHeader( data ) {
+		function parseHeader( data, headerLength = 0 ) {
 
 			const patternHeader = /^ply([\s\S]*)end_header(\r\n|\r|\n)/;
 			let headerText = '';
-			let headerLength = 0;
 			const result = patternHeader.exec( data );
 
 			if ( result !== null ) {
 
 				headerText = result[ 1 ];
-				headerLength = new Blob( [ result[ 0 ] ] ).size;
 
 			}
 
@@ -680,6 +678,9 @@ class PLYLoader extends Loader {
 			let line = '';
 			const lines = [];
 
+			const startLine = new TextDecoder().decode( bytes.subarray( 0, 5 ) );
+			const hasCRNL = /^ply\r\n/.test( startLine );
+
 			do {
 
 				const c = String.fromCharCode( bytes[ i ++ ] );
@@ -702,7 +703,10 @@ class PLYLoader extends Loader {
 
 			} while ( cont && i < bytes.length );
 
-			return lines.join( '\r' ) + '\r';
+			// ascii section using \r\n as line endings
+			if ( hasCRNL === true ) i ++;
+
+			return { headerText: lines.join( '\r' ) + '\r',  headerLength: i };
 
 		}
 
@@ -714,8 +718,8 @@ class PLYLoader extends Loader {
 		if ( data instanceof ArrayBuffer ) {
 
 			const bytes = new Uint8Array( data );
-			const headerText = extractHeaderText( bytes );
-			const header = parseHeader( headerText );
+			const { headerText, headerLength } = extractHeaderText( bytes );
+			const header = parseHeader( headerText, headerLength );
 
 			if ( header.format === 'ascii' ) {