SceneLoader.js 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243
  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. if ( textureJSON.url instanceof Array ) {
  540. var count = textureJSON.url.length;
  541. var url_array = [];
  542. for( var i = 0; i < count; i ++ ) {
  543. url_array[ i ] = get_url( textureJSON.url[ i ], data.urlBaseType );
  544. }
  545. var isCompressed = /\.dds$/i.test( url_array[ 0 ] );
  546. if ( isCompressed ) {
  547. texture = THREE.ImageUtils.loadCompressedTextureCube( url_array, textureJSON.mapping, generateTextureCallback( count ) );
  548. } else {
  549. texture = THREE.ImageUtils.loadTextureCube( url_array, textureJSON.mapping, generateTextureCallback( count ) );
  550. }
  551. } else {
  552. var isCompressed = /\.dds$/i.test( textureJSON.url );
  553. var isTGA = /\.tga$/i.test( textureJSON.url );
  554. var fullUrl = get_url( textureJSON.url, data.urlBaseType );
  555. var textureCallback = generateTextureCallback( 1 );
  556. if ( isCompressed ) {
  557. texture = THREE.ImageUtils.loadCompressedTexture( fullUrl, textureJSON.mapping, textureCallback );
  558. } else if ( isTGA ) {
  559. texture = THREE.ImageUtils.loadTGATexture( fullUrl, textureJSON.mapping, textureCallback );
  560. } else {
  561. texture = THREE.ImageUtils.loadTexture( fullUrl, textureJSON.mapping, textureCallback );
  562. }
  563. if ( THREE[ textureJSON.minFilter ] !== undefined )
  564. texture.minFilter = THREE[ textureJSON.minFilter ];
  565. if ( THREE[ textureJSON.magFilter ] !== undefined )
  566. texture.magFilter = THREE[ textureJSON.magFilter ];
  567. if ( textureJSON.anisotropy ) texture.anisotropy = textureJSON.anisotropy;
  568. if ( textureJSON.repeat ) {
  569. texture.repeat.set( textureJSON.repeat[ 0 ], textureJSON.repeat[ 1 ] );
  570. if ( textureJSON.repeat[ 0 ] !== 1 ) texture.wrapS = THREE.RepeatWrapping;
  571. if ( textureJSON.repeat[ 1 ] !== 1 ) texture.wrapT = THREE.RepeatWrapping;
  572. }
  573. if ( textureJSON.offset ) {
  574. texture.offset.set( textureJSON.offset[ 0 ], textureJSON.offset[ 1 ] );
  575. }
  576. // handle wrap after repeat so that default repeat can be overriden
  577. if ( textureJSON.wrap ) {
  578. var wrapMap = {
  579. "repeat": THREE.RepeatWrapping,
  580. "mirror": THREE.MirroredRepeatWrapping
  581. }
  582. if ( wrapMap[ textureJSON.wrap[ 0 ] ] !== undefined ) texture.wrapS = wrapMap[ textureJSON.wrap[ 0 ] ];
  583. if ( wrapMap[ textureJSON.wrap[ 1 ] ] !== undefined ) texture.wrapT = wrapMap[ textureJSON.wrap[ 1 ] ];
  584. }
  585. }
  586. result.textures[ textureID ] = texture;
  587. }
  588. // materials
  589. var matID, matJSON;
  590. var parID;
  591. for ( matID in data.materials ) {
  592. matJSON = data.materials[ matID ];
  593. for ( parID in matJSON.parameters ) {
  594. if ( parID === "envMap" || parID === "map" || parID === "lightMap" || parID === "bumpMap" ) {
  595. matJSON.parameters[ parID ] = result.textures[ matJSON.parameters[ parID ] ];
  596. } else if ( parID === "shading" ) {
  597. matJSON.parameters[ parID ] = ( matJSON.parameters[ parID ] === "flat" ) ? THREE.FlatShading : THREE.SmoothShading;
  598. } else if ( parID === "side" ) {
  599. if ( matJSON.parameters[ parID ] == "double" ) {
  600. matJSON.parameters[ parID ] = THREE.DoubleSide;
  601. } else if ( matJSON.parameters[ parID ] == "back" ) {
  602. matJSON.parameters[ parID ] = THREE.BackSide;
  603. } else {
  604. matJSON.parameters[ parID ] = THREE.FrontSide;
  605. }
  606. } else if ( parID === "blending" ) {
  607. matJSON.parameters[ parID ] = matJSON.parameters[ parID ] in THREE ? THREE[ matJSON.parameters[ parID ] ] : THREE.NormalBlending;
  608. } else if ( parID === "combine" ) {
  609. matJSON.parameters[ parID ] = matJSON.parameters[ parID ] in THREE ? THREE[ matJSON.parameters[ parID ] ] : THREE.MultiplyOperation;
  610. } else if ( parID === "vertexColors" ) {
  611. if ( matJSON.parameters[ parID ] == "face" ) {
  612. matJSON.parameters[ parID ] = THREE.FaceColors;
  613. // default to vertex colors if "vertexColors" is anything else face colors or 0 / null / false
  614. } else if ( matJSON.parameters[ parID ] ) {
  615. matJSON.parameters[ parID ] = THREE.VertexColors;
  616. }
  617. } else if ( parID === "wrapRGB" ) {
  618. var v3 = matJSON.parameters[ parID ];
  619. matJSON.parameters[ parID ] = new THREE.Vector3( v3[ 0 ], v3[ 1 ], v3[ 2 ] );
  620. }
  621. }
  622. if ( matJSON.parameters.opacity !== undefined && matJSON.parameters.opacity < 1.0 ) {
  623. matJSON.parameters.transparent = true;
  624. }
  625. if ( matJSON.parameters.normalMap ) {
  626. var shader = THREE.ShaderLib[ "normalmap" ];
  627. var uniforms = THREE.UniformsUtils.clone( shader.uniforms );
  628. var diffuse = matJSON.parameters.color;
  629. var specular = matJSON.parameters.specular;
  630. var ambient = matJSON.parameters.ambient;
  631. var shininess = matJSON.parameters.shininess;
  632. uniforms[ "tNormal" ].value = result.textures[ matJSON.parameters.normalMap ];
  633. if ( matJSON.parameters.normalScale ) {
  634. uniforms[ "uNormalScale" ].value.set( matJSON.parameters.normalScale[ 0 ], matJSON.parameters.normalScale[ 1 ] );
  635. }
  636. if ( matJSON.parameters.map ) {
  637. uniforms[ "tDiffuse" ].value = matJSON.parameters.map;
  638. uniforms[ "enableDiffuse" ].value = true;
  639. }
  640. if ( matJSON.parameters.envMap ) {
  641. uniforms[ "tCube" ].value = matJSON.parameters.envMap;
  642. uniforms[ "enableReflection" ].value = true;
  643. uniforms[ "reflectivity" ].value = matJSON.parameters.reflectivity;
  644. }
  645. if ( matJSON.parameters.lightMap ) {
  646. uniforms[ "tAO" ].value = matJSON.parameters.lightMap;
  647. uniforms[ "enableAO" ].value = true;
  648. }
  649. if ( matJSON.parameters.specularMap ) {
  650. uniforms[ "tSpecular" ].value = result.textures[ matJSON.parameters.specularMap ];
  651. uniforms[ "enableSpecular" ].value = true;
  652. }
  653. if ( matJSON.parameters.displacementMap ) {
  654. uniforms[ "tDisplacement" ].value = result.textures[ matJSON.parameters.displacementMap ];
  655. uniforms[ "enableDisplacement" ].value = true;
  656. uniforms[ "uDisplacementBias" ].value = matJSON.parameters.displacementBias;
  657. uniforms[ "uDisplacementScale" ].value = matJSON.parameters.displacementScale;
  658. }
  659. uniforms[ "diffuse" ].value.setHex( diffuse );
  660. uniforms[ "specular" ].value.setHex( specular );
  661. uniforms[ "ambient" ].value.setHex( ambient );
  662. uniforms[ "shininess" ].value = shininess;
  663. if ( matJSON.parameters.opacity ) {
  664. uniforms[ "opacity" ].value = matJSON.parameters.opacity;
  665. }
  666. var parameters = { fragmentShader: shader.fragmentShader, vertexShader: shader.vertexShader, uniforms: uniforms, lights: true, fog: true };
  667. material = new THREE.ShaderMaterial( parameters );
  668. } else {
  669. material = new THREE[ matJSON.type ]( matJSON.parameters );
  670. }
  671. material.name = matID;
  672. result.materials[ matID ] = material;
  673. }
  674. // second pass through all materials to initialize MeshFaceMaterials
  675. // that could be referring to other materials out of order
  676. for ( matID in data.materials ) {
  677. matJSON = data.materials[ matID ];
  678. if ( matJSON.parameters.materials ) {
  679. var materialArray = [];
  680. for ( var i = 0; i < matJSON.parameters.materials.length; i ++ ) {
  681. var label = matJSON.parameters.materials[ i ];
  682. materialArray.push( result.materials[ label ] );
  683. }
  684. result.materials[ matID ].materials = materialArray;
  685. }
  686. }
  687. // objects ( synchronous init of procedural primitives )
  688. handle_objects();
  689. // defaults
  690. if ( result.cameras && data.defaults.camera ) {
  691. result.currentCamera = result.cameras[ data.defaults.camera ];
  692. }
  693. if ( result.fogs && data.defaults.fog ) {
  694. result.scene.fog = result.fogs[ data.defaults.fog ];
  695. }
  696. // synchronous callback
  697. scope.callbackSync( result );
  698. // just in case there are no async elements
  699. async_callback_gate();
  700. }
  701. }