babylon.babylonFileLoader.js 43 KB

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