SceneLoader.js 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484
  1. /**
  2. * @author alteredq / http://alteredqualia.com/
  3. */
  4. THREE.SceneLoader = function ( ) {
  5. };
  6. THREE.SceneLoader.prototype = {
  7. load : function( url, callback_sync, callback_async, callback_progress ) {
  8. var worker = new Worker( url );
  9. worker.postMessage( 0 );
  10. var urlBase = THREE.Loader.prototype.extractUrlbase( url );
  11. worker.onmessage = function( event ) {
  12. var dg, dm, dd, dl, dc, df, dt,
  13. g, o, m, l, p, c, t, f, tt, pp,
  14. geometry, material, camera, fog,
  15. texture, images,
  16. materials,
  17. data, binLoader, jsonLoader,
  18. counter_models, counter_textures,
  19. total_models, total_textures,
  20. result;
  21. data = event.data;
  22. binLoader = new THREE.BinaryLoader();
  23. jsonLoader = new THREE.JSONLoader();
  24. counter_models = 0;
  25. counter_textures = 0;
  26. result = {
  27. scene: new THREE.Scene(),
  28. geometries: {},
  29. materials: {},
  30. textures: {},
  31. objects: {},
  32. cameras: {},
  33. lights: {},
  34. fogs: {}
  35. };
  36. if ( data.transform ) {
  37. var position = data.transform.position,
  38. rotation = data.transform.rotation,
  39. scale = data.transform.scale;
  40. if ( position )
  41. result.scene.position.set( position[ 0 ], position[ 1 ], position [ 2 ] );
  42. if ( rotation )
  43. result.scene.rotation.set( rotation[ 0 ], rotation[ 1 ], rotation [ 2 ] );
  44. if ( scale )
  45. result.scene.scale.set( scale[ 0 ], scale[ 1 ], scale [ 2 ] );
  46. if ( position || rotation || scale )
  47. result.scene.updateMatrix();
  48. }
  49. function get_url( source_url, url_type ) {
  50. if ( url_type == "relativeToHTML" ) {
  51. return source_url;
  52. } else {
  53. return urlBase + "/" + source_url;
  54. }
  55. };
  56. function handle_objects() {
  57. for( dd in data.objects ) {
  58. if ( !result.objects[ dd ] ) {
  59. o = data.objects[ dd ];
  60. geometry = result.geometries[ o.geometry ];
  61. if ( geometry ) {
  62. materials = [];
  63. for( i = 0; i < o.materials.length; i++ ) {
  64. materials[ i ] = result.materials[ o.materials[i] ];
  65. }
  66. p = o.position;
  67. r = o.rotation;
  68. q = o.quaternion;
  69. s = o.scale;
  70. // turn off quaternions, for the moment
  71. q = 0;
  72. if ( materials.length == 0 ) {
  73. materials[ 0 ] = new THREE.MeshFaceMaterial();
  74. }
  75. object = new THREE.Mesh( geometry, materials );
  76. object.position.set( p[0], p[1], p[2] );
  77. if ( q ) {
  78. object.quaternion.set( q[0], q[1], q[2], q[3] );
  79. object.useQuaternion = true;
  80. } else {
  81. object.rotation.set( r[0], r[1], r[2] );
  82. }
  83. object.scale.set( s[0], s[1], s[2] );
  84. object.visible = o.visible;
  85. result.scene.addObject( object );
  86. result.objects[ dd ] = object;
  87. if ( o.meshCollider ) {
  88. var meshCollider = THREE.CollisionUtils.MeshColliderWBox( object );
  89. THREE.Collisions.colliders.push( meshCollider );
  90. }
  91. }
  92. }
  93. }
  94. };
  95. function handle_mesh( geo, id ) {
  96. result.geometries[ id ] = geo;
  97. handle_objects();
  98. };
  99. function create_callback( id ) {
  100. return function( geo ) {
  101. handle_mesh( geo, id );
  102. counter_models -= 1;
  103. async_callback_gate();
  104. }
  105. };
  106. function async_callback_gate() {
  107. var progress = {
  108. total_models: total_models,
  109. total_textures: total_textures,
  110. loaded_models: total_models - counter_models,
  111. loaded_textures: total_textures - counter_textures
  112. };
  113. callback_progress( progress, result );
  114. if( counter_models == 0 && counter_textures == 0 ) {
  115. callback_async( result );
  116. }
  117. };
  118. var callback_texture = function( images ) {
  119. counter_textures -= 1;
  120. async_callback_gate();
  121. };
  122. // first go synchronous elements
  123. // cameras
  124. for( dc in data.cameras ) {
  125. c = data.cameras[ dc ];
  126. if ( c.type == "perspective" ) {
  127. camera = new THREE.Camera( c.fov, c.aspect, c.near, c.far );
  128. } else if ( c.type == "ortho" ) {
  129. camera = new THREE.Camera();
  130. camera.projectionMatrix = THREE.Matrix4.makeOrtho( c.left, c.right, c.top, c.bottom, c.near, c.far );
  131. }
  132. p = c.position;
  133. t = c.target;
  134. camera.position.set( p[0], p[1], p[2] );
  135. camera.target.position.set( t[0], t[1], t[2] );
  136. result.cameras[ dc ] = camera;
  137. }
  138. // lights
  139. var hex, intensity;
  140. for( dl in data.lights ) {
  141. l = data.lights[ dl ];
  142. hex = ( l.color !== undefined ) ? l.color : 0xffffff;
  143. intensity = ( l.intensity !== undefined ) ? l.intensity : 1;
  144. if ( l.type == "directional" ) {
  145. p = l.direction;
  146. light = new THREE.DirectionalLight( hex, intensity );
  147. light.position.set( p[0], p[1], p[2] );
  148. light.position.normalize();
  149. } else if ( l.type == "point" ) {
  150. p = l.position;
  151. light = new THREE.PointLight( hex, intensity );
  152. light.position.set( p[0], p[1], p[2] );
  153. }
  154. result.scene.addLight( light );
  155. result.lights[ dl ] = light;
  156. }
  157. // fogs
  158. for( df in data.fogs ) {
  159. f = data.fogs[ df ];
  160. if ( f.type == "linear" ) {
  161. fog = new THREE.Fog( 0x000000, f.near, f.far );
  162. } else if ( f.type == "exp2" ) {
  163. fog = new THREE.FogExp2( 0x000000, f.density );
  164. }
  165. c = f.color;
  166. fog.color.setRGB( c[0], c[1], c[2] );
  167. result.fogs[ df ] = fog;
  168. }
  169. // defaults
  170. if ( result.cameras && data.defaults.camera ) {
  171. result.currentCamera = result.cameras[ data.defaults.camera ];
  172. }
  173. if ( result.fogs && data.defaults.fog ) {
  174. result.scene.fog = result.fogs[ data.defaults.fog ];
  175. }
  176. c = data.defaults.bgcolor;
  177. result.bgColor = new THREE.Color();
  178. result.bgColor.setRGB( c[0], c[1], c[2] );
  179. result.bgColorAlpha = data.defaults.bgalpha;
  180. // now come potentially asynchronous elements
  181. // geometries
  182. // count how many models will be loaded asynchronously
  183. for( dg in data.geometries ) {
  184. g = data.geometries[ dg ];
  185. if ( g.type == "bin_mesh" || g.type == "ascii_mesh" ) {
  186. counter_models += 1;
  187. }
  188. }
  189. total_models = counter_models;
  190. for( dg in data.geometries ) {
  191. g = data.geometries[ dg ];
  192. if ( g.type == "cube" ) {
  193. geometry = new Cube( g.width, g.height, g.depth, g.segmentsWidth, g.segmentsHeight, g.segmentsDepth, null, g.flipped, g.sides );
  194. result.geometries[ dg ] = geometry;
  195. } else if ( g.type == "plane" ) {
  196. geometry = new Plane( g.width, g.height, g.segmentsWidth, g.segmentsHeight );
  197. result.geometries[ dg ] = geometry;
  198. } else if ( g.type == "sphere" ) {
  199. geometry = new Sphere( g.radius, g.segmentsWidth, g.segmentsHeight );
  200. result.geometries[ dg ] = geometry;
  201. } else if ( g.type == "cylinder" ) {
  202. geometry = new Cylinder( g.numSegs, g.topRad, g.botRad, g.height, g.topOffset, g.botOffset );
  203. result.geometries[ dg ] = geometry;
  204. } else if ( g.type == "torus" ) {
  205. geometry = new Torus( g.radius, g.tube, g.segmentsR, g.segmentsT );
  206. result.geometries[ dg ] = geometry;
  207. } else if ( g.type == "icosahedron" ) {
  208. geometry = new Icosahedron( g.subdivisions );
  209. result.geometries[ dg ] = geometry;
  210. } else if ( g.type == "bin_mesh" ) {
  211. binLoader.load( { model: get_url( g.url, data.urlBaseType ),
  212. callback: create_callback( dg )
  213. } );
  214. } else if ( g.type == "ascii_mesh" ) {
  215. jsonLoader.load( { model: get_url( g.url, data.urlBaseType ),
  216. callback: create_callback( dg )
  217. } );
  218. }
  219. }
  220. // textures
  221. // count how many textures will be loaded asynchronously
  222. for( dt in data.textures ) {
  223. tt = data.textures[ dt ];
  224. if( tt.url instanceof Array ) {
  225. counter_textures += tt.url.length;
  226. } else {
  227. counter_textures += 1;
  228. }
  229. }
  230. total_textures = counter_textures;
  231. for( dt in data.textures ) {
  232. tt = data.textures[ dt ];
  233. if ( tt.mapping != undefined && THREE[ tt.mapping ] != undefined ) {
  234. tt.mapping = new THREE[ tt.mapping ]();
  235. }
  236. if( tt.url instanceof Array ) {
  237. var url_array = [];
  238. for( var i = 0; i < tt.url.length; i ++ ) {
  239. url_array[ i ] = get_url( tt.url[ i ], data.urlBaseType );
  240. }
  241. texture = ImageUtils.loadTextureCube( url_array, tt.mapping, callback_texture );
  242. } else {
  243. texture = ImageUtils.loadTexture( get_url( tt.url, data.urlBaseType ), tt.mapping, callback_texture );
  244. if ( THREE[ tt.minFilter ] != undefined )
  245. texture.minFilter = THREE[ tt.minFilter ];
  246. if ( THREE[ tt.magFilter ] != undefined )
  247. texture.magFilter = THREE[ tt.magFilter ];
  248. }
  249. result.textures[ dt ] = texture;
  250. }
  251. // materials
  252. for( dm in data.materials ) {
  253. m = data.materials[ dm ];
  254. for( pp in m.parameters ) {
  255. if ( pp == "envMap" || pp == "map" || pp == "lightMap" ) {
  256. m.parameters[ pp ] = result.textures[ m.parameters[ pp ] ];
  257. } else if ( pp == "shading" ) {
  258. m.parameters[ pp ] = ( m.parameters[ pp ] == "flat" ) ? THREE.FlatShading : THREE.SmoothShading;
  259. } else if ( pp == "blending" ) {
  260. m.parameters[ pp ] = THREE[ m.parameters[ pp ] ] ? THREE[ m.parameters[ pp ] ] : THREE.NormalBlending;
  261. } else if ( pp == "combine" ) {
  262. m.parameters[ pp ] = ( m.parameters[ pp ] == "MixOperation" ) ? THREE.MixOperation : THREE.MultiplyOperation;
  263. }
  264. }
  265. material = new THREE[ m.type ]( m.parameters );
  266. result.materials[ dm ] = material;
  267. }
  268. // objects ( synchronous init of procedural primitives )
  269. handle_objects();
  270. // synchronous callback
  271. callback_sync( result );
  272. };
  273. }
  274. };