babylon.babylonFileLoader.js 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084
  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. if (parsedLight.excludedMeshesIds) {
  236. light._excludedMeshesIds = parsedLight.excludedMeshesIds;
  237. }
  238. };
  239. var parseCamera = function (parsedCamera, scene) {
  240. var camera = new BABYLON.FreeCamera(parsedCamera.name, BABYLON.Vector3.FromArray(parsedCamera.position), scene);
  241. camera.id = parsedCamera.id;
  242. BABYLON.Tags.AddTagsTo(camera, parsedCamera.tags);
  243. // Parent
  244. if (parsedCamera.parentId) {
  245. camera._waitingParentId = parsedCamera.parentId;
  246. }
  247. // Target
  248. if (parsedCamera.target) {
  249. camera.setTarget(BABYLON.Vector3.FromArray(parsedCamera.target));
  250. } else {
  251. camera.rotation = BABYLON.Vector3.FromArray(parsedCamera.rotation);
  252. }
  253. // Locked target
  254. if (parsedCamera.lockedTargetId) {
  255. camera._waitingLockedTargetId = parsedCamera.lockedTargetId;
  256. }
  257. camera.fov = parsedCamera.fov;
  258. camera.minZ = parsedCamera.minZ;
  259. camera.maxZ = parsedCamera.maxZ;
  260. camera.speed = parsedCamera.speed;
  261. camera.inertia = parsedCamera.inertia;
  262. camera.checkCollisions = parsedCamera.checkCollisions;
  263. camera.applyGravity = parsedCamera.applyGravity;
  264. if (parsedCamera.ellipsoid) {
  265. camera.ellipsoid = BABYLON.Vector3.FromArray(parsedCamera.ellipsoid);
  266. }
  267. // Animations
  268. if (parsedCamera.animations) {
  269. for (var animationIndex = 0; animationIndex < parsedCamera.animations.length; animationIndex++) {
  270. var parsedAnimation = parsedCamera.animations[animationIndex];
  271. camera.animations.push(parseAnimation(parsedAnimation));
  272. }
  273. }
  274. if (parsedCamera.autoAnimate) {
  275. scene.beginAnimation(camera, parsedCamera.autoAnimateFrom, parsedCamera.autoAnimateTo, parsedCamera.autoAnimateLoop, 1.0);
  276. }
  277. // Layer Mask
  278. if (parsedCamera.layerMask && (!isNaN(parsedCamera.layerMask))) {
  279. camera.layerMask = Math.abs(parseInt(parsedCamera.layerMask));
  280. } else {
  281. camera.layerMask = 0xFFFFFFFF;
  282. }
  283. return camera;
  284. };
  285. var parseGeometry = function (parsedGeometry, scene) {
  286. var id = parsedGeometry.id;
  287. return scene.getGeometryByID(id);
  288. };
  289. var parseBox = function (parsedBox, scene) {
  290. if (parseGeometry(parsedBox, scene)) {
  291. return null; // null since geometry could be something else than a box...
  292. }
  293. var box = new BABYLON.Geometry.Primitives.Box(parsedBox.id, scene.getEngine(), parsedBox.size, parsedBox.canBeRegenerated, null);
  294. BABYLON.Tags.AddTagsTo(box, parsedBox.tags);
  295. scene.pushGeometry(box, true);
  296. return box;
  297. };
  298. var parseSphere = function (parsedSphere, scene) {
  299. if (parseGeometry(parsedSphere, scene)) {
  300. return null; // null since geometry could be something else than a sphere...
  301. }
  302. var sphere = new BABYLON.Geometry.Primitives.Sphere(parsedSphere.id, scene.getEngine(), parsedSphere.segments, parsedSphere.diameter, parsedSphere.canBeRegenerated, null);
  303. BABYLON.Tags.AddTagsTo(sphere, parsedSphere.tags);
  304. scene.pushGeometry(sphere, true);
  305. return sphere;
  306. };
  307. var parseCylinder = function (parsedCylinder, scene) {
  308. if (parseGeometry(parsedCylinder, scene)) {
  309. return null; // null since geometry could be something else than a cylinder...
  310. }
  311. var cylinder = new BABYLON.Geometry.Primitives.Cylinder(parsedCylinder.id, scene.getEngine(), parsedCylinder.height, parsedCylinder.diameterTop, parsedCylinder.diameterBottom, parsedCylinder.tessellation, parsedCylinder.canBeRegenerated, null);
  312. BABYLON.Tags.AddTagsTo(cylinder, parsedCylinder.tags);
  313. scene.pushGeometry(cylinder, true);
  314. return cylinder;
  315. };
  316. var parseTorus = function (parsedTorus, scene) {
  317. if (parseGeometry(parsedTorus, scene)) {
  318. return null; // null since geometry could be something else than a torus...
  319. }
  320. var torus = new BABYLON.Geometry.Primitives.Torus(parsedTorus.id, scene.getEngine(), parsedTorus.diameter, parsedTorus.thickness, parsedTorus.tessellation, parsedTorus.canBeRegenerated, null);
  321. BABYLON.Tags.AddTagsTo(torus, parsedTorus.tags);
  322. scene.pushGeometry(torus, true);
  323. return torus;
  324. };
  325. var parseGround = function (parsedGround, scene) {
  326. if (parseGeometry(parsedGround, scene)) {
  327. return null; // null since geometry could be something else than a ground...
  328. }
  329. var ground = new BABYLON.Geometry.Primitives.Ground(parsedGround.id, scene.getEngine(), parsedGround.width, parsedGround.height, parsedGround.subdivisions, parsedGround.canBeRegenerated, null);
  330. BABYLON.Tags.AddTagsTo(ground, parsedGround.tags);
  331. scene.pushGeometry(ground, true);
  332. return ground;
  333. };
  334. var parsePlane = function (parsedPlane, scene) {
  335. if (parseGeometry(parsedPlane, scene)) {
  336. return null; // null since geometry could be something else than a plane...
  337. }
  338. var plane = new BABYLON.Geometry.Primitives.Plane(parsedPlane.id, scene.getEngine(), parsedPlane.size, parsedPlane.canBeRegenerated, null);
  339. BABYLON.Tags.AddTagsTo(plane, parsedPlane.tags);
  340. scene.pushGeometry(plane, true);
  341. return plane;
  342. };
  343. var parseTorusKnot = function (parsedTorusKnot, scene) {
  344. if (parseGeometry(parsedTorusKnot, scene)) {
  345. return null; // null since geometry could be something else than a torusKnot...
  346. }
  347. 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);
  348. BABYLON.Tags.AddTagsTo(torusKnot, parsedTorusKnot.tags);
  349. scene.pushGeometry(torusKnot, true);
  350. return torusKnot;
  351. };
  352. var parseVertexData = function (parsedVertexData, scene, rootUrl) {
  353. if (parseGeometry(parsedVertexData, scene)) {
  354. return null; // null since geometry could be a primitive
  355. }
  356. var geometry = new BABYLON.Geometry(parsedVertexData.id, scene.getEngine());
  357. BABYLON.Tags.AddTagsTo(geometry, parsedVertexData.tags);
  358. if (parsedVertexData.delayLoadingFile) {
  359. geometry.delayLoadState = BABYLON.Engine.DELAYLOADSTATE_NOTLOADED;
  360. geometry.delayLoadingFile = rootUrl + parsedVertexData.delayLoadingFile;
  361. geometry._boundingInfo = new BABYLON.BoundingInfo(BABYLON.Vector3.FromArray(parsedVertexData.boundingBoxMinimum), BABYLON.Vector3.FromArray(parsedVertexData.boundingBoxMaximum));
  362. geometry._delayInfo = [];
  363. if (parsedVertexData.hasUVs) {
  364. geometry._delayInfo.push(BABYLON.VertexBuffer.UVKind);
  365. }
  366. if (parsedVertexData.hasUVs2) {
  367. geometry._delayInfo.push(BABYLON.VertexBuffer.UV2Kind);
  368. }
  369. if (parsedVertexData.hasColors) {
  370. geometry._delayInfo.push(BABYLON.VertexBuffer.ColorKind);
  371. }
  372. if (parsedVertexData.hasMatricesIndices) {
  373. geometry._delayInfo.push(BABYLON.VertexBuffer.MatricesIndicesKind);
  374. }
  375. if (parsedVertexData.hasMatricesWeights) {
  376. geometry._delayInfo.push(BABYLON.VertexBuffer.MatricesWeightsKind);
  377. }
  378. geometry._delayLoadingFunction = importVertexData;
  379. } else {
  380. importVertexData(parsedVertexData, geometry);
  381. }
  382. scene.pushGeometry(geometry, true);
  383. return geometry;
  384. };
  385. var parseMesh = function (parsedMesh, scene, rootUrl) {
  386. var mesh = new BABYLON.Mesh(parsedMesh.name, scene);
  387. mesh.id = parsedMesh.id;
  388. BABYLON.Tags.AddTagsTo(mesh, parsedMesh.tags);
  389. mesh.position = BABYLON.Vector3.FromArray(parsedMesh.position);
  390. if (parsedMesh.rotationQuaternion) {
  391. mesh.rotationQuaternion = BABYLON.Quaternion.FromArray(parsedMesh.rotationQuaternion);
  392. } else if (parsedMesh.rotation) {
  393. mesh.rotation = BABYLON.Vector3.FromArray(parsedMesh.rotation);
  394. }
  395. mesh.scaling = BABYLON.Vector3.FromArray(parsedMesh.scaling);
  396. if (parsedMesh.localMatrix) {
  397. mesh.setPivotMatrix(BABYLON.Matrix.FromArray(parsedMesh.localMatrix));
  398. }
  399. mesh.setEnabled(parsedMesh.isEnabled);
  400. mesh.isVisible = parsedMesh.isVisible;
  401. mesh.infiniteDistance = parsedMesh.infiniteDistance;
  402. if (parsedMesh.pickable !== undefined) {
  403. mesh.isPickable = parsedMesh.pickable;
  404. }
  405. mesh.receiveShadows = parsedMesh.receiveShadows;
  406. mesh.billboardMode = parsedMesh.billboardMode;
  407. if (parsedMesh.visibility !== undefined) {
  408. mesh.visibility = parsedMesh.visibility;
  409. }
  410. mesh.checkCollisions = parsedMesh.checkCollisions;
  411. mesh._shouldGenerateFlatShading = parsedMesh.useFlatShading;
  412. // Parent
  413. if (parsedMesh.parentId) {
  414. mesh.parent = scene.getLastEntryByID(parsedMesh.parentId);
  415. }
  416. // Geometry
  417. if (parsedMesh.delayLoadingFile) {
  418. mesh.delayLoadState = BABYLON.Engine.DELAYLOADSTATE_NOTLOADED;
  419. mesh.delayLoadingFile = rootUrl + parsedMesh.delayLoadingFile;
  420. mesh._boundingInfo = new BABYLON.BoundingInfo(BABYLON.Vector3.FromArray(parsedMesh.boundingBoxMinimum), BABYLON.Vector3.FromArray(parsedMesh.boundingBoxMaximum));
  421. mesh._delayInfo = [];
  422. if (parsedMesh.hasUVs) {
  423. mesh._delayInfo.push(BABYLON.VertexBuffer.UVKind);
  424. }
  425. if (parsedMesh.hasUVs2) {
  426. mesh._delayInfo.push(BABYLON.VertexBuffer.UV2Kind);
  427. }
  428. if (parsedMesh.hasColors) {
  429. mesh._delayInfo.push(BABYLON.VertexBuffer.ColorKind);
  430. }
  431. if (parsedMesh.hasMatricesIndices) {
  432. mesh._delayInfo.push(BABYLON.VertexBuffer.MatricesIndicesKind);
  433. }
  434. if (parsedMesh.hasMatricesWeights) {
  435. mesh._delayInfo.push(BABYLON.VertexBuffer.MatricesWeightsKind);
  436. }
  437. mesh._delayLoadingFunction = importGeometry;
  438. if (BABYLON.SceneLoader.ForceFullSceneLoadingForIncremental) {
  439. mesh._checkDelayState();
  440. }
  441. } else {
  442. importGeometry(parsedMesh, mesh);
  443. }
  444. // Material
  445. if (parsedMesh.materialId) {
  446. mesh.setMaterialByID(parsedMesh.materialId);
  447. } else {
  448. mesh.material = null;
  449. }
  450. // Skeleton
  451. if (parsedMesh.skeletonId > -1) {
  452. mesh.skeleton = scene.getLastSkeletonByID(parsedMesh.skeletonId);
  453. }
  454. // Physics
  455. if (parsedMesh.physicsImpostor) {
  456. if (!scene.isPhysicsEnabled()) {
  457. scene.enablePhysics();
  458. }
  459. switch (parsedMesh.physicsImpostor) {
  460. case 1: // BOX
  461. mesh.setPhysicsState({ impostor: BABYLON.PhysicsEngine.BoxImpostor, mass: parsedMesh.physicsMass, friction: parsedMesh.physicsFriction, restitution: parsedMesh.physicsRestitution });
  462. break;
  463. case 2: // SPHERE
  464. mesh.setPhysicsState({ impostor: BABYLON.PhysicsEngine.SphereImpostor, mass: parsedMesh.physicsMass, friction: parsedMesh.physicsFriction, restitution: parsedMesh.physicsRestitution });
  465. break;
  466. }
  467. }
  468. // Animations
  469. if (parsedMesh.animations) {
  470. for (var animationIndex = 0; animationIndex < parsedMesh.animations.length; animationIndex++) {
  471. var parsedAnimation = parsedMesh.animations[animationIndex];
  472. mesh.animations.push(parseAnimation(parsedAnimation));
  473. }
  474. }
  475. if (parsedMesh.autoAnimate) {
  476. scene.beginAnimation(mesh, parsedMesh.autoAnimateFrom, parsedMesh.autoAnimateTo, parsedMesh.autoAnimateLoop, 1.0);
  477. }
  478. // Layer Mask
  479. if (parsedMesh.layerMask && (!isNaN(parsedMesh.layerMask))) {
  480. mesh.layerMask = Math.abs(parseInt(parsedMesh.layerMask));
  481. } else {
  482. mesh.layerMask = 0xFFFFFFFF;
  483. }
  484. // Instances
  485. if (parsedMesh.instances) {
  486. for (var index = 0; index < parsedMesh.instances.length; index++) {
  487. var parsedInstance = parsedMesh.instances[index];
  488. var instance = mesh.createInstance(parsedInstance.name);
  489. BABYLON.Tags.AddTagsTo(instance, parsedInstance.tags);
  490. instance.position = BABYLON.Vector3.FromArray(parsedInstance.position);
  491. if (parsedInstance.rotationQuaternion) {
  492. instance.rotationQuaternion = BABYLON.Quaternion.FromArray(parsedInstance.rotationQuaternion);
  493. } else if (parsedInstance.rotation) {
  494. instance.rotation = BABYLON.Vector3.FromArray(parsedInstance.rotation);
  495. }
  496. instance.scaling = BABYLON.Vector3.FromArray(parsedInstance.scaling);
  497. instance.checkCollisions = mesh.checkCollisions;
  498. if (parsedMesh.animations) {
  499. for (animationIndex = 0; animationIndex < parsedMesh.animations.length; animationIndex++) {
  500. parsedAnimation = parsedMesh.animations[animationIndex];
  501. instance.animations.push(parseAnimation(parsedAnimation));
  502. }
  503. }
  504. }
  505. }
  506. return mesh;
  507. };
  508. var isDescendantOf = function (mesh, names, hierarchyIds) {
  509. names = (names instanceof Array) ? names : [names];
  510. for (var i in names) {
  511. if (mesh.name === names[i]) {
  512. hierarchyIds.push(mesh.id);
  513. return true;
  514. }
  515. }
  516. if (mesh.parentId && hierarchyIds.indexOf(mesh.parentId) !== -1) {
  517. hierarchyIds.push(mesh.id);
  518. return true;
  519. }
  520. return false;
  521. };
  522. var importVertexData = function (parsedVertexData, geometry) {
  523. var vertexData = new BABYLON.VertexData();
  524. // positions
  525. var positions = parsedVertexData.positions;
  526. if (positions) {
  527. vertexData.set(positions, BABYLON.VertexBuffer.PositionKind);
  528. }
  529. // normals
  530. var normals = parsedVertexData.normals;
  531. if (normals) {
  532. vertexData.set(normals, BABYLON.VertexBuffer.NormalKind);
  533. }
  534. // uvs
  535. var uvs = parsedVertexData.uvs;
  536. if (uvs) {
  537. vertexData.set(uvs, BABYLON.VertexBuffer.UVKind);
  538. }
  539. // uv2s
  540. var uv2s = parsedVertexData.uv2s;
  541. if (uv2s) {
  542. vertexData.set(uv2s, BABYLON.VertexBuffer.UV2Kind);
  543. }
  544. // colors
  545. var colors = parsedVertexData.colors;
  546. if (colors) {
  547. vertexData.set(colors, BABYLON.VertexBuffer.ColorKind);
  548. }
  549. // matricesIndices
  550. var matricesIndices = parsedVertexData.matricesIndices;
  551. if (matricesIndices) {
  552. vertexData.set(matricesIndices, BABYLON.VertexBuffer.MatricesIndicesKind);
  553. }
  554. // matricesWeights
  555. var matricesWeights = parsedVertexData.matricesWeights;
  556. if (matricesWeights) {
  557. vertexData.set(matricesWeights, BABYLON.VertexBuffer.MatricesWeightsKind);
  558. }
  559. // indices
  560. var indices = parsedVertexData.indices;
  561. if (indices) {
  562. vertexData.indices = indices;
  563. }
  564. geometry.setAllVerticesData(vertexData, parsedVertexData.updatable);
  565. };
  566. var importGeometry = function (parsedGeometry, mesh) {
  567. var scene = mesh.getScene();
  568. // Geometry
  569. var geometryId = parsedGeometry.geometryId;
  570. if (geometryId) {
  571. var geometry = scene.getGeometryByID(geometryId);
  572. if (geometry) {
  573. geometry.applyToMesh(mesh);
  574. }
  575. }
  576. else if (parsedGeometry.positions && parsedGeometry.normals && parsedGeometry.indices) {
  577. mesh.setVerticesData(BABYLON.VertexBuffer.PositionKind, parsedGeometry.positions, false);
  578. mesh.setVerticesData(BABYLON.VertexBuffer.NormalKind, parsedGeometry.normals, false);
  579. if (parsedGeometry.uvs) {
  580. mesh.setVerticesData(BABYLON.VertexBuffer.UVKind, parsedGeometry.uvs, false);
  581. }
  582. if (parsedGeometry.uvs2) {
  583. mesh.setVerticesData(BABYLON.VertexBuffer.UV2Kind, parsedGeometry.uvs2, false);
  584. }
  585. if (parsedGeometry.colors) {
  586. mesh.setVerticesData(BABYLON.VertexBuffer.ColorKind, parsedGeometry.colors, false);
  587. }
  588. if (parsedGeometry.matricesIndices) {
  589. if (!parsedGeometry.matricesIndices._isExpanded) {
  590. var floatIndices = [];
  591. for (var i = 0; i < parsedGeometry.matricesIndices.length; i++) {
  592. var matricesIndex = parsedGeometry.matricesIndices[i];
  593. floatIndices.push(matricesIndex & 0x000000FF);
  594. floatIndices.push((matricesIndex & 0x0000FF00) >> 8);
  595. floatIndices.push((matricesIndex & 0x00FF0000) >> 16);
  596. floatIndices.push(matricesIndex >> 24);
  597. }
  598. mesh.setVerticesData(BABYLON.VertexBuffer.MatricesIndicesKind, floatIndices, false);
  599. } else {
  600. delete parsedGeometry.matricesIndices._isExpanded;
  601. mesh.setVerticesData(BABYLON.VertexBuffer.MatricesIndicesKind, parsedGeometry.matricesIndices, false);
  602. }
  603. }
  604. if (parsedGeometry.matricesWeights) {
  605. mesh.setVerticesData(BABYLON.VertexBuffer.MatricesWeightsKind, parsedGeometry.matricesWeights, false);
  606. }
  607. mesh.setIndices(parsedGeometry.indices);
  608. }
  609. // SubMeshes
  610. if (parsedGeometry.subMeshes) {
  611. mesh.subMeshes = [];
  612. for (var subIndex = 0; subIndex < parsedGeometry.subMeshes.length; subIndex++) {
  613. var parsedSubMesh = parsedGeometry.subMeshes[subIndex];
  614. var subMesh = new BABYLON.SubMesh(parsedSubMesh.materialIndex, parsedSubMesh.verticesStart, parsedSubMesh.verticesCount, parsedSubMesh.indexStart, parsedSubMesh.indexCount, mesh);
  615. }
  616. }
  617. // Update
  618. mesh.computeWorldMatrix(true);
  619. // Flat shading
  620. if (mesh._shouldGenerateFlatShading) {
  621. mesh.convertToFlatShadedMesh();
  622. delete mesh._shouldGenerateFlatShading;
  623. }
  624. if (scene._selectionOctree) {
  625. scene._selectionOctree.addMesh(mesh);
  626. }
  627. };
  628. BABYLON.SceneLoader.RegisterPlugin({
  629. extensions: ".babylon",
  630. importMesh: function (meshesNames, scene, data, rootUrl, meshes, particleSystems, skeletons) {
  631. var parsedData = JSON.parse(data);
  632. var loadedSkeletonsIds = [];
  633. var loadedMaterialsIds = [];
  634. var hierarchyIds = [];
  635. for (var index = 0; index < parsedData.meshes.length; index++) {
  636. var parsedMesh = parsedData.meshes[index];
  637. if (!meshesNames || isDescendantOf(parsedMesh, meshesNames, hierarchyIds)) {
  638. if (meshesNames instanceof Array) {
  639. // Remove found mesh name from list.
  640. delete meshesNames[meshesNames.indexOf(parsedMesh.name)];
  641. }
  642. // Material ?
  643. if (parsedMesh.materialId) {
  644. var materialFound = (loadedMaterialsIds.indexOf(parsedMesh.materialId) !== -1);
  645. if (!materialFound) {
  646. for (var multimatIndex = 0; multimatIndex < parsedData.multiMaterials.length; multimatIndex++) {
  647. var parsedMultiMaterial = parsedData.multiMaterials[multimatIndex];
  648. if (parsedMultiMaterial.id == parsedMesh.materialId) {
  649. for (var matIndex = 0; matIndex < parsedMultiMaterial.materials.length; matIndex++) {
  650. var subMatId = parsedMultiMaterial.materials[matIndex];
  651. loadedMaterialsIds.push(subMatId);
  652. parseMaterialById(subMatId, parsedData, scene, rootUrl);
  653. }
  654. loadedMaterialsIds.push(parsedMultiMaterial.id);
  655. parseMultiMaterial(parsedMultiMaterial, scene);
  656. materialFound = true;
  657. break;
  658. }
  659. }
  660. }
  661. if (!materialFound) {
  662. loadedMaterialsIds.push(parsedMesh.materialId);
  663. parseMaterialById(parsedMesh.materialId, parsedData, scene, rootUrl);
  664. }
  665. }
  666. // Skeleton ?
  667. if (parsedMesh.skeletonId > -1 && scene.skeletons) {
  668. var skeletonAlreadyLoaded = (loadedSkeletonsIds.indexOf(parsedMesh.skeletonId) > -1);
  669. if (!skeletonAlreadyLoaded) {
  670. for (var skeletonIndex = 0; skeletonIndex < parsedData.skeletons.length; skeletonIndex++) {
  671. var parsedSkeleton = parsedData.skeletons[skeletonIndex];
  672. if (parsedSkeleton.id === parsedMesh.skeletonId) {
  673. skeletons.push(parseSkeleton(parsedSkeleton, scene));
  674. loadedSkeletonsIds.push(parsedSkeleton.id);
  675. }
  676. }
  677. }
  678. }
  679. var mesh = parseMesh(parsedMesh, scene, rootUrl);
  680. meshes.push(mesh);
  681. }
  682. }
  683. // Particles
  684. if (parsedData.particleSystems) {
  685. for (var index = 0; index < parsedData.particleSystems.length; index++) {
  686. var parsedParticleSystem = parsedData.particleSystems[index];
  687. if (hierarchyIds.indexOf(parsedParticleSystem.emitterId) !== -1) {
  688. particleSystems.push(parseParticleSystem(parsedParticleSystem, scene, rootUrl));
  689. }
  690. }
  691. }
  692. return true;
  693. },
  694. load: function (scene, data, rootUrl) {
  695. var parsedData = JSON.parse(data);
  696. // Scene
  697. scene.useDelayedTextureLoading = parsedData.useDelayedTextureLoading && !BABYLON.SceneLoader.ForceFullSceneLoadingForIncremental;
  698. scene.autoClear = parsedData.autoClear;
  699. scene.clearColor = BABYLON.Color3.FromArray(parsedData.clearColor);
  700. scene.ambientColor = BABYLON.Color3.FromArray(parsedData.ambientColor);
  701. scene.gravity = BABYLON.Vector3.FromArray(parsedData.gravity);
  702. // Fog
  703. if (parsedData.fogMode && parsedData.fogMode !== 0) {
  704. scene.fogMode = parsedData.fogMode;
  705. scene.fogColor = BABYLON.Color3.FromArray(parsedData.fogColor);
  706. scene.fogStart = parsedData.fogStart;
  707. scene.fogEnd = parsedData.fogEnd;
  708. scene.fogDensity = parsedData.fogDensity;
  709. }
  710. // Lights
  711. for (var index = 0; index < parsedData.lights.length; index++) {
  712. var parsedLight = parsedData.lights[index];
  713. parseLight(parsedLight, scene);
  714. }
  715. // Cameras
  716. for (var index = 0; index < parsedData.cameras.length; index++) {
  717. var parsedCamera = parsedData.cameras[index];
  718. parseCamera(parsedCamera, scene);
  719. }
  720. if (parsedData.activeCameraID) {
  721. scene.setActiveCameraByID(parsedData.activeCameraID);
  722. }
  723. // Materials
  724. if (parsedData.materials) {
  725. for (var index = 0; index < parsedData.materials.length; index++) {
  726. var parsedMaterial = parsedData.materials[index];
  727. parseMaterial(parsedMaterial, scene, rootUrl);
  728. }
  729. }
  730. if (parsedData.multiMaterials) {
  731. for (var index = 0; index < parsedData.multiMaterials.length; index++) {
  732. var parsedMultiMaterial = parsedData.multiMaterials[index];
  733. parseMultiMaterial(parsedMultiMaterial, scene);
  734. }
  735. }
  736. // Skeletons
  737. if (parsedData.skeletons) {
  738. for (var index = 0; index < parsedData.skeletons.length; index++) {
  739. var parsedSkeleton = parsedData.skeletons[index];
  740. parseSkeleton(parsedSkeleton, scene);
  741. }
  742. }
  743. // Geometries
  744. var geometries = parsedData.geometries;
  745. if (geometries) {
  746. // Boxes
  747. var boxes = geometries.boxes;
  748. if (boxes) {
  749. for (var index = 0; index < boxes.length; index++) {
  750. var parsedBox = boxes[index];
  751. parseBox(parsedBox, scene);
  752. }
  753. }
  754. // Spheres
  755. var spheres = geometries.spheres;
  756. if (spheres) {
  757. for (var index = 0; index < spheres.length; index++) {
  758. var parsedSphere = spheres[index];
  759. parseSphere(parsedSphere, scene);
  760. }
  761. }
  762. // Cylinders
  763. var cylinders = geometries.cylinders;
  764. if (cylinders) {
  765. for (var index = 0; index < cylinders.length; index++) {
  766. var parsedCylinder = cylinders[index];
  767. parseCylinder(parsedCylinder, scene);
  768. }
  769. }
  770. // Toruses
  771. var toruses = geometries.toruses;
  772. if (toruses) {
  773. for (var index = 0; index < toruses.length; index++) {
  774. var parsedTorus = toruses[index];
  775. parseTorus(parsedTorus, scene);
  776. }
  777. }
  778. // Grounds
  779. var grounds = geometries.grounds;
  780. if (grounds) {
  781. for (var index = 0; index < grounds.length; index++) {
  782. var parsedGround = grounds[index];
  783. parseGround(parsedGround, scene);
  784. }
  785. }
  786. // Planes
  787. var planes = geometries.planes;
  788. if (planes) {
  789. for (var index = 0; index < planes.length; index++) {
  790. var parsedPlane = planes[index];
  791. parsePlane(parsedPlane, scene);
  792. }
  793. }
  794. // TorusKnots
  795. var torusKnots = geometries.torusKnots;
  796. if (torusKnots) {
  797. for (var index = 0; index < torusKnots.length; index++) {
  798. var parsedTorusKnot = torusKnots[index];
  799. parseTorusKnot(parsedTorusKnot, scene);
  800. }
  801. }
  802. // VertexData
  803. var vertexData = geometries.vertexData;
  804. if (vertexData) {
  805. for (var index = 0; index < vertexData.length; index++) {
  806. var parsedVertexData = vertexData[index];
  807. parseVertexData(parsedVertexData, scene, rootUrl);
  808. }
  809. }
  810. }
  811. // Meshes
  812. for (var index = 0; index < parsedData.meshes.length; index++) {
  813. var parsedMesh = parsedData.meshes[index];
  814. parseMesh(parsedMesh, scene, rootUrl);
  815. }
  816. // Connecting cameras parents and locked target
  817. for (var index = 0; index < scene.cameras.length; index++) {
  818. var camera = scene.cameras[index];
  819. if (camera._waitingParentId) {
  820. camera.parent = scene.getLastEntryByID(camera._waitingParentId);
  821. delete camera._waitingParentId;
  822. }
  823. if (camera._waitingLockedTargetId) {
  824. camera.lockedTarget = scene.getLastEntryByID(camera._waitingLockedTargetId);
  825. delete camera._waitingLockedTargetId;
  826. }
  827. }
  828. // Particles Systems
  829. if (parsedData.particleSystems) {
  830. for (var index = 0; index < parsedData.particleSystems.length; index++) {
  831. var parsedParticleSystem = parsedData.particleSystems[index];
  832. parseParticleSystem(parsedParticleSystem, scene, rootUrl);
  833. }
  834. }
  835. // Lens flares
  836. if (parsedData.lensFlareSystems) {
  837. for (var index = 0; index < parsedData.lensFlareSystems.length; index++) {
  838. var parsedLensFlareSystem = parsedData.lensFlareSystems[index];
  839. parseLensFlareSystem(parsedLensFlareSystem, scene, rootUrl);
  840. }
  841. }
  842. // Shadows
  843. if (parsedData.shadowGenerators) {
  844. for (var index = 0; index < parsedData.shadowGenerators.length; index++) {
  845. var parsedShadowGenerator = parsedData.shadowGenerators[index];
  846. parseShadowGenerator(parsedShadowGenerator, scene);
  847. }
  848. }
  849. // Finish
  850. return true;
  851. }
  852. });
  853. })();