Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 6 commits into from
Jun 12, 2023
Merged
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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 ) 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