|
@@ -33,139 +33,160 @@ THREE.STLLoader.prototype = {
|
|
hasEventListener: THREE.EventDispatcher.prototype.hasEventListener,
|
|
hasEventListener: THREE.EventDispatcher.prototype.hasEventListener,
|
|
removeEventListener: THREE.EventDispatcher.prototype.removeEventListener,
|
|
removeEventListener: THREE.EventDispatcher.prototype.removeEventListener,
|
|
dispatchEvent: THREE.EventDispatcher.prototype.dispatchEvent
|
|
dispatchEvent: THREE.EventDispatcher.prototype.dispatchEvent
|
|
|
|
+
|
|
};
|
|
};
|
|
|
|
|
|
THREE.STLLoader.prototype.load = function (url) {
|
|
THREE.STLLoader.prototype.load = function (url) {
|
|
- var scope = this;
|
|
|
|
- console.log("Attempting to load URL: [" + url + "]");
|
|
|
|
-
|
|
|
|
- var xhr = new XMLHttpRequest();
|
|
|
|
-
|
|
|
|
- function onloaded( event ) {
|
|
|
|
-
|
|
|
|
- if ( event.target.status === 200 || event.target.status === 0 ) {
|
|
|
|
- var data = event.target.responseText;
|
|
|
|
- return scope.dispatchEvent({
|
|
|
|
- type: 'load',
|
|
|
|
- content: scope.parse(data)
|
|
|
|
- });
|
|
|
|
- } else {
|
|
|
|
-
|
|
|
|
- scope.dispatchEvent( { type: 'error', message: 'Couldn\'t load URL [' + url + ']',
|
|
|
|
- response: event.target.responseText } );
|
|
|
|
-
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- xhr.addEventListener( 'load', onloaded, false );
|
|
|
|
-
|
|
|
|
- xhr.addEventListener( 'progress', function ( event ) {
|
|
|
|
-
|
|
|
|
- scope.dispatchEvent( { type: 'progress', loaded: event.loaded, total: event.total } );
|
|
|
|
-
|
|
|
|
- }, false );
|
|
|
|
-
|
|
|
|
- xhr.addEventListener( 'error', function () {
|
|
|
|
-
|
|
|
|
- scope.dispatchEvent( { type: 'error', message: 'Couldn\'t load URL [' + url + ']' } );
|
|
|
|
-
|
|
|
|
- }, false );
|
|
|
|
-
|
|
|
|
- xhr.overrideMimeType('text/plain; charset=x-user-defined');
|
|
|
|
- xhr.open( 'GET', url, true );
|
|
|
|
- xhr.send( null );
|
|
|
|
|
|
+
|
|
|
|
+ var scope = this;
|
|
|
|
+ console.log("Attempting to load URL: [" + url + "]");
|
|
|
|
+
|
|
|
|
+ var xhr = new XMLHttpRequest();
|
|
|
|
+
|
|
|
|
+ function onloaded( event ) {
|
|
|
|
+
|
|
|
|
+ if ( event.target.status === 200 || event.target.status === 0 ) {
|
|
|
|
+ var data = event.target.responseText;
|
|
|
|
+ return scope.dispatchEvent({
|
|
|
|
+ type: 'load',
|
|
|
|
+ content: scope.parse(data)
|
|
|
|
+ });
|
|
|
|
+ } else {
|
|
|
|
+
|
|
|
|
+ scope.dispatchEvent( { type: 'error', message: 'Couldn\'t load URL [' + url + ']',
|
|
|
|
+ response: event.target.responseText } );
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ xhr.addEventListener( 'load', onloaded, false );
|
|
|
|
+
|
|
|
|
+ xhr.addEventListener( 'progress', function ( event ) {
|
|
|
|
+
|
|
|
|
+ scope.dispatchEvent( { type: 'progress', loaded: event.loaded, total: event.total } );
|
|
|
|
+
|
|
|
|
+ }, false );
|
|
|
|
+
|
|
|
|
+ xhr.addEventListener( 'error', function () {
|
|
|
|
+
|
|
|
|
+ scope.dispatchEvent( { type: 'error', message: 'Couldn\'t load URL [' + url + ']' } );
|
|
|
|
+
|
|
|
|
+ }, false );
|
|
|
|
+
|
|
|
|
+ xhr.overrideMimeType('text/plain; charset=x-user-defined');
|
|
|
|
+ xhr.open( 'GET', url, true );
|
|
|
|
+ xhr.send( null );
|
|
|
|
+
|
|
};
|
|
};
|
|
|
|
|
|
THREE.STLLoader.prototype.parse = function (data) {
|
|
THREE.STLLoader.prototype.parse = function (data) {
|
|
- var isBinary,
|
|
|
|
- _this = this;
|
|
|
|
- isBinary = function (data) {
|
|
|
|
- var expect, face_size, n_faces, reader;
|
|
|
|
- reader = new THREE.STLLoader.BinaryReader(data);
|
|
|
|
- reader.seek(80);
|
|
|
|
- face_size = (32 / 8 * 3) + ((32 / 8 * 3) * 3) + (16 / 8);
|
|
|
|
- n_faces = reader.readUInt32();
|
|
|
|
- expect = 80 + (32 / 8) + (n_faces * face_size);
|
|
|
|
- return expect === reader.getSize();
|
|
|
|
- };
|
|
|
|
- if (isBinary(data)) {
|
|
|
|
- return this.parseBinary(data);
|
|
|
|
- } else {
|
|
|
|
- return this.parseASCII(data);
|
|
|
|
- }
|
|
|
|
|
|
+
|
|
|
|
+ var isBinary = function (data) {
|
|
|
|
+
|
|
|
|
+ var expect, face_size, n_faces, reader;
|
|
|
|
+ reader = new THREE.STLLoader.BinaryReader(data);
|
|
|
|
+ reader.seek(80);
|
|
|
|
+ face_size = (32 / 8 * 3) + ((32 / 8 * 3) * 3) + (16 / 8);
|
|
|
|
+ n_faces = reader.readUInt32();
|
|
|
|
+ expect = 80 + (32 / 8) + (n_faces * face_size);
|
|
|
|
+ return expect === reader.getSize();
|
|
|
|
+
|
|
|
|
+ };
|
|
|
|
+
|
|
|
|
+ if (isBinary(data)) {
|
|
|
|
+
|
|
|
|
+ return this.parseBinary(data);
|
|
|
|
+
|
|
|
|
+ } else {
|
|
|
|
+
|
|
|
|
+ return this.parseASCII(data);
|
|
|
|
+
|
|
|
|
+ }
|
|
};
|
|
};
|
|
|
|
|
|
THREE.STLLoader.prototype.parseBinary = function (data) {
|
|
THREE.STLLoader.prototype.parseBinary = function (data) {
|
|
- var face, geometry, n_faces, reader, length, normal, i;
|
|
|
|
-
|
|
|
|
- reader = new THREE.STLLoader.BinaryReader(data);
|
|
|
|
- reader.seek(80);
|
|
|
|
- n_faces = reader.readUInt32();
|
|
|
|
- geometry = new THREE.Geometry();
|
|
|
|
-
|
|
|
|
- for (face = 0; face < n_faces; face++) {
|
|
|
|
-
|
|
|
|
- normal = new THREE.Vector3(reader.readFloat(),reader.readFloat(),reader.readFloat());
|
|
|
|
-
|
|
|
|
- for (i = 1; i <= 3; i++) {
|
|
|
|
-
|
|
|
|
- geometry.vertices.push(new THREE.Vector3(reader.readFloat(),reader.readFloat(),reader.readFloat()));
|
|
|
|
-
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- reader.readUInt16(); // attr doesn't get used yet.
|
|
|
|
- length = geometry.vertices.length;
|
|
|
|
- geometry.faces.push(new THREE.Face3(length - 3, length - 2, length - 1, normal));
|
|
|
|
-
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- geometry.computeCentroids();
|
|
|
|
- geometry.computeBoundingBox();
|
|
|
|
- geometry.computeBoundingSphere();
|
|
|
|
-
|
|
|
|
- return geometry;
|
|
|
|
|
|
+
|
|
|
|
+ var face, geometry, n_faces, reader, length, normal, i;
|
|
|
|
+
|
|
|
|
+ reader = new THREE.STLLoader.BinaryReader(data);
|
|
|
|
+ reader.seek(80);
|
|
|
|
+ n_faces = reader.readUInt32();
|
|
|
|
+ geometry = new THREE.Geometry();
|
|
|
|
+
|
|
|
|
+ for (face = 0; face < n_faces; face++) {
|
|
|
|
+
|
|
|
|
+ normal = new THREE.Vector3(reader.readFloat(),reader.readFloat(),reader.readFloat());
|
|
|
|
+
|
|
|
|
+ for (i = 1; i <= 3; i++) {
|
|
|
|
+
|
|
|
|
+ geometry.vertices.push(new THREE.Vector3(reader.readFloat(),reader.readFloat(),reader.readFloat()));
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ reader.readUInt16(); // attr doesn't get used yet.
|
|
|
|
+ length = geometry.vertices.length;
|
|
|
|
+ geometry.faces.push(new THREE.Face3(length - 3, length - 2, length - 1, normal));
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ geometry.computeCentroids();
|
|
|
|
+ geometry.computeBoundingBox();
|
|
|
|
+ geometry.computeBoundingSphere();
|
|
|
|
+
|
|
|
|
+ return geometry;
|
|
|
|
+
|
|
};
|
|
};
|
|
|
|
|
|
THREE.STLLoader.prototype.parseASCII = function (data) {
|
|
THREE.STLLoader.prototype.parseASCII = function (data) {
|
|
- var geometry, length, normal, patternFace, patternNormal, patternVertex, result, text;
|
|
|
|
- geometry = new THREE.Geometry();
|
|
|
|
- patternFace = /facet([\s\S]*?)endfacet/g;
|
|
|
|
-
|
|
|
|
- while (((result = patternFace.exec(data)) != null)) {
|
|
|
|
-
|
|
|
|
- text = result[0];
|
|
|
|
- patternNormal = /normal[\s]+([\-+]?[0-9]+\.?[0-9]*([eE][\-+]?[0-9]+)?)+[\s]+([\-+]?[0-9]*\.?[0-9]+([eE][\-+]?[0-9]+)?)+[\s]+([\-+]?[0-9]*\.?[0-9]+([eE][\-+]?[0-9]+)?)+/g;
|
|
|
|
- while (((result = patternNormal.exec(text)) != null)) {
|
|
|
|
- normal = new THREE.Vector3(parseFloat(result[1]), parseFloat(result[3]), parseFloat(result[5]));
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- patternVertex = /vertex[\s]+([\-+]?[0-9]+\.?[0-9]*([eE][\-+]?[0-9]+)?)+[\s]+([\-+]?[0-9]*\.?[0-9]+([eE][\-+]?[0-9]+)?)+[\s]+([\-+]?[0-9]*\.?[0-9]+([eE][\-+]?[0-9]+)?)+/g;
|
|
|
|
- while (((result = patternVertex.exec(text)) != null)) {
|
|
|
|
- geometry.vertices.push(new THREE.Vector3(parseFloat(result[1]), parseFloat(result[3]), parseFloat(result[5])));
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- length = geometry.vertices.length;
|
|
|
|
- geometry.faces.push(new THREE.Face3(length - 3, length - 2, length - 1, normal));
|
|
|
|
-
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- geometry.computeCentroids();
|
|
|
|
- geometry.computeBoundingBox();
|
|
|
|
- geometry.computeBoundingSphere();
|
|
|
|
-
|
|
|
|
- return geometry;
|
|
|
|
|
|
+
|
|
|
|
+ var geometry, length, normal, patternFace, patternNormal, patternVertex, result, text;
|
|
|
|
+ geometry = new THREE.Geometry();
|
|
|
|
+ patternFace = /facet([\s\S]*?)endfacet/g;
|
|
|
|
+
|
|
|
|
+ while (((result = patternFace.exec(data)) != null)) {
|
|
|
|
+
|
|
|
|
+ text = result[0];
|
|
|
|
+ patternNormal = /normal[\s]+([\-+]?[0-9]+\.?[0-9]*([eE][\-+]?[0-9]+)?)+[\s]+([\-+]?[0-9]*\.?[0-9]+([eE][\-+]?[0-9]+)?)+[\s]+([\-+]?[0-9]*\.?[0-9]+([eE][\-+]?[0-9]+)?)+/g;
|
|
|
|
+
|
|
|
|
+ while (((result = patternNormal.exec(text)) != null)) {
|
|
|
|
+
|
|
|
|
+ normal = new THREE.Vector3(parseFloat(result[1]), parseFloat(result[3]), parseFloat(result[5]));
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ patternVertex = /vertex[\s]+([\-+]?[0-9]+\.?[0-9]*([eE][\-+]?[0-9]+)?)+[\s]+([\-+]?[0-9]*\.?[0-9]+([eE][\-+]?[0-9]+)?)+[\s]+([\-+]?[0-9]*\.?[0-9]+([eE][\-+]?[0-9]+)?)+/g;
|
|
|
|
+
|
|
|
|
+ while (((result = patternVertex.exec(text)) != null)) {
|
|
|
|
+
|
|
|
|
+ geometry.vertices.push(new THREE.Vector3(parseFloat(result[1]), parseFloat(result[3]), parseFloat(result[5])));
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ length = geometry.vertices.length;
|
|
|
|
+ geometry.faces.push(new THREE.Face3(length - 3, length - 2, length - 1, normal));
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ geometry.computeCentroids();
|
|
|
|
+ geometry.computeBoundingBox();
|
|
|
|
+ geometry.computeBoundingSphere();
|
|
|
|
+
|
|
|
|
+ return geometry;
|
|
|
|
+
|
|
};
|
|
};
|
|
|
|
|
|
|
|
|
|
THREE.STLLoader.BinaryReader = function (data) {
|
|
THREE.STLLoader.BinaryReader = function (data) {
|
|
- this._buffer = data;
|
|
|
|
- this._pos = 0;
|
|
|
|
|
|
+
|
|
|
|
+ this._buffer = data;
|
|
|
|
+ this._pos = 0;
|
|
|
|
+
|
|
};
|
|
};
|
|
|
|
|
|
THREE.STLLoader.BinaryReader.prototype = {
|
|
THREE.STLLoader.BinaryReader.prototype = {
|
|
|
|
|
|
- /* Public */
|
|
|
|
|
|
+ /* Public */
|
|
|
|
|
|
readInt8: function (){ return this._decodeInt(8, true); },
|
|
readInt8: function (){ return this._decodeInt(8, true); },
|
|
readUInt8: function (){ return this._decodeInt(8, false); },
|
|
readUInt8: function (){ return this._decodeInt(8, false); },
|
|
@@ -178,32 +199,43 @@ THREE.STLLoader.BinaryReader.prototype = {
|
|
readDouble: function (){ return this._decodeFloat(52, 11); },
|
|
readDouble: function (){ return this._decodeFloat(52, 11); },
|
|
|
|
|
|
readChar: function () { return this.readString(1); },
|
|
readChar: function () { return this.readString(1); },
|
|
|
|
+
|
|
readString: function (length) {
|
|
readString: function (length) {
|
|
|
|
+
|
|
this._checkSize(length * 8);
|
|
this._checkSize(length * 8);
|
|
var result = this._buffer.substr(this._pos, length);
|
|
var result = this._buffer.substr(this._pos, length);
|
|
this._pos += length;
|
|
this._pos += length;
|
|
return result;
|
|
return result;
|
|
|
|
+
|
|
},
|
|
},
|
|
|
|
|
|
seek: function (pos) {
|
|
seek: function (pos) {
|
|
|
|
+
|
|
this._pos = pos;
|
|
this._pos = pos;
|
|
this._checkSize(0);
|
|
this._checkSize(0);
|
|
|
|
+
|
|
},
|
|
},
|
|
|
|
|
|
getPosition: function () {
|
|
getPosition: function () {
|
|
|
|
+
|
|
return this._pos;
|
|
return this._pos;
|
|
|
|
+
|
|
},
|
|
},
|
|
|
|
|
|
getSize: function () {
|
|
getSize: function () {
|
|
|
|
+
|
|
return this._buffer.length;
|
|
return this._buffer.length;
|
|
|
|
+
|
|
},
|
|
},
|
|
|
|
|
|
|
|
|
|
/* Private */
|
|
/* Private */
|
|
|
|
|
|
_decodeFloat: function(precisionBits, exponentBits){
|
|
_decodeFloat: function(precisionBits, exponentBits){
|
|
|
|
+
|
|
var length = precisionBits + exponentBits + 1;
|
|
var length = precisionBits + exponentBits + 1;
|
|
var size = length >> 3;
|
|
var size = length >> 3;
|
|
|
|
+
|
|
this._checkSize(length);
|
|
this._checkSize(length);
|
|
|
|
|
|
var bias = Math.pow(2, exponentBits - 1) - 1;
|
|
var bias = Math.pow(2, exponentBits - 1) - 1;
|
|
@@ -211,7 +243,7 @@ THREE.STLLoader.BinaryReader.prototype = {
|
|
var exponent = this._readBits(precisionBits, exponentBits, size);
|
|
var exponent = this._readBits(precisionBits, exponentBits, size);
|
|
var significand = 0;
|
|
var significand = 0;
|
|
var divisor = 2;
|
|
var divisor = 2;
|
|
- // var curByte = length + (-precisionBits >> 3) - 1;
|
|
|
|
|
|
+ // var curByte = length + (-precisionBits >> 3) - 1;
|
|
var curByte = 0;
|
|
var curByte = 0;
|
|
do {
|
|
do {
|
|
var byteValue = this._readByte(++curByte, size);
|
|
var byteValue = this._readByte(++curByte, size);
|
|
@@ -230,27 +262,35 @@ THREE.STLLoader.BinaryReader.prototype = {
|
|
return exponent == (bias << 1) + 1 ? significand ? NaN : signal ? -Infinity : +Infinity
|
|
return exponent == (bias << 1) + 1 ? significand ? NaN : signal ? -Infinity : +Infinity
|
|
: (1 + signal * -2) * (exponent || significand ? !exponent ? Math.pow(2, -bias + 1) * significand
|
|
: (1 + signal * -2) * (exponent || significand ? !exponent ? Math.pow(2, -bias + 1) * significand
|
|
: Math.pow(2, exponent - bias) * (1 + significand) : 0);
|
|
: Math.pow(2, exponent - bias) * (1 + significand) : 0);
|
|
|
|
+
|
|
},
|
|
},
|
|
|
|
|
|
_decodeInt: function(bits, signed){
|
|
_decodeInt: function(bits, signed){
|
|
|
|
+
|
|
var x = this._readBits(0, bits, bits / 8), max = Math.pow(2, bits);
|
|
var x = this._readBits(0, bits, bits / 8), max = Math.pow(2, bits);
|
|
var result = signed && x >= max / 2 ? x - max : x;
|
|
var result = signed && x >= max / 2 ? x - max : x;
|
|
|
|
|
|
this._pos += bits / 8;
|
|
this._pos += bits / 8;
|
|
return result;
|
|
return result;
|
|
|
|
+
|
|
},
|
|
},
|
|
|
|
|
|
//shl fix: Henri Torgemane ~1996 (compressed by Jonas Raoni)
|
|
//shl fix: Henri Torgemane ~1996 (compressed by Jonas Raoni)
|
|
_shl: function (a, b){
|
|
_shl: function (a, b){
|
|
|
|
+
|
|
for (++b; --b; a = ((a %= 0x7fffffff + 1) & 0x40000000) == 0x40000000 ? a * 2 : (a - 0x40000000) * 2 + 0x7fffffff + 1);
|
|
for (++b; --b; a = ((a %= 0x7fffffff + 1) & 0x40000000) == 0x40000000 ? a * 2 : (a - 0x40000000) * 2 + 0x7fffffff + 1);
|
|
return a;
|
|
return a;
|
|
|
|
+
|
|
},
|
|
},
|
|
|
|
|
|
_readByte: function (i, size) {
|
|
_readByte: function (i, size) {
|
|
|
|
+
|
|
return this._buffer.charCodeAt(this._pos + size - i - 1) & 0xff;
|
|
return this._buffer.charCodeAt(this._pos + size - i - 1) & 0xff;
|
|
|
|
+
|
|
},
|
|
},
|
|
|
|
|
|
_readBits: function (start, length, size) {
|
|
_readBits: function (start, length, size) {
|
|
|
|
+
|
|
var offsetLeft = (start + length) % 8;
|
|
var offsetLeft = (start + length) % 8;
|
|
var offsetRight = start % 8;
|
|
var offsetRight = start % 8;
|
|
var curByte = size - (start >> 3) - 1;
|
|
var curByte = size - (start >> 3) - 1;
|
|
@@ -260,19 +300,29 @@ THREE.STLLoader.BinaryReader.prototype = {
|
|
var sum = (this._readByte(curByte, size) >> offsetRight) & ((1 << (diff ? 8 - offsetRight : length)) - 1);
|
|
var sum = (this._readByte(curByte, size) >> offsetRight) & ((1 << (diff ? 8 - offsetRight : length)) - 1);
|
|
|
|
|
|
if (diff && offsetLeft) {
|
|
if (diff && offsetLeft) {
|
|
|
|
+
|
|
sum += (this._readByte(lastByte++, size) & ((1 << offsetLeft) - 1)) << (diff-- << 3) - offsetRight;
|
|
sum += (this._readByte(lastByte++, size) & ((1 << offsetLeft) - 1)) << (diff-- << 3) - offsetRight;
|
|
|
|
+
|
|
}
|
|
}
|
|
|
|
|
|
while (diff) {
|
|
while (diff) {
|
|
|
|
+
|
|
sum += this._shl(this._readByte(lastByte++, size), (diff-- << 3) - offsetRight);
|
|
sum += this._shl(this._readByte(lastByte++, size), (diff-- << 3) - offsetRight);
|
|
|
|
+
|
|
}
|
|
}
|
|
|
|
|
|
return sum;
|
|
return sum;
|
|
|
|
+
|
|
},
|
|
},
|
|
|
|
|
|
_checkSize: function (neededBits) {
|
|
_checkSize: function (neededBits) {
|
|
|
|
+
|
|
if (!(this._pos + Math.ceil(neededBits / 8) < this._buffer.length)) {
|
|
if (!(this._pos + Math.ceil(neededBits / 8) < this._buffer.length)) {
|
|
|
|
+
|
|
throw new Error("Index out of bound");
|
|
throw new Error("Index out of bound");
|
|
|
|
+
|
|
}
|
|
}
|
|
|
|
+
|
|
}
|
|
}
|
|
-};
|
|
|
|
|
|
+
|
|
|
|
+};
|