UTF8Loader.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. /**
  2. * Loader for UTF8 encoded models generated by:
  3. * http://code.google.com/p/webgl-loader/
  4. *
  5. * Limitations:
  6. * - number of vertices < 65536 (this is after optimizations in compressor, input OBJ may have even less)
  7. * - models must have normals and texture coordinates
  8. * - texture coordinates must be only from <0,1>
  9. * - no materials support yet
  10. * - models are scaled and offset (copy numbers from compressor and use them as parameters in UTF8Loader.load() )
  11. *
  12. * @author alteredq / http://alteredqualia.com/
  13. * @author won3d / http://twitter.com/won3d
  14. */
  15. THREE.UTF8Loader = function () {};
  16. THREE.UTF8Loader.prototype.load = function ( url, callback, metadata ) {
  17. var xhr = new XMLHttpRequest(),
  18. callbackProgress = null,
  19. scale = metadata.scale !== undefined ? metadata.scale : 1,
  20. offsetX = metadata.offsetX !== undefined ? metadata.offsetX : 0,
  21. offsetY = metadata.offsetY !== undefined ? metadata.offsetY : 0,
  22. offsetZ = metadata.offsetZ !== undefined ? metadata.offsetZ : 0;
  23. var length = 0;
  24. xhr.onreadystatechange = function() {
  25. if ( xhr.readyState == 4 ) {
  26. if ( xhr.status == 200 || xhr.status == 0 ) {
  27. THREE.UTF8Loader.prototype.createModel( xhr.responseText, callback, scale, offsetX, offsetY, offsetZ );
  28. } else {
  29. console.error( "THREE.UTF8Loader: Couldn't load [" + url + "] [" + xhr.status + "]" );
  30. }
  31. } else if ( xhr.readyState == 3 ) {
  32. if ( callbackProgress ) {
  33. if ( length == 0 ) {
  34. length = xhr.getResponseHeader( "Content-Length" );
  35. }
  36. callbackProgress( { total: length, loaded: xhr.responseText.length } );
  37. }
  38. } else if ( xhr.readyState == 2 ) {
  39. length = xhr.getResponseHeader( "Content-Length" );
  40. }
  41. }
  42. xhr.open( "GET", url, true );
  43. xhr.send( null );
  44. };
  45. // UTF-8 decoder from webgl-loader
  46. // http://code.google.com/p/webgl-loader/
  47. // Copyright 2011 Google Inc. All Rights Reserved.
  48. //
  49. // Licensed under the Apache License, Version 2.0 (the "License"); you
  50. // may not use this file except in compliance with the License. You
  51. // may obtain a copy of the License at
  52. //
  53. // http://www.apache.org/licenses/LICENSE-2.0
  54. //
  55. // Unless required by applicable law or agreed to in writing, software
  56. // distributed under the License is distributed on an "AS IS" BASIS,
  57. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
  58. // implied. See the License for the specific language governing
  59. // permissions and limitations under the License.
  60. THREE.UTF8Loader.prototype.decompressMesh = function ( str ) {
  61. var num_verts = str.charCodeAt( 0 );
  62. if ( num_verts >= 0xE000 ) {
  63. num_verts -= 0x0800;
  64. }
  65. num_verts ++;
  66. var attribs_out = new Float32Array( 8 * num_verts );
  67. var offset = 1;
  68. for ( var i = 0; i < 8; i ++ ) {
  69. var prev_attrib = 0;
  70. for ( var j = 0; j < num_verts; ++ j ) {
  71. var code = str.charCodeAt( j + offset );
  72. prev_attrib += ( code >> 1 ) ^ ( - ( code & 1 ) );
  73. attribs_out[ 8 * j + i ] = prev_attrib;
  74. }
  75. offset += num_verts;
  76. }
  77. var num_indices = str.length - offset;
  78. var indices_out = new Uint16Array( num_indices );
  79. var index_high_water_mark = 0;
  80. for ( var i = 0; i < num_indices; i ++ ) {
  81. var code = str.charCodeAt( i + offset );
  82. indices_out[ i ] = index_high_water_mark - code;
  83. if ( code == 0 ) {
  84. index_high_water_mark ++;
  85. }
  86. }
  87. return [ attribs_out, indices_out ];
  88. };
  89. THREE.UTF8Loader.prototype.createModel = function ( data, callback, scale, offsetX, offsetY, offsetZ ) {
  90. var Model = function ( texture_path ) {
  91. //var s = (new Date).getTime();
  92. var scope = this;
  93. scope.materials = [];
  94. THREE.Geometry.call( this );
  95. var buffers = THREE.UTF8Loader.prototype.decompressMesh( data );
  96. var normals = [],
  97. uvs = [];
  98. init_vertices( buffers[ 0 ], 8, 0 );
  99. init_uvs( buffers[ 0 ], 8, 3 );
  100. init_normals( buffers[ 0 ], 8, 5 );
  101. init_faces( buffers[ 1 ] );
  102. this.computeCentroids();
  103. this.computeFaceNormals();
  104. //this.computeTangents();
  105. //var e = (new Date).getTime();
  106. //console.log( "utf8 data parse time: " + (e-s) + " ms" );
  107. function init_vertices( data, stride, offset ) {
  108. var i, x, y, z,
  109. end = data.length;
  110. for( i = offset; i < end; i += stride ) {
  111. x = data[ i ];
  112. y = data[ i + 1 ];
  113. z = data[ i + 2 ];
  114. // fix scale and offsets
  115. x = ( x / 16383 ) * scale;
  116. y = ( y / 16383 ) * scale;
  117. z = ( z / 16383 ) * scale;
  118. x += offsetX;
  119. y += offsetY;
  120. z += offsetZ;
  121. vertex( scope, x, y, z );
  122. }
  123. };
  124. function init_normals( data, stride, offset ) {
  125. var i, x, y, z,
  126. end = data.length;
  127. for( i = offset; i < end; i += stride ) {
  128. x = data[ i ];
  129. y = data[ i + 1 ];
  130. z = data[ i + 2 ];
  131. // normalize to <-1,1>
  132. x = ( x - 512 ) / 511;
  133. y = ( y - 512 ) / 511;
  134. z = ( z - 512 ) / 511;
  135. normals.push( x, y, z );
  136. }
  137. };
  138. function init_uvs( data, stride, offset ) {
  139. var i, u, v,
  140. end = data.length;
  141. for( i = offset; i < end; i += stride ) {
  142. u = data[ i ];
  143. v = data[ i + 1 ];
  144. // normalize to <0,1>
  145. u /= 1023;
  146. v /= 1023;
  147. uvs.push( u, 1 - v );
  148. }
  149. };
  150. function init_faces( indices ) {
  151. var i,
  152. a, b, c,
  153. u1, v1, u2, v2, u3, v3,
  154. m,
  155. end = indices.length;
  156. m = 0; // all faces defaulting to material 0
  157. for( i = 0; i < end; i += 3 ) {
  158. a = indices[ i ];
  159. b = indices[ i + 1 ];
  160. c = indices[ i + 2 ];
  161. f3n( scope, normals, a, b, c, m, a, b, c );
  162. u1 = uvs[ a * 2 ];
  163. v1 = uvs[ a * 2 + 1 ];
  164. u2 = uvs[ b * 2 ];
  165. v2 = uvs[ b * 2 + 1 ];
  166. u3 = uvs[ c * 2 ];
  167. v3 = uvs[ c * 2 + 1 ];
  168. uv3( scope.faceVertexUvs[ 0 ], u1, v1, u2, v2, u3, v3 );
  169. }
  170. }
  171. };
  172. function vertex ( scope, x, y, z ) {
  173. scope.vertices.push( new THREE.Vector3( x, y, z ) );
  174. };
  175. function f3n ( scope, normals, a, b, c, mi, nai, nbi, nci ) {
  176. var nax = normals[ nai * 3 ],
  177. nay = normals[ nai * 3 + 1 ],
  178. naz = normals[ nai * 3 + 2 ],
  179. nbx = normals[ nbi * 3 ],
  180. nby = normals[ nbi * 3 + 1 ],
  181. nbz = normals[ nbi * 3 + 2 ],
  182. ncx = normals[ nci * 3 ],
  183. ncy = normals[ nci * 3 + 1 ],
  184. ncz = normals[ nci * 3 + 2 ];
  185. var na = new THREE.Vector3( nax, nay, naz ),
  186. nb = new THREE.Vector3( nbx, nby, nbz ),
  187. nc = new THREE.Vector3( ncx, ncy, ncz );
  188. scope.faces.push( new THREE.Face3( a, b, c, [ na, nb, nc ], null, mi ) );
  189. };
  190. function uv3 ( where, u1, v1, u2, v2, u3, v3 ) {
  191. var uv = [];
  192. uv.push( new THREE.UV( u1, v1 ) );
  193. uv.push( new THREE.UV( u2, v2 ) );
  194. uv.push( new THREE.UV( u3, v3 ) );
  195. where.push( uv );
  196. };
  197. Model.prototype = Object.create( THREE.Geometry.prototype );
  198. callback( new Model() );
  199. };