Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 24 additions & 7 deletions examples/js/loaders/STLLoader.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;

}

Expand Down