babylon.babylonFileLoader.js 43 KB

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