babylon.babylonFileLoader.js 43 KB

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