babylon.babylonFileLoader.js 43 KB

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