2
0

SceneLoader.js 24 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138
  1. /**
  2. * @author alteredq / http://alteredqualia.com/
  3. */
  4. THREE.SceneLoader = function ( manager ) {
  5. this.onLoadStart = function () {};
  6. this.onLoadProgress = function() {};
  7. this.onLoadComplete = function () {};
  8. this.callbackSync = function () {};
  9. this.callbackProgress = function () {};
  10. this.geometryHandlers = {};
  11. this.hierarchyHandlers = {};
  12. this.addGeometryHandler( "ascii", THREE.JSONLoader );
  13. this.manager = ( manager !== undefined ) ? manager : THREE.DefaultLoadingManager;
  14. };
  15. THREE.SceneLoader.prototype = {
  16. constructor: THREE.SceneLoader,
  17. load: function ( url, onLoad, onProgress, onError ) {
  18. var scope = this;
  19. var loader = new THREE.XHRLoader( scope.manager );
  20. loader.setCrossOrigin( this.crossOrigin );
  21. loader.load( url, function ( text ) {
  22. scope.parse( JSON.parse( text ), onLoad, url );
  23. }, onProgress, onError );
  24. },
  25. setCrossOrigin: function ( value ) {
  26. this.crossOrigin = value;
  27. },
  28. addGeometryHandler: function ( typeID, loaderClass ) {
  29. this.geometryHandlers[ typeID ] = { "loaderClass": loaderClass };
  30. },
  31. addHierarchyHandler: function ( typeID, loaderClass ) {
  32. this.hierarchyHandlers[ typeID ] = { "loaderClass": loaderClass };
  33. },
  34. parse: function ( json, callbackFinished, url ) {
  35. var scope = this;
  36. var urlBase = THREE.Loader.prototype.extractUrlBase( url );
  37. var geometry, material, camera, fog,
  38. texture, images, color,
  39. light, hex, intensity,
  40. counter_models, counter_textures,
  41. total_models, total_textures,
  42. result;
  43. var target_array = [];
  44. var data = json;
  45. // async geometry loaders
  46. for ( var typeID in this.geometryHandlers ) {
  47. var loaderClass = this.geometryHandlers[ typeID ][ "loaderClass" ];
  48. this.geometryHandlers[ typeID ][ "loaderObject" ] = new loaderClass();
  49. }
  50. // async hierachy loaders
  51. for ( var typeID in this.hierarchyHandlers ) {
  52. var loaderClass = this.hierarchyHandlers[ typeID ][ "loaderClass" ];
  53. this.hierarchyHandlers[ typeID ][ "loaderObject" ] = new loaderClass();
  54. }
  55. counter_models = 0;
  56. counter_textures = 0;
  57. result = {
  58. scene: new THREE.Scene(),
  59. geometries: {},
  60. face_materials: {},
  61. materials: {},
  62. textures: {},
  63. objects: {},
  64. cameras: {},
  65. lights: {},
  66. fogs: {},
  67. empties: {},
  68. groups: {}
  69. };
  70. if ( data.transform ) {
  71. var position = data.transform.position,
  72. rotation = data.transform.rotation,
  73. scale = data.transform.scale;
  74. if ( position ) {
  75. result.scene.position.fromArray( position );
  76. }
  77. if ( rotation ) {
  78. result.scene.rotation.fromArray( rotation );
  79. }
  80. if ( scale ) {
  81. result.scene.scale.fromArray( scale );
  82. }
  83. if ( position || rotation || scale ) {
  84. result.scene.updateMatrix();
  85. result.scene.updateMatrixWorld();
  86. }
  87. }
  88. function get_url( source_url, url_type ) {
  89. if ( url_type == "relativeToHTML" ) {
  90. return source_url;
  91. } else {
  92. return urlBase + source_url;
  93. }
  94. }
  95. // toplevel loader function, delegates to handle_children
  96. function handle_objects() {
  97. handle_children( result.scene, data.objects );
  98. }
  99. // handle all the children from the loaded json and attach them to given parent
  100. function handle_children( parent, children ) {
  101. var mat, dst, pos, rot, scl, quat;
  102. for ( var objID in children ) {
  103. // check by id if child has already been handled,
  104. // if not, create new object
  105. var object = result.objects[ objID ];
  106. var objJSON = children[ objID ];
  107. if ( object === undefined ) {
  108. // meshes
  109. if ( objJSON.type && ( objJSON.type in scope.hierarchyHandlers ) ) {
  110. if ( objJSON.loading === undefined ) {
  111. material = result.materials[ objJSON.material ];
  112. objJSON.loading = true;
  113. var loader = scope.hierarchyHandlers[ objJSON.type ][ "loaderObject" ];
  114. // ColladaLoader
  115. if ( loader.options ) {
  116. loader.load( get_url( objJSON.url, data.urlBaseType ), create_callback_hierachy( objID, parent, material, objJSON ) );
  117. // UTF8Loader
  118. // OBJLoader
  119. } else {
  120. loader.load( get_url( objJSON.url, data.urlBaseType ), create_callback_hierachy( objID, parent, material, objJSON ) );
  121. }
  122. }
  123. } else if ( objJSON.geometry !== undefined ) {
  124. geometry = result.geometries[ objJSON.geometry ];
  125. // geometry already loaded
  126. if ( geometry ) {
  127. material = result.materials[ objJSON.material ];
  128. pos = objJSON.position;
  129. rot = objJSON.rotation;
  130. scl = objJSON.scale;
  131. mat = objJSON.matrix;
  132. quat = objJSON.quaternion;
  133. // use materials from the model file
  134. // if there is no material specified in the object
  135. if ( ! objJSON.material ) {
  136. material = new THREE.MeshFaceMaterial( result.face_materials[ objJSON.geometry ] );
  137. }
  138. // use materials from the model file
  139. // if there is just empty face material
  140. // (must create new material as each model has its own face material)
  141. if ( ( material instanceof THREE.MeshFaceMaterial ) && material.materials.length === 0 ) {
  142. material = new THREE.MeshFaceMaterial( result.face_materials[ objJSON.geometry ] );
  143. }
  144. if ( objJSON.skin ) {
  145. object = new THREE.SkinnedMesh( geometry, material );
  146. } else if ( objJSON.morph ) {
  147. object = new THREE.MorphAnimMesh( geometry, material );
  148. if ( objJSON.duration !== undefined ) {
  149. object.duration = objJSON.duration;
  150. }
  151. if ( objJSON.time !== undefined ) {
  152. object.time = objJSON.time;
  153. }
  154. if ( objJSON.mirroredLoop !== undefined ) {
  155. object.mirroredLoop = objJSON.mirroredLoop;
  156. }
  157. if ( material.morphNormals ) {
  158. geometry.computeMorphNormals();
  159. }
  160. } else {
  161. object = new THREE.Mesh( geometry, material );
  162. }
  163. object.name = objID;
  164. if ( mat ) {
  165. object.matrixAutoUpdate = false;
  166. object.matrix.set(
  167. mat[ 0 ], mat[ 1 ], mat[ 2 ], mat[ 3 ],
  168. mat[ 4 ], mat[ 5 ], mat[ 6 ], mat[ 7 ],
  169. mat[ 8 ], mat[ 9 ], mat[ 10 ], mat[ 11 ],
  170. mat[ 12 ], mat[ 13 ], mat[ 14 ], mat[ 15 ]
  171. );
  172. } else {
  173. object.position.fromArray( pos );
  174. if ( quat ) {
  175. object.quaternion.fromArray( quat );
  176. } else {
  177. object.rotation.fromArray( rot );
  178. }
  179. object.scale.fromArray( scl );
  180. }
  181. object.visible = objJSON.visible;
  182. object.castShadow = objJSON.castShadow;
  183. object.receiveShadow = objJSON.receiveShadow;
  184. parent.add( object );
  185. result.objects[ objID ] = object;
  186. }
  187. // lights
  188. } else if ( objJSON.type === "AmbientLight" || objJSON.type === "PointLight" ||
  189. objJSON.type === "DirectionalLight" || objJSON.type === "SpotLight" ||
  190. objJSON.type === "HemisphereLight" ) {
  191. var color = objJSON.color;
  192. var intensity = objJSON.intensity;
  193. var distance = objJSON.distance;
  194. var position = objJSON.position;
  195. var rotation = objJSON.rotation;
  196. switch ( objJSON.type ) {
  197. case 'AmbientLight':
  198. light = new THREE.AmbientLight( color );
  199. break;
  200. case 'PointLight':
  201. light = new THREE.PointLight( color, intensity, distance );
  202. light.position.fromArray( position );
  203. break;
  204. case 'DirectionalLight':
  205. light = new THREE.DirectionalLight( color, intensity );
  206. light.position.fromArray( objJSON.direction );
  207. break;
  208. case 'SpotLight':
  209. light = new THREE.SpotLight( color, intensity, distance, 1 );
  210. light.angle = objJSON.angle;
  211. light.position.fromArray( position );
  212. light.target.set( position[ 0 ], position[ 1 ] - distance, position[ 2 ] );
  213. light.target.applyEuler( new THREE.Euler( rotation[ 0 ], rotation[ 1 ], rotation[ 2 ], 'XYZ' ) );
  214. break;
  215. case 'HemisphereLight':
  216. light = new THREE.DirectionalLight( color, intensity, distance );
  217. light.target.set( position[ 0 ], position[ 1 ] - distance, position[ 2 ] );
  218. light.target.applyEuler( new THREE.Euler( rotation[ 0 ], rotation[ 1 ], rotation[ 2 ], 'XYZ' ) );
  219. break;
  220. }
  221. parent.add( light );
  222. light.name = objID;
  223. result.lights[ objID ] = light;
  224. result.objects[ objID ] = light;
  225. // cameras
  226. } else if ( objJSON.type === "PerspectiveCamera" || objJSON.type === "OrthographicCamera" ) {
  227. pos = objJSON.position;
  228. rot = objJSON.rotation;
  229. quat = objJSON.quaternion;
  230. if ( objJSON.type === "PerspectiveCamera" ) {
  231. camera = new THREE.PerspectiveCamera( objJSON.fov, objJSON.aspect, objJSON.near, objJSON.far );
  232. } else if ( objJSON.type === "OrthographicCamera" ) {
  233. camera = new THREE.OrthographicCamera( objJSON.left, objJSON.right, objJSON.top, objJSON.bottom, objJSON.near, objJSON.far );
  234. }
  235. camera.name = objID;
  236. camera.position.fromArray( pos );
  237. if ( quat !== undefined ) {
  238. camera.quaternion.fromArray( quat );
  239. } else if ( rot !== undefined ) {
  240. camera.rotation.fromArray( rot );
  241. } else if ( objJSON.target ) {
  242. camera.lookAt( new THREE.Vector3().fromArray( objJSON.target ) );
  243. }
  244. parent.add( camera );
  245. result.cameras[ objID ] = camera;
  246. result.objects[ objID ] = camera;
  247. // pure Object3D
  248. } else {
  249. pos = objJSON.position;
  250. rot = objJSON.rotation;
  251. scl = objJSON.scale;
  252. quat = objJSON.quaternion;
  253. object = new THREE.Object3D();
  254. object.name = objID;
  255. object.position.fromArray( pos );
  256. if ( quat ) {
  257. object.quaternion.fromArray( quat );
  258. } else {
  259. object.rotation.fromArray( rot );
  260. }
  261. object.scale.fromArray( scl );
  262. object.visible = ( objJSON.visible !== undefined ) ? objJSON.visible : false;
  263. parent.add( object );
  264. result.objects[ objID ] = object;
  265. result.empties[ objID ] = object;
  266. }
  267. if ( object ) {
  268. if ( objJSON.userData !== undefined ) {
  269. for ( var key in objJSON.userData ) {
  270. var value = objJSON.userData[ key ];
  271. object.userData[ key ] = value;
  272. }
  273. }
  274. if ( objJSON.groups !== undefined ) {
  275. for ( var i = 0; i < objJSON.groups.length; i ++ ) {
  276. var groupID = objJSON.groups[ i ];
  277. if ( result.groups[ groupID ] === undefined ) {
  278. result.groups[ groupID ] = [];
  279. }
  280. result.groups[ groupID ].push( objID );
  281. }
  282. }
  283. }
  284. }
  285. if ( object !== undefined && objJSON.children !== undefined ) {
  286. handle_children( object, objJSON.children );
  287. }
  288. }
  289. }
  290. function handle_mesh( geo, mat, id ) {
  291. result.geometries[ id ] = geo;
  292. result.face_materials[ id ] = mat;
  293. handle_objects();
  294. }
  295. function handle_hierarchy( node, id, parent, material, obj ) {
  296. var p = obj.position;
  297. var r = obj.rotation;
  298. var q = obj.quaternion;
  299. var s = obj.scale;
  300. node.position.fromArray( p );
  301. if ( q ) {
  302. node.quaternion.fromArray( q );
  303. } else {
  304. node.rotation.fromArray( r );
  305. }
  306. node.scale.fromArray( s );
  307. // override children materials
  308. // if object material was specified in JSON explicitly
  309. if ( material ) {
  310. node.traverse( function ( child ) {
  311. child.material = material;
  312. } );
  313. }
  314. // override children visibility
  315. // with root node visibility as specified in JSON
  316. var visible = ( obj.visible !== undefined ) ? obj.visible : true;
  317. node.traverse( function ( child ) {
  318. child.visible = visible;
  319. } );
  320. parent.add( node );
  321. node.name = id;
  322. result.objects[ id ] = node;
  323. handle_objects();
  324. }
  325. function create_callback_geometry( id ) {
  326. return function ( geo, mat ) {
  327. geo.name = id;
  328. handle_mesh( geo, mat, id );
  329. counter_models -= 1;
  330. scope.onLoadComplete();
  331. async_callback_gate();
  332. }
  333. }
  334. function create_callback_hierachy( id, parent, material, obj ) {
  335. return function ( event ) {
  336. var result;
  337. // loaders which use EventDispatcher
  338. if ( event.content ) {
  339. result = event.content;
  340. // ColladaLoader
  341. } else if ( event.dae ) {
  342. result = event.scene;
  343. // UTF8Loader
  344. } else {
  345. result = event;
  346. }
  347. handle_hierarchy( result, id, parent, material, obj );
  348. counter_models -= 1;
  349. scope.onLoadComplete();
  350. async_callback_gate();
  351. }
  352. }
  353. function create_callback_embed( id ) {
  354. return function ( geo, mat ) {
  355. geo.name = id;
  356. result.geometries[ id ] = geo;
  357. result.face_materials[ id ] = mat;
  358. }
  359. }
  360. function async_callback_gate() {
  361. var progress = {
  362. totalModels : total_models,
  363. totalTextures : total_textures,
  364. loadedModels : total_models - counter_models,
  365. loadedTextures : total_textures - counter_textures
  366. };
  367. scope.callbackProgress( progress, result );
  368. scope.onLoadProgress();
  369. if ( counter_models === 0 && counter_textures === 0 ) {
  370. finalize();
  371. callbackFinished( result );
  372. }
  373. }
  374. function finalize() {
  375. // take care of targets which could be asynchronously loaded objects
  376. for ( var i = 0; i < target_array.length; i ++ ) {
  377. var ta = target_array[ i ];
  378. var target = result.objects[ ta.targetName ];
  379. if ( target ) {
  380. ta.object.target = target;
  381. } else {
  382. // if there was error and target of specified name doesn't exist in the scene file
  383. // create instead dummy target
  384. // (target must be added to scene explicitly as parent is already added)
  385. ta.object.target = new THREE.Object3D();
  386. result.scene.add( ta.object.target );
  387. }
  388. ta.object.target.userData.targetInverse = ta.object;
  389. }
  390. }
  391. var callbackTexture = function ( count ) {
  392. counter_textures -= count;
  393. async_callback_gate();
  394. scope.onLoadComplete();
  395. };
  396. // must use this instead of just directly calling callbackTexture
  397. // because of closure in the calling context loop
  398. var generateTextureCallback = function ( count ) {
  399. return function () {
  400. callbackTexture( count );
  401. };
  402. };
  403. function traverse_json_hierarchy( objJSON, callback ) {
  404. callback( objJSON );
  405. if ( objJSON.children !== undefined ) {
  406. for ( var objChildID in objJSON.children ) {
  407. traverse_json_hierarchy( objJSON.children[ objChildID ], callback );
  408. }
  409. }
  410. }
  411. // first go synchronous elements
  412. // fogs
  413. var fogID, fogJSON;
  414. for ( fogID in data.fogs ) {
  415. fogJSON = data.fogs[ fogID ];
  416. if ( fogJSON.type === "linear" ) {
  417. fog = new THREE.Fog( 0x000000, fogJSON.near, fogJSON.far );
  418. } else if ( fogJSON.type === "exp2" ) {
  419. fog = new THREE.FogExp2( 0x000000, fogJSON.density );
  420. }
  421. color = fogJSON.color;
  422. fog.color.setRGB( color[ 0 ], color[ 1 ], color[ 2 ] );
  423. result.fogs[ fogID ] = fog;
  424. }
  425. // now come potentially asynchronous elements
  426. // geometries
  427. // count how many geometries will be loaded asynchronously
  428. var geoID, geoJSON;
  429. for ( geoID in data.geometries ) {
  430. geoJSON = data.geometries[ geoID ];
  431. if ( geoJSON.type in this.geometryHandlers ) {
  432. counter_models += 1;
  433. scope.onLoadStart();
  434. }
  435. }
  436. // count how many hierarchies will be loaded asynchronously
  437. for ( var objID in data.objects ) {
  438. traverse_json_hierarchy( data.objects[ objID ], function ( objJSON ) {
  439. if ( objJSON.type && ( objJSON.type in scope.hierarchyHandlers ) ) {
  440. counter_models += 1;
  441. scope.onLoadStart();
  442. }
  443. } );
  444. }
  445. total_models = counter_models;
  446. for ( geoID in data.geometries ) {
  447. geoJSON = data.geometries[ geoID ];
  448. if ( geoJSON.type === "cube" ) {
  449. geometry = new THREE.BoxGeometry( geoJSON.width, geoJSON.height, geoJSON.depth, geoJSON.widthSegments, geoJSON.heightSegments, geoJSON.depthSegments );
  450. geometry.name = geoID;
  451. result.geometries[ geoID ] = geometry;
  452. } else if ( geoJSON.type === "plane" ) {
  453. geometry = new THREE.PlaneGeometry( geoJSON.width, geoJSON.height, geoJSON.widthSegments, geoJSON.heightSegments );
  454. geometry.name = geoID;
  455. result.geometries[ geoID ] = geometry;
  456. } else if ( geoJSON.type === "sphere" ) {
  457. geometry = new THREE.SphereGeometry( geoJSON.radius, geoJSON.widthSegments, geoJSON.heightSegments );
  458. geometry.name = geoID;
  459. result.geometries[ geoID ] = geometry;
  460. } else if ( geoJSON.type === "cylinder" ) {
  461. geometry = new THREE.CylinderGeometry( geoJSON.topRad, geoJSON.botRad, geoJSON.height, geoJSON.radSegs, geoJSON.heightSegs );
  462. geometry.name = geoID;
  463. result.geometries[ geoID ] = geometry;
  464. } else if ( geoJSON.type === "torus" ) {
  465. geometry = new THREE.TorusGeometry( geoJSON.radius, geoJSON.tube, geoJSON.segmentsR, geoJSON.segmentsT );
  466. geometry.name = geoID;
  467. result.geometries[ geoID ] = geometry;
  468. } else if ( geoJSON.type === "icosahedron" ) {
  469. geometry = new THREE.IcosahedronGeometry( geoJSON.radius, geoJSON.subdivisions );
  470. geometry.name = geoID;
  471. result.geometries[ geoID ] = geometry;
  472. } else if ( geoJSON.type in this.geometryHandlers ) {
  473. var loader = this.geometryHandlers[ geoJSON.type ][ "loaderObject" ];
  474. loader.load( get_url( geoJSON.url, data.urlBaseType ), create_callback_geometry( geoID ) );
  475. } else if ( geoJSON.type === "embedded" ) {
  476. var modelJson = data.embeds[ geoJSON.id ],
  477. texture_path = "";
  478. // pass metadata along to jsonLoader so it knows the format version
  479. modelJson.metadata = data.metadata;
  480. if ( modelJson ) {
  481. var jsonLoader = this.geometryHandlers[ "ascii" ][ "loaderObject" ];
  482. var model = jsonLoader.parse( modelJson, texture_path );
  483. create_callback_embed( geoID )( model.geometry, model.materials );
  484. }
  485. }
  486. }
  487. // textures
  488. // count how many textures will be loaded asynchronously
  489. var textureID, textureJSON;
  490. for ( textureID in data.textures ) {
  491. textureJSON = data.textures[ textureID ];
  492. if ( Array.isArray( textureJSON.url ) ) {
  493. counter_textures += textureJSON.url.length;
  494. for ( var n = 0; n < textureJSON.url.length; n ++ ) {
  495. scope.onLoadStart();
  496. }
  497. } else {
  498. counter_textures += 1;
  499. scope.onLoadStart();
  500. }
  501. }
  502. total_textures = counter_textures;
  503. for ( textureID in data.textures ) {
  504. textureJSON = data.textures[ textureID ];
  505. if ( textureJSON.mapping !== undefined && THREE[ textureJSON.mapping ] !== undefined ) {
  506. textureJSON.mapping = THREE[ textureJSON.mapping ];
  507. }
  508. var texture;
  509. if ( Array.isArray( textureJSON.url ) ) {
  510. var count = textureJSON.url.length;
  511. var url_array = [];
  512. for ( var i = 0; i < count; i ++ ) {
  513. url_array[ i ] = get_url( textureJSON.url[ i ], data.urlBaseType );
  514. }
  515. var loader = THREE.Loader.Handlers.get( url_array[ 0 ] );
  516. if ( loader !== null ) {
  517. texture = loader.load( url_array, generateTextureCallback( count ) );
  518. if ( textureJSON.mapping !== undefined )
  519. texture.mapping = textureJSON.mapping;
  520. } else {
  521. texture = THREE.ImageUtils.loadTextureCube( url_array, textureJSON.mapping, generateTextureCallback( count ) );
  522. }
  523. } else {
  524. var fullUrl = get_url( textureJSON.url, data.urlBaseType );
  525. var textureCallback = generateTextureCallback( 1 );
  526. var loader = THREE.Loader.Handlers.get( fullUrl );
  527. if ( loader !== null ) {
  528. texture = loader.load( fullUrl, textureCallback );
  529. } else {
  530. texture = new THREE.Texture();
  531. loader = new THREE.ImageLoader();
  532. ( function ( texture ) {
  533. loader.load( fullUrl, function ( image ) {
  534. texture.image = image;
  535. texture.needsUpdate = true;
  536. textureCallback();
  537. } );
  538. } )( texture )
  539. }
  540. if ( textureJSON.mapping !== undefined )
  541. texture.mapping = textureJSON.mapping;
  542. if ( THREE[ textureJSON.minFilter ] !== undefined )
  543. texture.minFilter = THREE[ textureJSON.minFilter ];
  544. if ( THREE[ textureJSON.magFilter ] !== undefined )
  545. texture.magFilter = THREE[ textureJSON.magFilter ];
  546. if ( textureJSON.anisotropy ) texture.anisotropy = textureJSON.anisotropy;
  547. if ( textureJSON.repeat ) {
  548. texture.repeat.set( textureJSON.repeat[ 0 ], textureJSON.repeat[ 1 ] );
  549. if ( textureJSON.repeat[ 0 ] !== 1 ) texture.wrapS = THREE.RepeatWrapping;
  550. if ( textureJSON.repeat[ 1 ] !== 1 ) texture.wrapT = THREE.RepeatWrapping;
  551. }
  552. if ( textureJSON.offset ) {
  553. texture.offset.set( textureJSON.offset[ 0 ], textureJSON.offset[ 1 ] );
  554. }
  555. // handle wrap after repeat so that default repeat can be overriden
  556. if ( textureJSON.wrap ) {
  557. var wrapMap = {
  558. "repeat": THREE.RepeatWrapping,
  559. "mirror": THREE.MirroredRepeatWrapping
  560. };
  561. if ( wrapMap[ textureJSON.wrap[ 0 ] ] !== undefined ) texture.wrapS = wrapMap[ textureJSON.wrap[ 0 ] ];
  562. if ( wrapMap[ textureJSON.wrap[ 1 ] ] !== undefined ) texture.wrapT = wrapMap[ textureJSON.wrap[ 1 ] ];
  563. }
  564. }
  565. result.textures[ textureID ] = texture;
  566. }
  567. // materials
  568. var matID, matJSON;
  569. var parID;
  570. for ( matID in data.materials ) {
  571. matJSON = data.materials[ matID ];
  572. for ( parID in matJSON.parameters ) {
  573. if ( parID === "envMap" || parID === "map" || parID === "lightMap" || parID === "bumpMap" || parID === "normalMap" || parID === "alphaMap" ) {
  574. matJSON.parameters[ parID ] = result.textures[ matJSON.parameters[ parID ] ];
  575. } else if ( parID === "shading" ) {
  576. matJSON.parameters[ parID ] = ( matJSON.parameters[ parID ] === "flat" ) ? THREE.FlatShading : THREE.SmoothShading;
  577. } else if ( parID === "side" ) {
  578. if ( matJSON.parameters[ parID ] == "double" ) {
  579. matJSON.parameters[ parID ] = THREE.DoubleSide;
  580. } else if ( matJSON.parameters[ parID ] == "back" ) {
  581. matJSON.parameters[ parID ] = THREE.BackSide;
  582. } else {
  583. matJSON.parameters[ parID ] = THREE.FrontSide;
  584. }
  585. } else if ( parID === "blending" ) {
  586. matJSON.parameters[ parID ] = matJSON.parameters[ parID ] in THREE ? THREE[ matJSON.parameters[ parID ] ] : THREE.NormalBlending;
  587. } else if ( parID === "combine" ) {
  588. matJSON.parameters[ parID ] = matJSON.parameters[ parID ] in THREE ? THREE[ matJSON.parameters[ parID ] ] : THREE.MultiplyOperation;
  589. } else if ( parID === "vertexColors" ) {
  590. if ( matJSON.parameters[ parID ] == "face" ) {
  591. matJSON.parameters[ parID ] = THREE.FaceColors;
  592. // default to vertex colors if "vertexColors" is anything else face colors or 0 / null / false
  593. } else if ( matJSON.parameters[ parID ] ) {
  594. matJSON.parameters[ parID ] = THREE.VertexColors;
  595. }
  596. } else if ( parID === "wrapRGB" ) {
  597. var v3 = matJSON.parameters[ parID ];
  598. matJSON.parameters[ parID ] = new THREE.Vector3( v3[ 0 ], v3[ 1 ], v3[ 2 ] );
  599. } else if ( parID === "normalScale" ) {
  600. var v2 = matJSON.parameters[ parID ];
  601. matJSON.parameters[ parID ] = new THREE.Vector2( v2[ 0 ], v2[ 1 ] );
  602. }
  603. }
  604. if ( matJSON.parameters.opacity !== undefined && matJSON.parameters.opacity < 1.0 ) {
  605. matJSON.parameters.transparent = true;
  606. }
  607. material = new THREE[ matJSON.type ]( matJSON.parameters );
  608. material.name = matID;
  609. result.materials[ matID ] = material;
  610. }
  611. // second pass through all materials to initialize MeshFaceMaterials
  612. // that could be referring to other materials out of order
  613. for ( matID in data.materials ) {
  614. matJSON = data.materials[ matID ];
  615. if ( matJSON.parameters.materials ) {
  616. var materialArray = [];
  617. for ( var i = 0; i < matJSON.parameters.materials.length; i ++ ) {
  618. var label = matJSON.parameters.materials[ i ];
  619. materialArray.push( result.materials[ label ] );
  620. }
  621. result.materials[ matID ].materials = materialArray;
  622. }
  623. }
  624. // objects ( synchronous init of procedural primitives )
  625. handle_objects();
  626. // defaults
  627. if ( result.cameras && data.defaults.camera ) {
  628. result.currentCamera = result.cameras[ data.defaults.camera ];
  629. }
  630. if ( result.fogs && data.defaults.fog ) {
  631. result.scene.fog = result.fogs[ data.defaults.fog ];
  632. }
  633. // synchronous callback
  634. scope.callbackSync( result );
  635. // just in case there are no async elements
  636. async_callback_gate();
  637. }
  638. };