SceneLoader.js 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164
  1. /**
  2. * @author alteredq / http://alteredqualia.com/
  3. */
  4. THREE.SceneLoader = function ( manager ) {
  5. this.onLoadStart = function () {};
  6. this.onLoadProgress = function() {};
  7. this.onLoadComplete = function () {};
  8. this.callbackSync = function () {};
  9. this.callbackProgress = function () {};
  10. this.geometryHandlers = {};
  11. this.hierarchyHandlers = {};
  12. this.addGeometryHandler( "ascii", THREE.JSONLoader );
  13. this.manager = ( manager !== undefined ) ? manager : THREE.DefaultLoadingManager;
  14. };
  15. THREE.SceneLoader.prototype = {
  16. constructor: THREE.SceneLoader,
  17. load: function ( url, onLoad, onProgress, onError ) {
  18. var scope = this;
  19. var loader = new THREE.XHRLoader( scope.manager );
  20. loader.setCrossOrigin( this.crossOrigin );
  21. loader.load( url, function ( text ) {
  22. scope.parse( JSON.parse( text ), onLoad, url );
  23. }, onProgress, onError );
  24. },
  25. setCrossOrigin: function ( value ) {
  26. this.crossOrigin = value;
  27. },
  28. addGeometryHandler: function ( typeID, loaderClass ) {
  29. this.geometryHandlers[ typeID ] = { "loaderClass": loaderClass };
  30. },
  31. addHierarchyHandler: function ( typeID, loaderClass ) {
  32. this.hierarchyHandlers[ typeID ] = { "loaderClass": loaderClass };
  33. },
  34. parse: function ( json, callbackFinished, url ) {
  35. var scope = this;
  36. var urlBase = THREE.Loader.prototype.extractUrlBase( url );
  37. var geometry, material, camera, fog,
  38. texture, images, color,
  39. light, hex, intensity,
  40. counter_models, counter_textures,
  41. total_models, total_textures,
  42. result;
  43. var target_array = [];
  44. var data = json;
  45. // async geometry loaders
  46. for ( var typeID in this.geometryHandlers ) {
  47. var loaderClass = this.geometryHandlers[ typeID ][ "loaderClass" ];
  48. this.geometryHandlers[ typeID ][ "loaderObject" ] = new loaderClass();
  49. }
  50. // async hierachy loaders
  51. for ( var typeID in this.hierarchyHandlers ) {
  52. var loaderClass = this.hierarchyHandlers[ typeID ][ "loaderClass" ];
  53. this.hierarchyHandlers[ typeID ][ "loaderObject" ] = new loaderClass();
  54. }
  55. counter_models = 0;
  56. counter_textures = 0;
  57. result = {
  58. scene: new THREE.Scene(),
  59. geometries: {},
  60. face_materials: {},
  61. materials: {},
  62. textures: {},
  63. objects: {},
  64. cameras: {},
  65. lights: {},
  66. fogs: {},
  67. empties: {},
  68. groups: {}
  69. };
  70. if ( data.transform ) {
  71. var position = data.transform.position,
  72. rotation = data.transform.rotation,
  73. scale = data.transform.scale;
  74. if ( position ) {
  75. result.scene.position.fromArray( position );
  76. }
  77. if ( rotation ) {
  78. result.scene.rotation.fromArray( rotation );
  79. }
  80. if ( scale ) {
  81. result.scene.scale.fromArray( scale );
  82. }
  83. if ( position || rotation || scale ) {
  84. result.scene.updateMatrix();
  85. result.scene.updateMatrixWorld();
  86. }
  87. }
  88. function get_url( source_url, url_type ) {
  89. if ( url_type == "relativeToHTML" ) {
  90. return source_url;
  91. } else {
  92. return urlBase + source_url;
  93. }
  94. };
  95. // toplevel loader function, delegates to handle_children
  96. function handle_objects() {
  97. handle_children( result.scene, data.objects );
  98. }
  99. // handle all the children from the loaded json and attach them to given parent
  100. function handle_children( parent, children ) {
  101. var mat, dst, pos, rot, scl, quat;
  102. for ( var objID in children ) {
  103. // check by id if child has already been handled,
  104. // if not, create new object
  105. var object = result.objects[ objID ];
  106. var objJSON = children[ objID ];
  107. if ( object === undefined ) {
  108. // meshes
  109. if ( objJSON.type && ( objJSON.type in scope.hierarchyHandlers ) ) {
  110. if ( objJSON.loading === undefined ) {
  111. material = result.materials[ objJSON.material ];
  112. objJSON.loading = true;
  113. var loader = scope.hierarchyHandlers[ objJSON.type ][ "loaderObject" ];
  114. // ColladaLoader
  115. if ( loader.options ) {
  116. loader.load( get_url( objJSON.url, data.urlBaseType ), create_callback_hierachy( objID, parent, material, objJSON ) );
  117. // UTF8Loader
  118. // OBJLoader
  119. } else {
  120. loader.load( get_url( objJSON.url, data.urlBaseType ), create_callback_hierachy( objID, parent, material, objJSON ) );
  121. }
  122. }
  123. } else if ( objJSON.geometry !== undefined ) {
  124. geometry = result.geometries[ objJSON.geometry ];
  125. // geometry already loaded
  126. if ( geometry ) {
  127. var needsTangents = false;
  128. material = result.materials[ objJSON.material ];
  129. needsTangents = material instanceof THREE.ShaderMaterial;
  130. pos = objJSON.position;
  131. rot = objJSON.rotation;
  132. scl = objJSON.scale;
  133. mat = objJSON.matrix;
  134. quat = objJSON.quaternion;
  135. // use materials from the model file
  136. // if there is no material specified in the object
  137. if ( ! objJSON.material ) {
  138. material = new THREE.MeshFaceMaterial( result.face_materials[ objJSON.geometry ] );
  139. }
  140. // use materials from the model file
  141. // if there is just empty face material
  142. // (must create new material as each model has its own face material)
  143. if ( ( material instanceof THREE.MeshFaceMaterial ) && material.materials.length === 0 ) {
  144. material = new THREE.MeshFaceMaterial( result.face_materials[ objJSON.geometry ] );
  145. }
  146. if ( material instanceof THREE.MeshFaceMaterial ) {
  147. for ( var i = 0; i < material.materials.length; i ++ ) {
  148. needsTangents = needsTangents || ( material.materials[ i ] instanceof THREE.ShaderMaterial );
  149. }
  150. }
  151. if ( needsTangents ) {
  152. geometry.computeTangents();
  153. }
  154. if ( objJSON.skin ) {
  155. object = new THREE.SkinnedMesh( geometry, material );
  156. } else if ( objJSON.morph ) {
  157. object = new THREE.MorphAnimMesh( geometry, material );
  158. if ( objJSON.duration !== undefined ) {
  159. object.duration = objJSON.duration;
  160. }
  161. if ( objJSON.time !== undefined ) {
  162. object.time = objJSON.time;
  163. }
  164. if ( objJSON.mirroredLoop !== undefined ) {
  165. object.mirroredLoop = objJSON.mirroredLoop;
  166. }
  167. if ( material.morphNormals ) {
  168. geometry.computeMorphNormals();
  169. }
  170. } else {
  171. object = new THREE.Mesh( geometry, material );
  172. }
  173. object.name = objID;
  174. if ( mat ) {
  175. object.matrixAutoUpdate = false;
  176. object.matrix.set(
  177. mat[0], mat[1], mat[2], mat[3],
  178. mat[4], mat[5], mat[6], mat[7],
  179. mat[8], mat[9], mat[10], mat[11],
  180. mat[12], mat[13], mat[14], mat[15]
  181. );
  182. } else {
  183. object.position.fromArray( pos );
  184. if ( quat ) {
  185. object.quaternion.fromArray( quat );
  186. } else {
  187. object.rotation.fromArray( rot );
  188. }
  189. object.scale.fromArray( scl );
  190. }
  191. object.visible = objJSON.visible;
  192. object.castShadow = objJSON.castShadow;
  193. object.receiveShadow = objJSON.receiveShadow;
  194. parent.add( object );
  195. result.objects[ objID ] = object;
  196. }
  197. // lights
  198. } else if ( objJSON.type === "AmbientLight" || objJSON.type === "PointLight" ||
  199. objJSON.type === "DirectionalLight" || objJSON.type === "SpotLight" ||
  200. objJSON.type === "HemisphereLight" || objJSON.type === "AreaLight" ) {
  201. var color = objJSON.color;
  202. var intensity = objJSON.intensity;
  203. var distance = objJSON.distance;
  204. var position = objJSON.position;
  205. var rotation = objJSON.rotation;
  206. switch ( objJSON.type ) {
  207. case 'AmbientLight':
  208. light = new THREE.AmbientLight( color );
  209. break;
  210. case 'PointLight':
  211. light = new THREE.PointLight( color, intensity, distance );
  212. light.position.fromArray( position );
  213. break;
  214. case 'DirectionalLight':
  215. light = new THREE.DirectionalLight( color, intensity );
  216. light.position.fromArray( objJSON.direction );
  217. break;
  218. case 'SpotLight':
  219. light = new THREE.SpotLight( color, intensity, distance, 1 );
  220. light.angle = objJSON.angle;
  221. light.position.fromArray( position );
  222. light.target.set( position[ 0 ], position[ 1 ] - distance, position[ 2 ] );
  223. light.target.applyEuler( new THREE.Euler( rotation[ 0 ], rotation[ 1 ], rotation[ 2 ], 'XYZ' ) );
  224. break;
  225. case 'HemisphereLight':
  226. light = new THREE.DirectionalLight( color, intensity, distance );
  227. light.target.set( position[ 0 ], position[ 1 ] - distance, position[ 2 ] );
  228. light.target.applyEuler( new THREE.Euler( rotation[ 0 ], rotation[ 1 ], rotation[ 2 ], 'XYZ' ) );
  229. break;
  230. case 'AreaLight':
  231. light = new THREE.AreaLight(color, intensity);
  232. light.position.fromArray( position );
  233. light.width = objJSON.size;
  234. light.height = objJSON.size_y;
  235. break;
  236. }
  237. parent.add( light );
  238. light.name = objID;
  239. result.lights[ objID ] = light;
  240. result.objects[ objID ] = light;
  241. // cameras
  242. } else if ( objJSON.type === "PerspectiveCamera" || objJSON.type === "OrthographicCamera" ) {
  243. pos = objJSON.position;
  244. rot = objJSON.rotation;
  245. quat = objJSON.quaternion;
  246. if ( objJSON.type === "PerspectiveCamera" ) {
  247. camera = new THREE.PerspectiveCamera( objJSON.fov, objJSON.aspect, objJSON.near, objJSON.far );
  248. } else if ( objJSON.type === "OrthographicCamera" ) {
  249. camera = new THREE.OrthographicCamera( objJSON.left, objJSON.right, objJSON.top, objJSON.bottom, objJSON.near, objJSON.far );
  250. }
  251. camera.name = objID;
  252. camera.position.fromArray( pos );
  253. if ( quat !== undefined ) {
  254. camera.quaternion.fromArray( quat );
  255. } else if ( rot !== undefined ) {
  256. camera.rotation.fromArray( rot );
  257. } else if ( objJSON.target ) {
  258. camera.lookAt( new THREE.Vector3().fromArray( objJSON.target ) );
  259. }
  260. parent.add( camera );
  261. result.cameras[ objID ] = camera;
  262. result.objects[ objID ] = camera;
  263. // pure Object3D
  264. } else {
  265. pos = objJSON.position;
  266. rot = objJSON.rotation;
  267. scl = objJSON.scale;
  268. quat = objJSON.quaternion;
  269. object = new THREE.Object3D();
  270. object.name = objID;
  271. object.position.fromArray( pos );
  272. if ( quat ) {
  273. object.quaternion.fromArray( quat );
  274. } else {
  275. object.rotation.fromArray( rot );
  276. }
  277. object.scale.fromArray( scl );
  278. object.visible = ( objJSON.visible !== undefined ) ? objJSON.visible : false;
  279. parent.add( object );
  280. result.objects[ objID ] = object;
  281. result.empties[ objID ] = object;
  282. }
  283. if ( object ) {
  284. if ( objJSON.userData !== undefined ) {
  285. for ( var key in objJSON.userData ) {
  286. var value = objJSON.userData[ key ];
  287. object.userData[ key ] = value;
  288. }
  289. }
  290. if ( objJSON.groups !== undefined ) {
  291. for ( var i = 0; i < objJSON.groups.length; i ++ ) {
  292. var groupID = objJSON.groups[ i ];
  293. if ( result.groups[ groupID ] === undefined ) {
  294. result.groups[ groupID ] = [];
  295. }
  296. result.groups[ groupID ].push( objID );
  297. }
  298. }
  299. }
  300. }
  301. if ( object !== undefined && objJSON.children !== undefined ) {
  302. handle_children( object, objJSON.children );
  303. }
  304. }
  305. };
  306. function handle_mesh( geo, mat, id ) {
  307. result.geometries[ id ] = geo;
  308. result.face_materials[ id ] = mat;
  309. handle_objects();
  310. };
  311. function handle_hierarchy( node, id, parent, material, obj ) {
  312. var p = obj.position;
  313. var r = obj.rotation;
  314. var q = obj.quaternion;
  315. var s = obj.scale;
  316. node.position.fromArray( p );
  317. if ( q ) {
  318. node.quaternion.fromArray( q );
  319. } else {
  320. node.rotation.fromArray( r );
  321. }
  322. node.scale.fromArray( s );
  323. // override children materials
  324. // if object material was specified in JSON explicitly
  325. if ( material ) {
  326. node.traverse( function ( child ) {
  327. child.material = material;
  328. } );
  329. }
  330. // override children visibility
  331. // with root node visibility as specified in JSON
  332. var visible = ( obj.visible !== undefined ) ? obj.visible : true;
  333. node.traverse( function ( child ) {
  334. child.visible = visible;
  335. } );
  336. parent.add( node );
  337. node.name = id;
  338. result.objects[ id ] = node;
  339. handle_objects();
  340. };
  341. function create_callback_geometry( id ) {
  342. return function ( geo, mat ) {
  343. geo.name = id;
  344. handle_mesh( geo, mat, id );
  345. counter_models -= 1;
  346. scope.onLoadComplete();
  347. async_callback_gate();
  348. }
  349. };
  350. function create_callback_hierachy( id, parent, material, obj ) {
  351. return function ( event ) {
  352. var result;
  353. // loaders which use EventDispatcher
  354. if ( event.content ) {
  355. result = event.content;
  356. // ColladaLoader
  357. } else if ( event.dae ) {
  358. result = event.scene;
  359. // UTF8Loader
  360. } else {
  361. result = event;
  362. }
  363. handle_hierarchy( result, id, parent, material, obj );
  364. counter_models -= 1;
  365. scope.onLoadComplete();
  366. async_callback_gate();
  367. }
  368. };
  369. function create_callback_embed( id ) {
  370. return function ( geo, mat ) {
  371. geo.name = id;
  372. result.geometries[ id ] = geo;
  373. result.face_materials[ id ] = mat;
  374. }
  375. };
  376. function async_callback_gate() {
  377. var progress = {
  378. totalModels : total_models,
  379. totalTextures : total_textures,
  380. loadedModels : total_models - counter_models,
  381. loadedTextures : total_textures - counter_textures
  382. };
  383. scope.callbackProgress( progress, result );
  384. scope.onLoadProgress();
  385. if ( counter_models === 0 && counter_textures === 0 ) {
  386. finalize();
  387. callbackFinished( result );
  388. }
  389. };
  390. function finalize() {
  391. // take care of targets which could be asynchronously loaded objects
  392. for ( var i = 0; i < target_array.length; i ++ ) {
  393. var ta = target_array[ i ];
  394. var target = result.objects[ ta.targetName ];
  395. if ( target ) {
  396. ta.object.target = target;
  397. } else {
  398. // if there was error and target of specified name doesn't exist in the scene file
  399. // create instead dummy target
  400. // (target must be added to scene explicitly as parent is already added)
  401. ta.object.target = new THREE.Object3D();
  402. result.scene.add( ta.object.target );
  403. }
  404. ta.object.target.userData.targetInverse = ta.object;
  405. }
  406. };
  407. var callbackTexture = function ( count ) {
  408. counter_textures -= count;
  409. async_callback_gate();
  410. scope.onLoadComplete();
  411. };
  412. // must use this instead of just directly calling callbackTexture
  413. // because of closure in the calling context loop
  414. var generateTextureCallback = function ( count ) {
  415. return function () {
  416. callbackTexture( count );
  417. };
  418. };
  419. function traverse_json_hierarchy( objJSON, callback ) {
  420. callback( objJSON );
  421. if ( objJSON.children !== undefined ) {
  422. for ( var objChildID in objJSON.children ) {
  423. traverse_json_hierarchy( objJSON.children[ objChildID ], callback );
  424. }
  425. }
  426. };
  427. // first go synchronous elements
  428. // fogs
  429. var fogID, fogJSON;
  430. for ( fogID in data.fogs ) {
  431. fogJSON = data.fogs[ fogID ];
  432. if ( fogJSON.type === "linear" ) {
  433. fog = new THREE.Fog( 0x000000, fogJSON.near, fogJSON.far );
  434. } else if ( fogJSON.type === "exp2" ) {
  435. fog = new THREE.FogExp2( 0x000000, fogJSON.density );
  436. }
  437. color = fogJSON.color;
  438. fog.color.setRGB( color[0], color[1], color[2] );
  439. result.fogs[ fogID ] = fog;
  440. }
  441. // now come potentially asynchronous elements
  442. // geometries
  443. // count how many geometries will be loaded asynchronously
  444. var geoID, geoJSON;
  445. for ( geoID in data.geometries ) {
  446. geoJSON = data.geometries[ geoID ];
  447. if ( geoJSON.type in this.geometryHandlers ) {
  448. counter_models += 1;
  449. scope.onLoadStart();
  450. }
  451. }
  452. // count how many hierarchies will be loaded asynchronously
  453. for ( var objID in data.objects ) {
  454. traverse_json_hierarchy( data.objects[ objID ], function ( objJSON ) {
  455. if ( objJSON.type && ( objJSON.type in scope.hierarchyHandlers ) ) {
  456. counter_models += 1;
  457. scope.onLoadStart();
  458. }
  459. });
  460. }
  461. total_models = counter_models;
  462. for ( geoID in data.geometries ) {
  463. geoJSON = data.geometries[ geoID ];
  464. if ( geoJSON.type === "cube" ) {
  465. geometry = new THREE.BoxGeometry( geoJSON.width, geoJSON.height, geoJSON.depth, geoJSON.widthSegments, geoJSON.heightSegments, geoJSON.depthSegments );
  466. geometry.name = geoID;
  467. result.geometries[ geoID ] = geometry;
  468. } else if ( geoJSON.type === "plane" ) {
  469. geometry = new THREE.PlaneGeometry( geoJSON.width, geoJSON.height, geoJSON.widthSegments, geoJSON.heightSegments );
  470. geometry.name = geoID;
  471. result.geometries[ geoID ] = geometry;
  472. } else if ( geoJSON.type === "sphere" ) {
  473. geometry = new THREE.SphereGeometry( geoJSON.radius, geoJSON.widthSegments, geoJSON.heightSegments );
  474. geometry.name = geoID;
  475. result.geometries[ geoID ] = geometry;
  476. } else if ( geoJSON.type === "cylinder" ) {
  477. geometry = new THREE.CylinderGeometry( geoJSON.topRad, geoJSON.botRad, geoJSON.height, geoJSON.radSegs, geoJSON.heightSegs );
  478. geometry.name = geoID;
  479. result.geometries[ geoID ] = geometry;
  480. } else if ( geoJSON.type === "torus" ) {
  481. geometry = new THREE.TorusGeometry( geoJSON.radius, geoJSON.tube, geoJSON.segmentsR, geoJSON.segmentsT );
  482. geometry.name = geoID;
  483. result.geometries[ geoID ] = geometry;
  484. } else if ( geoJSON.type === "icosahedron" ) {
  485. geometry = new THREE.IcosahedronGeometry( geoJSON.radius, geoJSON.subdivisions );
  486. geometry.name = geoID;
  487. result.geometries[ geoID ] = geometry;
  488. } else if ( geoJSON.type in this.geometryHandlers ) {
  489. var loader = this.geometryHandlers[ geoJSON.type ][ "loaderObject" ];
  490. loader.load( get_url( geoJSON.url, data.urlBaseType ), create_callback_geometry( geoID ) );
  491. } else if ( geoJSON.type === "embedded" ) {
  492. var modelJson = data.embeds[ geoJSON.id ],
  493. texture_path = "";
  494. // pass metadata along to jsonLoader so it knows the format version
  495. modelJson.metadata = data.metadata;
  496. if ( modelJson ) {
  497. var jsonLoader = this.geometryHandlers[ "ascii" ][ "loaderObject" ];
  498. var model = jsonLoader.parse( modelJson, texture_path );
  499. create_callback_embed( geoID )( model.geometry, model.materials );
  500. }
  501. }
  502. }
  503. // textures
  504. // count how many textures will be loaded asynchronously
  505. var textureID, textureJSON;
  506. for ( textureID in data.textures ) {
  507. textureJSON = data.textures[ textureID ];
  508. if ( textureJSON.url instanceof Array ) {
  509. counter_textures += textureJSON.url.length;
  510. for ( var n = 0; n < textureJSON.url.length; n ++ ) {
  511. scope.onLoadStart();
  512. }
  513. } else {
  514. counter_textures += 1;
  515. scope.onLoadStart();
  516. }
  517. }
  518. total_textures = counter_textures;
  519. for ( textureID in data.textures ) {
  520. textureJSON = data.textures[ textureID ];
  521. if ( textureJSON.mapping !== undefined && THREE[ textureJSON.mapping ] !== undefined ) {
  522. textureJSON.mapping = THREE[ textureJSON.mapping ];
  523. }
  524. var texture;
  525. if ( textureJSON.url instanceof Array ) {
  526. var count = textureJSON.url.length;
  527. var url_array = [];
  528. for ( var i = 0; i < count; i ++ ) {
  529. url_array[ i ] = get_url( textureJSON.url[ i ], data.urlBaseType );
  530. }
  531. var loader = THREE.Loader.Handlers.get( url_array[ 0 ] );
  532. if ( loader !== null ) {
  533. texture = loader.load( url_array, generateTextureCallback( count ) );
  534. if ( textureJSON.mapping !== undefined )
  535. texture.mapping = textureJSON.mapping;
  536. } else {
  537. texture = THREE.ImageUtils.loadTextureCube( url_array, textureJSON.mapping, generateTextureCallback( count ) );
  538. }
  539. } else {
  540. var fullUrl = get_url( textureJSON.url, data.urlBaseType );
  541. var textureCallback = generateTextureCallback( 1 );
  542. var loader = THREE.Loader.Handlers.get( fullUrl );
  543. if ( loader !== null ) {
  544. texture = loader.load( fullUrl, textureCallback );
  545. } else {
  546. texture = new THREE.Texture();
  547. loader = new THREE.ImageLoader();
  548. ( function ( texture ) {
  549. loader.load( fullUrl, function ( image ) {
  550. texture.image = image;
  551. texture.needsUpdate = true;
  552. textureCallback();
  553. } );
  554. } )( texture )
  555. }
  556. if ( textureJSON.mapping !== undefined )
  557. texture.mapping = textureJSON.mapping;
  558. if ( THREE[ textureJSON.minFilter ] !== undefined )
  559. texture.minFilter = THREE[ textureJSON.minFilter ];
  560. if ( THREE[ textureJSON.magFilter ] !== undefined )
  561. texture.magFilter = THREE[ textureJSON.magFilter ];
  562. if ( textureJSON.anisotropy ) texture.anisotropy = textureJSON.anisotropy;
  563. if ( textureJSON.repeat ) {
  564. texture.repeat.set( textureJSON.repeat[ 0 ], textureJSON.repeat[ 1 ] );
  565. if ( textureJSON.repeat[ 0 ] !== 1 ) texture.wrapS = THREE.RepeatWrapping;
  566. if ( textureJSON.repeat[ 1 ] !== 1 ) texture.wrapT = THREE.RepeatWrapping;
  567. }
  568. if ( textureJSON.offset ) {
  569. texture.offset.set( textureJSON.offset[ 0 ], textureJSON.offset[ 1 ] );
  570. }
  571. // handle wrap after repeat so that default repeat can be overriden
  572. if ( textureJSON.wrap ) {
  573. var wrapMap = {
  574. "repeat": THREE.RepeatWrapping,
  575. "mirror": THREE.MirroredRepeatWrapping
  576. }
  577. if ( wrapMap[ textureJSON.wrap[ 0 ] ] !== undefined ) texture.wrapS = wrapMap[ textureJSON.wrap[ 0 ] ];
  578. if ( wrapMap[ textureJSON.wrap[ 1 ] ] !== undefined ) texture.wrapT = wrapMap[ textureJSON.wrap[ 1 ] ];
  579. }
  580. }
  581. result.textures[ textureID ] = texture;
  582. }
  583. // materials
  584. var matID, matJSON;
  585. var parID;
  586. for ( matID in data.materials ) {
  587. matJSON = data.materials[ matID ];
  588. for ( parID in matJSON.parameters ) {
  589. if ( parID === "envMap" || parID === "map" || parID === "lightMap" || parID === "bumpMap" || parID === "normalMap" || parID === "alphaMap" ) {
  590. matJSON.parameters[ parID ] = result.textures[ matJSON.parameters[ parID ] ];
  591. } else if ( parID === "shading" ) {
  592. matJSON.parameters[ parID ] = ( matJSON.parameters[ parID ] === "flat" ) ? THREE.FlatShading : THREE.SmoothShading;
  593. } else if ( parID === "side" ) {
  594. if ( matJSON.parameters[ parID ] == "double" ) {
  595. matJSON.parameters[ parID ] = THREE.DoubleSide;
  596. } else if ( matJSON.parameters[ parID ] == "back" ) {
  597. matJSON.parameters[ parID ] = THREE.BackSide;
  598. } else {
  599. matJSON.parameters[ parID ] = THREE.FrontSide;
  600. }
  601. } else if ( parID === "blending" ) {
  602. matJSON.parameters[ parID ] = matJSON.parameters[ parID ] in THREE ? THREE[ matJSON.parameters[ parID ] ] : THREE.NormalBlending;
  603. } else if ( parID === "combine" ) {
  604. matJSON.parameters[ parID ] = matJSON.parameters[ parID ] in THREE ? THREE[ matJSON.parameters[ parID ] ] : THREE.MultiplyOperation;
  605. } else if ( parID === "vertexColors" ) {
  606. if ( matJSON.parameters[ parID ] == "face" ) {
  607. matJSON.parameters[ parID ] = THREE.FaceColors;
  608. // default to vertex colors if "vertexColors" is anything else face colors or 0 / null / false
  609. } else if ( matJSON.parameters[ parID ] ) {
  610. matJSON.parameters[ parID ] = THREE.VertexColors;
  611. }
  612. } else if ( parID === "wrapRGB" ) {
  613. var v3 = matJSON.parameters[ parID ];
  614. matJSON.parameters[ parID ] = new THREE.Vector3( v3[ 0 ], v3[ 1 ], v3[ 2 ] );
  615. } else if ( parID === "normalScale" ) {
  616. var v2 = matJSON.parameters[ parID ];
  617. matJSON.parameters[ parID ] = new THREE.Vector2( v2[ 0 ], v2[ 1 ] );
  618. }
  619. }
  620. if ( matJSON.parameters.opacity !== undefined && matJSON.parameters.opacity < 1.0 ) {
  621. matJSON.parameters.transparent = true;
  622. }
  623. material = new THREE[ matJSON.type ]( matJSON.parameters );
  624. material.name = matID;
  625. result.materials[ matID ] = material;
  626. }
  627. // second pass through all materials to initialize MeshFaceMaterials
  628. // that could be referring to other materials out of order
  629. for ( matID in data.materials ) {
  630. matJSON = data.materials[ matID ];
  631. if ( matJSON.parameters.materials ) {
  632. var materialArray = [];
  633. for ( var i = 0; i < matJSON.parameters.materials.length; i ++ ) {
  634. var label = matJSON.parameters.materials[ i ];
  635. materialArray.push( result.materials[ label ] );
  636. }
  637. result.materials[ matID ].materials = materialArray;
  638. }
  639. }
  640. // objects ( synchronous init of procedural primitives )
  641. handle_objects();
  642. // defaults
  643. if ( result.cameras && data.defaults.camera ) {
  644. result.currentCamera = result.cameras[ data.defaults.camera ];
  645. }
  646. if ( result.fogs && data.defaults.fog ) {
  647. result.scene.fog = result.fogs[ data.defaults.fog ];
  648. }
  649. // synchronous callback
  650. scope.callbackSync( result );
  651. // just in case there are no async elements
  652. async_callback_gate();
  653. }
  654. }