SceneLoader.js 27 KB

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