SceneLoader.js 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188
  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.geometryHandlerMap = {};
  11. this.hierarchyHandlerMap = {};
  12. this.addGeometryHandler( "ascii", THREE.JSONLoader );
  13. this.addGeometryHandler( "binary", THREE.BinaryLoader );
  14. };
  15. THREE.SceneLoader.prototype.constructor = THREE.SceneLoader;
  16. THREE.SceneLoader.prototype.load = function ( url, callbackFinished ) {
  17. var scope = this;
  18. var xhr = new XMLHttpRequest();
  19. xhr.onreadystatechange = function () {
  20. if ( xhr.readyState === 4 ) {
  21. if ( xhr.status === 200 || xhr.status === 0 ) {
  22. var json = JSON.parse( xhr.responseText );
  23. scope.parse( json, callbackFinished, url );
  24. } else {
  25. console.error( "THREE.SceneLoader: Couldn't load [" + url + "] [" + xhr.status + "]" );
  26. }
  27. }
  28. };
  29. xhr.open( "GET", url, true );
  30. xhr.send( null );
  31. };
  32. THREE.SceneLoader.prototype.addGeometryHandler = function ( typeID, loaderClass ) {
  33. this.geometryHandlerMap[ typeID ] = { "loaderClass": loaderClass };
  34. };
  35. THREE.SceneLoader.prototype.addHierarchyHandler = function ( typeID, loaderClass ) {
  36. this.hierarchyHandlerMap[ typeID ] = { "loaderClass": loaderClass };
  37. };
  38. THREE.SceneLoader.prototype.parse = function ( json, callbackFinished, url ) {
  39. var scope = this;
  40. var urlBase = THREE.Loader.prototype.extractUrlBase( url );
  41. var geometry, material, camera, fog,
  42. texture, images, color,
  43. light, hex, intensity,
  44. counter_models, counter_textures,
  45. total_models, total_textures,
  46. result;
  47. var target_array = [];
  48. var data = json;
  49. // async geometry loaders
  50. for ( var typeID in this.geometryHandlerMap ) {
  51. var loaderClass = this.geometryHandlerMap[ typeID ][ "loaderClass" ];
  52. this.geometryHandlerMap[ typeID ][ "loaderObject" ] = new loaderClass();
  53. }
  54. // async hierachy loaders
  55. for ( var typeID in this.hierarchyHandlerMap ) {
  56. var loaderClass = this.hierarchyHandlerMap[ typeID ][ "loaderClass" ];
  57. this.hierarchyHandlerMap[ typeID ][ "loaderObject" ] = new loaderClass();
  58. }
  59. counter_models = 0;
  60. counter_textures = 0;
  61. result = {
  62. scene: new THREE.Scene(),
  63. geometries: {},
  64. face_materials: {},
  65. materials: {},
  66. textures: {},
  67. objects: {},
  68. cameras: {},
  69. lights: {},
  70. fogs: {},
  71. empties: {}
  72. };
  73. if ( data.transform ) {
  74. var position = data.transform.position,
  75. rotation = data.transform.rotation,
  76. scale = data.transform.scale;
  77. if ( position )
  78. result.scene.position.set( position[ 0 ], position[ 1 ], position [ 2 ] );
  79. if ( rotation )
  80. result.scene.rotation.set( rotation[ 0 ], rotation[ 1 ], rotation [ 2 ] );
  81. if ( scale )
  82. result.scene.scale.set( scale[ 0 ], scale[ 1 ], scale [ 2 ] );
  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. if ( result.objects[ objID ] === undefined ) {
  106. var objJSON = children[ objID ];
  107. var object = null;
  108. // meshes
  109. if ( objJSON.type && ( objJSON.type in scope.hierarchyHandlerMap ) ) {
  110. if ( objJSON.loading === undefined ) {
  111. var reservedTypes = { "type": 1, "url": 1, "material": 1,
  112. "position": 1, "rotation": 1, "scale" : 1,
  113. "visible": 1, "children": 1, "properties": 1,
  114. "skin": 1, "morph": 1, "mirroredLoop": 1, "duration": 1 };
  115. var loaderParameters = {};
  116. for ( var parType in objJSON ) {
  117. if ( ! ( parType in reservedTypes ) ) {
  118. loaderParameters[ parType ] = objJSON[ parType ];
  119. }
  120. }
  121. material = result.materials[ objJSON.material ];
  122. objJSON.loading = true;
  123. var loader = scope.hierarchyHandlerMap[ objJSON.type ][ "loaderObject" ];
  124. // OBJLoader
  125. if ( loader.addEventListener ) {
  126. loader.addEventListener( 'load', create_callback_hierachy( objID, parent, material, objJSON ) );
  127. loader.load( get_url( objJSON.url, data.urlBaseType ) );
  128. } else {
  129. // ColladaLoader
  130. if ( loader.options ) {
  131. loader.load( get_url( objJSON.url, data.urlBaseType ), create_callback_hierachy( objID, parent, material, objJSON ) );
  132. // UTF8Loader
  133. } else {
  134. loader.load( get_url( objJSON.url, data.urlBaseType ), create_callback_hierachy( objID, parent, material, objJSON ), loaderParameters );
  135. }
  136. }
  137. }
  138. } else if ( objJSON.geometry !== undefined ) {
  139. geometry = result.geometries[ objJSON.geometry ];
  140. // geometry already loaded
  141. if ( geometry ) {
  142. var needsTangents = false;
  143. material = result.materials[ objJSON.material ];
  144. needsTangents = material instanceof THREE.ShaderMaterial;
  145. pos = objJSON.position;
  146. rot = objJSON.rotation;
  147. scl = objJSON.scale;
  148. mat = objJSON.matrix;
  149. quat = objJSON.quaternion;
  150. // turn off quaternions, for the moment
  151. quat = 0;
  152. // use materials from the model file
  153. // if there is no material specified in the object
  154. if ( ! objJSON.material ) {
  155. material = new THREE.MeshFaceMaterial( result.face_materials[ objJSON.geometry ] );
  156. }
  157. // use materials from the model file
  158. // if there is just empty face material
  159. // (must create new material as each model has its own face material)
  160. if ( ( material instanceof THREE.MeshFaceMaterial ) && material.materials.length === 0 ) {
  161. material = new THREE.MeshFaceMaterial( result.face_materials[ objJSON.geometry ] );
  162. }
  163. if ( material instanceof THREE.MeshFaceMaterial ) {
  164. for ( var i = 0; i < material.materials.length; i ++ ) {
  165. needsTangents = needsTangents || ( material.materials[ i ] instanceof THREE.ShaderMaterial );
  166. }
  167. }
  168. if ( needsTangents ) {
  169. geometry.computeTangents();
  170. }
  171. if ( objJSON.skin ) {
  172. object = new THREE.SkinnedMesh( geometry, material );
  173. } else if ( objJSON.morph ) {
  174. object = new THREE.MorphAnimMesh( geometry, material );
  175. if ( objJSON.duration !== undefined ) {
  176. object.duration = objJSON.duration;
  177. }
  178. if ( objJSON.time !== undefined ) {
  179. object.time = objJSON.time;
  180. }
  181. if ( objJSON.mirroredLoop !== undefined ) {
  182. object.mirroredLoop = objJSON.mirroredLoop;
  183. }
  184. if ( material.morphNormals ) {
  185. geometry.computeMorphNormals();
  186. }
  187. } else {
  188. object = new THREE.Mesh( geometry, material );
  189. }
  190. object.name = objID;
  191. if ( mat ) {
  192. object.matrixAutoUpdate = false;
  193. object.matrix.set(
  194. mat[0], mat[1], mat[2], mat[3],
  195. mat[4], mat[5], mat[6], mat[7],
  196. mat[8], mat[9], mat[10], mat[11],
  197. mat[12], mat[13], mat[14], mat[15]
  198. );
  199. } else {
  200. object.position.set( pos[0], pos[1], pos[2] );
  201. if ( quat ) {
  202. object.quaternion.set( quat[0], quat[1], quat[2], quat[3] );
  203. object.useQuaternion = true;
  204. } else {
  205. object.rotation.set( rot[0], rot[1], rot[2] );
  206. }
  207. object.scale.set( scl[0], scl[1], scl[2] );
  208. }
  209. object.visible = objJSON.visible;
  210. object.castShadow = objJSON.castShadow;
  211. object.receiveShadow = objJSON.receiveShadow;
  212. parent.add( object );
  213. result.objects[ objID ] = object;
  214. }
  215. // lights
  216. } else if ( objJSON.type === "DirectionalLight" || objJSON.type === "PointLight" || objJSON.type === "AmbientLight" ) {
  217. hex = ( objJSON.color !== undefined ) ? objJSON.color : 0xffffff;
  218. intensity = ( objJSON.intensity !== undefined ) ? objJSON.intensity : 1;
  219. if ( objJSON.type === "DirectionalLight" ) {
  220. pos = objJSON.direction;
  221. light = new THREE.DirectionalLight( hex, intensity );
  222. light.position.set( pos[0], pos[1], pos[2] );
  223. if ( objJSON.target ) {
  224. target_array.push( { "object": light, "targetName" : objJSON.target } );
  225. // kill existing default target
  226. // otherwise it gets added to scene when parent gets added
  227. light.target = null;
  228. }
  229. } else if ( objJSON.type === "PointLight" ) {
  230. pos = objJSON.position;
  231. dst = objJSON.distance;
  232. light = new THREE.PointLight( hex, intensity, dst );
  233. light.position.set( pos[0], pos[1], pos[2] );
  234. } else if ( objJSON.type === "AmbientLight" ) {
  235. light = new THREE.AmbientLight( hex );
  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. if ( objJSON.type === "PerspectiveCamera" ) {
  244. camera = new THREE.PerspectiveCamera( objJSON.fov, objJSON.aspect, objJSON.near, objJSON.far );
  245. } else if ( objJSON.type === "OrthographicCamera" ) {
  246. camera = new THREE.OrthographicCamera( objJSON.left, objJSON.right, objJSON.top, objJSON.bottom, objJSON.near, objJSON.far );
  247. }
  248. pos = objJSON.position;
  249. camera.position.set( pos[0], pos[1], pos[2] );
  250. parent.add( camera );
  251. camera.name = objID;
  252. result.cameras[ objID ] = camera;
  253. result.objects[ objID ] = camera;
  254. // pure Object3D
  255. } else {
  256. pos = objJSON.position;
  257. rot = objJSON.rotation;
  258. scl = objJSON.scale;
  259. quat = objJSON.quaternion;
  260. // turn off quaternions, for the moment
  261. quat = 0;
  262. object = new THREE.Object3D();
  263. object.name = objID;
  264. object.position.set( pos[0], pos[1], pos[2] );
  265. if ( quat ) {
  266. object.quaternion.set( quat[0], quat[1], quat[2], quat[3] );
  267. object.useQuaternion = true;
  268. } else {
  269. object.rotation.set( rot[0], rot[1], rot[2] );
  270. }
  271. object.scale.set( scl[0], scl[1], scl[2] );
  272. object.visible = ( objJSON.visible !== undefined ) ? objJSON.visible : false;
  273. parent.add( object );
  274. result.objects[ objID ] = object;
  275. result.empties[ objID ] = object;
  276. }
  277. if ( object ) {
  278. if ( objJSON.properties !== undefined ) {
  279. for ( var key in objJSON.properties ) {
  280. var value = objJSON.properties[ key ];
  281. object.properties[ key ] = value;
  282. }
  283. }
  284. if ( objJSON.children !== undefined ) {
  285. handle_children( object, objJSON.children );
  286. }
  287. }
  288. }
  289. }
  290. };
  291. function handle_mesh( geo, mat, id ) {
  292. result.geometries[ id ] = geo;
  293. result.face_materials[ id ] = mat;
  294. handle_objects();
  295. };
  296. function handle_hierarchy( node, id, parent, material, obj ) {
  297. var p = obj.position;
  298. var r = obj.rotation;
  299. var q = obj.quaternion;
  300. var s = obj.scale;
  301. node.position.set( p[0], p[1], p[2] );
  302. if ( q ) {
  303. node.quaternion.set( q[0], q[1], q[2], q[3] );
  304. node.useQuaternion = true;
  305. } else {
  306. node.rotation.set( r[0], r[1], r[2] );
  307. }
  308. node.scale.set( s[0], s[1], s[2] );
  309. // override children materials
  310. // if object material was specified in JSON explicitly
  311. if ( material ) {
  312. node.traverse( function ( child ) {
  313. child.material = material;
  314. } );
  315. }
  316. // override children visibility
  317. // with root node visibility as specified in JSON
  318. var visible = ( obj.visible !== undefined ) ? obj.visible : true;
  319. node.traverse( function ( child ) {
  320. child.visible = visible;
  321. } );
  322. parent.add( node );
  323. node.name = id;
  324. result.objects[ id ] = node;
  325. handle_objects();
  326. };
  327. function create_callback_geometry( id ) {
  328. return function( geo, mat ) {
  329. handle_mesh( geo, mat, id );
  330. counter_models -= 1;
  331. scope.onLoadComplete();
  332. async_callback_gate();
  333. }
  334. };
  335. function create_callback_hierachy( id, parent, material, obj ) {
  336. return function( event ) {
  337. var result;
  338. // loaders which use EventDispatcher
  339. if ( event.content ) {
  340. result = event.content;
  341. // ColladaLoader
  342. } else if ( event.dae ) {
  343. result = event.scene;
  344. // UTF8Loader
  345. } else {
  346. result = event;
  347. }
  348. handle_hierarchy( result, id, parent, material, obj );
  349. counter_models -= 1;
  350. scope.onLoadComplete();
  351. async_callback_gate();
  352. }
  353. };
  354. function create_callback_embed( id ) {
  355. return function( geo, mat ) {
  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.properties.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. // first go synchronous elements
  404. // fogs
  405. var fogID, fogJSON;
  406. for ( fogID in data.fogs ) {
  407. fogJSON = data.fogs[ fogID ];
  408. if ( fogJSON.type === "linear" ) {
  409. fog = new THREE.Fog( 0x000000, fogJSON.near, fogJSON.far );
  410. } else if ( fogJSON.type === "exp2" ) {
  411. fog = new THREE.FogExp2( 0x000000, fogJSON.density );
  412. }
  413. color = fogJSON.color;
  414. fog.color.setRGB( color[0], color[1], color[2] );
  415. result.fogs[ fogID ] = fog;
  416. }
  417. // now come potentially asynchronous elements
  418. // geometries
  419. // count how many geometries will be loaded asynchronously
  420. var geoID, geoJSON;
  421. for ( geoID in data.geometries ) {
  422. geoJSON = data.geometries[ geoID ];
  423. if ( geoJSON.type in this.geometryHandlerMap ) {
  424. counter_models += 1;
  425. scope.onLoadStart();
  426. }
  427. }
  428. // count how many hierarchies will be loaded asynchronously
  429. var objID, objJSON;
  430. for ( objID in data.objects ) {
  431. objJSON = data.objects[ objID ];
  432. if ( objJSON.type && ( objJSON.type in this.hierarchyHandlerMap ) ) {
  433. counter_models += 1;
  434. scope.onLoadStart();
  435. }
  436. }
  437. total_models = counter_models;
  438. for ( geoID in data.geometries ) {
  439. geoJSON = data.geometries[ geoID ];
  440. if ( geoJSON.type === "cube" ) {
  441. geometry = new THREE.CubeGeometry( geoJSON.width, geoJSON.height, geoJSON.depth, geoJSON.widthSegments, geoJSON.heightSegments, geoJSON.depthSegments );
  442. result.geometries[ geoID ] = geometry;
  443. } else if ( geoJSON.type === "plane" ) {
  444. geometry = new THREE.PlaneGeometry( geoJSON.width, geoJSON.height, geoJSON.widthSegments, geoJSON.heightSegments );
  445. result.geometries[ geoID ] = geometry;
  446. } else if ( geoJSON.type === "sphere" ) {
  447. geometry = new THREE.SphereGeometry( geoJSON.radius, geoJSON.widthSegments, geoJSON.heightSegments );
  448. result.geometries[ geoID ] = geometry;
  449. } else if ( geoJSON.type === "cylinder" ) {
  450. geometry = new THREE.CylinderGeometry( geoJSON.topRad, geoJSON.botRad, geoJSON.height, geoJSON.radSegs, geoJSON.heightSegs );
  451. result.geometries[ geoID ] = geometry;
  452. } else if ( geoJSON.type === "torus" ) {
  453. geometry = new THREE.TorusGeometry( geoJSON.radius, geoJSON.tube, geoJSON.segmentsR, geoJSON.segmentsT );
  454. result.geometries[ geoID ] = geometry;
  455. } else if ( geoJSON.type === "icosahedron" ) {
  456. geometry = new THREE.IcosahedronGeometry( geoJSON.radius, geoJSON.subdivisions );
  457. result.geometries[ geoID ] = geometry;
  458. } else if ( geoJSON.type in this.geometryHandlerMap ) {
  459. var loaderParameters = {};
  460. for ( var parType in geoJSON ) {
  461. if ( parType !== "type" && parType !== "url" ) {
  462. loaderParameters[ parType ] = geoJSON[ parType ];
  463. }
  464. }
  465. var loader = this.geometryHandlerMap[ geoJSON.type ][ "loaderObject" ];
  466. loader.load( get_url( geoJSON.url, data.urlBaseType ), create_callback_geometry( geoID ), loaderParameters );
  467. } else if ( geoJSON.type === "embedded" ) {
  468. var modelJson = data.embeds[ geoJSON.id ],
  469. texture_path = "";
  470. // pass metadata along to jsonLoader so it knows the format version
  471. modelJson.metadata = data.metadata;
  472. if ( modelJson ) {
  473. var jsonLoader = this.geometryHandlerMap[ "ascii" ][ "loaderObject" ];
  474. jsonLoader.createModel( modelJson, create_callback_embed( geoID ), texture_path );
  475. }
  476. }
  477. }
  478. // textures
  479. // count how many textures will be loaded asynchronously
  480. var textureID, textureJSON;
  481. for ( textureID in data.textures ) {
  482. textureJSON = data.textures[ textureID ];
  483. if ( textureJSON.url instanceof Array ) {
  484. counter_textures += textureJSON.url.length;
  485. for( var n = 0; n < textureJSON.url.length; n ++ ) {
  486. scope.onLoadStart();
  487. }
  488. } else {
  489. counter_textures += 1;
  490. scope.onLoadStart();
  491. }
  492. }
  493. total_textures = counter_textures;
  494. for ( textureID in data.textures ) {
  495. textureJSON = data.textures[ textureID ];
  496. if ( textureJSON.mapping !== undefined && THREE[ textureJSON.mapping ] !== undefined ) {
  497. textureJSON.mapping = new THREE[ textureJSON.mapping ]();
  498. }
  499. if ( textureJSON.url instanceof Array ) {
  500. var count = textureJSON.url.length;
  501. var url_array = [];
  502. for( var i = 0; i < count; i ++ ) {
  503. url_array[ i ] = get_url( textureJSON.url[ i ], data.urlBaseType );
  504. }
  505. var isCompressed = url_array[ 0 ].endsWith( ".dds" );
  506. if ( isCompressed ) {
  507. texture = THREE.ImageUtils.loadCompressedTextureCube( url_array, textureJSON.mapping, generateTextureCallback( count ) );
  508. } else {
  509. texture = THREE.ImageUtils.loadTextureCube( url_array, textureJSON.mapping, generateTextureCallback( count ) );
  510. }
  511. } else {
  512. var isCompressed = textureJSON.url.toLowerCase().endsWith( ".dds" );
  513. var fullUrl = get_url( textureJSON.url, data.urlBaseType );
  514. var textureCallback = generateTextureCallback( 1 );
  515. if ( isCompressed ) {
  516. texture = THREE.ImageUtils.loadCompressedTexture( fullUrl, textureJSON.mapping, textureCallback );
  517. } else {
  518. texture = THREE.ImageUtils.loadTexture( fullUrl, textureJSON.mapping, textureCallback );
  519. }
  520. if ( THREE[ textureJSON.minFilter ] !== undefined )
  521. texture.minFilter = THREE[ textureJSON.minFilter ];
  522. if ( THREE[ textureJSON.magFilter ] !== undefined )
  523. texture.magFilter = THREE[ textureJSON.magFilter ];
  524. if ( textureJSON.anisotropy ) texture.anisotropy = textureJSON.anisotropy;
  525. if ( textureJSON.repeat ) {
  526. texture.repeat.set( textureJSON.repeat[ 0 ], textureJSON.repeat[ 1 ] );
  527. if ( textureJSON.repeat[ 0 ] !== 1 ) texture.wrapS = THREE.RepeatWrapping;
  528. if ( textureJSON.repeat[ 1 ] !== 1 ) texture.wrapT = THREE.RepeatWrapping;
  529. }
  530. if ( textureJSON.offset ) {
  531. texture.offset.set( textureJSON.offset[ 0 ], textureJSON.offset[ 1 ] );
  532. }
  533. // handle wrap after repeat so that default repeat can be overriden
  534. if ( textureJSON.wrap ) {
  535. var wrapMap = {
  536. "repeat" : THREE.RepeatWrapping,
  537. "mirror" : THREE.MirroredRepeatWrapping
  538. }
  539. if ( wrapMap[ textureJSON.wrap[ 0 ] ] !== undefined ) texture.wrapS = wrapMap[ textureJSON.wrap[ 0 ] ];
  540. if ( wrapMap[ textureJSON.wrap[ 1 ] ] !== undefined ) texture.wrapT = wrapMap[ textureJSON.wrap[ 1 ] ];
  541. }
  542. }
  543. result.textures[ textureID ] = texture;
  544. }
  545. // materials
  546. var matID, matJSON;
  547. var parID;
  548. for ( matID in data.materials ) {
  549. matJSON = data.materials[ matID ];
  550. for ( parID in matJSON.parameters ) {
  551. if ( parID === "envMap" || parID === "map" || parID === "lightMap" || parID === "bumpMap" ) {
  552. matJSON.parameters[ parID ] = result.textures[ matJSON.parameters[ parID ] ];
  553. } else if ( parID === "shading" ) {
  554. matJSON.parameters[ parID ] = ( matJSON.parameters[ parID ] === "flat" ) ? THREE.FlatShading : THREE.SmoothShading;
  555. } else if ( parID === "side" ) {
  556. if ( matJSON.parameters[ parID ] == "double" ) {
  557. matJSON.parameters[ parID ] = THREE.DoubleSide;
  558. } else if ( matJSON.parameters[ parID ] == "back" ) {
  559. matJSON.parameters[ parID ] = THREE.BackSide;
  560. } else {
  561. matJSON.parameters[ parID ] = THREE.FrontSide;
  562. }
  563. } else if ( parID === "blending" ) {
  564. matJSON.parameters[ parID ] = matJSON.parameters[ parID ] in THREE ? THREE[ matJSON.parameters[ parID ] ] : THREE.NormalBlending;
  565. } else if ( parID === "combine" ) {
  566. matJSON.parameters[ parID ] = ( matJSON.parameters[ parID ] == "MixOperation" ) ? THREE.MixOperation : THREE.MultiplyOperation;
  567. } else if ( parID === "vertexColors" ) {
  568. if ( matJSON.parameters[ parID ] == "face" ) {
  569. matJSON.parameters[ parID ] = THREE.FaceColors;
  570. // default to vertex colors if "vertexColors" is anything else face colors or 0 / null / false
  571. } else if ( matJSON.parameters[ parID ] ) {
  572. matJSON.parameters[ parID ] = THREE.VertexColors;
  573. }
  574. } else if ( parID === "wrapRGB" ) {
  575. var v3 = matJSON.parameters[ parID ];
  576. matJSON.parameters[ parID ] = new THREE.Vector3( v3[ 0 ], v3[ 1 ], v3[ 2 ] );
  577. }
  578. }
  579. if ( matJSON.parameters.opacity !== undefined && matJSON.parameters.opacity < 1.0 ) {
  580. matJSON.parameters.transparent = true;
  581. }
  582. if ( matJSON.parameters.normalMap ) {
  583. var shader = THREE.ShaderUtils.lib[ "normal" ];
  584. var uniforms = THREE.UniformsUtils.clone( shader.uniforms );
  585. var diffuse = matJSON.parameters.color;
  586. var specular = matJSON.parameters.specular;
  587. var ambient = matJSON.parameters.ambient;
  588. var shininess = matJSON.parameters.shininess;
  589. uniforms[ "tNormal" ].value = result.textures[ matJSON.parameters.normalMap ];
  590. if ( matJSON.parameters.normalScale ) {
  591. uniforms[ "uNormalScale" ].value.set( matJSON.parameters.normalScale[ 0 ], matJSON.parameters.normalScale[ 1 ] );
  592. }
  593. if ( matJSON.parameters.map ) {
  594. uniforms[ "tDiffuse" ].value = matJSON.parameters.map;
  595. uniforms[ "enableDiffuse" ].value = true;
  596. }
  597. if ( matJSON.parameters.envMap ) {
  598. uniforms[ "tCube" ].value = matJSON.parameters.envMap;
  599. uniforms[ "enableReflection" ].value = true;
  600. uniforms[ "uReflectivity" ].value = matJSON.parameters.reflectivity;
  601. }
  602. if ( matJSON.parameters.lightMap ) {
  603. uniforms[ "tAO" ].value = matJSON.parameters.lightMap;
  604. uniforms[ "enableAO" ].value = true;
  605. }
  606. if ( matJSON.parameters.specularMap ) {
  607. uniforms[ "tSpecular" ].value = result.textures[ matJSON.parameters.specularMap ];
  608. uniforms[ "enableSpecular" ].value = true;
  609. }
  610. if ( matJSON.parameters.displacementMap ) {
  611. uniforms[ "tDisplacement" ].value = result.textures[ matJSON.parameters.displacementMap ];
  612. uniforms[ "enableDisplacement" ].value = true;
  613. uniforms[ "uDisplacementBias" ].value = matJSON.parameters.displacementBias;
  614. uniforms[ "uDisplacementScale" ].value = matJSON.parameters.displacementScale;
  615. }
  616. uniforms[ "uDiffuseColor" ].value.setHex( diffuse );
  617. uniforms[ "uSpecularColor" ].value.setHex( specular );
  618. uniforms[ "uAmbientColor" ].value.setHex( ambient );
  619. uniforms[ "uShininess" ].value = shininess;
  620. if ( matJSON.parameters.opacity ) {
  621. uniforms[ "uOpacity" ].value = matJSON.parameters.opacity;
  622. }
  623. var parameters = { fragmentShader: shader.fragmentShader, vertexShader: shader.vertexShader, uniforms: uniforms, lights: true, fog: true };
  624. material = new THREE.ShaderMaterial( parameters );
  625. } else {
  626. material = new THREE[ matJSON.type ]( matJSON.parameters );
  627. }
  628. result.materials[ matID ] = material;
  629. }
  630. // second pass through all materials to initialize MeshFaceMaterials
  631. // that could be referring to other materials out of order
  632. for ( matID in data.materials ) {
  633. matJSON = data.materials[ matID ];
  634. if ( matJSON.parameters.materials ) {
  635. var materialArray = [];
  636. for ( var i = 0; i < matJSON.parameters.materials.length; i ++ ) {
  637. var label = matJSON.parameters.materials[ i ];
  638. materialArray.push( result.materials[ label ] );
  639. }
  640. result.materials[ matID ].materials = materialArray;
  641. }
  642. }
  643. // objects ( synchronous init of procedural primitives )
  644. handle_objects();
  645. // defaults
  646. if ( result.cameras && data.defaults.camera ) {
  647. result.currentCamera = result.cameras[ data.defaults.camera ];
  648. }
  649. if ( result.fogs && data.defaults.fog ) {
  650. result.scene.fog = result.fogs[ data.defaults.fog ];
  651. }
  652. color = data.defaults.bgcolor;
  653. result.bgColor = new THREE.Color();
  654. result.bgColor.setRGB( color[0], color[1], color[2] );
  655. result.bgColorAlpha = data.defaults.bgalpha;
  656. // synchronous callback
  657. scope.callbackSync( result );
  658. // just in case there are no async elements
  659. async_callback_gate();
  660. };