SceneLoader.js 26 KB

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