SceneLoader.js 27 KB

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