SceneLoader.js 24 KB

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