SceneLoader.js 24 KB

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