Skip to content

Commit

Permalink
PLYLoader: Fix handling binary files with \n\r line endings in header. (
Browse files Browse the repository at this point in the history
#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 <angus.sawyer@email.com>
Co-authored-by: Michael Herzog <michael.herzog@human-interactive.org>
  • Loading branch information
3 people committed Jun 12, 2023
1 parent fddc463 commit f71e2ee
Showing 1 changed file with 10 additions and 6 deletions.
16 changes: 10 additions & 6 deletions examples/jsm/loaders/PLYLoader.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;

}

Expand Down Expand Up @@ -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 ++ ] );
Expand All @@ -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 };

}

Expand All @@ -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' ) {

Expand Down

0 comments on commit f71e2ee

Please sign in to comment.