babylon.babylonFileLoader.js 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035
  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. if (BABYLON.SceneLoader.ForceFullSceneLoadingForIncremental) {
  429. mesh._checkDelayState();
  430. }
  431. } else {
  432. importGeometry(parsedMesh, mesh);
  433. }
  434. // Material
  435. if (parsedMesh.materialId) {
  436. mesh.setMaterialByID(parsedMesh.materialId);
  437. } else {
  438. mesh.material = null;
  439. }
  440. // Skeleton
  441. if (parsedMesh.skeletonId > -1) {
  442. mesh.skeleton = scene.getLastSkeletonByID(parsedMesh.skeletonId);
  443. }
  444. // Physics
  445. if (parsedMesh.physicsImpostor) {
  446. if (!scene.isPhysicsEnabled()) {
  447. scene.enablePhysics();
  448. }
  449. switch (parsedMesh.physicsImpostor) {
  450. case 1: // BOX
  451. mesh.setPhysicsState({ impostor: BABYLON.PhysicsEngine.BoxImpostor, mass: parsedMesh.physicsMass, friction: parsedMesh.physicsFriction, restitution: parsedMesh.physicsRestitution });
  452. break;
  453. case 2: // SPHERE
  454. mesh.setPhysicsState({ impostor: BABYLON.PhysicsEngine.SphereImpostor, mass: parsedMesh.physicsMass, friction: parsedMesh.physicsFriction, restitution: parsedMesh.physicsRestitution });
  455. break;
  456. }
  457. }
  458. // Animations
  459. if (parsedMesh.animations) {
  460. for (var animationIndex = 0; animationIndex < parsedMesh.animations.length; animationIndex++) {
  461. var parsedAnimation = parsedMesh.animations[animationIndex];
  462. mesh.animations.push(parseAnimation(parsedAnimation));
  463. }
  464. }
  465. if (parsedMesh.autoAnimate) {
  466. scene.beginAnimation(mesh, parsedMesh.autoAnimateFrom, parsedMesh.autoAnimateTo, parsedMesh.autoAnimateLoop, 1.0);
  467. }
  468. return mesh;
  469. };
  470. var isDescendantOf = function (mesh, names, hierarchyIds) {
  471. names = (names instanceof Array) ? names : [names];
  472. for (var i in names) {
  473. if (mesh.name === names[i]) {
  474. hierarchyIds.push(mesh.id);
  475. return true;
  476. }
  477. }
  478. if (mesh.parentId && hierarchyIds.indexOf(mesh.parentId) !== -1) {
  479. hierarchyIds.push(mesh.id);
  480. return true;
  481. }
  482. return false;
  483. };
  484. var importVertexData = function (parsedVertexData, geometry) {
  485. var vertexData = new BABYLON.VertexData();
  486. // positions
  487. var positions = parsedVertexData.positions;
  488. if (positions) {
  489. vertexData.set(positions, BABYLON.VertexBuffer.PositionKind);
  490. }
  491. // normals
  492. var normals = parsedVertexData.normals;
  493. if (normals) {
  494. vertexData.set(normals, BABYLON.VertexBuffer.NormalKind);
  495. }
  496. // uvs
  497. var uvs = parsedVertexData.uvs;
  498. if (uvs) {
  499. vertexData.set(uvs, BABYLON.VertexBuffer.UVKind);
  500. }
  501. // uv2s
  502. var uv2s = parsedVertexData.uv2s;
  503. if (uv2s) {
  504. vertexData.set(uv2s, BABYLON.VertexBuffer.UV2Kind);
  505. }
  506. // colors
  507. var colors = parsedVertexData.colors;
  508. if (colors) {
  509. vertexData.set(colors, BABYLON.VertexBuffer.ColorKind);
  510. }
  511. // matricesIndices
  512. var matricesIndices = parsedVertexData.matricesIndices;
  513. if (matricesIndices) {
  514. vertexData.set(matricesIndices, BABYLON.VertexBuffer.MatricesIndicesKind);
  515. }
  516. // matricesWeights
  517. var matricesWeights = parsedVertexData.matricesWeights;
  518. if (matricesWeights) {
  519. vertexData.set(matricesWeights, BABYLON.VertexBuffer.MatricesWeightsKind);
  520. }
  521. // indices
  522. var indices = parsedVertexData.indices;
  523. if (indices) {
  524. vertexData.indices = indices;
  525. }
  526. geometry.setAllVerticesData(vertexData, parsedVertexData.updatable);
  527. };
  528. var importGeometry = function (parsedGeometry, mesh) {
  529. var scene = mesh.getScene();
  530. // Geometry
  531. var geometryId = parsedGeometry.geometryId;
  532. if (geometryId) {
  533. var geometry = scene.getGeometryByID(geometryId);
  534. if (geometry) {
  535. geometry.applyToMesh(mesh);
  536. }
  537. }
  538. else if (parsedGeometry.positions && parsedGeometry.normals && parsedGeometry.indices) {
  539. mesh.setVerticesData(parsedGeometry.positions, BABYLON.VertexBuffer.PositionKind, false);
  540. mesh.setVerticesData(parsedGeometry.normals, BABYLON.VertexBuffer.NormalKind, false);
  541. if (parsedGeometry.uvs) {
  542. mesh.setVerticesData(parsedGeometry.uvs, BABYLON.VertexBuffer.UVKind, false);
  543. }
  544. if (parsedGeometry.uvs2) {
  545. mesh.setVerticesData(parsedGeometry.uvs2, BABYLON.VertexBuffer.UV2Kind, false);
  546. }
  547. if (parsedGeometry.colors) {
  548. mesh.setVerticesData(parsedGeometry.colors, BABYLON.VertexBuffer.ColorKind, false);
  549. }
  550. if (parsedGeometry.matricesIndices) {
  551. if (!parsedGeometry.matricesIndices._isExpanded) {
  552. var floatIndices = [];
  553. for (var i = 0; i < parsedGeometry.matricesIndices.length; i++) {
  554. var matricesIndex = parsedGeometry.matricesIndices[i];
  555. floatIndices.push(matricesIndex & 0x000000FF);
  556. floatIndices.push((matricesIndex & 0x0000FF00) >> 8);
  557. floatIndices.push((matricesIndex & 0x00FF0000) >> 16);
  558. floatIndices.push(matricesIndex >> 24);
  559. }
  560. mesh.setVerticesData(floatIndices, BABYLON.VertexBuffer.MatricesIndicesKind, false);
  561. } else {
  562. delete parsedGeometry.matricesIndices._isExpanded;
  563. mesh.setVerticesData(parsedGeometry.matricesIndices, BABYLON.VertexBuffer.MatricesIndicesKind, false);
  564. }
  565. }
  566. if (parsedGeometry.matricesWeights) {
  567. mesh.setVerticesData(parsedGeometry.matricesWeights, BABYLON.VertexBuffer.MatricesWeightsKind, false);
  568. }
  569. mesh.setIndices(parsedGeometry.indices);
  570. }
  571. // SubMeshes
  572. if (parsedGeometry.subMeshes) {
  573. mesh.subMeshes = [];
  574. for (var subIndex = 0; subIndex < parsedGeometry.subMeshes.length; subIndex++) {
  575. var parsedSubMesh = parsedGeometry.subMeshes[subIndex];
  576. var subMesh = new BABYLON.SubMesh(parsedSubMesh.materialIndex, parsedSubMesh.verticesStart, parsedSubMesh.verticesCount, parsedSubMesh.indexStart, parsedSubMesh.indexCount, mesh);
  577. }
  578. }
  579. // Update
  580. mesh.computeWorldMatrix(true);
  581. // Flat shading
  582. if (mesh._shouldGenerateFlatShading) {
  583. mesh.convertToFlatShadedMesh();
  584. delete mesh._shouldGenerateFlatShading;
  585. }
  586. if (scene._selectionOctree) {
  587. scene._selectionOctree.addMesh(mesh);
  588. }
  589. };
  590. BABYLON.SceneLoader.RegisterPlugin({
  591. extensions: ".babylon",
  592. importMesh: function (meshesNames, scene, data, rootUrl, meshes, particleSystems, skeletons) {
  593. var parsedData = JSON.parse(data);
  594. var loadedSkeletonsIds = [];
  595. var loadedMaterialsIds = [];
  596. var hierarchyIds = [];
  597. for (var index = 0; index < parsedData.meshes.length; index++) {
  598. var parsedMesh = parsedData.meshes[index];
  599. if (!meshesNames || isDescendantOf(parsedMesh, meshesNames, hierarchyIds)) {
  600. if (meshesNames instanceof Array) {
  601. // Remove found mesh name from list.
  602. delete meshesNames[meshesNames.indexOf(parsedMesh.name)];
  603. }
  604. // Material ?
  605. if (parsedMesh.materialId) {
  606. var materialFound = (loadedMaterialsIds.indexOf(parsedMesh.materialId) !== -1);
  607. if (!materialFound) {
  608. for (var multimatIndex = 0; multimatIndex < parsedData.multiMaterials.length; multimatIndex++) {
  609. var parsedMultiMaterial = parsedData.multiMaterials[multimatIndex];
  610. if (parsedMultiMaterial.id == parsedMesh.materialId) {
  611. for (var matIndex = 0; matIndex < parsedMultiMaterial.materials.length; matIndex++) {
  612. var subMatId = parsedMultiMaterial.materials[matIndex];
  613. loadedMaterialsIds.push(subMatId);
  614. parseMaterialById(subMatId, parsedData, scene, rootUrl);
  615. }
  616. loadedMaterialsIds.push(parsedMultiMaterial.id);
  617. parseMultiMaterial(parsedMultiMaterial, scene);
  618. materialFound = true;
  619. break;
  620. }
  621. }
  622. }
  623. if (!materialFound) {
  624. loadedMaterialsIds.push(parsedMesh.materialId);
  625. parseMaterialById(parsedMesh.materialId, parsedData, scene, rootUrl);
  626. }
  627. }
  628. // Skeleton ?
  629. if (parsedMesh.skeletonId > -1 && scene.skeletons) {
  630. var skeletonAlreadyLoaded = (loadedSkeletonsIds.indexOf(parsedMesh.skeletonId) > -1);
  631. if (!skeletonAlreadyLoaded) {
  632. for (var skeletonIndex = 0; skeletonIndex < parsedData.skeletons.length; skeletonIndex++) {
  633. var parsedSkeleton = parsedData.skeletons[skeletonIndex];
  634. if (parsedSkeleton.id === parsedMesh.skeletonId) {
  635. skeletons.push(parseSkeleton(parsedSkeleton, scene));
  636. loadedSkeletonsIds.push(parsedSkeleton.id);
  637. }
  638. }
  639. }
  640. }
  641. var mesh = parseMesh(parsedMesh, scene, rootUrl);
  642. meshes.push(mesh);
  643. }
  644. }
  645. // Particles
  646. if (parsedData.particleSystems) {
  647. for (var index = 0; index < parsedData.particleSystems.length; index++) {
  648. var parsedParticleSystem = parsedData.particleSystems[index];
  649. if (hierarchyIds.indexOf(parsedParticleSystem.emitterId) !== -1) {
  650. particleSystems.push(parseParticleSystem(parsedParticleSystem, scene, rootUrl));
  651. }
  652. }
  653. }
  654. return true;
  655. },
  656. load: function (scene, data, rootUrl) {
  657. var parsedData = JSON.parse(data);
  658. // Scene
  659. scene.useDelayedTextureLoading = parsedData.useDelayedTextureLoading && !BABYLON.SceneLoader.ForceFullSceneLoadingForIncremental;
  660. scene.autoClear = parsedData.autoClear;
  661. scene.clearColor = BABYLON.Color3.FromArray(parsedData.clearColor);
  662. scene.ambientColor = BABYLON.Color3.FromArray(parsedData.ambientColor);
  663. scene.gravity = BABYLON.Vector3.FromArray(parsedData.gravity);
  664. // Fog
  665. if (parsedData.fogMode && parsedData.fogMode !== 0) {
  666. scene.fogMode = parsedData.fogMode;
  667. scene.fogColor = BABYLON.Color3.FromArray(parsedData.fogColor);
  668. scene.fogStart = parsedData.fogStart;
  669. scene.fogEnd = parsedData.fogEnd;
  670. scene.fogDensity = parsedData.fogDensity;
  671. }
  672. // Lights
  673. for (var index = 0; index < parsedData.lights.length; index++) {
  674. var parsedLight = parsedData.lights[index];
  675. parseLight(parsedLight, scene);
  676. }
  677. // Cameras
  678. for (var index = 0; index < parsedData.cameras.length; index++) {
  679. var parsedCamera = parsedData.cameras[index];
  680. parseCamera(parsedCamera, scene);
  681. }
  682. if (parsedData.activeCameraID) {
  683. scene.setActiveCameraByID(parsedData.activeCameraID);
  684. }
  685. // Materials
  686. if (parsedData.materials) {
  687. for (var index = 0; index < parsedData.materials.length; index++) {
  688. var parsedMaterial = parsedData.materials[index];
  689. parseMaterial(parsedMaterial, scene, rootUrl);
  690. }
  691. }
  692. if (parsedData.multiMaterials) {
  693. for (var index = 0; index < parsedData.multiMaterials.length; index++) {
  694. var parsedMultiMaterial = parsedData.multiMaterials[index];
  695. parseMultiMaterial(parsedMultiMaterial, scene);
  696. }
  697. }
  698. // Skeletons
  699. if (parsedData.skeletons) {
  700. for (var index = 0; index < parsedData.skeletons.length; index++) {
  701. var parsedSkeleton = parsedData.skeletons[index];
  702. parseSkeleton(parsedSkeleton, scene);
  703. }
  704. }
  705. // Geometries
  706. var geometries = parsedData.geometries;
  707. if (geometries) {
  708. // Boxes
  709. var boxes = geometries.boxes;
  710. if (boxes) {
  711. for (var index = 0; index < boxes.length; index++) {
  712. var parsedBox = boxes[index];
  713. parseBox(parsedBox, scene);
  714. }
  715. }
  716. // Spheres
  717. var spheres = geometries.spheres;
  718. if (spheres) {
  719. for (var index = 0; index < spheres.length; index++) {
  720. var parsedSphere = spheres[index];
  721. parseSphere(parsedSphere, scene);
  722. }
  723. }
  724. // Cylinders
  725. var cylinders = geometries.cylinders;
  726. if (cylinders) {
  727. for (var index = 0; index < cylinders.length; index++) {
  728. var parsedCylinder = cylinders[index];
  729. parseCylinder(parsedCylinder, scene);
  730. }
  731. }
  732. // Toruses
  733. var toruses = geometries.toruses;
  734. if (toruses) {
  735. for (var index = 0; index < toruses.length; index++) {
  736. var parsedTorus = toruses[index];
  737. parseTorus(parsedTorus, scene);
  738. }
  739. }
  740. // Grounds
  741. var grounds = geometries.grounds;
  742. if (grounds) {
  743. for (var index = 0; index < grounds.length; index++) {
  744. var parsedGround = grounds[index];
  745. parseGround(parsedGround, scene);
  746. }
  747. }
  748. // Planes
  749. var planes = geometries.planes;
  750. if (planes) {
  751. for (var index = 0; index < planes.length; index++) {
  752. var parsedPlane = planes[index];
  753. parsePlane(parsedPlane, scene);
  754. }
  755. }
  756. // TorusKnots
  757. var torusKnots = geometries.torusKnots;
  758. if (torusKnots) {
  759. for (var index = 0; index < torusKnots.length; index++) {
  760. var parsedTorusKnot = torusKnots[index];
  761. parseTorusKnot(parsedTorusKnot, scene);
  762. }
  763. }
  764. // VertexData
  765. var vertexData = geometries.vertexData;
  766. if (vertexData) {
  767. for (var index = 0; index < vertexData.length; index++) {
  768. var parsedVertexData = vertexData[index];
  769. parseVertexData(parsedVertexData, scene, rootUrl);
  770. }
  771. }
  772. }
  773. // Meshes
  774. for (var index = 0; index < parsedData.meshes.length; index++) {
  775. var parsedMesh = parsedData.meshes[index];
  776. parseMesh(parsedMesh, scene, rootUrl);
  777. }
  778. // Connecting cameras parents and locked target
  779. for (var index = 0; index < scene.cameras.length; index++) {
  780. var camera = scene.cameras[index];
  781. if (camera._waitingParentId) {
  782. camera.parent = scene.getLastEntryByID(camera._waitingParentId);
  783. delete camera._waitingParentId;
  784. }
  785. if (camera._waitingLockedTargetId) {
  786. camera.lockedTarget = scene.getLastEntryByID(camera._waitingLockedTargetId);
  787. delete camera._waitingLockedTargetId;
  788. }
  789. }
  790. // Particles Systems
  791. if (parsedData.particleSystems) {
  792. for (var index = 0; index < parsedData.particleSystems.length; index++) {
  793. var parsedParticleSystem = parsedData.particleSystems[index];
  794. parseParticleSystem(parsedParticleSystem, scene, rootUrl);
  795. }
  796. }
  797. // Lens flares
  798. if (parsedData.lensFlareSystems) {
  799. for (var index = 0; index < parsedData.lensFlareSystems.length; index++) {
  800. var parsedLensFlareSystem = parsedData.lensFlareSystems[index];
  801. parseLensFlareSystem(parsedLensFlareSystem, scene, rootUrl);
  802. }
  803. }
  804. // Shadows
  805. if (parsedData.shadowGenerators) {
  806. for (var index = 0; index < parsedData.shadowGenerators.length; index++) {
  807. var parsedShadowGenerator = parsedData.shadowGenerators[index];
  808. parseShadowGenerator(parsedShadowGenerator, scene);
  809. }
  810. }
  811. // Finish
  812. return true;
  813. }
  814. });
  815. })();