SceneLoader.js 24 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157
  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" ) {
  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. }
  231. parent.add( light );
  232. light.name = objID;
  233. result.lights[ objID ] = light;
  234. result.objects[ objID ] = light;
  235. // cameras
  236. } else if ( objJSON.type === "PerspectiveCamera" || objJSON.type === "OrthographicCamera" ) {
  237. pos = objJSON.position;
  238. rot = objJSON.rotation;
  239. quat = objJSON.quaternion;
  240. if ( objJSON.type === "PerspectiveCamera" ) {
  241. camera = new THREE.PerspectiveCamera( objJSON.fov, objJSON.aspect, objJSON.near, objJSON.far );
  242. } else if ( objJSON.type === "OrthographicCamera" ) {
  243. camera = new THREE.OrthographicCamera( objJSON.left, objJSON.right, objJSON.top, objJSON.bottom, objJSON.near, objJSON.far );
  244. }
  245. camera.name = objID;
  246. camera.position.fromArray( pos );
  247. if ( quat !== undefined ) {
  248. camera.quaternion.fromArray( quat );
  249. } else if ( rot !== undefined ) {
  250. camera.rotation.fromArray( rot );
  251. } else if ( objJSON.target ) {
  252. camera.lookAt( new THREE.Vector3().fromArray( objJSON.target ) );
  253. }
  254. parent.add( camera );
  255. result.cameras[ objID ] = camera;
  256. result.objects[ objID ] = camera;
  257. // pure Object3D
  258. } else {
  259. pos = objJSON.position;
  260. rot = objJSON.rotation;
  261. scl = objJSON.scale;
  262. quat = objJSON.quaternion;
  263. object = new THREE.Object3D();
  264. object.name = objID;
  265. object.position.fromArray( pos );
  266. if ( quat ) {
  267. object.quaternion.fromArray( quat );
  268. } else {
  269. object.rotation.fromArray( rot );
  270. }
  271. object.scale.fromArray( scl );
  272. object.visible = ( objJSON.visible !== undefined ) ? objJSON.visible : false;
  273. parent.add( object );
  274. result.objects[ objID ] = object;
  275. result.empties[ objID ] = object;
  276. }
  277. if ( object ) {
  278. if ( objJSON.userData !== undefined ) {
  279. for ( var key in objJSON.userData ) {
  280. var value = objJSON.userData[ key ];
  281. object.userData[ key ] = value;
  282. }
  283. }
  284. if ( objJSON.groups !== undefined ) {
  285. for ( var i = 0; i < objJSON.groups.length; i ++ ) {
  286. var groupID = objJSON.groups[ i ];
  287. if ( result.groups[ groupID ] === undefined ) {
  288. result.groups[ groupID ] = [];
  289. }
  290. result.groups[ groupID ].push( objID );
  291. }
  292. }
  293. }
  294. }
  295. if ( object !== undefined && objJSON.children !== undefined ) {
  296. handle_children( object, objJSON.children );
  297. }
  298. }
  299. }
  300. function handle_mesh( geo, mat, id ) {
  301. result.geometries[ id ] = geo;
  302. result.face_materials[ id ] = mat;
  303. handle_objects();
  304. }
  305. function handle_hierarchy( node, id, parent, material, obj ) {
  306. var p = obj.position;
  307. var r = obj.rotation;
  308. var q = obj.quaternion;
  309. var s = obj.scale;
  310. node.position.fromArray( p );
  311. if ( q ) {
  312. node.quaternion.fromArray( q );
  313. } else {
  314. node.rotation.fromArray( r );
  315. }
  316. node.scale.fromArray( s );
  317. // override children materials
  318. // if object material was specified in JSON explicitly
  319. if ( material ) {
  320. node.traverse( function ( child ) {
  321. child.material = material;
  322. } );
  323. }
  324. // override children visibility
  325. // with root node visibility as specified in JSON
  326. var visible = ( obj.visible !== undefined ) ? obj.visible : true;
  327. node.traverse( function ( child ) {
  328. child.visible = visible;
  329. } );
  330. parent.add( node );
  331. node.name = id;
  332. result.objects[ id ] = node;
  333. handle_objects();
  334. }
  335. function create_callback_geometry( id ) {
  336. return function ( geo, mat ) {
  337. geo.name = id;
  338. handle_mesh( geo, mat, id );
  339. counter_models -= 1;
  340. scope.onLoadComplete();
  341. async_callback_gate();
  342. }
  343. }
  344. function create_callback_hierachy( id, parent, material, obj ) {
  345. return function ( event ) {
  346. var result;
  347. // loaders which use EventDispatcher
  348. if ( event.content ) {
  349. result = event.content;
  350. // ColladaLoader
  351. } else if ( event.dae ) {
  352. result = event.scene;
  353. // UTF8Loader
  354. } else {
  355. result = event;
  356. }
  357. handle_hierarchy( result, id, parent, material, obj );
  358. counter_models -= 1;
  359. scope.onLoadComplete();
  360. async_callback_gate();
  361. }
  362. }
  363. function create_callback_embed( id ) {
  364. return function ( geo, mat ) {
  365. geo.name = id;
  366. result.geometries[ id ] = geo;
  367. result.face_materials[ id ] = mat;
  368. }
  369. }
  370. function async_callback_gate() {
  371. var progress = {
  372. totalModels : total_models,
  373. totalTextures : total_textures,
  374. loadedModels : total_models - counter_models,
  375. loadedTextures : total_textures - counter_textures
  376. };
  377. scope.callbackProgress( progress, result );
  378. scope.onLoadProgress();
  379. if ( counter_models === 0 && counter_textures === 0 ) {
  380. finalize();
  381. callbackFinished( result );
  382. }
  383. }
  384. function finalize() {
  385. // take care of targets which could be asynchronously loaded objects
  386. for ( var i = 0; i < target_array.length; i ++ ) {
  387. var ta = target_array[ i ];
  388. var target = result.objects[ ta.targetName ];
  389. if ( target ) {
  390. ta.object.target = target;
  391. } else {
  392. // if there was error and target of specified name doesn't exist in the scene file
  393. // create instead dummy target
  394. // (target must be added to scene explicitly as parent is already added)
  395. ta.object.target = new THREE.Object3D();
  396. result.scene.add( ta.object.target );
  397. }
  398. ta.object.target.userData.targetInverse = ta.object;
  399. }
  400. }
  401. var callbackTexture = function ( count ) {
  402. counter_textures -= count;
  403. async_callback_gate();
  404. scope.onLoadComplete();
  405. };
  406. // must use this instead of just directly calling callbackTexture
  407. // because of closure in the calling context loop
  408. var generateTextureCallback = function ( count ) {
  409. return function () {
  410. callbackTexture( count );
  411. };
  412. };
  413. function traverse_json_hierarchy( objJSON, callback ) {
  414. callback( objJSON );
  415. if ( objJSON.children !== undefined ) {
  416. for ( var objChildID in objJSON.children ) {
  417. traverse_json_hierarchy( objJSON.children[ objChildID ], callback );
  418. }
  419. }
  420. }
  421. // first go synchronous elements
  422. // fogs
  423. var fogID, fogJSON;
  424. for ( fogID in data.fogs ) {
  425. fogJSON = data.fogs[ fogID ];
  426. if ( fogJSON.type === "linear" ) {
  427. fog = new THREE.Fog( 0x000000, fogJSON.near, fogJSON.far );
  428. } else if ( fogJSON.type === "exp2" ) {
  429. fog = new THREE.FogExp2( 0x000000, fogJSON.density );
  430. }
  431. color = fogJSON.color;
  432. fog.color.setRGB( color[ 0 ], color[ 1 ], color[ 2 ] );
  433. result.fogs[ fogID ] = fog;
  434. }
  435. // now come potentially asynchronous elements
  436. // geometries
  437. // count how many geometries will be loaded asynchronously
  438. var geoID, geoJSON;
  439. for ( geoID in data.geometries ) {
  440. geoJSON = data.geometries[ geoID ];
  441. if ( geoJSON.type in this.geometryHandlers ) {
  442. counter_models += 1;
  443. scope.onLoadStart();
  444. }
  445. }
  446. // count how many hierarchies will be loaded asynchronously
  447. for ( var objID in data.objects ) {
  448. traverse_json_hierarchy( data.objects[ objID ], function ( objJSON ) {
  449. if ( objJSON.type && ( objJSON.type in scope.hierarchyHandlers ) ) {
  450. counter_models += 1;
  451. scope.onLoadStart();
  452. }
  453. } );
  454. }
  455. total_models = counter_models;
  456. for ( geoID in data.geometries ) {
  457. geoJSON = data.geometries[ geoID ];
  458. if ( geoJSON.type === "cube" ) {
  459. geometry = new THREE.BoxGeometry( geoJSON.width, geoJSON.height, geoJSON.depth, geoJSON.widthSegments, geoJSON.heightSegments, geoJSON.depthSegments );
  460. geometry.name = geoID;
  461. result.geometries[ geoID ] = geometry;
  462. } else if ( geoJSON.type === "plane" ) {
  463. geometry = new THREE.PlaneGeometry( geoJSON.width, geoJSON.height, geoJSON.widthSegments, geoJSON.heightSegments );
  464. geometry.name = geoID;
  465. result.geometries[ geoID ] = geometry;
  466. } else if ( geoJSON.type === "sphere" ) {
  467. geometry = new THREE.SphereGeometry( geoJSON.radius, geoJSON.widthSegments, geoJSON.heightSegments );
  468. geometry.name = geoID;
  469. result.geometries[ geoID ] = geometry;
  470. } else if ( geoJSON.type === "cylinder" ) {
  471. geometry = new THREE.CylinderGeometry( geoJSON.topRad, geoJSON.botRad, geoJSON.height, geoJSON.radSegs, geoJSON.heightSegs );
  472. geometry.name = geoID;
  473. result.geometries[ geoID ] = geometry;
  474. } else if ( geoJSON.type === "torus" ) {
  475. geometry = new THREE.TorusGeometry( geoJSON.radius, geoJSON.tube, geoJSON.segmentsR, geoJSON.segmentsT );
  476. geometry.name = geoID;
  477. result.geometries[ geoID ] = geometry;
  478. } else if ( geoJSON.type === "icosahedron" ) {
  479. geometry = new THREE.IcosahedronGeometry( geoJSON.radius, geoJSON.subdivisions );
  480. geometry.name = geoID;
  481. result.geometries[ geoID ] = geometry;
  482. } else if ( geoJSON.type in this.geometryHandlers ) {
  483. var loader = this.geometryHandlers[ geoJSON.type ][ "loaderObject" ];
  484. loader.load( get_url( geoJSON.url, data.urlBaseType ), create_callback_geometry( geoID ) );
  485. } else if ( geoJSON.type === "embedded" ) {
  486. var modelJson = data.embeds[ geoJSON.id ],
  487. texture_path = "";
  488. // pass metadata along to jsonLoader so it knows the format version
  489. modelJson.metadata = data.metadata;
  490. if ( modelJson ) {
  491. var jsonLoader = this.geometryHandlers[ "ascii" ][ "loaderObject" ];
  492. var model = jsonLoader.parse( modelJson, texture_path );
  493. create_callback_embed( geoID )( model.geometry, model.materials );
  494. }
  495. }
  496. }
  497. // textures
  498. // count how many textures will be loaded asynchronously
  499. var textureID, textureJSON;
  500. for ( textureID in data.textures ) {
  501. textureJSON = data.textures[ textureID ];
  502. if ( Array.isArray( textureJSON.url ) ) {
  503. counter_textures += textureJSON.url.length;
  504. for ( var n = 0; n < textureJSON.url.length; n ++ ) {
  505. scope.onLoadStart();
  506. }
  507. } else {
  508. counter_textures += 1;
  509. scope.onLoadStart();
  510. }
  511. }
  512. total_textures = counter_textures;
  513. for ( textureID in data.textures ) {
  514. textureJSON = data.textures[ textureID ];
  515. if ( textureJSON.mapping !== undefined && THREE[ textureJSON.mapping ] !== undefined ) {
  516. textureJSON.mapping = THREE[ textureJSON.mapping ];
  517. }
  518. var texture;
  519. if ( Array.isArray( textureJSON.url ) ) {
  520. var count = textureJSON.url.length;
  521. var url_array = [];
  522. for ( var i = 0; i < count; i ++ ) {
  523. url_array[ i ] = get_url( textureJSON.url[ i ], data.urlBaseType );
  524. }
  525. var loader = THREE.Loader.Handlers.get( url_array[ 0 ] );
  526. if ( loader !== null ) {
  527. texture = loader.load( url_array, generateTextureCallback( count ) );
  528. if ( textureJSON.mapping !== undefined )
  529. texture.mapping = textureJSON.mapping;
  530. } else {
  531. texture = THREE.ImageUtils.loadTextureCube( url_array, textureJSON.mapping, generateTextureCallback( count ) );
  532. }
  533. } else {
  534. var fullUrl = get_url( textureJSON.url, data.urlBaseType );
  535. var textureCallback = generateTextureCallback( 1 );
  536. var loader = THREE.Loader.Handlers.get( fullUrl );
  537. if ( loader !== null ) {
  538. texture = loader.load( fullUrl, textureCallback );
  539. } else {
  540. texture = new THREE.Texture();
  541. loader = new THREE.ImageLoader();
  542. ( function ( texture ) {
  543. loader.load( fullUrl, function ( image ) {
  544. texture.image = image;
  545. texture.needsUpdate = true;
  546. textureCallback();
  547. } );
  548. } )( texture )
  549. }
  550. if ( textureJSON.mapping !== undefined )
  551. texture.mapping = textureJSON.mapping;
  552. if ( THREE[ textureJSON.minFilter ] !== undefined )
  553. texture.minFilter = THREE[ textureJSON.minFilter ];
  554. if ( THREE[ textureJSON.magFilter ] !== undefined )
  555. texture.magFilter = THREE[ textureJSON.magFilter ];
  556. if ( textureJSON.anisotropy ) texture.anisotropy = textureJSON.anisotropy;
  557. if ( textureJSON.repeat ) {
  558. texture.repeat.set( textureJSON.repeat[ 0 ], textureJSON.repeat[ 1 ] );
  559. if ( textureJSON.repeat[ 0 ] !== 1 ) texture.wrapS = THREE.RepeatWrapping;
  560. if ( textureJSON.repeat[ 1 ] !== 1 ) texture.wrapT = THREE.RepeatWrapping;
  561. }
  562. if ( textureJSON.offset ) {
  563. texture.offset.set( textureJSON.offset[ 0 ], textureJSON.offset[ 1 ] );
  564. }
  565. // handle wrap after repeat so that default repeat can be overriden
  566. if ( textureJSON.wrap ) {
  567. var wrapMap = {
  568. "repeat": THREE.RepeatWrapping,
  569. "mirror": THREE.MirroredRepeatWrapping
  570. };
  571. if ( wrapMap[ textureJSON.wrap[ 0 ] ] !== undefined ) texture.wrapS = wrapMap[ textureJSON.wrap[ 0 ] ];
  572. if ( wrapMap[ textureJSON.wrap[ 1 ] ] !== undefined ) texture.wrapT = wrapMap[ textureJSON.wrap[ 1 ] ];
  573. }
  574. }
  575. result.textures[ textureID ] = texture;
  576. }
  577. // materials
  578. var matID, matJSON;
  579. var parID;
  580. for ( matID in data.materials ) {
  581. matJSON = data.materials[ matID ];
  582. for ( parID in matJSON.parameters ) {
  583. if ( parID === "envMap" || parID === "map" || parID === "lightMap" || parID === "bumpMap" || parID === "normalMap" || parID === "alphaMap" ) {
  584. matJSON.parameters[ parID ] = result.textures[ matJSON.parameters[ parID ] ];
  585. } else if ( parID === "shading" ) {
  586. matJSON.parameters[ parID ] = ( matJSON.parameters[ parID ] === "flat" ) ? THREE.FlatShading : THREE.SmoothShading;
  587. } else if ( parID === "side" ) {
  588. if ( matJSON.parameters[ parID ] == "double" ) {
  589. matJSON.parameters[ parID ] = THREE.DoubleSide;
  590. } else if ( matJSON.parameters[ parID ] == "back" ) {
  591. matJSON.parameters[ parID ] = THREE.BackSide;
  592. } else {
  593. matJSON.parameters[ parID ] = THREE.FrontSide;
  594. }
  595. } else if ( parID === "blending" ) {
  596. matJSON.parameters[ parID ] = matJSON.parameters[ parID ] in THREE ? THREE[ matJSON.parameters[ parID ] ] : THREE.NormalBlending;
  597. } else if ( parID === "combine" ) {
  598. matJSON.parameters[ parID ] = matJSON.parameters[ parID ] in THREE ? THREE[ matJSON.parameters[ parID ] ] : THREE.MultiplyOperation;
  599. } else if ( parID === "vertexColors" ) {
  600. if ( matJSON.parameters[ parID ] == "face" ) {
  601. matJSON.parameters[ parID ] = THREE.FaceColors;
  602. // default to vertex colors if "vertexColors" is anything else face colors or 0 / null / false
  603. } else if ( matJSON.parameters[ parID ] ) {
  604. matJSON.parameters[ parID ] = THREE.VertexColors;
  605. }
  606. } else if ( parID === "wrapRGB" ) {
  607. var v3 = matJSON.parameters[ parID ];
  608. matJSON.parameters[ parID ] = new THREE.Vector3( v3[ 0 ], v3[ 1 ], v3[ 2 ] );
  609. } else if ( parID === "normalScale" ) {
  610. var v2 = matJSON.parameters[ parID ];
  611. matJSON.parameters[ parID ] = new THREE.Vector2( v2[ 0 ], v2[ 1 ] );
  612. }
  613. }
  614. if ( matJSON.parameters.opacity !== undefined && matJSON.parameters.opacity < 1.0 ) {
  615. matJSON.parameters.transparent = true;
  616. }
  617. material = new THREE[ matJSON.type ]( matJSON.parameters );
  618. material.name = matID;
  619. result.materials[ matID ] = material;
  620. }
  621. // second pass through all materials to initialize MeshFaceMaterials
  622. // that could be referring to other materials out of order
  623. for ( matID in data.materials ) {
  624. matJSON = data.materials[ matID ];
  625. if ( matJSON.parameters.materials ) {
  626. var materialArray = [];
  627. for ( var i = 0; i < matJSON.parameters.materials.length; i ++ ) {
  628. var label = matJSON.parameters.materials[ i ];
  629. materialArray.push( result.materials[ label ] );
  630. }
  631. result.materials[ matID ].materials = materialArray;
  632. }
  633. }
  634. // objects ( synchronous init of procedural primitives )
  635. handle_objects();
  636. // defaults
  637. if ( result.cameras && data.defaults.camera ) {
  638. result.currentCamera = result.cameras[ data.defaults.camera ];
  639. }
  640. if ( result.fogs && data.defaults.fog ) {
  641. result.scene.fog = result.fogs[ data.defaults.fog ];
  642. }
  643. // synchronous callback
  644. scope.callbackSync( result );
  645. // just in case there are no async elements
  646. async_callback_gate();
  647. }
  648. };