babylon.babylonFileLoader.js 41 KB

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