SceneLoader.js 25 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183
  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. };
  14. THREE.SceneLoader.prototype.constructor = THREE.SceneLoader;
  15. THREE.SceneLoader.prototype.load = function ( url, callbackFinished ) {
  16. var scope = this;
  17. var xhr = new XMLHttpRequest();
  18. xhr.onreadystatechange = function () {
  19. if ( xhr.readyState === 4 ) {
  20. if ( xhr.status === 200 || xhr.status === 0 ) {
  21. var json = JSON.parse( xhr.responseText );
  22. scope.parse( json, callbackFinished, url );
  23. } else {
  24. console.error( "THREE.SceneLoader: Couldn't load [" + url + "] [" + xhr.status + "]" );
  25. }
  26. }
  27. };
  28. xhr.open( "GET", url, true );
  29. xhr.send( null );
  30. };
  31. THREE.SceneLoader.prototype.addGeometryHandler = function ( typeID, loaderClass ) {
  32. this.geometryHandlerMap[ typeID ] = { "loaderClass": loaderClass };
  33. };
  34. THREE.SceneLoader.prototype.addHierarchyHandler = function ( typeID, loaderClass ) {
  35. this.hierarchyHandlerMap[ typeID ] = { "loaderClass": loaderClass };
  36. };
  37. THREE.SceneLoader.prototype.parse = function ( json, callbackFinished, url ) {
  38. var scope = this;
  39. var urlBase = THREE.Loader.prototype.extractUrlBase( url );
  40. var geometry, material, camera, fog,
  41. texture, images, color,
  42. light, hex, intensity,
  43. counter_models, counter_textures,
  44. total_models, total_textures,
  45. result;
  46. var target_array = [];
  47. var data = json;
  48. // async geometry loaders
  49. for ( var typeID in this.geometryHandlerMap ) {
  50. var loaderClass = this.geometryHandlerMap[ typeID ][ "loaderClass" ];
  51. this.geometryHandlerMap[ typeID ][ "loaderObject" ] = new loaderClass();
  52. }
  53. // async hierachy loaders
  54. for ( var typeID in this.hierarchyHandlerMap ) {
  55. var loaderClass = this.hierarchyHandlerMap[ typeID ][ "loaderClass" ];
  56. this.hierarchyHandlerMap[ typeID ][ "loaderObject" ] = new loaderClass();
  57. }
  58. counter_models = 0;
  59. counter_textures = 0;
  60. result = {
  61. scene: new THREE.Scene(),
  62. geometries: {},
  63. face_materials: {},
  64. materials: {},
  65. textures: {},
  66. objects: {},
  67. cameras: {},
  68. lights: {},
  69. fogs: {},
  70. empties: {},
  71. groups: {}
  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, "userData": 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. // ColladaLoader
  125. if ( loader.options ) {
  126. loader.load( get_url( objJSON.url, data.urlBaseType ), create_callback_hierachy( objID, parent, material, objJSON ) );
  127. // UTF8Loader
  128. // OBJLoader
  129. } else {
  130. loader.load( get_url( objJSON.url, data.urlBaseType ), create_callback_hierachy( objID, parent, material, objJSON ), loaderParameters );
  131. }
  132. }
  133. } else if ( objJSON.geometry !== undefined ) {
  134. geometry = result.geometries[ objJSON.geometry ];
  135. // geometry already loaded
  136. if ( geometry ) {
  137. var needsTangents = false;
  138. material = result.materials[ objJSON.material ];
  139. needsTangents = material instanceof THREE.ShaderMaterial;
  140. pos = objJSON.position;
  141. rot = objJSON.rotation;
  142. scl = objJSON.scale;
  143. mat = objJSON.matrix;
  144. quat = objJSON.quaternion;
  145. // use materials from the model file
  146. // if there is no material specified in the object
  147. if ( ! objJSON.material ) {
  148. material = new THREE.MeshFaceMaterial( result.face_materials[ objJSON.geometry ] );
  149. }
  150. // use materials from the model file
  151. // if there is just empty face material
  152. // (must create new material as each model has its own face material)
  153. if ( ( material instanceof THREE.MeshFaceMaterial ) && material.materials.length === 0 ) {
  154. material = new THREE.MeshFaceMaterial( result.face_materials[ objJSON.geometry ] );
  155. }
  156. if ( material instanceof THREE.MeshFaceMaterial ) {
  157. for ( var i = 0; i < material.materials.length; i ++ ) {
  158. needsTangents = needsTangents || ( material.materials[ i ] instanceof THREE.ShaderMaterial );
  159. }
  160. }
  161. if ( needsTangents ) {
  162. geometry.computeTangents();
  163. }
  164. if ( objJSON.skin ) {
  165. object = new THREE.SkinnedMesh( geometry, material );
  166. } else if ( objJSON.morph ) {
  167. object = new THREE.MorphAnimMesh( geometry, material );
  168. if ( objJSON.duration !== undefined ) {
  169. object.duration = objJSON.duration;
  170. }
  171. if ( objJSON.time !== undefined ) {
  172. object.time = objJSON.time;
  173. }
  174. if ( objJSON.mirroredLoop !== undefined ) {
  175. object.mirroredLoop = objJSON.mirroredLoop;
  176. }
  177. if ( material.morphNormals ) {
  178. geometry.computeMorphNormals();
  179. }
  180. } else {
  181. object = new THREE.Mesh( geometry, material );
  182. }
  183. object.name = objID;
  184. if ( mat ) {
  185. object.matrixAutoUpdate = false;
  186. object.matrix.set(
  187. mat[0], mat[1], mat[2], mat[3],
  188. mat[4], mat[5], mat[6], mat[7],
  189. mat[8], mat[9], mat[10], mat[11],
  190. mat[12], mat[13], mat[14], mat[15]
  191. );
  192. } else {
  193. object.position.set( pos[0], pos[1], pos[2] );
  194. if ( quat ) {
  195. object.quaternion.set( quat[0], quat[1], quat[2], quat[3] );
  196. object.useQuaternion = true;
  197. } else {
  198. object.rotation.set( rot[0], rot[1], rot[2] );
  199. }
  200. object.scale.set( scl[0], scl[1], scl[2] );
  201. }
  202. object.visible = objJSON.visible;
  203. object.castShadow = objJSON.castShadow;
  204. object.receiveShadow = objJSON.receiveShadow;
  205. parent.add( object );
  206. result.objects[ objID ] = object;
  207. }
  208. // lights
  209. } else if ( objJSON.type === "DirectionalLight" || objJSON.type === "PointLight" || objJSON.type === "AmbientLight" ) {
  210. hex = ( objJSON.color !== undefined ) ? objJSON.color : 0xffffff;
  211. intensity = ( objJSON.intensity !== undefined ) ? objJSON.intensity : 1;
  212. if ( objJSON.type === "DirectionalLight" ) {
  213. pos = objJSON.direction;
  214. light = new THREE.DirectionalLight( hex, intensity );
  215. light.position.set( pos[0], pos[1], pos[2] );
  216. if ( objJSON.target ) {
  217. target_array.push( { "object": light, "targetName" : objJSON.target } );
  218. // kill existing default target
  219. // otherwise it gets added to scene when parent gets added
  220. light.target = null;
  221. }
  222. } else if ( objJSON.type === "PointLight" ) {
  223. pos = objJSON.position;
  224. dst = objJSON.distance;
  225. light = new THREE.PointLight( hex, intensity, dst );
  226. light.position.set( pos[0], pos[1], pos[2] );
  227. } else if ( objJSON.type === "AmbientLight" ) {
  228. light = new THREE.AmbientLight( hex );
  229. }
  230. parent.add( light );
  231. light.name = objID;
  232. result.lights[ objID ] = light;
  233. result.objects[ objID ] = light;
  234. // cameras
  235. } else if ( objJSON.type === "PerspectiveCamera" || objJSON.type === "OrthographicCamera" ) {
  236. if ( objJSON.type === "PerspectiveCamera" ) {
  237. camera = new THREE.PerspectiveCamera( objJSON.fov, objJSON.aspect, objJSON.near, objJSON.far );
  238. } else if ( objJSON.type === "OrthographicCamera" ) {
  239. camera = new THREE.OrthographicCamera( objJSON.left, objJSON.right, objJSON.top, objJSON.bottom, objJSON.near, objJSON.far );
  240. }
  241. pos = objJSON.position;
  242. camera.position.set( pos[0], pos[1], pos[2] );
  243. parent.add( camera );
  244. camera.name = objID;
  245. result.cameras[ objID ] = camera;
  246. result.objects[ objID ] = camera;
  247. // pure Object3D
  248. } else {
  249. pos = objJSON.position;
  250. rot = objJSON.rotation;
  251. scl = objJSON.scale;
  252. quat = objJSON.quaternion;
  253. object = new THREE.Object3D();
  254. object.name = objID;
  255. object.position.set( pos[0], pos[1], pos[2] );
  256. if ( quat ) {
  257. object.quaternion.set( quat[0], quat[1], quat[2], quat[3] );
  258. object.useQuaternion = true;
  259. } else {
  260. object.rotation.set( rot[0], rot[1], rot[2] );
  261. }
  262. object.scale.set( scl[0], scl[1], scl[2] );
  263. object.visible = ( objJSON.visible !== undefined ) ? objJSON.visible : false;
  264. parent.add( object );
  265. result.objects[ objID ] = object;
  266. result.empties[ objID ] = object;
  267. }
  268. if ( object ) {
  269. if ( objJSON.userData !== undefined ) {
  270. for ( var key in objJSON.userData ) {
  271. var value = objJSON.userData[ key ];
  272. object.userData[ key ] = value;
  273. }
  274. }
  275. if ( objJSON.groups !== undefined ) {
  276. for ( var i = 0; i < objJSON.groups.length; i ++ ) {
  277. var groupID = objJSON.groups[ i ];
  278. if ( result.groups[ groupID ] === undefined ) {
  279. result.groups[ groupID ] = [];
  280. }
  281. result.groups[ groupID ].push( objID );
  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.userData.targetInverse = ta.object;
  389. }
  390. };
  391. var callbackTexture = function ( count ) {
  392. counter_textures -= count;
  393. async_callback_gate();
  394. scope.onLoadComplete();
  395. };
  396. // must use this instead of just directly calling callbackTexture
  397. // because of closure in the calling context loop
  398. var generateTextureCallback = function ( count ) {
  399. return function () {
  400. callbackTexture( count );
  401. };
  402. };
  403. // 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. var model = jsonLoader.parse( modelJson, texture_path );
  475. create_callback_embed( geoID )( model.geometry, model.materials );
  476. }
  477. }
  478. }
  479. // textures
  480. // count how many textures will be loaded asynchronously
  481. var textureID, textureJSON;
  482. for ( textureID in data.textures ) {
  483. textureJSON = data.textures[ textureID ];
  484. if ( textureJSON.url instanceof Array ) {
  485. counter_textures += textureJSON.url.length;
  486. for( var n = 0; n < textureJSON.url.length; n ++ ) {
  487. scope.onLoadStart();
  488. }
  489. } else {
  490. counter_textures += 1;
  491. scope.onLoadStart();
  492. }
  493. }
  494. total_textures = counter_textures;
  495. for ( textureID in data.textures ) {
  496. textureJSON = data.textures[ textureID ];
  497. if ( textureJSON.mapping !== undefined && THREE[ textureJSON.mapping ] !== undefined ) {
  498. textureJSON.mapping = new THREE[ textureJSON.mapping ]();
  499. }
  500. if ( textureJSON.url instanceof Array ) {
  501. var count = textureJSON.url.length;
  502. var url_array = [];
  503. for( var i = 0; i < count; i ++ ) {
  504. url_array[ i ] = get_url( textureJSON.url[ i ], data.urlBaseType );
  505. }
  506. var isCompressed = /\.dds$/i.test( url_array[ 0 ] );
  507. if ( isCompressed ) {
  508. texture = THREE.ImageUtils.loadCompressedTextureCube( url_array, textureJSON.mapping, generateTextureCallback( count ) );
  509. } else {
  510. texture = THREE.ImageUtils.loadTextureCube( url_array, textureJSON.mapping, generateTextureCallback( count ) );
  511. }
  512. } else {
  513. var isCompressed = /\.dds$/i.test( textureJSON.url );
  514. var fullUrl = get_url( textureJSON.url, data.urlBaseType );
  515. var textureCallback = generateTextureCallback( 1 );
  516. if ( isCompressed ) {
  517. texture = THREE.ImageUtils.loadCompressedTexture( fullUrl, textureJSON.mapping, textureCallback );
  518. } else {
  519. texture = THREE.ImageUtils.loadTexture( fullUrl, textureJSON.mapping, textureCallback );
  520. }
  521. if ( THREE[ textureJSON.minFilter ] !== undefined )
  522. texture.minFilter = THREE[ textureJSON.minFilter ];
  523. if ( THREE[ textureJSON.magFilter ] !== undefined )
  524. texture.magFilter = THREE[ textureJSON.magFilter ];
  525. if ( textureJSON.anisotropy ) texture.anisotropy = textureJSON.anisotropy;
  526. if ( textureJSON.repeat ) {
  527. texture.repeat.set( textureJSON.repeat[ 0 ], textureJSON.repeat[ 1 ] );
  528. if ( textureJSON.repeat[ 0 ] !== 1 ) texture.wrapS = THREE.RepeatWrapping;
  529. if ( textureJSON.repeat[ 1 ] !== 1 ) texture.wrapT = THREE.RepeatWrapping;
  530. }
  531. if ( textureJSON.offset ) {
  532. texture.offset.set( textureJSON.offset[ 0 ], textureJSON.offset[ 1 ] );
  533. }
  534. // handle wrap after repeat so that default repeat can be overriden
  535. if ( textureJSON.wrap ) {
  536. var wrapMap = {
  537. "repeat" : THREE.RepeatWrapping,
  538. "mirror" : THREE.MirroredRepeatWrapping
  539. }
  540. if ( wrapMap[ textureJSON.wrap[ 0 ] ] !== undefined ) texture.wrapS = wrapMap[ textureJSON.wrap[ 0 ] ];
  541. if ( wrapMap[ textureJSON.wrap[ 1 ] ] !== undefined ) texture.wrapT = wrapMap[ textureJSON.wrap[ 1 ] ];
  542. }
  543. }
  544. result.textures[ textureID ] = texture;
  545. }
  546. // materials
  547. var matID, matJSON;
  548. var parID;
  549. for ( matID in data.materials ) {
  550. matJSON = data.materials[ matID ];
  551. for ( parID in matJSON.parameters ) {
  552. if ( parID === "envMap" || parID === "map" || parID === "lightMap" || parID === "bumpMap" ) {
  553. matJSON.parameters[ parID ] = result.textures[ matJSON.parameters[ parID ] ];
  554. } else if ( parID === "shading" ) {
  555. matJSON.parameters[ parID ] = ( matJSON.parameters[ parID ] === "flat" ) ? THREE.FlatShading : THREE.SmoothShading;
  556. } else if ( parID === "side" ) {
  557. if ( matJSON.parameters[ parID ] == "double" ) {
  558. matJSON.parameters[ parID ] = THREE.DoubleSide;
  559. } else if ( matJSON.parameters[ parID ] == "back" ) {
  560. matJSON.parameters[ parID ] = THREE.BackSide;
  561. } else {
  562. matJSON.parameters[ parID ] = THREE.FrontSide;
  563. }
  564. } else if ( parID === "blending" ) {
  565. matJSON.parameters[ parID ] = matJSON.parameters[ parID ] in THREE ? THREE[ matJSON.parameters[ parID ] ] : THREE.NormalBlending;
  566. } else if ( parID === "combine" ) {
  567. matJSON.parameters[ parID ] = matJSON.parameters[ parID ] in THREE ? THREE[ matJSON.parameters[ parID ] ] : THREE.MultiplyOperation;
  568. } else if ( parID === "vertexColors" ) {
  569. if ( matJSON.parameters[ parID ] == "face" ) {
  570. matJSON.parameters[ parID ] = THREE.FaceColors;
  571. // default to vertex colors if "vertexColors" is anything else face colors or 0 / null / false
  572. } else if ( matJSON.parameters[ parID ] ) {
  573. matJSON.parameters[ parID ] = THREE.VertexColors;
  574. }
  575. } else if ( parID === "wrapRGB" ) {
  576. var v3 = matJSON.parameters[ parID ];
  577. matJSON.parameters[ parID ] = new THREE.Vector3( v3[ 0 ], v3[ 1 ], v3[ 2 ] );
  578. }
  579. }
  580. if ( matJSON.parameters.opacity !== undefined && matJSON.parameters.opacity < 1.0 ) {
  581. matJSON.parameters.transparent = true;
  582. }
  583. if ( matJSON.parameters.normalMap ) {
  584. var shader = THREE.ShaderLib[ "normalmap" ];
  585. var uniforms = THREE.UniformsUtils.clone( shader.uniforms );
  586. var diffuse = matJSON.parameters.color;
  587. var specular = matJSON.parameters.specular;
  588. var ambient = matJSON.parameters.ambient;
  589. var shininess = matJSON.parameters.shininess;
  590. uniforms[ "tNormal" ].value = result.textures[ matJSON.parameters.normalMap ];
  591. if ( matJSON.parameters.normalScale ) {
  592. uniforms[ "uNormalScale" ].value.set( matJSON.parameters.normalScale[ 0 ], matJSON.parameters.normalScale[ 1 ] );
  593. }
  594. if ( matJSON.parameters.map ) {
  595. uniforms[ "tDiffuse" ].value = matJSON.parameters.map;
  596. uniforms[ "enableDiffuse" ].value = true;
  597. }
  598. if ( matJSON.parameters.envMap ) {
  599. uniforms[ "tCube" ].value = matJSON.parameters.envMap;
  600. uniforms[ "enableReflection" ].value = true;
  601. uniforms[ "uReflectivity" ].value = matJSON.parameters.reflectivity;
  602. }
  603. if ( matJSON.parameters.lightMap ) {
  604. uniforms[ "tAO" ].value = matJSON.parameters.lightMap;
  605. uniforms[ "enableAO" ].value = true;
  606. }
  607. if ( matJSON.parameters.specularMap ) {
  608. uniforms[ "tSpecular" ].value = result.textures[ matJSON.parameters.specularMap ];
  609. uniforms[ "enableSpecular" ].value = true;
  610. }
  611. if ( matJSON.parameters.displacementMap ) {
  612. uniforms[ "tDisplacement" ].value = result.textures[ matJSON.parameters.displacementMap ];
  613. uniforms[ "enableDisplacement" ].value = true;
  614. uniforms[ "uDisplacementBias" ].value = matJSON.parameters.displacementBias;
  615. uniforms[ "uDisplacementScale" ].value = matJSON.parameters.displacementScale;
  616. }
  617. uniforms[ "uDiffuseColor" ].value.setHex( diffuse );
  618. uniforms[ "uSpecularColor" ].value.setHex( specular );
  619. uniforms[ "uAmbientColor" ].value.setHex( ambient );
  620. uniforms[ "uShininess" ].value = shininess;
  621. if ( matJSON.parameters.opacity ) {
  622. uniforms[ "uOpacity" ].value = matJSON.parameters.opacity;
  623. }
  624. var parameters = { fragmentShader: shader.fragmentShader, vertexShader: shader.vertexShader, uniforms: uniforms, lights: true, fog: true };
  625. material = new THREE.ShaderMaterial( parameters );
  626. } else {
  627. material = new THREE[ matJSON.type ]( matJSON.parameters );
  628. }
  629. result.materials[ matID ] = material;
  630. }
  631. // second pass through all materials to initialize MeshFaceMaterials
  632. // that could be referring to other materials out of order
  633. for ( matID in data.materials ) {
  634. matJSON = data.materials[ matID ];
  635. if ( matJSON.parameters.materials ) {
  636. var materialArray = [];
  637. for ( var i = 0; i < matJSON.parameters.materials.length; i ++ ) {
  638. var label = matJSON.parameters.materials[ i ];
  639. materialArray.push( result.materials[ label ] );
  640. }
  641. result.materials[ matID ].materials = materialArray;
  642. }
  643. }
  644. // objects ( synchronous init of procedural primitives )
  645. handle_objects();
  646. // defaults
  647. if ( result.cameras && data.defaults.camera ) {
  648. result.currentCamera = result.cameras[ data.defaults.camera ];
  649. }
  650. if ( result.fogs && data.defaults.fog ) {
  651. result.scene.fog = result.fogs[ data.defaults.fog ];
  652. }
  653. // synchronous callback
  654. scope.callbackSync( result );
  655. // just in case there are no async elements
  656. async_callback_gate();
  657. };
粤ICP备19079148号