LegacyJSONLoader.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578
  1. /**
  2. * @author mrdoob / http://mrdoob.com/
  3. * @author alteredq / http://alteredqualia.com/
  4. */
  5. THREE.LegacyJSONLoader = ( function () {
  6. function LegacyJSONLoader( manager ) {
  7. if ( typeof manager === 'boolean' ) {
  8. console.warn( 'THREE.JSONLoader: showStatus parameter has been removed from constructor.' );
  9. manager = undefined;
  10. }
  11. this.manager = ( manager !== undefined ) ? manager : THREE.DefaultLoadingManager;
  12. this.withCredentials = false;
  13. }
  14. Object.assign( LegacyJSONLoader.prototype, {
  15. crossOrigin: 'anonymous',
  16. load: function ( url, onLoad, onProgress, onError ) {
  17. var scope = this;
  18. var path = ( this.path === undefined ) ? THREE.LoaderUtils.extractUrlBase( url ) : this.path;
  19. var loader = new THREE.FileLoader( this.manager );
  20. loader.setPath( this.path );
  21. loader.setWithCredentials( this.withCredentials );
  22. loader.load( url, function ( text ) {
  23. var json = JSON.parse( text );
  24. var metadata = json.metadata;
  25. if ( metadata !== undefined ) {
  26. var type = metadata.type;
  27. if ( type !== undefined ) {
  28. if ( type.toLowerCase() === 'object' ) {
  29. console.error( 'THREE.JSONLoader: ' + url + ' should be loaded with THREE.ObjectLoader instead.' );
  30. return;
  31. }
  32. }
  33. }
  34. var object = scope.parse( json, path );
  35. onLoad( object.geometry, object.materials );
  36. }, onProgress, onError );
  37. },
  38. setPath: function ( value ) {
  39. this.path = value;
  40. return this;
  41. },
  42. setResourcePath: function ( value ) {
  43. this.resourcePath = value;
  44. return this;
  45. },
  46. setCrossOrigin: function ( value ) {
  47. this.crossOrigin = value;
  48. return this;
  49. },
  50. parse: ( function () {
  51. function parseModel( json, geometry ) {
  52. function isBitSet( value, position ) {
  53. return value & ( 1 << position );
  54. }
  55. var i, j, fi,
  56. offset, zLength,
  57. colorIndex, normalIndex, uvIndex, materialIndex,
  58. type,
  59. isQuad,
  60. hasMaterial,
  61. hasFaceVertexUv,
  62. hasFaceNormal, hasFaceVertexNormal,
  63. hasFaceColor, hasFaceVertexColor,
  64. vertex, face, faceA, faceB, hex, normal,
  65. uvLayer, uv, u, v,
  66. faces = json.faces,
  67. vertices = json.vertices,
  68. normals = json.normals,
  69. colors = json.colors,
  70. scale = json.scale,
  71. nUvLayers = 0;
  72. if ( json.uvs !== undefined ) {
  73. // disregard empty arrays
  74. for ( i = 0; i < json.uvs.length; i ++ ) {
  75. if ( json.uvs[ i ].length ) nUvLayers ++;
  76. }
  77. for ( i = 0; i < nUvLayers; i ++ ) {
  78. geometry.faceVertexUvs[ i ] = [];
  79. }
  80. }
  81. offset = 0;
  82. zLength = vertices.length;
  83. while ( offset < zLength ) {
  84. vertex = new THREE.Vector3();
  85. vertex.x = vertices[ offset ++ ] * scale;
  86. vertex.y = vertices[ offset ++ ] * scale;
  87. vertex.z = vertices[ offset ++ ] * scale;
  88. geometry.vertices.push( vertex );
  89. }
  90. offset = 0;
  91. zLength = faces.length;
  92. while ( offset < zLength ) {
  93. type = faces[ offset ++ ];
  94. isQuad = isBitSet( type, 0 );
  95. hasMaterial = isBitSet( type, 1 );
  96. hasFaceVertexUv = isBitSet( type, 3 );
  97. hasFaceNormal = isBitSet( type, 4 );
  98. hasFaceVertexNormal = isBitSet( type, 5 );
  99. hasFaceColor = isBitSet( type, 6 );
  100. hasFaceVertexColor = isBitSet( type, 7 );
  101. // console.log("type", type, "bits", isQuad, hasMaterial, hasFaceVertexUv, hasFaceNormal, hasFaceVertexNormal, hasFaceColor, hasFaceVertexColor);
  102. if ( isQuad ) {
  103. faceA = new THREE.Face3();
  104. faceA.a = faces[ offset ];
  105. faceA.b = faces[ offset + 1 ];
  106. faceA.c = faces[ offset + 3 ];
  107. faceB = new THREE.Face3();
  108. faceB.a = faces[ offset + 1 ];
  109. faceB.b = faces[ offset + 2 ];
  110. faceB.c = faces[ offset + 3 ];
  111. offset += 4;
  112. if ( hasMaterial ) {
  113. materialIndex = faces[ offset ++ ];
  114. faceA.materialIndex = materialIndex;
  115. faceB.materialIndex = materialIndex;
  116. }
  117. // to get face <=> uv index correspondence
  118. fi = geometry.faces.length;
  119. if ( hasFaceVertexUv ) {
  120. for ( i = 0; i < nUvLayers; i ++ ) {
  121. uvLayer = json.uvs[ i ];
  122. geometry.faceVertexUvs[ i ][ fi ] = [];
  123. geometry.faceVertexUvs[ i ][ fi + 1 ] = [];
  124. for ( j = 0; j < 4; j ++ ) {
  125. uvIndex = faces[ offset ++ ];
  126. u = uvLayer[ uvIndex * 2 ];
  127. v = uvLayer[ uvIndex * 2 + 1 ];
  128. uv = new THREE.Vector2( u, v );
  129. if ( j !== 2 ) geometry.faceVertexUvs[ i ][ fi ].push( uv );
  130. if ( j !== 0 ) geometry.faceVertexUvs[ i ][ fi + 1 ].push( uv );
  131. }
  132. }
  133. }
  134. if ( hasFaceNormal ) {
  135. normalIndex = faces[ offset ++ ] * 3;
  136. faceA.normal.set(
  137. normals[ normalIndex ++ ],
  138. normals[ normalIndex ++ ],
  139. normals[ normalIndex ]
  140. );
  141. faceB.normal.copy( faceA.normal );
  142. }
  143. if ( hasFaceVertexNormal ) {
  144. for ( i = 0; i < 4; i ++ ) {
  145. normalIndex = faces[ offset ++ ] * 3;
  146. normal = new THREE.Vector3(
  147. normals[ normalIndex ++ ],
  148. normals[ normalIndex ++ ],
  149. normals[ normalIndex ]
  150. );
  151. if ( i !== 2 ) faceA.vertexNormals.push( normal );
  152. if ( i !== 0 ) faceB.vertexNormals.push( normal );
  153. }
  154. }
  155. if ( hasFaceColor ) {
  156. colorIndex = faces[ offset ++ ];
  157. hex = colors[ colorIndex ];
  158. faceA.color.setHex( hex );
  159. faceB.color.setHex( hex );
  160. }
  161. if ( hasFaceVertexColor ) {
  162. for ( i = 0; i < 4; i ++ ) {
  163. colorIndex = faces[ offset ++ ];
  164. hex = colors[ colorIndex ];
  165. if ( i !== 2 ) faceA.vertexColors.push( new THREE.Color( hex ) );
  166. if ( i !== 0 ) faceB.vertexColors.push( new THREE.Color( hex ) );
  167. }
  168. }
  169. geometry.faces.push( faceA );
  170. geometry.faces.push( faceB );
  171. } else {
  172. face = new THREE.Face3();
  173. face.a = faces[ offset ++ ];
  174. face.b = faces[ offset ++ ];
  175. face.c = faces[ offset ++ ];
  176. if ( hasMaterial ) {
  177. materialIndex = faces[ offset ++ ];
  178. face.materialIndex = materialIndex;
  179. }
  180. // to get face <=> uv index correspondence
  181. fi = geometry.faces.length;
  182. if ( hasFaceVertexUv ) {
  183. for ( i = 0; i < nUvLayers; i ++ ) {
  184. uvLayer = json.uvs[ i ];
  185. geometry.faceVertexUvs[ i ][ fi ] = [];
  186. for ( j = 0; j < 3; j ++ ) {
  187. uvIndex = faces[ offset ++ ];
  188. u = uvLayer[ uvIndex * 2 ];
  189. v = uvLayer[ uvIndex * 2 + 1 ];
  190. uv = new THREE.Vector2( u, v );
  191. geometry.faceVertexUvs[ i ][ fi ].push( uv );
  192. }
  193. }
  194. }
  195. if ( hasFaceNormal ) {
  196. normalIndex = faces[ offset ++ ] * 3;
  197. face.normal.set(
  198. normals[ normalIndex ++ ],
  199. normals[ normalIndex ++ ],
  200. normals[ normalIndex ]
  201. );
  202. }
  203. if ( hasFaceVertexNormal ) {
  204. for ( i = 0; i < 3; i ++ ) {
  205. normalIndex = faces[ offset ++ ] * 3;
  206. normal = new THREE.Vector3(
  207. normals[ normalIndex ++ ],
  208. normals[ normalIndex ++ ],
  209. normals[ normalIndex ]
  210. );
  211. face.vertexNormals.push( normal );
  212. }
  213. }
  214. if ( hasFaceColor ) {
  215. colorIndex = faces[ offset ++ ];
  216. face.color.setHex( colors[ colorIndex ] );
  217. }
  218. if ( hasFaceVertexColor ) {
  219. for ( i = 0; i < 3; i ++ ) {
  220. colorIndex = faces[ offset ++ ];
  221. face.vertexColors.push( new THREE.Color( colors[ colorIndex ] ) );
  222. }
  223. }
  224. geometry.faces.push( face );
  225. }
  226. }
  227. }
  228. function parseSkin( json, geometry ) {
  229. var influencesPerVertex = ( json.influencesPerVertex !== undefined ) ? json.influencesPerVertex : 2;
  230. if ( json.skinWeights ) {
  231. for ( var i = 0, l = json.skinWeights.length; i < l; i += influencesPerVertex ) {
  232. var x = json.skinWeights[ i ];
  233. var y = ( influencesPerVertex > 1 ) ? json.skinWeights[ i + 1 ] : 0;
  234. var z = ( influencesPerVertex > 2 ) ? json.skinWeights[ i + 2 ] : 0;
  235. var w = ( influencesPerVertex > 3 ) ? json.skinWeights[ i + 3 ] : 0;
  236. geometry.skinWeights.push( new THREE.Vector4( x, y, z, w ) );
  237. }
  238. }
  239. if ( json.skinIndices ) {
  240. for ( var i = 0, l = json.skinIndices.length; i < l; i += influencesPerVertex ) {
  241. var a = json.skinIndices[ i ];
  242. var b = ( influencesPerVertex > 1 ) ? json.skinIndices[ i + 1 ] : 0;
  243. var c = ( influencesPerVertex > 2 ) ? json.skinIndices[ i + 2 ] : 0;
  244. var d = ( influencesPerVertex > 3 ) ? json.skinIndices[ i + 3 ] : 0;
  245. geometry.skinIndices.push( new THREE.Vector4( a, b, c, d ) );
  246. }
  247. }
  248. geometry.bones = json.bones;
  249. if ( geometry.bones && geometry.bones.length > 0 && ( geometry.skinWeights.length !== geometry.skinIndices.length || geometry.skinIndices.length !== geometry.vertices.length ) ) {
  250. console.warn( 'When skinning, number of vertices (' + geometry.vertices.length + '), skinIndices (' +
  251. geometry.skinIndices.length + '), and skinWeights (' + geometry.skinWeights.length + ') should match.' );
  252. }
  253. }
  254. function parseMorphing( json, geometry ) {
  255. var scale = json.scale;
  256. if ( json.morphTargets !== undefined ) {
  257. for ( var i = 0, l = json.morphTargets.length; i < l; i ++ ) {
  258. geometry.morphTargets[ i ] = {};
  259. geometry.morphTargets[ i ].name = json.morphTargets[ i ].name;
  260. geometry.morphTargets[ i ].vertices = [];
  261. var dstVertices = geometry.morphTargets[ i ].vertices;
  262. var srcVertices = json.morphTargets[ i ].vertices;
  263. for ( var v = 0, vl = srcVertices.length; v < vl; v += 3 ) {
  264. var vertex = new THREE.Vector3();
  265. vertex.x = srcVertices[ v ] * scale;
  266. vertex.y = srcVertices[ v + 1 ] * scale;
  267. vertex.z = srcVertices[ v + 2 ] * scale;
  268. dstVertices.push( vertex );
  269. }
  270. }
  271. }
  272. if ( json.morphColors !== undefined && json.morphColors.length > 0 ) {
  273. console.warn( 'THREE.JSONLoader: "morphColors" no longer supported. Using them as face colors.' );
  274. var faces = geometry.faces;
  275. var morphColors = json.morphColors[ 0 ].colors;
  276. for ( var i = 0, l = faces.length; i < l; i ++ ) {
  277. faces[ i ].color.fromArray( morphColors, i * 3 );
  278. }
  279. }
  280. }
  281. function parseAnimations( json, geometry ) {
  282. var outputAnimations = [];
  283. // parse old style Bone/Hierarchy animations
  284. var animations = [];
  285. if ( json.animation !== undefined ) {
  286. animations.push( json.animation );
  287. }
  288. if ( json.animations !== undefined ) {
  289. if ( json.animations.length ) {
  290. animations = animations.concat( json.animations );
  291. } else {
  292. animations.push( json.animations );
  293. }
  294. }
  295. for ( var i = 0; i < animations.length; i ++ ) {
  296. var clip = THREE.AnimationClip.parseAnimation( animations[ i ], geometry.bones );
  297. if ( clip ) outputAnimations.push( clip );
  298. }
  299. // parse implicit morph animations
  300. if ( geometry.morphTargets ) {
  301. // TODO: Figure out what an appropraite FPS is for morph target animations -- defaulting to 10, but really it is completely arbitrary.
  302. var morphAnimationClips = THREE.AnimationClip.CreateClipsFromMorphTargetSequences( geometry.morphTargets, 10 );
  303. outputAnimations = outputAnimations.concat( morphAnimationClips );
  304. }
  305. if ( outputAnimations.length > 0 ) geometry.animations = outputAnimations;
  306. }
  307. return function parse( json, path ) {
  308. if ( json.data !== undefined ) {
  309. // Geometry 4.0 spec
  310. json = json.data;
  311. }
  312. if ( json.scale !== undefined ) {
  313. json.scale = 1.0 / json.scale;
  314. } else {
  315. json.scale = 1.0;
  316. }
  317. var geometry = new THREE.Geometry();
  318. parseModel( json, geometry );
  319. parseSkin( json, geometry );
  320. parseMorphing( json, geometry );
  321. parseAnimations( json, geometry );
  322. geometry.computeFaceNormals();
  323. geometry.computeBoundingSphere();
  324. if ( json.materials === undefined || json.materials.length === 0 ) {
  325. return { geometry: geometry };
  326. } else {
  327. var materials = THREE.Loader.prototype.initMaterials( json.materials, this.resourcePath || path, this.crossOrigin );
  328. return { geometry: geometry, materials: materials };
  329. }
  330. };
  331. } )()
  332. } );
  333. return LegacyJSONLoader;
  334. } )();