SceneLoader.js 27 KB

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