SceneLoader.js 28 KB

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