babylon.babylonFileLoader.js 40 KB

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