babylon.babylonFileLoader.js 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080
  1. "use strict";
  2. var BABYLON = BABYLON || {};
  3. (function () {
  4. var loadCubeTexture = function (rootUrl, parsedTexture, scene) {
  5. var texture = new BABYLON.CubeTexture(rootUrl + parsedTexture.name, scene);
  6. texture.name = parsedTexture.name;
  7. texture.hasAlpha = parsedTexture.hasAlpha;
  8. texture.level = parsedTexture.level;
  9. texture.coordinatesMode = parsedTexture.coordinatesMode;
  10. return texture;
  11. };
  12. var loadTexture = function (rootUrl, parsedTexture, scene) {
  13. if (!parsedTexture.name && !parsedTexture.isRenderTarget) {
  14. return null;
  15. }
  16. if (parsedTexture.isCube) {
  17. return loadCubeTexture(rootUrl, parsedTexture, scene);
  18. }
  19. var texture;
  20. if (parsedTexture.mirrorPlane) {
  21. texture = new BABYLON.MirrorTexture(parsedTexture.name, parsedTexture.renderTargetSize, scene);
  22. texture._waitingRenderList = parsedTexture.renderList;
  23. texture.mirrorPlane = BABYLON.Plane.FromArray(parsedTexture.mirrorPlane);
  24. } else if (parsedTexture.isRenderTarget) {
  25. texture = new BABYLON.RenderTargetTexture(parsedTexture.name, parsedTexture.renderTargetSize, scene);
  26. texture._waitingRenderList = parsedTexture.renderList;
  27. } else {
  28. texture = new BABYLON.Texture(rootUrl + parsedTexture.name, scene);
  29. }
  30. texture.name = parsedTexture.name;
  31. texture.hasAlpha = parsedTexture.hasAlpha;
  32. texture.getAlphaFromRGB = parsedTexture.getAlphaFromRGB;
  33. texture.level = parsedTexture.level;
  34. texture.coordinatesIndex = parsedTexture.coordinatesIndex;
  35. texture.coordinatesMode = parsedTexture.coordinatesMode;
  36. texture.uOffset = parsedTexture.uOffset;
  37. texture.vOffset = parsedTexture.vOffset;
  38. texture.uScale = parsedTexture.uScale;
  39. texture.vScale = parsedTexture.vScale;
  40. texture.uAng = parsedTexture.uAng;
  41. texture.vAng = parsedTexture.vAng;
  42. texture.wAng = parsedTexture.wAng;
  43. texture.wrapU = parsedTexture.wrapU;
  44. texture.wrapV = parsedTexture.wrapV;
  45. // Animations
  46. if (parsedTexture.animations) {
  47. for (var animationIndex = 0; animationIndex < parsedTexture.animations.length; animationIndex++) {
  48. var parsedAnimation = parsedTexture.animations[animationIndex];
  49. texture.animations.push(parseAnimation(parsedAnimation));
  50. }
  51. }
  52. return texture;
  53. };
  54. var parseSkeleton = function (parsedSkeleton, scene) {
  55. var skeleton = new BABYLON.Skeleton(parsedSkeleton.name, parsedSkeleton.id, scene);
  56. for (var index = 0; index < parsedSkeleton.bones.length; index++) {
  57. var parsedBone = parsedSkeleton.bones[index];
  58. var parentBone = null;
  59. if (parsedBone.parentBoneIndex > -1) {
  60. parentBone = skeleton.bones[parsedBone.parentBoneIndex];
  61. }
  62. var bone = new BABYLON.Bone(parsedBone.name, skeleton, parentBone, BABYLON.Matrix.FromArray(parsedBone.matrix));
  63. if (parsedBone.animation) {
  64. bone.animations.push(parseAnimation(parsedBone.animation));
  65. }
  66. }
  67. return skeleton;
  68. };
  69. var parseMaterial = function (parsedMaterial, scene, rootUrl) {
  70. var material;
  71. material = new BABYLON.StandardMaterial(parsedMaterial.name, scene);
  72. material.ambientColor = BABYLON.Color3.FromArray(parsedMaterial.ambient);
  73. material.diffuseColor = BABYLON.Color3.FromArray(parsedMaterial.diffuse);
  74. material.specularColor = BABYLON.Color3.FromArray(parsedMaterial.specular);
  75. material.specularPower = parsedMaterial.specularPower;
  76. material.emissiveColor = BABYLON.Color3.FromArray(parsedMaterial.emissive);
  77. material.alpha = parsedMaterial.alpha;
  78. material.id = parsedMaterial.id;
  79. BABYLON.Tags.AddTagsTo(material, parsedMaterial.tags);
  80. material.backFaceCulling = parsedMaterial.backFaceCulling;
  81. if (parsedMaterial.diffuseTexture) {
  82. material.diffuseTexture = loadTexture(rootUrl, parsedMaterial.diffuseTexture, scene);
  83. }
  84. if (parsedMaterial.ambientTexture) {
  85. material.ambientTexture = loadTexture(rootUrl, parsedMaterial.ambientTexture, scene);
  86. }
  87. if (parsedMaterial.opacityTexture) {
  88. material.opacityTexture = loadTexture(rootUrl, parsedMaterial.opacityTexture, scene);
  89. }
  90. if (parsedMaterial.reflectionTexture) {
  91. material.reflectionTexture = loadTexture(rootUrl, parsedMaterial.reflectionTexture, scene);
  92. }
  93. if (parsedMaterial.emissiveTexture) {
  94. material.emissiveTexture = loadTexture(rootUrl, parsedMaterial.emissiveTexture, scene);
  95. }
  96. if (parsedMaterial.specularTexture) {
  97. material.specularTexture = loadTexture(rootUrl, parsedMaterial.specularTexture, scene);
  98. }
  99. if (parsedMaterial.bumpTexture) {
  100. material.bumpTexture = loadTexture(rootUrl, parsedMaterial.bumpTexture, scene);
  101. }
  102. return material;
  103. };
  104. var parseMaterialById = function (id, parsedData, scene, rootUrl) {
  105. for (var index = 0; index < parsedData.materials.length; index++) {
  106. var parsedMaterial = parsedData.materials[index];
  107. if (parsedMaterial.id === id) {
  108. return parseMaterial(parsedMaterial, scene, rootUrl);
  109. }
  110. }
  111. return null;
  112. };
  113. var parseMultiMaterial = function (parsedMultiMaterial, scene) {
  114. var multiMaterial = new BABYLON.MultiMaterial(parsedMultiMaterial.name, scene);
  115. multiMaterial.id = parsedMultiMaterial.id;
  116. BABYLON.Tags.AddTagsTo(multiMaterial, parsedMultiMaterial.tags);
  117. for (var matIndex = 0; matIndex < parsedMultiMaterial.materials.length; matIndex++) {
  118. var subMatId = parsedMultiMaterial.materials[matIndex];
  119. if (subMatId) {
  120. multiMaterial.subMaterials.push(scene.getMaterialByID(subMatId));
  121. } else {
  122. multiMaterial.subMaterials.push(null);
  123. }
  124. }
  125. return multiMaterial;
  126. };
  127. var parseLensFlareSystem = function (parsedLensFlareSystem, scene, rootUrl) {
  128. var emitter = scene.getLastEntryByID(parsedLensFlareSystem.emitterId);
  129. var lensFlareSystem = new BABYLON.LensFlareSystem("lensFlareSystem#" + parsedLensFlareSystem.emitterId, emitter, scene);
  130. lensFlareSystem.borderLimit = parsedLensFlareSystem.borderLimit;
  131. for (var index = 0; index < parsedLensFlareSystem.flares.length; index++) {
  132. var parsedFlare = parsedLensFlareSystem.flares[index];
  133. var flare = new BABYLON.LensFlare(parsedFlare.size, parsedFlare.position, BABYLON.Color3.FromArray(parsedFlare.color), rootUrl + parsedFlare.textureName, lensFlareSystem);
  134. }
  135. return lensFlareSystem;
  136. };
  137. var parseParticleSystem = function (parsedParticleSystem, scene, rootUrl) {
  138. var emitter = scene.getLastMeshByID(parsedParticleSystem.emitterId);
  139. var particleSystem = new BABYLON.ParticleSystem("particles#" + emitter.name, parsedParticleSystem.capacity, scene);
  140. if (parsedParticleSystem.textureName) {
  141. particleSystem.particleTexture = new BABYLON.Texture(rootUrl + parsedParticleSystem.textureName, scene);
  142. particleSystem.particleTexture.name = parsedParticleSystem.textureName;
  143. }
  144. particleSystem.minAngularSpeed = parsedParticleSystem.minAngularSpeed;
  145. particleSystem.maxAngularSpeed = parsedParticleSystem.maxAngularSpeed;
  146. particleSystem.minSize = parsedParticleSystem.minSize;
  147. particleSystem.maxSize = parsedParticleSystem.maxSize;
  148. particleSystem.minLifeTime = parsedParticleSystem.minLifeTime;
  149. particleSystem.maxLifeTime = parsedParticleSystem.maxLifeTime;
  150. particleSystem.emitter = emitter;
  151. particleSystem.emitRate = parsedParticleSystem.emitRate;
  152. particleSystem.minEmitBox = BABYLON.Vector3.FromArray(parsedParticleSystem.minEmitBox);
  153. particleSystem.maxEmitBox = BABYLON.Vector3.FromArray(parsedParticleSystem.maxEmitBox);
  154. particleSystem.gravity = BABYLON.Vector3.FromArray(parsedParticleSystem.gravity);
  155. particleSystem.direction1 = BABYLON.Vector3.FromArray(parsedParticleSystem.direction1);
  156. particleSystem.direction2 = BABYLON.Vector3.FromArray(parsedParticleSystem.direction2);
  157. particleSystem.color1 = BABYLON.Color4.FromArray(parsedParticleSystem.color1);
  158. particleSystem.color2 = BABYLON.Color4.FromArray(parsedParticleSystem.color2);
  159. particleSystem.colorDead = BABYLON.Color4.FromArray(parsedParticleSystem.colorDead);
  160. particleSystem.updateSpeed = parsedParticleSystem.updateSpeed;
  161. particleSystem.targetStopDuration = parsedParticleSystem.targetStopFrame;
  162. particleSystem.textureMask = BABYLON.Color4.FromArray(parsedParticleSystem.textureMask);
  163. particleSystem.blendMode = parsedParticleSystem.blendMode;
  164. particleSystem.start();
  165. return particleSystem;
  166. };
  167. var parseShadowGenerator = function (parsedShadowGenerator, scene) {
  168. var light = scene.getLightByID(parsedShadowGenerator.lightId);
  169. var shadowGenerator = new BABYLON.ShadowGenerator(parsedShadowGenerator.mapSize, light);
  170. for (var meshIndex = 0; meshIndex < parsedShadowGenerator.renderList.length; meshIndex++) {
  171. var mesh = scene.getMeshByID(parsedShadowGenerator.renderList[meshIndex]);
  172. shadowGenerator.getShadowMap().renderList.push(mesh);
  173. }
  174. shadowGenerator.useVarianceShadowMap = parsedShadowGenerator.useVarianceShadowMap;
  175. return shadowGenerator;
  176. };
  177. var parseAnimation = function (parsedAnimation) {
  178. var animation = new BABYLON.Animation(parsedAnimation.name, parsedAnimation.property, parsedAnimation.framePerSecond, parsedAnimation.dataType, parsedAnimation.loopBehavior);
  179. var dataType = parsedAnimation.dataType;
  180. var keys = [];
  181. for (var index = 0; index < parsedAnimation.keys.length; index++) {
  182. var key = parsedAnimation.keys[index];
  183. var data;
  184. switch (dataType) {
  185. case BABYLON.Animation.ANIMATIONTYPE_FLOAT:
  186. data = key.values[0];
  187. break;
  188. case BABYLON.Animation.ANIMATIONTYPE_QUATERNION:
  189. data = BABYLON.Quaternion.FromArray(key.values);
  190. break;
  191. case BABYLON.Animation.ANIMATIONTYPE_MATRIX:
  192. data = BABYLON.Matrix.FromArray(key.values);
  193. break;
  194. case BABYLON.Animation.ANIMATIONTYPE_VECTOR3:
  195. default:
  196. data = BABYLON.Vector3.FromArray(key.values);
  197. break;
  198. }
  199. keys.push({
  200. frame: key.frame,
  201. value: data
  202. });
  203. }
  204. animation.setKeys(keys);
  205. return animation;
  206. };
  207. var parseLight = function (parsedLight, scene) {
  208. var light;
  209. switch (parsedLight.type) {
  210. case 0:
  211. light = new BABYLON.PointLight(parsedLight.name, BABYLON.Vector3.FromArray(parsedLight.position), scene);
  212. break;
  213. case 1:
  214. light = new BABYLON.DirectionalLight(parsedLight.name, BABYLON.Vector3.FromArray(parsedLight.direction), scene);
  215. light.position = BABYLON.Vector3.FromArray(parsedLight.position);
  216. break;
  217. case 2:
  218. light = new BABYLON.SpotLight(parsedLight.name, BABYLON.Vector3.FromArray(parsedLight.position), BABYLON.Vector3.FromArray(parsedLight.direction), parsedLight.angle, parsedLight.exponent, scene);
  219. break;
  220. case 3:
  221. light = new BABYLON.HemisphericLight(parsedLight.name, BABYLON.Vector3.FromArray(parsedLight.direction), scene);
  222. light.groundColor = BABYLON.Color3.FromArray(parsedLight.groundColor);
  223. break;
  224. }
  225. light.id = parsedLight.id;
  226. BABYLON.Tags.AddTagsTo(light, parsedLight.tags);
  227. if (parsedLight.intensity) {
  228. light.intensity = parsedLight.intensity;
  229. }
  230. if (parsedLight.range) {
  231. light.range = parsedLight.range;
  232. }
  233. light.diffuse = BABYLON.Color3.FromArray(parsedLight.diffuse);
  234. light.specular = BABYLON.Color3.FromArray(parsedLight.specular);
  235. };
  236. var parseCamera = function (parsedCamera, scene) {
  237. var camera = new BABYLON.FreeCamera(parsedCamera.name, BABYLON.Vector3.FromArray(parsedCamera.position), scene);
  238. camera.id = parsedCamera.id;
  239. BABYLON.Tags.AddTagsTo(camera, parsedCamera.tags);
  240. // Parent
  241. if (parsedCamera.parentId) {
  242. camera._waitingParentId = parsedCamera.parentId;
  243. }
  244. // Target
  245. if (parsedCamera.target) {
  246. camera.setTarget(BABYLON.Vector3.FromArray(parsedCamera.target));
  247. } else {
  248. camera.rotation = BABYLON.Vector3.FromArray(parsedCamera.rotation);
  249. }
  250. // Locked target
  251. if (parsedCamera.lockedTargetId) {
  252. camera._waitingLockedTargetId = parsedCamera.lockedTargetId;
  253. }
  254. camera.fov = parsedCamera.fov;
  255. camera.minZ = parsedCamera.minZ;
  256. camera.maxZ = parsedCamera.maxZ;
  257. camera.speed = parsedCamera.speed;
  258. camera.inertia = parsedCamera.inertia;
  259. camera.checkCollisions = parsedCamera.checkCollisions;
  260. camera.applyGravity = parsedCamera.applyGravity;
  261. if (parsedCamera.ellipsoid) {
  262. camera.ellipsoid = BABYLON.Vector3.FromArray(parsedCamera.ellipsoid);
  263. }
  264. // Animations
  265. if (parsedCamera.animations) {
  266. for (var animationIndex = 0; animationIndex < parsedCamera.animations.length; animationIndex++) {
  267. var parsedAnimation = parsedCamera.animations[animationIndex];
  268. camera.animations.push(parseAnimation(parsedAnimation));
  269. }
  270. }
  271. if (parsedCamera.autoAnimate) {
  272. scene.beginAnimation(camera, parsedCamera.autoAnimateFrom, parsedCamera.autoAnimateTo, parsedCamera.autoAnimateLoop, 1.0);
  273. }
  274. // Layer Mask
  275. if (parsedCamera.layerMask && (!isNaN(parsedCamera.layerMask))) {
  276. camera.layerMask = Math.abs(parseInt(parsedCamera.layerMask));
  277. } else {
  278. camera.layerMask = 0xFFFFFFFF;
  279. }
  280. return camera;
  281. };
  282. var parseGeometry = function (parsedGeometry, scene) {
  283. var id = parsedGeometry.id;
  284. return scene.getGeometryByID(id);
  285. };
  286. var parseBox = function (parsedBox, scene) {
  287. if (parseGeometry(parsedBox, scene)) {
  288. return null; // null since geometry could be something else than a box...
  289. }
  290. var box = new BABYLON.Geometry.Primitives.Box(parsedBox.id, scene.getEngine(), parsedBox.size, parsedBox.canBeRegenerated, null);
  291. BABYLON.Tags.AddTagsTo(box, parsedBox.tags);
  292. scene.pushGeometry(box, true);
  293. return box;
  294. };
  295. var parseSphere = function (parsedSphere, scene) {
  296. if (parseGeometry(parsedSphere, scene)) {
  297. return null; // null since geometry could be something else than a sphere...
  298. }
  299. var sphere = new BABYLON.Geometry.Primitives.Sphere(parsedSphere.id, scene.getEngine(), parsedSphere.segments, parsedSphere.diameter, parsedSphere.canBeRegenerated, null);
  300. BABYLON.Tags.AddTagsTo(sphere, parsedSphere.tags);
  301. scene.pushGeometry(sphere, true);
  302. return sphere;
  303. };
  304. var parseCylinder = function (parsedCylinder, scene) {
  305. if (parseGeometry(parsedCylinder, scene)) {
  306. return null; // null since geometry could be something else than a cylinder...
  307. }
  308. var cylinder = new BABYLON.Geometry.Primitives.Cylinder(parsedCylinder.id, scene.getEngine(), parsedCylinder.height, parsedCylinder.diameterTop, parsedCylinder.diameterBottom, parsedCylinder.tessellation, parsedCylinder.canBeRegenerated, null);
  309. BABYLON.Tags.AddTagsTo(cylinder, parsedCylinder.tags);
  310. scene.pushGeometry(cylinder, true);
  311. return cylinder;
  312. };
  313. var parseTorus = function (parsedTorus, scene) {
  314. if (parseGeometry(parsedTorus, scene)) {
  315. return null; // null since geometry could be something else than a torus...
  316. }
  317. var torus = new BABYLON.Geometry.Primitives.Torus(parsedTorus.id, scene.getEngine(), parsedTorus.diameter, parsedTorus.thickness, parsedTorus.tessellation, parsedTorus.canBeRegenerated, null);
  318. BABYLON.Tags.AddTagsTo(torus, parsedTorus.tags);
  319. scene.pushGeometry(torus, true);
  320. return torus;
  321. };
  322. var parseGround = function (parsedGround, scene) {
  323. if (parseGeometry(parsedGround, scene)) {
  324. return null; // null since geometry could be something else than a ground...
  325. }
  326. var ground = new BABYLON.Geometry.Primitives.Ground(parsedGround.id, scene.getEngine(), parsedGround.width, parsedGround.height, parsedGround.subdivisions, parsedGround.canBeRegenerated, null);
  327. BABYLON.Tags.AddTagsTo(ground, parsedGround.tags);
  328. scene.pushGeometry(ground, true);
  329. return ground;
  330. };
  331. var parsePlane = function (parsedPlane, scene) {
  332. if (parseGeometry(parsedPlane, scene)) {
  333. return null; // null since geometry could be something else than a plane...
  334. }
  335. var plane = new BABYLON.Geometry.Primitives.Plane(parsedPlane.id, scene.getEngine(), parsedPlane.size, parsedPlane.canBeRegenerated, null);
  336. BABYLON.Tags.AddTagsTo(plane, parsedPlane.tags);
  337. scene.pushGeometry(plane, true);
  338. return plane;
  339. };
  340. var parseTorusKnot = function (parsedTorusKnot, scene) {
  341. if (parseGeometry(parsedTorusKnot, scene)) {
  342. return null; // null since geometry could be something else than a torusKnot...
  343. }
  344. var torusKnot = new BABYLON.Geometry.Primitives.TorusKnot(parsedTorusKnot.id, scene.getEngine(), parsedTorusKnot.radius, parsedTorusKnot.tube, parsedTorusKnot.radialSegments, parsedTorusKnot.tubularSegments, parsedTorusKnot.p, parsedTorusKnot.q, parsedTorusKnot.canBeRegenerated, null);
  345. BABYLON.Tags.AddTagsTo(torusKnot, parsedTorusKnot.tags);
  346. scene.pushGeometry(torusKnot, true);
  347. return torusKnot;
  348. };
  349. var parseVertexData = function (parsedVertexData, scene, rootUrl) {
  350. if (parseGeometry(parsedVertexData, scene)) {
  351. return null; // null since geometry could be a primitive
  352. }
  353. var geometry = new BABYLON.Geometry(parsedVertexData.id, scene.getEngine());
  354. BABYLON.Tags.AddTagsTo(geometry, parsedVertexData.tags);
  355. if (parsedVertexData.delayLoadingFile) {
  356. geometry.delayLoadState = BABYLON.Engine.DELAYLOADSTATE_NOTLOADED;
  357. geometry.delayLoadingFile = rootUrl + parsedVertexData.delayLoadingFile;
  358. geometry._boundingInfo = new BABYLON.BoundingInfo(BABYLON.Vector3.FromArray(parsedVertexData.boundingBoxMinimum), BABYLON.Vector3.FromArray(parsedVertexData.boundingBoxMaximum));
  359. geometry._delayInfo = [];
  360. if (parsedVertexData.hasUVs) {
  361. geometry._delayInfo.push(BABYLON.VertexBuffer.UVKind);
  362. }
  363. if (parsedVertexData.hasUVs2) {
  364. geometry._delayInfo.push(BABYLON.VertexBuffer.UV2Kind);
  365. }
  366. if (parsedVertexData.hasColors) {
  367. geometry._delayInfo.push(BABYLON.VertexBuffer.ColorKind);
  368. }
  369. if (parsedVertexData.hasMatricesIndices) {
  370. geometry._delayInfo.push(BABYLON.VertexBuffer.MatricesIndicesKind);
  371. }
  372. if (parsedVertexData.hasMatricesWeights) {
  373. geometry._delayInfo.push(BABYLON.VertexBuffer.MatricesWeightsKind);
  374. }
  375. geometry._delayLoadingFunction = importVertexData;
  376. } else {
  377. importVertexData(parsedVertexData, geometry);
  378. }
  379. scene.pushGeometry(geometry, true);
  380. return geometry;
  381. };
  382. var parseMesh = function (parsedMesh, scene, rootUrl) {
  383. var mesh = new BABYLON.Mesh(parsedMesh.name, scene);
  384. mesh.id = parsedMesh.id;
  385. BABYLON.Tags.AddTagsTo(mesh, parsedMesh.tags);
  386. mesh.position = BABYLON.Vector3.FromArray(parsedMesh.position);
  387. if (parsedMesh.rotationQuaternion) {
  388. mesh.rotationQuaternion = BABYLON.Quaternion.FromArray(parsedMesh.rotationQuaternion);
  389. } else if (parsedMesh.rotation) {
  390. mesh.rotation = BABYLON.Vector3.FromArray(parsedMesh.rotation);
  391. }
  392. mesh.scaling = BABYLON.Vector3.FromArray(parsedMesh.scaling);
  393. if (parsedMesh.localMatrix) {
  394. mesh.setPivotMatrix(BABYLON.Matrix.FromArray(parsedMesh.localMatrix));
  395. }
  396. mesh.setEnabled(parsedMesh.isEnabled);
  397. mesh.isVisible = parsedMesh.isVisible;
  398. mesh.infiniteDistance = parsedMesh.infiniteDistance;
  399. if (parsedMesh.pickable !== undefined) {
  400. mesh.isPickable = parsedMesh.pickable;
  401. }
  402. mesh.receiveShadows = parsedMesh.receiveShadows;
  403. mesh.billboardMode = parsedMesh.billboardMode;
  404. if (parsedMesh.visibility !== undefined) {
  405. mesh.visibility = parsedMesh.visibility;
  406. }
  407. mesh.checkCollisions = parsedMesh.checkCollisions;
  408. mesh._shouldGenerateFlatShading = parsedMesh.useFlatShading;
  409. // Parent
  410. if (parsedMesh.parentId) {
  411. mesh.parent = scene.getLastEntryByID(parsedMesh.parentId);
  412. }
  413. // Geometry
  414. if (parsedMesh.delayLoadingFile) {
  415. mesh.delayLoadState = BABYLON.Engine.DELAYLOADSTATE_NOTLOADED;
  416. mesh.delayLoadingFile = rootUrl + parsedMesh.delayLoadingFile;
  417. mesh._boundingInfo = new BABYLON.BoundingInfo(BABYLON.Vector3.FromArray(parsedMesh.boundingBoxMinimum), BABYLON.Vector3.FromArray(parsedMesh.boundingBoxMaximum));
  418. mesh._delayInfo = [];
  419. if (parsedMesh.hasUVs) {
  420. mesh._delayInfo.push(BABYLON.VertexBuffer.UVKind);
  421. }
  422. if (parsedMesh.hasUVs2) {
  423. mesh._delayInfo.push(BABYLON.VertexBuffer.UV2Kind);
  424. }
  425. if (parsedMesh.hasColors) {
  426. mesh._delayInfo.push(BABYLON.VertexBuffer.ColorKind);
  427. }
  428. if (parsedMesh.hasMatricesIndices) {
  429. mesh._delayInfo.push(BABYLON.VertexBuffer.MatricesIndicesKind);
  430. }
  431. if (parsedMesh.hasMatricesWeights) {
  432. mesh._delayInfo.push(BABYLON.VertexBuffer.MatricesWeightsKind);
  433. }
  434. mesh._delayLoadingFunction = importGeometry;
  435. if (BABYLON.SceneLoader.ForceFullSceneLoadingForIncremental) {
  436. mesh._checkDelayState();
  437. }
  438. } else {
  439. importGeometry(parsedMesh, mesh);
  440. }
  441. // Material
  442. if (parsedMesh.materialId) {
  443. mesh.setMaterialByID(parsedMesh.materialId);
  444. } else {
  445. mesh.material = null;
  446. }
  447. // Skeleton
  448. if (parsedMesh.skeletonId > -1) {
  449. mesh.skeleton = scene.getLastSkeletonByID(parsedMesh.skeletonId);
  450. }
  451. // Physics
  452. if (parsedMesh.physicsImpostor) {
  453. if (!scene.isPhysicsEnabled()) {
  454. scene.enablePhysics();
  455. }
  456. switch (parsedMesh.physicsImpostor) {
  457. case 1: // BOX
  458. mesh.setPhysicsState({ impostor: BABYLON.PhysicsEngine.BoxImpostor, mass: parsedMesh.physicsMass, friction: parsedMesh.physicsFriction, restitution: parsedMesh.physicsRestitution });
  459. break;
  460. case 2: // SPHERE
  461. mesh.setPhysicsState({ impostor: BABYLON.PhysicsEngine.SphereImpostor, mass: parsedMesh.physicsMass, friction: parsedMesh.physicsFriction, restitution: parsedMesh.physicsRestitution });
  462. break;
  463. }
  464. }
  465. // Animations
  466. if (parsedMesh.animations) {
  467. for (var animationIndex = 0; animationIndex < parsedMesh.animations.length; animationIndex++) {
  468. var parsedAnimation = parsedMesh.animations[animationIndex];
  469. mesh.animations.push(parseAnimation(parsedAnimation));
  470. }
  471. }
  472. if (parsedMesh.autoAnimate) {
  473. scene.beginAnimation(mesh, parsedMesh.autoAnimateFrom, parsedMesh.autoAnimateTo, parsedMesh.autoAnimateLoop, 1.0);
  474. }
  475. // Layer Mask
  476. if (parsedMesh.layerMask && (!isNaN(parsedMesh.layerMask))) {
  477. mesh.layerMask = Math.abs(parseInt(parsedMesh.layerMask));
  478. } else {
  479. mesh.layerMask = 0xFFFFFFFF;
  480. }
  481. // Instances
  482. if (parsedMesh.instances) {
  483. for (var index = 0; index < parsedMesh.instances.length; index++) {
  484. var parsedInstance = parsedMesh.instances[index];
  485. var instance = mesh.createInstance(parsedInstance.name);
  486. BABYLON.Tags.AddTagsTo(instance, parsedInstance.tags);
  487. instance.position = BABYLON.Vector3.FromArray(parsedInstance.position);
  488. if (parsedInstance.rotationQuaternion) {
  489. instance.rotationQuaternion = BABYLON.Quaternion.FromArray(parsedInstance.rotationQuaternion);
  490. } else if (parsedInstance.rotation) {
  491. instance.rotation = BABYLON.Vector3.FromArray(parsedInstance.rotation);
  492. }
  493. instance.scaling = BABYLON.Vector3.FromArray(parsedInstance.scaling);
  494. instance.checkCollisions = mesh.checkCollisions;
  495. if (parsedMesh.animations) {
  496. for (animationIndex = 0; animationIndex < parsedMesh.animations.length; animationIndex++) {
  497. parsedAnimation = parsedMesh.animations[animationIndex];
  498. instance.animations.push(parseAnimation(parsedAnimation));
  499. }
  500. }
  501. }
  502. }
  503. return mesh;
  504. };
  505. var isDescendantOf = function (mesh, names, hierarchyIds) {
  506. names = (names instanceof Array) ? names : [names];
  507. for (var i in names) {
  508. if (mesh.name === names[i]) {
  509. hierarchyIds.push(mesh.id);
  510. return true;
  511. }
  512. }
  513. if (mesh.parentId && hierarchyIds.indexOf(mesh.parentId) !== -1) {
  514. hierarchyIds.push(mesh.id);
  515. return true;
  516. }
  517. return false;
  518. };
  519. var importVertexData = function (parsedVertexData, geometry) {
  520. var vertexData = new BABYLON.VertexData();
  521. // positions
  522. var positions = parsedVertexData.positions;
  523. if (positions) {
  524. vertexData.set(positions, BABYLON.VertexBuffer.PositionKind);
  525. }
  526. // normals
  527. var normals = parsedVertexData.normals;
  528. if (normals) {
  529. vertexData.set(normals, BABYLON.VertexBuffer.NormalKind);
  530. }
  531. // uvs
  532. var uvs = parsedVertexData.uvs;
  533. if (uvs) {
  534. vertexData.set(uvs, BABYLON.VertexBuffer.UVKind);
  535. }
  536. // uv2s
  537. var uv2s = parsedVertexData.uv2s;
  538. if (uv2s) {
  539. vertexData.set(uv2s, BABYLON.VertexBuffer.UV2Kind);
  540. }
  541. // colors
  542. var colors = parsedVertexData.colors;
  543. if (colors) {
  544. vertexData.set(colors, BABYLON.VertexBuffer.ColorKind);
  545. }
  546. // matricesIndices
  547. var matricesIndices = parsedVertexData.matricesIndices;
  548. if (matricesIndices) {
  549. vertexData.set(matricesIndices, BABYLON.VertexBuffer.MatricesIndicesKind);
  550. }
  551. // matricesWeights
  552. var matricesWeights = parsedVertexData.matricesWeights;
  553. if (matricesWeights) {
  554. vertexData.set(matricesWeights, BABYLON.VertexBuffer.MatricesWeightsKind);
  555. }
  556. // indices
  557. var indices = parsedVertexData.indices;
  558. if (indices) {
  559. vertexData.indices = indices;
  560. }
  561. geometry.setAllVerticesData(vertexData, parsedVertexData.updatable);
  562. };
  563. var importGeometry = function (parsedGeometry, mesh) {
  564. var scene = mesh.getScene();
  565. // Geometry
  566. var geometryId = parsedGeometry.geometryId;
  567. if (geometryId) {
  568. var geometry = scene.getGeometryByID(geometryId);
  569. if (geometry) {
  570. geometry.applyToMesh(mesh);
  571. }
  572. }
  573. else if (parsedGeometry.positions && parsedGeometry.normals && parsedGeometry.indices) {
  574. mesh.setVerticesData(BABYLON.VertexBuffer.PositionKind, parsedGeometry.positions, false);
  575. mesh.setVerticesData(BABYLON.VertexBuffer.NormalKind, parsedGeometry.normals, false);
  576. if (parsedGeometry.uvs) {
  577. mesh.setVerticesData(BABYLON.VertexBuffer.UVKind, parsedGeometry.uvs, false);
  578. }
  579. if (parsedGeometry.uvs2) {
  580. mesh.setVerticesData(BABYLON.VertexBuffer.UV2Kind, parsedGeometry.uvs2, false);
  581. }
  582. if (parsedGeometry.colors) {
  583. mesh.setVerticesData(BABYLON.VertexBuffer.ColorKind, parsedGeometry.colors, false);
  584. }
  585. if (parsedGeometry.matricesIndices) {
  586. if (!parsedGeometry.matricesIndices._isExpanded) {
  587. var floatIndices = [];
  588. for (var i = 0; i < parsedGeometry.matricesIndices.length; i++) {
  589. var matricesIndex = parsedGeometry.matricesIndices[i];
  590. floatIndices.push(matricesIndex & 0x000000FF);
  591. floatIndices.push((matricesIndex & 0x0000FF00) >> 8);
  592. floatIndices.push((matricesIndex & 0x00FF0000) >> 16);
  593. floatIndices.push(matricesIndex >> 24);
  594. }
  595. mesh.setVerticesData(BABYLON.VertexBuffer.MatricesIndicesKind, floatIndices, false);
  596. } else {
  597. delete parsedGeometry.matricesIndices._isExpanded;
  598. mesh.setVerticesData(BABYLON.VertexBuffer.MatricesIndicesKind, parsedGeometry.matricesIndices, false);
  599. }
  600. }
  601. if (parsedGeometry.matricesWeights) {
  602. mesh.setVerticesData(BABYLON.VertexBuffer.MatricesWeightsKind, parsedGeometry.matricesWeights, false);
  603. }
  604. mesh.setIndices(parsedGeometry.indices);
  605. }
  606. // SubMeshes
  607. if (parsedGeometry.subMeshes) {
  608. mesh.subMeshes = [];
  609. for (var subIndex = 0; subIndex < parsedGeometry.subMeshes.length; subIndex++) {
  610. var parsedSubMesh = parsedGeometry.subMeshes[subIndex];
  611. var subMesh = new BABYLON.SubMesh(parsedSubMesh.materialIndex, parsedSubMesh.verticesStart, parsedSubMesh.verticesCount, parsedSubMesh.indexStart, parsedSubMesh.indexCount, mesh);
  612. }
  613. }
  614. // Update
  615. mesh.computeWorldMatrix(true);
  616. // Flat shading
  617. if (mesh._shouldGenerateFlatShading) {
  618. mesh.convertToFlatShadedMesh();
  619. delete mesh._shouldGenerateFlatShading;
  620. }
  621. if (scene._selectionOctree) {
  622. scene._selectionOctree.addMesh(mesh);
  623. }
  624. };
  625. BABYLON.SceneLoader.RegisterPlugin({
  626. extensions: ".babylon",
  627. importMesh: function (meshesNames, scene, data, rootUrl, meshes, particleSystems, skeletons) {
  628. var parsedData = JSON.parse(data);
  629. var loadedSkeletonsIds = [];
  630. var loadedMaterialsIds = [];
  631. var hierarchyIds = [];
  632. for (var index = 0; index < parsedData.meshes.length; index++) {
  633. var parsedMesh = parsedData.meshes[index];
  634. if (!meshesNames || isDescendantOf(parsedMesh, meshesNames, hierarchyIds)) {
  635. if (meshesNames instanceof Array) {
  636. // Remove found mesh name from list.
  637. delete meshesNames[meshesNames.indexOf(parsedMesh.name)];
  638. }
  639. // Material ?
  640. if (parsedMesh.materialId) {
  641. var materialFound = (loadedMaterialsIds.indexOf(parsedMesh.materialId) !== -1);
  642. if (!materialFound) {
  643. for (var multimatIndex = 0; multimatIndex < parsedData.multiMaterials.length; multimatIndex++) {
  644. var parsedMultiMaterial = parsedData.multiMaterials[multimatIndex];
  645. if (parsedMultiMaterial.id == parsedMesh.materialId) {
  646. for (var matIndex = 0; matIndex < parsedMultiMaterial.materials.length; matIndex++) {
  647. var subMatId = parsedMultiMaterial.materials[matIndex];
  648. loadedMaterialsIds.push(subMatId);
  649. parseMaterialById(subMatId, parsedData, scene, rootUrl);
  650. }
  651. loadedMaterialsIds.push(parsedMultiMaterial.id);
  652. parseMultiMaterial(parsedMultiMaterial, scene);
  653. materialFound = true;
  654. break;
  655. }
  656. }
  657. }
  658. if (!materialFound) {
  659. loadedMaterialsIds.push(parsedMesh.materialId);
  660. parseMaterialById(parsedMesh.materialId, parsedData, scene, rootUrl);
  661. }
  662. }
  663. // Skeleton ?
  664. if (parsedMesh.skeletonId > -1 && scene.skeletons) {
  665. var skeletonAlreadyLoaded = (loadedSkeletonsIds.indexOf(parsedMesh.skeletonId) > -1);
  666. if (!skeletonAlreadyLoaded) {
  667. for (var skeletonIndex = 0; skeletonIndex < parsedData.skeletons.length; skeletonIndex++) {
  668. var parsedSkeleton = parsedData.skeletons[skeletonIndex];
  669. if (parsedSkeleton.id === parsedMesh.skeletonId) {
  670. skeletons.push(parseSkeleton(parsedSkeleton, scene));
  671. loadedSkeletonsIds.push(parsedSkeleton.id);
  672. }
  673. }
  674. }
  675. }
  676. var mesh = parseMesh(parsedMesh, scene, rootUrl);
  677. meshes.push(mesh);
  678. }
  679. }
  680. // Particles
  681. if (parsedData.particleSystems) {
  682. for (var index = 0; index < parsedData.particleSystems.length; index++) {
  683. var parsedParticleSystem = parsedData.particleSystems[index];
  684. if (hierarchyIds.indexOf(parsedParticleSystem.emitterId) !== -1) {
  685. particleSystems.push(parseParticleSystem(parsedParticleSystem, scene, rootUrl));
  686. }
  687. }
  688. }
  689. return true;
  690. },
  691. load: function (scene, data, rootUrl) {
  692. var parsedData = JSON.parse(data);
  693. // Scene
  694. scene.useDelayedTextureLoading = parsedData.useDelayedTextureLoading && !BABYLON.SceneLoader.ForceFullSceneLoadingForIncremental;
  695. scene.autoClear = parsedData.autoClear;
  696. scene.clearColor = BABYLON.Color3.FromArray(parsedData.clearColor);
  697. scene.ambientColor = BABYLON.Color3.FromArray(parsedData.ambientColor);
  698. scene.gravity = BABYLON.Vector3.FromArray(parsedData.gravity);
  699. // Fog
  700. if (parsedData.fogMode && parsedData.fogMode !== 0) {
  701. scene.fogMode = parsedData.fogMode;
  702. scene.fogColor = BABYLON.Color3.FromArray(parsedData.fogColor);
  703. scene.fogStart = parsedData.fogStart;
  704. scene.fogEnd = parsedData.fogEnd;
  705. scene.fogDensity = parsedData.fogDensity;
  706. }
  707. // Lights
  708. for (var index = 0; index < parsedData.lights.length; index++) {
  709. var parsedLight = parsedData.lights[index];
  710. parseLight(parsedLight, scene);
  711. }
  712. // Cameras
  713. for (var index = 0; index < parsedData.cameras.length; index++) {
  714. var parsedCamera = parsedData.cameras[index];
  715. parseCamera(parsedCamera, scene);
  716. }
  717. if (parsedData.activeCameraID) {
  718. scene.setActiveCameraByID(parsedData.activeCameraID);
  719. }
  720. // Materials
  721. if (parsedData.materials) {
  722. for (var index = 0; index < parsedData.materials.length; index++) {
  723. var parsedMaterial = parsedData.materials[index];
  724. parseMaterial(parsedMaterial, scene, rootUrl);
  725. }
  726. }
  727. if (parsedData.multiMaterials) {
  728. for (var index = 0; index < parsedData.multiMaterials.length; index++) {
  729. var parsedMultiMaterial = parsedData.multiMaterials[index];
  730. parseMultiMaterial(parsedMultiMaterial, scene);
  731. }
  732. }
  733. // Skeletons
  734. if (parsedData.skeletons) {
  735. for (var index = 0; index < parsedData.skeletons.length; index++) {
  736. var parsedSkeleton = parsedData.skeletons[index];
  737. parseSkeleton(parsedSkeleton, scene);
  738. }
  739. }
  740. // Geometries
  741. var geometries = parsedData.geometries;
  742. if (geometries) {
  743. // Boxes
  744. var boxes = geometries.boxes;
  745. if (boxes) {
  746. for (var index = 0; index < boxes.length; index++) {
  747. var parsedBox = boxes[index];
  748. parseBox(parsedBox, scene);
  749. }
  750. }
  751. // Spheres
  752. var spheres = geometries.spheres;
  753. if (spheres) {
  754. for (var index = 0; index < spheres.length; index++) {
  755. var parsedSphere = spheres[index];
  756. parseSphere(parsedSphere, scene);
  757. }
  758. }
  759. // Cylinders
  760. var cylinders = geometries.cylinders;
  761. if (cylinders) {
  762. for (var index = 0; index < cylinders.length; index++) {
  763. var parsedCylinder = cylinders[index];
  764. parseCylinder(parsedCylinder, scene);
  765. }
  766. }
  767. // Toruses
  768. var toruses = geometries.toruses;
  769. if (toruses) {
  770. for (var index = 0; index < toruses.length; index++) {
  771. var parsedTorus = toruses[index];
  772. parseTorus(parsedTorus, scene);
  773. }
  774. }
  775. // Grounds
  776. var grounds = geometries.grounds;
  777. if (grounds) {
  778. for (var index = 0; index < grounds.length; index++) {
  779. var parsedGround = grounds[index];
  780. parseGround(parsedGround, scene);
  781. }
  782. }
  783. // Planes
  784. var planes = geometries.planes;
  785. if (planes) {
  786. for (var index = 0; index < planes.length; index++) {
  787. var parsedPlane = planes[index];
  788. parsePlane(parsedPlane, scene);
  789. }
  790. }
  791. // TorusKnots
  792. var torusKnots = geometries.torusKnots;
  793. if (torusKnots) {
  794. for (var index = 0; index < torusKnots.length; index++) {
  795. var parsedTorusKnot = torusKnots[index];
  796. parseTorusKnot(parsedTorusKnot, scene);
  797. }
  798. }
  799. // VertexData
  800. var vertexData = geometries.vertexData;
  801. if (vertexData) {
  802. for (var index = 0; index < vertexData.length; index++) {
  803. var parsedVertexData = vertexData[index];
  804. parseVertexData(parsedVertexData, scene, rootUrl);
  805. }
  806. }
  807. }
  808. // Meshes
  809. for (var index = 0; index < parsedData.meshes.length; index++) {
  810. var parsedMesh = parsedData.meshes[index];
  811. parseMesh(parsedMesh, scene, rootUrl);
  812. }
  813. // Connecting cameras parents and locked target
  814. for (var index = 0; index < scene.cameras.length; index++) {
  815. var camera = scene.cameras[index];
  816. if (camera._waitingParentId) {
  817. camera.parent = scene.getLastEntryByID(camera._waitingParentId);
  818. delete camera._waitingParentId;
  819. }
  820. if (camera._waitingLockedTargetId) {
  821. camera.lockedTarget = scene.getLastEntryByID(camera._waitingLockedTargetId);
  822. delete camera._waitingLockedTargetId;
  823. }
  824. }
  825. // Particles Systems
  826. if (parsedData.particleSystems) {
  827. for (var index = 0; index < parsedData.particleSystems.length; index++) {
  828. var parsedParticleSystem = parsedData.particleSystems[index];
  829. parseParticleSystem(parsedParticleSystem, scene, rootUrl);
  830. }
  831. }
  832. // Lens flares
  833. if (parsedData.lensFlareSystems) {
  834. for (var index = 0; index < parsedData.lensFlareSystems.length; index++) {
  835. var parsedLensFlareSystem = parsedData.lensFlareSystems[index];
  836. parseLensFlareSystem(parsedLensFlareSystem, scene, rootUrl);
  837. }
  838. }
  839. // Shadows
  840. if (parsedData.shadowGenerators) {
  841. for (var index = 0; index < parsedData.shadowGenerators.length; index++) {
  842. var parsedShadowGenerator = parsedData.shadowGenerators[index];
  843. parseShadowGenerator(parsedShadowGenerator, scene);
  844. }
  845. }
  846. // Finish
  847. return true;
  848. }
  849. });
  850. })();