babylon.sceneLoader.js 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682
  1. var BABYLON = BABYLON || {};
  2. (function () {
  3. var loadCubeTexture = function (rootUrl, parsedTexture, scene) {
  4. var texture = new BABYLON.CubeTexture(rootUrl + parsedTexture.name, scene);
  5. texture.name = parsedTexture.name;
  6. texture.hasAlpha = parsedTexture.hasAlpha;
  7. texture.level = parsedTexture.level;
  8. texture.coordinatesMode = parsedTexture.coordinatesMode;
  9. return texture;
  10. };
  11. var loadTexture = function (rootUrl, parsedTexture, scene) {
  12. if (!parsedTexture.name && !parsedTexture.isRenderTarget) {
  13. return null;
  14. }
  15. if (parsedTexture.isCube) {
  16. return loadCubeTexture(rootUrl, parsedTexture, scene);
  17. }
  18. var texture;
  19. if (parsedTexture.mirrorPlane) {
  20. texture = new BABYLON.MirrorTexture(parsedTexture.name, parsedTexture.renderTargetSize, scene);
  21. texture._waitingRenderList = parsedTexture.renderList;
  22. texture.mirrorPlane = BABYLON.Plane.FromArray(parsedTexture.mirrorPlane);
  23. } else if (parsedTexture.isRenderTarget) {
  24. texture = new BABYLON.RenderTargetTexture(parsedTexture.name, parsedTexture.renderTargetSize, scene);
  25. texture._waitingRenderList = parsedTexture.renderList;
  26. } else {
  27. texture = new BABYLON.Texture(rootUrl + parsedTexture.name, scene);
  28. }
  29. texture.name = parsedTexture.name;
  30. texture.hasAlpha = parsedTexture.hasAlpha;
  31. texture.level = parsedTexture.level;
  32. texture.coordinatesIndex = parsedTexture.coordinatesIndex;
  33. texture.coordinatesMode = parsedTexture.coordinatesMode;
  34. texture.uOffset = parsedTexture.uOffset;
  35. texture.vOffset = parsedTexture.vOffset;
  36. texture.uScale = parsedTexture.uScale;
  37. texture.vScale = parsedTexture.vScale;
  38. texture.uAng = parsedTexture.uAng;
  39. texture.vAng = parsedTexture.vAng;
  40. texture.wAng = parsedTexture.wAng;
  41. texture.wrapU = parsedTexture.wrapU;
  42. texture.wrapV = parsedTexture.wrapV;
  43. // Animations
  44. if (parsedTexture.animations) {
  45. for (var animationIndex = 0; animationIndex < parsedTexture.animations.length; animationIndex++) {
  46. var parsedAnimation = parsedTexture.animations[animationIndex];
  47. texture.animations.push(parseAnimation(parsedAnimation));
  48. }
  49. }
  50. return texture;
  51. };
  52. var parseSkeleton = function (parsedSkeleton, scene) {
  53. var skeleton = new BABYLON.Skeleton(parsedSkeleton.name, parsedSkeleton.id, scene);
  54. for (var index = 0; index < parsedSkeleton.bones.length; index++) {
  55. var parsedBone = parsedSkeleton.bones[index];
  56. var parentBone = null;
  57. if (parsedBone.parentBoneIndex > -1) {
  58. parentBone = skeleton.bones[parsedBone.parentBoneIndex];
  59. }
  60. var bone = new BABYLON.Bone(parsedBone.name, skeleton, parentBone, BABYLON.Matrix.FromArray(parsedBone.matrix));
  61. if (parsedBone.animation) {
  62. bone.animations.push(parseAnimation(parsedBone.animation));
  63. }
  64. }
  65. return skeleton;
  66. };
  67. var parseMaterial = function (parsedMaterial, scene, rootUrl) {
  68. var material;
  69. material = new BABYLON.StandardMaterial(parsedMaterial.name, scene);
  70. material.ambientColor = BABYLON.Color3.FromArray(parsedMaterial.ambient);
  71. material.diffuseColor = BABYLON.Color3.FromArray(parsedMaterial.diffuse);
  72. material.specularColor = BABYLON.Color3.FromArray(parsedMaterial.specular);
  73. material.specularPower = parsedMaterial.specularPower;
  74. material.emissiveColor = BABYLON.Color3.FromArray(parsedMaterial.emissive);
  75. material.alpha = parsedMaterial.alpha;
  76. material.id = parsedMaterial.id;
  77. material.backFaceCulling = parsedMaterial.backFaceCulling;
  78. if (parsedMaterial.diffuseTexture) {
  79. material.diffuseTexture = loadTexture(rootUrl, parsedMaterial.diffuseTexture, scene);
  80. }
  81. if (parsedMaterial.ambientTexture) {
  82. material.ambientTexture = loadTexture(rootUrl, parsedMaterial.ambientTexture, scene);
  83. }
  84. if (parsedMaterial.opacityTexture) {
  85. material.opacityTexture = loadTexture(rootUrl, parsedMaterial.opacityTexture, scene);
  86. }
  87. if (parsedMaterial.reflectionTexture) {
  88. material.reflectionTexture = loadTexture(rootUrl, parsedMaterial.reflectionTexture, scene);
  89. }
  90. if (parsedMaterial.emissiveTexture) {
  91. material.emissiveTexture = loadTexture(rootUrl, parsedMaterial.emissiveTexture, scene);
  92. }
  93. if (parsedMaterial.specularTexture) {
  94. material.specularTexture = loadTexture(rootUrl, parsedMaterial.specularTexture, scene);
  95. }
  96. if (parsedMaterial.bumpTexture) {
  97. material.bumpTexture = loadTexture(rootUrl, parsedMaterial.bumpTexture, scene);
  98. }
  99. return material;
  100. };
  101. var parseMaterialById = function (id, parsedData, scene, rootUrl) {
  102. for (var index = 0; index < parsedData.materials.length; index++) {
  103. var parsedMaterial = parsedData.materials[index];
  104. if (parsedMaterial.id === id) {
  105. return parseMaterial(parsedMaterial, scene, rootUrl);
  106. }
  107. }
  108. return null;
  109. };
  110. var parseMultiMaterial = function (parsedMultiMaterial, scene) {
  111. var multiMaterial = new BABYLON.MultiMaterial(parsedMultiMaterial.name, scene);
  112. multiMaterial.id = parsedMultiMaterial.id;
  113. for (var matIndex = 0; matIndex < parsedMultiMaterial.materials.length; matIndex++) {
  114. var subMatId = parsedMultiMaterial.materials[matIndex];
  115. if (subMatId) {
  116. multiMaterial.subMaterials.push(scene.getMaterialByID(subMatId));
  117. } else {
  118. multiMaterial.subMaterials.push(null);
  119. }
  120. }
  121. return multiMaterial;
  122. };
  123. var parseParticleSystem = function (parsedParticleSystem, scene, rootUrl) {
  124. var emitter = scene.getLastMeshByID(parsedParticleSystem.emitterId);
  125. var particleSystem = new BABYLON.ParticleSystem("particles#" + emitter.name, parsedParticleSystem.capacity, scene);
  126. if (parsedParticleSystem.textureName) {
  127. particleSystem.particleTexture = new BABYLON.Texture(rootUrl + parsedParticleSystem.textureName, scene);
  128. }
  129. particleSystem.minAngularSpeed = parsedParticleSystem.minAngularSpeed;
  130. particleSystem.maxAngularSpeed = parsedParticleSystem.maxAngularSpeed;
  131. particleSystem.minSize = parsedParticleSystem.minSize;
  132. particleSystem.maxSize = parsedParticleSystem.maxSize;
  133. particleSystem.minLifeTime = parsedParticleSystem.minLifeTime;
  134. particleSystem.maxLifeTime = parsedParticleSystem.maxLifeTime;
  135. particleSystem.emitter = emitter;
  136. particleSystem.emitRate = parsedParticleSystem.emitRate;
  137. particleSystem.minEmitBox = BABYLON.Vector3.FromArray(parsedParticleSystem.minEmitBox);
  138. particleSystem.maxEmitBox = BABYLON.Vector3.FromArray(parsedParticleSystem.maxEmitBox);
  139. particleSystem.gravity = BABYLON.Vector3.FromArray(parsedParticleSystem.gravity);
  140. particleSystem.direction1 = BABYLON.Vector3.FromArray(parsedParticleSystem.direction1);
  141. particleSystem.direction2 = BABYLON.Vector3.FromArray(parsedParticleSystem.direction2);
  142. particleSystem.color1 = BABYLON.Color4.FromArray(parsedParticleSystem.color1);
  143. particleSystem.color2 = BABYLON.Color4.FromArray(parsedParticleSystem.color2);
  144. particleSystem.colorDead = BABYLON.Color4.FromArray(parsedParticleSystem.colorDead);
  145. particleSystem.updateSpeed = parsedParticleSystem.updateSpeed;
  146. particleSystem.targetStopDuration = parsedParticleSystem.targetStopFrame;
  147. particleSystem.textureMask = BABYLON.Color4.FromArray(parsedParticleSystem.textureMask);
  148. particleSystem.blendMode = parsedParticleSystem.blendMode;
  149. particleSystem.start();
  150. return particleSystem;
  151. };
  152. var parseShadowGenerator = function (parsedShadowGenerator, scene) {
  153. var light = scene.getLightByID(parsedShadowGenerator.lightId);
  154. var shadowGenerator = new BABYLON.ShadowGenerator(parsedShadowGenerator.mapSize, light);
  155. for (var meshIndex = 0; meshIndex < parsedShadowGenerator.renderList.length; meshIndex++) {
  156. var mesh = scene.getMeshByID(parsedShadowGenerator.renderList[meshIndex]);
  157. shadowGenerator.getShadowMap().renderList.push(mesh);
  158. }
  159. shadowGenerator.useVarianceShadowMap = parsedShadowGenerator.useVarianceShadowMap;
  160. return shadowGenerator;
  161. };
  162. var parseAnimation = function (parsedAnimation) {
  163. var animation = new BABYLON.Animation(parsedAnimation.name, parsedAnimation.property, parsedAnimation.framePerSecond, parsedAnimation.dataType, parsedAnimation.loopBehavior);
  164. var dataType = parsedAnimation.dataType;
  165. var keys = [];
  166. for (var index = 0; index < parsedAnimation.keys.length; index++) {
  167. var key = parsedAnimation.keys[index];
  168. var data;
  169. switch (dataType) {
  170. case BABYLON.Animation.ANIMATIONTYPE_FLOAT:
  171. data = key.values[0];
  172. break;
  173. case BABYLON.Animation.ANIMATIONTYPE_QUATERNION:
  174. data = BABYLON.Quaternion.FromArray(key.values);
  175. break;
  176. case BABYLON.Animation.ANIMATIONTYPE_MATRIX:
  177. data = BABYLON.Matrix.FromArray(key.values);
  178. break;
  179. case BABYLON.Animation.ANIMATIONTYPE_VECTOR3:
  180. default:
  181. data = BABYLON.Vector3.FromArray(key.values);
  182. break;
  183. }
  184. keys.push({
  185. frame: key.frame,
  186. value: data
  187. });
  188. }
  189. animation.setKeys(keys);
  190. return animation;
  191. };
  192. var parseLight = function (parsedLight, scene) {
  193. var light;
  194. switch (parsedLight.type) {
  195. case 0:
  196. light = new BABYLON.PointLight(parsedLight.name, BABYLON.Vector3.FromArray(parsedLight.position), scene);
  197. break;
  198. case 1:
  199. light = new BABYLON.DirectionalLight(parsedLight.name, BABYLON.Vector3.FromArray(parsedLight.direction), scene);
  200. light.position = BABYLON.Vector3.FromArray(parsedLight.position);
  201. break;
  202. case 2:
  203. light = new BABYLON.SpotLight(parsedLight.name, BABYLON.Vector3.FromArray(parsedLight.position), BABYLON.Vector3.FromArray(parsedLight.direction), parsedLight.angle, parsedLight.exponent, scene);
  204. break;
  205. case 3:
  206. light = new BABYLON.HemisphericLight(parsedLight.name, BABYLON.Vector3.FromArray(parsedLight.direction), scene);
  207. light.groundColor = BABYLON.Color3.FromArray(parsedLight.groundColor);
  208. break;
  209. }
  210. light.id = parsedLight.id;
  211. if (parsedLight.intensity) {
  212. light.intensity = parsedLight.intensity;
  213. }
  214. light.diffuse = BABYLON.Color3.FromArray(parsedLight.diffuse);
  215. light.specular = BABYLON.Color3.FromArray(parsedLight.specular);
  216. };
  217. var parseCamera = function (parsedCamera, scene) {
  218. var camera = new BABYLON.FreeCamera(parsedCamera.name, BABYLON.Vector3.FromArray(parsedCamera.position), scene);
  219. camera.id = parsedCamera.id;
  220. // Parent
  221. if (parsedCamera.parentId) {
  222. camera._waitingParentId = parsedCamera.parentId;
  223. }
  224. // Target
  225. if (parsedCamera.target) {
  226. camera.setTarget(BABYLON.Vector3.FromArray(parsedCamera.target));
  227. } else {
  228. camera.rotation = BABYLON.Vector3.FromArray(parsedCamera.rotation);
  229. }
  230. // Locked target
  231. if (parsedCamera.lockedTargetId) {
  232. camera._waitingLockedTargetId = parsedCamera.lockedTargetId;
  233. }
  234. camera.fov = parsedCamera.fov;
  235. camera.minZ = parsedCamera.minZ;
  236. camera.maxZ = parsedCamera.maxZ;
  237. camera.speed = parsedCamera.speed;
  238. camera.inertia = parsedCamera.inertia;
  239. camera.checkCollisions = parsedCamera.checkCollisions;
  240. camera.applyGravity = parsedCamera.applyGravity;
  241. if (parsedCamera.ellipsoid) {
  242. camera.ellipsoid = BABYLON.Vector3.FromArray(parsedCamera.ellipsoid);
  243. }
  244. // Animations
  245. if (parsedCamera.animations) {
  246. for (var animationIndex = 0; animationIndex < parsedCamera.animations.length; animationIndex++) {
  247. var parsedAnimation = parsedCamera.animations[animationIndex];
  248. camera.animations.push(parseAnimation(parsedAnimation));
  249. }
  250. }
  251. if (parsedCamera.autoAnimate) {
  252. scene.beginAnimation(camera, parsedCamera.autoAnimateFrom, parsedCamera.autoAnimateTo, parsedCamera.autoAnimateLoop, 1.0);
  253. }
  254. return camera;
  255. };
  256. var parseMesh = function (parsedMesh, scene, rootUrl) {
  257. var mesh = new BABYLON.Mesh(parsedMesh.name, scene);
  258. mesh.id = parsedMesh.id;
  259. mesh.position = BABYLON.Vector3.FromArray(parsedMesh.position);
  260. if (parsedMesh.rotation) {
  261. mesh.rotation = BABYLON.Vector3.FromArray(parsedMesh.rotation);
  262. } else if (parsedMesh.rotationQuaternion) {
  263. mesh.rotationQuaternion = BABYLON.Quaternion.FromArray(parsedMesh.rotationQuaternion);
  264. }
  265. mesh.scaling = BABYLON.Vector3.FromArray(parsedMesh.scaling);
  266. if (parsedMesh.localMatrix) {
  267. mesh.setPivotMatrix(BABYLON.Matrix.FromArray(parsedMesh.localMatrix));
  268. }
  269. mesh.setEnabled(parsedMesh.isEnabled);
  270. mesh.isVisible = parsedMesh.isVisible;
  271. mesh.receiveShadows = parsedMesh.receiveShadows;
  272. mesh.billboardMode = parsedMesh.billboardMode;
  273. if (parsedMesh.visibility !== undefined) {
  274. mesh.visibility = parsedMesh.visibility;
  275. }
  276. mesh.checkCollisions = parsedMesh.checkCollisions;
  277. if (parsedMesh.delayLoadingFile) {
  278. mesh.delayLoadState = BABYLON.Engine.DELAYLOADSTATE_NOTLOADED;
  279. mesh.delayLoadingFile = rootUrl + parsedMesh.delayLoadingFile;
  280. mesh._boundingInfo = new BABYLON.BoundingInfo(BABYLON.Vector3.FromArray(parsedMesh.boundingBoxMinimum), BABYLON.Vector3.FromArray(parsedMesh.boundingBoxMaximum));
  281. mesh._delayInfo = [];
  282. if (parsedMesh.hasUVs) {
  283. mesh._delayInfo.push(BABYLON.VertexBuffer.UVKind);
  284. }
  285. if (parsedMesh.hasUVs2) {
  286. mesh._delayInfo.push(BABYLON.VertexBuffer.UV2Kind);
  287. }
  288. if (parsedMesh.hasColors) {
  289. mesh._delayInfo.push(BABYLON.VertexBuffer.ColorKind);
  290. }
  291. if (parsedMesh.hasMatricesIndices) {
  292. mesh._delayInfo.push(BABYLON.VertexBuffer.MatricesIndicesKind);
  293. }
  294. if (parsedMesh.hasMatricesWeights) {
  295. mesh._delayInfo.push(BABYLON.VertexBuffer.MatricesWeightsKind);
  296. }
  297. } else {
  298. BABYLON.SceneLoader._ImportGeometry(parsedMesh, mesh);
  299. }
  300. // Parent
  301. if (parsedMesh.parentId) {
  302. mesh.parent = scene.getLastEntryByID(parsedMesh.parentId);
  303. }
  304. // Material
  305. if (parsedMesh.materialId) {
  306. mesh.setMaterialByID(parsedMesh.materialId);
  307. } else {
  308. mesh.material = null;
  309. }
  310. // Skeleton
  311. if (parsedMesh.skeletonId > -1) {
  312. mesh.skeleton = scene.getLastSkeletonByID(parsedMesh.skeletonId);
  313. }
  314. // Animations
  315. if (parsedMesh.animations) {
  316. for (var animationIndex = 0; animationIndex < parsedMesh.animations.length; animationIndex++) {
  317. var parsedAnimation = parsedMesh.animations[animationIndex];
  318. mesh.animations.push(parseAnimation(parsedAnimation));
  319. }
  320. }
  321. if (parsedMesh.autoAnimate) {
  322. scene.beginAnimation(mesh, parsedMesh.autoAnimateFrom, parsedMesh.autoAnimateTo, parsedMesh.autoAnimateLoop, 1.0);
  323. }
  324. return mesh;
  325. };
  326. var isDescendantOf = function (mesh, name, hierarchyIds) {
  327. if (mesh.name === name) {
  328. hierarchyIds.push(mesh.id);
  329. return true;
  330. }
  331. if (mesh.parentId && hierarchyIds.indexOf(mesh.parentId) !== -1) {
  332. hierarchyIds.push(mesh.id);
  333. return true;
  334. }
  335. return false;
  336. };
  337. BABYLON.SceneLoader = {
  338. _ImportGeometry: function (parsedGeometry, mesh) {
  339. // Geometry
  340. if (parsedGeometry.positions && parsedGeometry.normals && parsedGeometry.indices) {
  341. mesh.setVerticesData(parsedGeometry.positions, BABYLON.VertexBuffer.PositionKind, false);
  342. mesh.setVerticesData(parsedGeometry.normals, BABYLON.VertexBuffer.NormalKind, false);
  343. if (parsedGeometry.uvs) {
  344. mesh.setVerticesData(parsedGeometry.uvs, BABYLON.VertexBuffer.UVKind, false);
  345. }
  346. if (parsedGeometry.uvs2) {
  347. mesh.setVerticesData(parsedGeometry.uvs2, BABYLON.VertexBuffer.UV2Kind, false);
  348. }
  349. if (parsedGeometry.colors) {
  350. mesh.setVerticesData(parsedGeometry.colors, BABYLON.VertexBuffer.ColorKind, false);
  351. }
  352. if (parsedGeometry.matricesIndices) {
  353. var floatIndices = [];
  354. for (var i = 0; i < parsedGeometry.matricesIndices.length; i++) {
  355. var matricesIndex = parsedGeometry.matricesIndices[i];
  356. floatIndices.push(matricesIndex & 0x000000FF);
  357. floatIndices.push((matricesIndex & 0x0000FF00) >> 8);
  358. floatIndices.push((matricesIndex & 0x00FF0000) >> 16);
  359. floatIndices.push(matricesIndex >> 24);
  360. }
  361. mesh.setVerticesData(floatIndices, BABYLON.VertexBuffer.MatricesIndicesKind, false);
  362. }
  363. if (parsedGeometry.matricesWeights) {
  364. mesh.setVerticesData(parsedGeometry.matricesWeights, BABYLON.VertexBuffer.MatricesWeightsKind, false);
  365. }
  366. mesh.setIndices(parsedGeometry.indices);
  367. }
  368. // SubMeshes
  369. if (parsedGeometry.subMeshes) {
  370. mesh.subMeshes = [];
  371. for (var subIndex = 0; subIndex < parsedGeometry.subMeshes.length; subIndex++) {
  372. var parsedSubMesh = parsedGeometry.subMeshes[subIndex];
  373. var subMesh = new BABYLON.SubMesh(parsedSubMesh.materialIndex, parsedSubMesh.verticesStart, parsedSubMesh.verticesCount, parsedSubMesh.indexStart, parsedSubMesh.indexCount, mesh);
  374. }
  375. }
  376. // Update
  377. mesh.computeWorldMatrix(true);
  378. var scene = mesh.getScene();
  379. if (scene._selectionOctree) {
  380. scene._selectionOctree.addMesh(mesh);
  381. }
  382. },
  383. ImportMesh: function (meshName, rootUrl, sceneFilename, scene, then, progressCallBack) {
  384. // Checking if a manifest file has been set for this scene and if offline mode has been requested
  385. var database = new BABYLON.Database(rootUrl + sceneFilename);
  386. scene.database = database;
  387. BABYLON.Tools.LoadFile(rootUrl + sceneFilename, function (data) {
  388. var parsedData = JSON.parse(data);
  389. // Meshes
  390. var meshes = [];
  391. var particleSystems = [];
  392. var skeletons = [];
  393. var loadedSkeletonsIds = [];
  394. var loadedMaterialsIds = [];
  395. var hierarchyIds = [];
  396. for (var index = 0; index < parsedData.meshes.length; index++) {
  397. var parsedMesh = parsedData.meshes[index];
  398. if (!meshName || isDescendantOf(parsedMesh, meshName, hierarchyIds)) {
  399. // Material ?
  400. if (parsedMesh.materialId) {
  401. var materialFound = (loadedMaterialsIds.indexOf(parsedMesh.materialId) !== -1);
  402. if (!materialFound) {
  403. for (var multimatIndex = 0; multimatIndex < parsedData.multiMaterials.length; multimatIndex++) {
  404. var parsedMultiMaterial = parsedData.multiMaterials[multimatIndex];
  405. if (parsedMultiMaterial.id == parsedMesh.materialId) {
  406. for (var matIndex = 0; matIndex < parsedMultiMaterial.materials.length; matIndex++) {
  407. var subMatId = parsedMultiMaterial.materials[matIndex];
  408. loadedMaterialsIds.push(subMatId);
  409. parseMaterialById(subMatId, parsedData, scene, rootUrl);
  410. }
  411. loadedMaterialsIds.push(parsedMultiMaterial.id);
  412. parseMultiMaterial(parsedMultiMaterial, scene);
  413. materialFound = true;
  414. break;
  415. }
  416. }
  417. }
  418. if (!materialFound) {
  419. loadedMaterialsIds.push(parsedMesh.materialId);
  420. parseMaterialById(parsedMesh.materialId, parsedData, scene, rootUrl);
  421. }
  422. }
  423. // Skeleton ?
  424. if (parsedMesh.skeletonId > -1 && scene.skeletons) {
  425. var skeletonAlreadyLoaded = (loadedSkeletonsIds.indexOf(parsedMesh.skeletonId) > -1);
  426. if (!skeletonAlreadyLoaded) {
  427. for (var skeletonIndex = 0; skeletonIndex < parsedData.skeletons.length; skeletonIndex++) {
  428. var parsedSkeleton = parsedData.skeletons[skeletonIndex];
  429. if (parsedSkeleton.id === parsedMesh.skeletonId) {
  430. skeletons.push(parseSkeleton(parsedSkeleton, scene));
  431. loadedSkeletonsIds.push(parsedSkeleton.id);
  432. }
  433. }
  434. }
  435. }
  436. var mesh = parseMesh(parsedMesh, scene, rootUrl);
  437. meshes.push(mesh);
  438. }
  439. }
  440. // Particles
  441. if (parsedData.particleSystems) {
  442. for (var index = 0; index < parsedData.particleSystems.length; index++) {
  443. var parsedParticleSystem = parsedData.particleSystems[index];
  444. if (hierarchyIds.indexOf(parsedParticleSystem.emitterId) !== -1) {
  445. particleSystems.push(parseParticleSystem(parsedParticleSystem, scene, rootUrl));
  446. }
  447. }
  448. }
  449. if (then) {
  450. then(meshes, particleSystems, skeletons);
  451. }
  452. }, progressCallBack, database);
  453. },
  454. Load: function (rootUrl, sceneFilename, engine, then, progressCallBack) {
  455. // Checking if a manifest file has been set for this scene and if offline mode has been requested
  456. var database = new BABYLON.Database(rootUrl + sceneFilename);
  457. BABYLON.Tools.LoadFile(rootUrl + sceneFilename, function (data) {
  458. var parsedData = JSON.parse(data);
  459. var scene = new BABYLON.Scene(engine);
  460. scene.database = database;
  461. // Scene
  462. scene.useDelayedTextureLoading = parsedData.useDelayedTextureLoading;
  463. scene.autoClear = parsedData.autoClear;
  464. scene.clearColor = BABYLON.Color3.FromArray(parsedData.clearColor);
  465. scene.ambientColor = BABYLON.Color3.FromArray(parsedData.ambientColor);
  466. scene.gravity = BABYLON.Vector3.FromArray(parsedData.gravity);
  467. // Fog
  468. if (parsedData.fogMode && parsedData.fogMode !== 0) {
  469. scene.fogMode = parsedData.fogMode;
  470. scene.fogColor = BABYLON.Color3.FromArray(parsedData.fogColor);
  471. scene.fogStart = parsedData.fogStart;
  472. scene.fogEnd = parsedData.fogEnd;
  473. scene.fogDensity = parsedData.fogDensity;
  474. }
  475. // Lights
  476. for (var index = 0; index < parsedData.lights.length; index++) {
  477. var parsedLight = parsedData.lights[index];
  478. parseLight(parsedLight, scene);
  479. }
  480. // Cameras
  481. for (var index = 0; index < parsedData.cameras.length; index++) {
  482. var parsedCamera = parsedData.cameras[index];
  483. parseCamera(parsedCamera, scene);
  484. }
  485. if (parsedData.activeCameraID) {
  486. scene.activeCameraByID(parsedData.activeCameraID);
  487. }
  488. // Materials
  489. if (parsedData.materials) {
  490. for (var index = 0; index < parsedData.materials.length; index++) {
  491. var parsedMaterial = parsedData.materials[index];
  492. parseMaterial(parsedMaterial, scene, rootUrl);
  493. }
  494. }
  495. if (parsedData.multiMaterials) {
  496. for (var index = 0; index < parsedData.multiMaterials.length; index++) {
  497. var parsedMultiMaterial = parsedData.multiMaterials[index];
  498. parseMultiMaterial(parsedMultiMaterial, scene);
  499. }
  500. }
  501. // Skeletons
  502. if (parsedData.skeletons) {
  503. for (var index = 0; index < parsedData.skeletons.length; index++) {
  504. var parsedSkeleton = parsedData.skeletons[index];
  505. parseSkeleton(parsedSkeleton, scene);
  506. }
  507. }
  508. // Meshes
  509. for (var index = 0; index < parsedData.meshes.length; index++) {
  510. var parsedMesh = parsedData.meshes[index];
  511. parseMesh(parsedMesh, scene, rootUrl);
  512. }
  513. // Connecting cameras parents and locked target
  514. for (var index = 0; index < scene.cameras.length; index++) {
  515. var camera = scene.cameras[index];
  516. if (camera._waitingParentId) {
  517. camera.parent = scene.getLastEntryByID(camera._waitingParentId);
  518. delete camera._waitingParentId;
  519. }
  520. if (camera._waitingLockedTargetId) {
  521. camera.lockedTarget = scene.getLastEntryByID(camera._waitingLockedTargetId);
  522. delete camera._waitingLockedTargetId;
  523. }
  524. }
  525. // Particles Systems
  526. if (parsedData.particleSystems) {
  527. for (var index = 0; index < parsedData.particleSystems.length; index++) {
  528. var parsedParticleSystem = parsedData.particleSystems[index];
  529. parseParticleSystem(parsedParticleSystem, scene, rootUrl);
  530. }
  531. }
  532. // Shadows
  533. if (parsedData.shadowGenerators) {
  534. for (var index = 0; index < parsedData.shadowGenerators.length; index++) {
  535. var parsedShadowGenerator = parsedData.shadowGenerators[index];
  536. parseShadowGenerator(parsedShadowGenerator, scene);
  537. }
  538. }
  539. // Finish
  540. if (then) {
  541. then(scene);
  542. }
  543. }, progressCallBack, database);
  544. }
  545. };
  546. })();