babylon.scene.js 75 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696
  1. var BABYLON;
  2. (function (BABYLON) {
  3. /**
  4. * Represents a scene to be rendered by the engine.
  5. * @see http://doc.babylonjs.com/page.php?p=21911
  6. */
  7. var Scene = (function () {
  8. /**
  9. * @constructor
  10. * @param {BABYLON.Engine} engine - the engine to be used to render this scene.
  11. */
  12. function Scene(engine) {
  13. // Members
  14. this.autoClear = true;
  15. this.clearColor = new BABYLON.Color3(0.2, 0.2, 0.3);
  16. this.ambientColor = new BABYLON.Color3(0, 0, 0);
  17. this.forceWireframe = false;
  18. this.forcePointsCloud = false;
  19. this.forceShowBoundingBoxes = false;
  20. this.animationsEnabled = true;
  21. this.cameraToUseForPointers = null; // Define this parameter if you are using multiple cameras and you want to specify which one should be used for pointer position
  22. // Fog
  23. /**
  24. * is fog enabled on this scene.
  25. * @type {boolean}
  26. */
  27. this.fogEnabled = true;
  28. this.fogMode = Scene.FOGMODE_NONE;
  29. this.fogColor = new BABYLON.Color3(0.2, 0.2, 0.3);
  30. this.fogDensity = 0.1;
  31. this.fogStart = 0;
  32. this.fogEnd = 1000.0;
  33. // Lights
  34. /**
  35. * is shadow enabled on this scene.
  36. * @type {boolean}
  37. */
  38. this.shadowsEnabled = true;
  39. /**
  40. * is light enabled on this scene.
  41. * @type {boolean}
  42. */
  43. this.lightsEnabled = true;
  44. /**
  45. * All of the lights added to this scene.
  46. * @see BABYLON.Light
  47. * @type {BABYLON.Light[]}
  48. */
  49. this.lights = new Array();
  50. // Cameras
  51. /**
  52. * All of the cameras added to this scene.
  53. * @see BABYLON.Camera
  54. * @type {BABYLON.Camera[]}
  55. */
  56. this.cameras = new Array();
  57. this.activeCameras = new Array();
  58. // Meshes
  59. /**
  60. * All of the (abstract) meshes added to this scene.
  61. * @see BABYLON.AbstractMesh
  62. * @type {BABYLON.AbstractMesh[]}
  63. */
  64. this.meshes = new Array();
  65. // Geometries
  66. this._geometries = new Array();
  67. this.materials = new Array();
  68. this.multiMaterials = new Array();
  69. this.defaultMaterial = new BABYLON.StandardMaterial("default material", this);
  70. // Textures
  71. this.texturesEnabled = true;
  72. this.textures = new Array();
  73. // Particles
  74. this.particlesEnabled = true;
  75. this.particleSystems = new Array();
  76. // Sprites
  77. this.spritesEnabled = true;
  78. this.spriteManagers = new Array();
  79. // Layers
  80. this.layers = new Array();
  81. // Skeletons
  82. this.skeletonsEnabled = true;
  83. this.skeletons = new Array();
  84. // Lens flares
  85. this.lensFlaresEnabled = true;
  86. this.lensFlareSystems = new Array();
  87. // Collisions
  88. this.collisionsEnabled = true;
  89. this.gravity = new BABYLON.Vector3(0, -9.0, 0);
  90. // Postprocesses
  91. this.postProcessesEnabled = true;
  92. // Customs render targets
  93. this.renderTargetsEnabled = true;
  94. this.dumpNextRenderTargets = false;
  95. this.customRenderTargets = new Array();
  96. // Imported meshes
  97. this.importedMeshesFiles = new Array();
  98. this._actionManagers = new Array();
  99. this._meshesForIntersections = new BABYLON.SmartArray(256);
  100. // Procedural textures
  101. this.proceduralTexturesEnabled = true;
  102. this._proceduralTextures = new Array();
  103. this.soundTracks = new Array();
  104. this._audioEnabled = true;
  105. this._headphone = false;
  106. this._totalVertices = 0;
  107. this._activeVertices = 0;
  108. this._activeParticles = 0;
  109. this._lastFrameDuration = 0;
  110. this._evaluateActiveMeshesDuration = 0;
  111. this._renderTargetsDuration = 0;
  112. this._particlesDuration = 0;
  113. this._renderDuration = 0;
  114. this._spritesDuration = 0;
  115. this._animationRatio = 0;
  116. this._renderId = 0;
  117. this._executeWhenReadyTimeoutId = -1;
  118. this._toBeDisposed = new BABYLON.SmartArray(256);
  119. this._onReadyCallbacks = new Array();
  120. this._pendingData = []; //ANY
  121. this._onBeforeRenderCallbacks = new Array();
  122. this._onAfterRenderCallbacks = new Array();
  123. this._activeMeshes = new BABYLON.SmartArray(256);
  124. this._processedMaterials = new BABYLON.SmartArray(256);
  125. this._renderTargets = new BABYLON.SmartArray(256);
  126. this._activeParticleSystems = new BABYLON.SmartArray(256);
  127. this._activeSkeletons = new BABYLON.SmartArray(32);
  128. this._activeBones = 0;
  129. this._activeAnimatables = new Array();
  130. this._transformMatrix = BABYLON.Matrix.Zero();
  131. this._scaledPosition = BABYLON.Vector3.Zero();
  132. this._scaledVelocity = BABYLON.Vector3.Zero();
  133. this._uniqueIdCounter = 0;
  134. this._engine = engine;
  135. engine.scenes.push(this);
  136. this._renderingManager = new BABYLON.RenderingManager(this);
  137. this.postProcessManager = new BABYLON.PostProcessManager(this);
  138. this.postProcessRenderPipelineManager = new BABYLON.PostProcessRenderPipelineManager();
  139. this._boundingBoxRenderer = new BABYLON.BoundingBoxRenderer(this);
  140. this._outlineRenderer = new BABYLON.OutlineRenderer(this);
  141. this.attachControl();
  142. this._debugLayer = new BABYLON.DebugLayer(this);
  143. this.mainSoundTrack = new BABYLON.SoundTrack(this, { mainTrack: true });
  144. //simplification queue
  145. this.simplificationQueue = new BABYLON.SimplificationQueue();
  146. }
  147. Object.defineProperty(Scene, "FOGMODE_NONE", {
  148. get: function () {
  149. return Scene._FOGMODE_NONE;
  150. },
  151. enumerable: true,
  152. configurable: true
  153. });
  154. Object.defineProperty(Scene, "FOGMODE_EXP", {
  155. get: function () {
  156. return Scene._FOGMODE_EXP;
  157. },
  158. enumerable: true,
  159. configurable: true
  160. });
  161. Object.defineProperty(Scene, "FOGMODE_EXP2", {
  162. get: function () {
  163. return Scene._FOGMODE_EXP2;
  164. },
  165. enumerable: true,
  166. configurable: true
  167. });
  168. Object.defineProperty(Scene, "FOGMODE_LINEAR", {
  169. get: function () {
  170. return Scene._FOGMODE_LINEAR;
  171. },
  172. enumerable: true,
  173. configurable: true
  174. });
  175. Object.defineProperty(Scene.prototype, "debugLayer", {
  176. // Properties
  177. get: function () {
  178. return this._debugLayer;
  179. },
  180. enumerable: true,
  181. configurable: true
  182. });
  183. Object.defineProperty(Scene.prototype, "meshUnderPointer", {
  184. /**
  185. * The mesh that is currently under the pointer.
  186. * @return {BABYLON.AbstractMesh} mesh under the pointer/mouse cursor or null if none.
  187. */
  188. get: function () {
  189. return this._meshUnderPointer;
  190. },
  191. enumerable: true,
  192. configurable: true
  193. });
  194. Object.defineProperty(Scene.prototype, "pointerX", {
  195. /**
  196. * Current on-screen X position of the pointer
  197. * @return {number} X position of the pointer
  198. */
  199. get: function () {
  200. return this._pointerX;
  201. },
  202. enumerable: true,
  203. configurable: true
  204. });
  205. Object.defineProperty(Scene.prototype, "pointerY", {
  206. /**
  207. * Current on-screen Y position of the pointer
  208. * @return {number} Y position of the pointer
  209. */
  210. get: function () {
  211. return this._pointerY;
  212. },
  213. enumerable: true,
  214. configurable: true
  215. });
  216. Scene.prototype.getCachedMaterial = function () {
  217. return this._cachedMaterial;
  218. };
  219. Scene.prototype.getBoundingBoxRenderer = function () {
  220. return this._boundingBoxRenderer;
  221. };
  222. Scene.prototype.getOutlineRenderer = function () {
  223. return this._outlineRenderer;
  224. };
  225. Scene.prototype.getEngine = function () {
  226. return this._engine;
  227. };
  228. Scene.prototype.getTotalVertices = function () {
  229. return this._totalVertices;
  230. };
  231. Scene.prototype.getActiveVertices = function () {
  232. return this._activeVertices;
  233. };
  234. Scene.prototype.getActiveParticles = function () {
  235. return this._activeParticles;
  236. };
  237. Scene.prototype.getActiveBones = function () {
  238. return this._activeBones;
  239. };
  240. // Stats
  241. Scene.prototype.getLastFrameDuration = function () {
  242. return this._lastFrameDuration;
  243. };
  244. Scene.prototype.getEvaluateActiveMeshesDuration = function () {
  245. return this._evaluateActiveMeshesDuration;
  246. };
  247. Scene.prototype.getActiveMeshes = function () {
  248. return this._activeMeshes;
  249. };
  250. Scene.prototype.getRenderTargetsDuration = function () {
  251. return this._renderTargetsDuration;
  252. };
  253. Scene.prototype.getRenderDuration = function () {
  254. return this._renderDuration;
  255. };
  256. Scene.prototype.getParticlesDuration = function () {
  257. return this._particlesDuration;
  258. };
  259. Scene.prototype.getSpritesDuration = function () {
  260. return this._spritesDuration;
  261. };
  262. Scene.prototype.getAnimationRatio = function () {
  263. return this._animationRatio;
  264. };
  265. Scene.prototype.getRenderId = function () {
  266. return this._renderId;
  267. };
  268. Scene.prototype.incrementRenderId = function () {
  269. this._renderId++;
  270. };
  271. Scene.prototype._updatePointerPosition = function (evt) {
  272. var canvasRect = this._engine.getRenderingCanvasClientRect();
  273. this._pointerX = evt.clientX - canvasRect.left;
  274. this._pointerY = evt.clientY - canvasRect.top;
  275. if (this.cameraToUseForPointers) {
  276. this._pointerX = this._pointerX - this.cameraToUseForPointers.viewport.x * this._engine.getRenderWidth();
  277. this._pointerY = this._pointerY - this.cameraToUseForPointers.viewport.y * this._engine.getRenderHeight();
  278. }
  279. };
  280. // Pointers handling
  281. Scene.prototype.attachControl = function () {
  282. var _this = this;
  283. this._onPointerMove = function (evt) {
  284. var canvas = _this._engine.getRenderingCanvas();
  285. _this._updatePointerPosition(evt);
  286. var pickResult = _this.pick(_this._pointerX, _this._pointerY, function (mesh) { return mesh.isPickable && mesh.isVisible && mesh.isReady() && mesh.actionManager && mesh.actionManager.hasPointerTriggers; }, false, _this.cameraToUseForPointers);
  287. if (pickResult.hit) {
  288. _this._meshUnderPointer = pickResult.pickedMesh;
  289. _this.setPointerOverMesh(pickResult.pickedMesh);
  290. canvas.style.cursor = "pointer";
  291. }
  292. else {
  293. _this.setPointerOverMesh(null);
  294. canvas.style.cursor = "";
  295. _this._meshUnderPointer = null;
  296. }
  297. };
  298. this._onPointerDown = function (evt) {
  299. var predicate = null;
  300. if (!_this.onPointerDown) {
  301. predicate = function (mesh) {
  302. return mesh.isPickable && mesh.isVisible && mesh.isReady() && mesh.actionManager && mesh.actionManager.hasPickTriggers;
  303. };
  304. }
  305. _this._updatePointerPosition(evt);
  306. var pickResult = _this.pick(_this._pointerX, _this._pointerY, predicate, false, _this.cameraToUseForPointers);
  307. if (pickResult.hit) {
  308. if (pickResult.pickedMesh.actionManager) {
  309. switch (evt.button) {
  310. case 0:
  311. pickResult.pickedMesh.actionManager.processTrigger(BABYLON.ActionManager.OnLeftPickTrigger, BABYLON.ActionEvent.CreateNew(pickResult.pickedMesh, evt));
  312. break;
  313. case 1:
  314. pickResult.pickedMesh.actionManager.processTrigger(BABYLON.ActionManager.OnCenterPickTrigger, BABYLON.ActionEvent.CreateNew(pickResult.pickedMesh, evt));
  315. break;
  316. case 2:
  317. pickResult.pickedMesh.actionManager.processTrigger(BABYLON.ActionManager.OnRightPickTrigger, BABYLON.ActionEvent.CreateNew(pickResult.pickedMesh, evt));
  318. break;
  319. }
  320. pickResult.pickedMesh.actionManager.processTrigger(BABYLON.ActionManager.OnPickTrigger, BABYLON.ActionEvent.CreateNew(pickResult.pickedMesh, evt));
  321. }
  322. }
  323. if (_this.onPointerDown) {
  324. _this.onPointerDown(evt, pickResult);
  325. }
  326. };
  327. this._onKeyDown = function (evt) {
  328. if (_this.actionManager) {
  329. _this.actionManager.processTrigger(BABYLON.ActionManager.OnKeyDownTrigger, BABYLON.ActionEvent.CreateNewFromScene(_this, evt));
  330. }
  331. };
  332. this._onKeyUp = function (evt) {
  333. if (_this.actionManager) {
  334. _this.actionManager.processTrigger(BABYLON.ActionManager.OnKeyUpTrigger, BABYLON.ActionEvent.CreateNewFromScene(_this, evt));
  335. }
  336. };
  337. var eventPrefix = BABYLON.Tools.GetPointerPrefix();
  338. this._engine.getRenderingCanvas().addEventListener(eventPrefix + "move", this._onPointerMove, false);
  339. this._engine.getRenderingCanvas().addEventListener(eventPrefix + "down", this._onPointerDown, false);
  340. BABYLON.Tools.RegisterTopRootEvents([
  341. { name: "keydown", handler: this._onKeyDown },
  342. { name: "keyup", handler: this._onKeyUp }
  343. ]);
  344. };
  345. Scene.prototype.detachControl = function () {
  346. var eventPrefix = BABYLON.Tools.GetPointerPrefix();
  347. this._engine.getRenderingCanvas().removeEventListener(eventPrefix + "move", this._onPointerMove);
  348. this._engine.getRenderingCanvas().removeEventListener(eventPrefix + "down", this._onPointerDown);
  349. BABYLON.Tools.UnregisterTopRootEvents([
  350. { name: "keydown", handler: this._onKeyDown },
  351. { name: "keyup", handler: this._onKeyUp }
  352. ]);
  353. };
  354. // Ready
  355. Scene.prototype.isReady = function () {
  356. if (this._pendingData.length > 0) {
  357. return false;
  358. }
  359. for (var index = 0; index < this._geometries.length; index++) {
  360. var geometry = this._geometries[index];
  361. if (geometry.delayLoadState === BABYLON.Engine.DELAYLOADSTATE_LOADING) {
  362. return false;
  363. }
  364. }
  365. for (index = 0; index < this.meshes.length; index++) {
  366. var mesh = this.meshes[index];
  367. if (!mesh.isReady()) {
  368. return false;
  369. }
  370. var mat = mesh.material;
  371. if (mat) {
  372. if (!mat.isReady(mesh)) {
  373. return false;
  374. }
  375. }
  376. }
  377. return true;
  378. };
  379. Scene.prototype.resetCachedMaterial = function () {
  380. this._cachedMaterial = null;
  381. };
  382. Scene.prototype.registerBeforeRender = function (func) {
  383. this._onBeforeRenderCallbacks.push(func);
  384. };
  385. Scene.prototype.unregisterBeforeRender = function (func) {
  386. var index = this._onBeforeRenderCallbacks.indexOf(func);
  387. if (index > -1) {
  388. this._onBeforeRenderCallbacks.splice(index, 1);
  389. }
  390. };
  391. Scene.prototype.registerAfterRender = function (func) {
  392. this._onAfterRenderCallbacks.push(func);
  393. };
  394. Scene.prototype.unregisterAfterRender = function (func) {
  395. var index = this._onAfterRenderCallbacks.indexOf(func);
  396. if (index > -1) {
  397. this._onAfterRenderCallbacks.splice(index, 1);
  398. }
  399. };
  400. Scene.prototype._addPendingData = function (data) {
  401. this._pendingData.push(data);
  402. };
  403. Scene.prototype._removePendingData = function (data) {
  404. var index = this._pendingData.indexOf(data);
  405. if (index !== -1) {
  406. this._pendingData.splice(index, 1);
  407. }
  408. };
  409. Scene.prototype.getWaitingItemsCount = function () {
  410. return this._pendingData.length;
  411. };
  412. /**
  413. * Registers a function to be executed when the scene is ready.
  414. * @param {Function} func - the function to be executed.
  415. */
  416. Scene.prototype.executeWhenReady = function (func) {
  417. var _this = this;
  418. this._onReadyCallbacks.push(func);
  419. if (this._executeWhenReadyTimeoutId !== -1) {
  420. return;
  421. }
  422. this._executeWhenReadyTimeoutId = setTimeout(function () {
  423. _this._checkIsReady();
  424. }, 150);
  425. };
  426. Scene.prototype._checkIsReady = function () {
  427. var _this = this;
  428. if (this.isReady()) {
  429. this._onReadyCallbacks.forEach(function (func) {
  430. func();
  431. });
  432. this._onReadyCallbacks = [];
  433. this._executeWhenReadyTimeoutId = -1;
  434. return;
  435. }
  436. this._executeWhenReadyTimeoutId = setTimeout(function () {
  437. _this._checkIsReady();
  438. }, 150);
  439. };
  440. // Animations
  441. /**
  442. * Will start the animation sequence of a given target
  443. * @param target - the target
  444. * @param {number} from - from which frame should animation start
  445. * @param {number} to - till which frame should animation run.
  446. * @param {boolean} [loop] - should the animation loop
  447. * @param {number} [speedRatio] - the speed in which to run the animation
  448. * @param {Function} [onAnimationEnd] function to be executed when the animation ended.
  449. * @param {BABYLON.Animatable} [animatable] an animatable object. If not provided a new one will be created from the given params.
  450. * @return {BABYLON.Animatable} the animatable object created for this animation
  451. * @see BABYLON.Animatable
  452. * @see http://doc.babylonjs.com/page.php?p=22081
  453. */
  454. Scene.prototype.beginAnimation = function (target, from, to, loop, speedRatio, onAnimationEnd, animatable) {
  455. if (speedRatio === undefined) {
  456. speedRatio = 1.0;
  457. }
  458. this.stopAnimation(target);
  459. if (!animatable) {
  460. animatable = new BABYLON.Animatable(this, target, from, to, loop, speedRatio, onAnimationEnd);
  461. }
  462. // Local animations
  463. if (target.animations) {
  464. animatable.appendAnimations(target, target.animations);
  465. }
  466. // Children animations
  467. if (target.getAnimatables) {
  468. var animatables = target.getAnimatables();
  469. for (var index = 0; index < animatables.length; index++) {
  470. this.beginAnimation(animatables[index], from, to, loop, speedRatio, onAnimationEnd, animatable);
  471. }
  472. }
  473. return animatable;
  474. };
  475. Scene.prototype.beginDirectAnimation = function (target, animations, from, to, loop, speedRatio, onAnimationEnd) {
  476. if (speedRatio === undefined) {
  477. speedRatio = 1.0;
  478. }
  479. var animatable = new BABYLON.Animatable(this, target, from, to, loop, speedRatio, onAnimationEnd, animations);
  480. return animatable;
  481. };
  482. Scene.prototype.getAnimatableByTarget = function (target) {
  483. for (var index = 0; index < this._activeAnimatables.length; index++) {
  484. if (this._activeAnimatables[index].target === target) {
  485. return this._activeAnimatables[index];
  486. }
  487. }
  488. return null;
  489. };
  490. /**
  491. * Will stop the animation of the given target
  492. * @param target - the target
  493. * @see beginAnimation
  494. */
  495. Scene.prototype.stopAnimation = function (target) {
  496. var animatable = this.getAnimatableByTarget(target);
  497. if (animatable) {
  498. animatable.stop();
  499. }
  500. };
  501. Scene.prototype._animate = function () {
  502. if (!this.animationsEnabled) {
  503. return;
  504. }
  505. if (!this._animationStartDate) {
  506. this._animationStartDate = BABYLON.Tools.Now;
  507. }
  508. // Getting time
  509. var now = BABYLON.Tools.Now;
  510. var delay = now - this._animationStartDate;
  511. for (var index = 0; index < this._activeAnimatables.length; index++) {
  512. this._activeAnimatables[index]._animate(delay);
  513. }
  514. };
  515. // Matrix
  516. Scene.prototype.getViewMatrix = function () {
  517. return this._viewMatrix;
  518. };
  519. Scene.prototype.getProjectionMatrix = function () {
  520. return this._projectionMatrix;
  521. };
  522. Scene.prototype.getTransformMatrix = function () {
  523. return this._transformMatrix;
  524. };
  525. Scene.prototype.setTransformMatrix = function (view, projection) {
  526. this._viewMatrix = view;
  527. this._projectionMatrix = projection;
  528. this._viewMatrix.multiplyToRef(this._projectionMatrix, this._transformMatrix);
  529. };
  530. // Methods
  531. Scene.prototype.addMesh = function (newMesh) {
  532. newMesh.uniqueId = this._uniqueIdCounter++;
  533. var position = this.meshes.push(newMesh);
  534. if (this.onNewMeshAdded) {
  535. this.onNewMeshAdded(newMesh, position, this);
  536. }
  537. };
  538. Scene.prototype.removeMesh = function (toRemove) {
  539. var index = this.meshes.indexOf(toRemove);
  540. if (index !== -1) {
  541. // Remove from the scene if mesh found
  542. this.meshes.splice(index, 1);
  543. }
  544. if (this.onMeshRemoved) {
  545. this.onMeshRemoved(toRemove);
  546. }
  547. return index;
  548. };
  549. Scene.prototype.removeLight = function (toRemove) {
  550. var index = this.lights.indexOf(toRemove);
  551. if (index !== -1) {
  552. // Remove from the scene if mesh found
  553. this.lights.splice(index, 1);
  554. }
  555. if (this.onLightRemoved) {
  556. this.onLightRemoved(toRemove);
  557. }
  558. return index;
  559. };
  560. Scene.prototype.removeCamera = function (toRemove) {
  561. var index = this.cameras.indexOf(toRemove);
  562. if (index !== -1) {
  563. // Remove from the scene if mesh found
  564. this.cameras.splice(index, 1);
  565. }
  566. if (this.onCameraRemoved) {
  567. this.onCameraRemoved(toRemove);
  568. }
  569. return index;
  570. };
  571. Scene.prototype.addLight = function (newLight) {
  572. newLight.uniqueId = this._uniqueIdCounter++;
  573. var position = this.lights.push(newLight);
  574. if (this.onNewLightAdded) {
  575. this.onNewLightAdded(newLight, position, this);
  576. }
  577. };
  578. Scene.prototype.addCamera = function (newCamera) {
  579. newCamera.uniqueId = this._uniqueIdCounter++;
  580. var position = this.cameras.push(newCamera);
  581. if (this.onNewCameraAdded) {
  582. this.onNewCameraAdded(newCamera, position, this);
  583. }
  584. };
  585. /**
  586. * sets the active camera of the scene using its ID
  587. * @param {string} id - the camera's ID
  588. * @return {BABYLON.Camera|null} the new active camera or null if none found.
  589. * @see activeCamera
  590. */
  591. Scene.prototype.setActiveCameraByID = function (id) {
  592. var camera = this.getCameraByID(id);
  593. if (camera) {
  594. this.activeCamera = camera;
  595. return camera;
  596. }
  597. return null;
  598. };
  599. /**
  600. * sets the active camera of the scene using its name
  601. * @param {string} name - the camera's name
  602. * @return {BABYLON.Camera|null} the new active camera or null if none found.
  603. * @see activeCamera
  604. */
  605. Scene.prototype.setActiveCameraByName = function (name) {
  606. var camera = this.getCameraByName(name);
  607. if (camera) {
  608. this.activeCamera = camera;
  609. return camera;
  610. }
  611. return null;
  612. };
  613. /**
  614. * get a material using its id
  615. * @param {string} the material's ID
  616. * @return {BABYLON.Material|null} the material or null if none found.
  617. */
  618. Scene.prototype.getMaterialByID = function (id) {
  619. for (var index = 0; index < this.materials.length; index++) {
  620. if (this.materials[index].id === id) {
  621. return this.materials[index];
  622. }
  623. }
  624. return null;
  625. };
  626. /**
  627. * get a material using its name
  628. * @param {string} the material's name
  629. * @return {BABYLON.Material|null} the material or null if none found.
  630. */
  631. Scene.prototype.getMaterialByName = function (name) {
  632. for (var index = 0; index < this.materials.length; index++) {
  633. if (this.materials[index].name === name) {
  634. return this.materials[index];
  635. }
  636. }
  637. return null;
  638. };
  639. Scene.prototype.getCameraByID = function (id) {
  640. for (var index = 0; index < this.cameras.length; index++) {
  641. if (this.cameras[index].id === id) {
  642. return this.cameras[index];
  643. }
  644. }
  645. return null;
  646. };
  647. Scene.prototype.getCameraByUniqueID = function (uniqueId) {
  648. for (var index = 0; index < this.cameras.length; index++) {
  649. if (this.cameras[index].uniqueId === uniqueId) {
  650. return this.cameras[index];
  651. }
  652. }
  653. return null;
  654. };
  655. /**
  656. * get a camera using its name
  657. * @param {string} the camera's name
  658. * @return {BABYLON.Camera|null} the camera or null if none found.
  659. */
  660. Scene.prototype.getCameraByName = function (name) {
  661. for (var index = 0; index < this.cameras.length; index++) {
  662. if (this.cameras[index].name === name) {
  663. return this.cameras[index];
  664. }
  665. }
  666. return null;
  667. };
  668. /**
  669. * get a light node using its name
  670. * @param {string} the light's name
  671. * @return {BABYLON.Light|null} the light or null if none found.
  672. */
  673. Scene.prototype.getLightByName = function (name) {
  674. for (var index = 0; index < this.lights.length; index++) {
  675. if (this.lights[index].name === name) {
  676. return this.lights[index];
  677. }
  678. }
  679. return null;
  680. };
  681. /**
  682. * get a light node using its ID
  683. * @param {string} the light's id
  684. * @return {BABYLON.Light|null} the light or null if none found.
  685. */
  686. Scene.prototype.getLightByID = function (id) {
  687. for (var index = 0; index < this.lights.length; index++) {
  688. if (this.lights[index].id === id) {
  689. return this.lights[index];
  690. }
  691. }
  692. return null;
  693. };
  694. /**
  695. * get a light node using its scene-generated unique ID
  696. * @param {number} the light's unique id
  697. * @return {BABYLON.Light|null} the light or null if none found.
  698. */
  699. Scene.prototype.getLightByUniqueID = function (uniqueId) {
  700. for (var index = 0; index < this.lights.length; index++) {
  701. if (this.lights[index].uniqueId === uniqueId) {
  702. return this.lights[index];
  703. }
  704. }
  705. return null;
  706. };
  707. /**
  708. * get a geometry using its ID
  709. * @param {string} the geometry's id
  710. * @return {BABYLON.Geometry|null} the geometry or null if none found.
  711. */
  712. Scene.prototype.getGeometryByID = function (id) {
  713. for (var index = 0; index < this._geometries.length; index++) {
  714. if (this._geometries[index].id === id) {
  715. return this._geometries[index];
  716. }
  717. }
  718. return null;
  719. };
  720. /**
  721. * add a new geometry to this scene.
  722. * @param {BABYLON.Geometry} geometry - the geometry to be added to the scene.
  723. * @param {boolean} [force] - force addition, even if a geometry with this ID already exists
  724. * @return {boolean} was the geometry added or not
  725. */
  726. Scene.prototype.pushGeometry = function (geometry, force) {
  727. if (!force && this.getGeometryByID(geometry.id)) {
  728. return false;
  729. }
  730. this._geometries.push(geometry);
  731. return true;
  732. };
  733. Scene.prototype.getGeometries = function () {
  734. return this._geometries;
  735. };
  736. /**
  737. * Get the first added mesh found of a given ID
  738. * @param {string} id - the id to search for
  739. * @return {BABYLON.AbstractMesh|null} the mesh found or null if not found at all.
  740. */
  741. Scene.prototype.getMeshByID = function (id) {
  742. for (var index = 0; index < this.meshes.length; index++) {
  743. if (this.meshes[index].id === id) {
  744. return this.meshes[index];
  745. }
  746. }
  747. return null;
  748. };
  749. /**
  750. * Get a mesh with its auto-generated unique id
  751. * @param {number} uniqueId - the unique id to search for
  752. * @return {BABYLON.AbstractMesh|null} the mesh found or null if not found at all.
  753. */
  754. Scene.prototype.getMeshByUniqueID = function (uniqueId) {
  755. for (var index = 0; index < this.meshes.length; index++) {
  756. if (this.meshes[index].uniqueId === uniqueId) {
  757. return this.meshes[index];
  758. }
  759. }
  760. return null;
  761. };
  762. /**
  763. * Get a the last added mesh found of a given ID
  764. * @param {string} id - the id to search for
  765. * @return {BABYLON.AbstractMesh|null} the mesh found or null if not found at all.
  766. */
  767. Scene.prototype.getLastMeshByID = function (id) {
  768. for (var index = this.meshes.length - 1; index >= 0; index--) {
  769. if (this.meshes[index].id === id) {
  770. return this.meshes[index];
  771. }
  772. }
  773. return null;
  774. };
  775. /**
  776. * Get a the last added node (Mesh, Camera, Light) found of a given ID
  777. * @param {string} id - the id to search for
  778. * @return {BABYLON.Node|null} the node found or null if not found at all.
  779. */
  780. Scene.prototype.getLastEntryByID = function (id) {
  781. for (var index = this.meshes.length - 1; index >= 0; index--) {
  782. if (this.meshes[index].id === id) {
  783. return this.meshes[index];
  784. }
  785. }
  786. for (index = this.cameras.length - 1; index >= 0; index--) {
  787. if (this.cameras[index].id === id) {
  788. return this.cameras[index];
  789. }
  790. }
  791. for (index = this.lights.length - 1; index >= 0; index--) {
  792. if (this.lights[index].id === id) {
  793. return this.lights[index];
  794. }
  795. }
  796. return null;
  797. };
  798. Scene.prototype.getNodeByName = function (name) {
  799. var mesh = this.getMeshByName(name);
  800. if (mesh) {
  801. return mesh;
  802. }
  803. var light = this.getLightByName(name);
  804. if (light) {
  805. return light;
  806. }
  807. return this.getCameraByName(name);
  808. };
  809. Scene.prototype.getMeshByName = function (name) {
  810. for (var index = 0; index < this.meshes.length; index++) {
  811. if (this.meshes[index].name === name) {
  812. return this.meshes[index];
  813. }
  814. }
  815. return null;
  816. };
  817. Scene.prototype.getSoundByName = function (name) {
  818. for (var index = 0; index < this.mainSoundTrack.soundCollection.length; index++) {
  819. if (this.mainSoundTrack.soundCollection[index].name === name) {
  820. return this.mainSoundTrack.soundCollection[index];
  821. }
  822. }
  823. for (var sdIndex = 0; sdIndex < this.soundTracks.length; sdIndex++) {
  824. for (index = 0; index < this.soundTracks[sdIndex].soundCollection.length; index++) {
  825. if (this.soundTracks[sdIndex].soundCollection[index].name === name) {
  826. return this.soundTracks[sdIndex].soundCollection[index];
  827. }
  828. }
  829. }
  830. return null;
  831. };
  832. Scene.prototype.getLastSkeletonByID = function (id) {
  833. for (var index = this.skeletons.length - 1; index >= 0; index--) {
  834. if (this.skeletons[index].id === id) {
  835. return this.skeletons[index];
  836. }
  837. }
  838. return null;
  839. };
  840. Scene.prototype.getSkeletonById = function (id) {
  841. for (var index = 0; index < this.skeletons.length; index++) {
  842. if (this.skeletons[index].id === id) {
  843. return this.skeletons[index];
  844. }
  845. }
  846. return null;
  847. };
  848. Scene.prototype.getSkeletonByName = function (name) {
  849. for (var index = 0; index < this.skeletons.length; index++) {
  850. if (this.skeletons[index].name === name) {
  851. return this.skeletons[index];
  852. }
  853. }
  854. return null;
  855. };
  856. Scene.prototype.isActiveMesh = function (mesh) {
  857. return (this._activeMeshes.indexOf(mesh) !== -1);
  858. };
  859. Scene.prototype._evaluateSubMesh = function (subMesh, mesh) {
  860. if (mesh.subMeshes.length === 1 || subMesh.isInFrustum(this._frustumPlanes)) {
  861. var material = subMesh.getMaterial();
  862. if (mesh.showSubMeshesBoundingBox) {
  863. this._boundingBoxRenderer.renderList.push(subMesh.getBoundingInfo().boundingBox);
  864. }
  865. if (material) {
  866. // Render targets
  867. if (material.getRenderTargetTextures) {
  868. if (this._processedMaterials.indexOf(material) === -1) {
  869. this._processedMaterials.push(material);
  870. this._renderTargets.concat(material.getRenderTargetTextures());
  871. }
  872. }
  873. // Dispatch
  874. this._activeVertices += subMesh.indexCount;
  875. this._renderingManager.dispatch(subMesh);
  876. }
  877. }
  878. };
  879. Scene.prototype._evaluateActiveMeshes = function () {
  880. this.activeCamera._activeMeshes.reset();
  881. this._activeMeshes.reset();
  882. this._renderingManager.reset();
  883. this._processedMaterials.reset();
  884. this._activeParticleSystems.reset();
  885. this._activeSkeletons.reset();
  886. this._boundingBoxRenderer.reset();
  887. if (!this._frustumPlanes) {
  888. this._frustumPlanes = BABYLON.Frustum.GetPlanes(this._transformMatrix);
  889. }
  890. else {
  891. BABYLON.Frustum.GetPlanesToRef(this._transformMatrix, this._frustumPlanes);
  892. }
  893. // Meshes
  894. var meshes;
  895. var len;
  896. if (this._selectionOctree) {
  897. var selection = this._selectionOctree.select(this._frustumPlanes);
  898. meshes = selection.data;
  899. len = selection.length;
  900. }
  901. else {
  902. len = this.meshes.length;
  903. meshes = this.meshes;
  904. }
  905. for (var meshIndex = 0; meshIndex < len; meshIndex++) {
  906. var mesh = meshes[meshIndex];
  907. if (mesh.isBlocked) {
  908. continue;
  909. }
  910. this._totalVertices += mesh.getTotalVertices();
  911. if (!mesh.isReady()) {
  912. continue;
  913. }
  914. mesh.computeWorldMatrix();
  915. // Intersections
  916. if (mesh.actionManager && mesh.actionManager.hasSpecificTriggers([BABYLON.ActionManager.OnIntersectionEnterTrigger, BABYLON.ActionManager.OnIntersectionExitTrigger])) {
  917. this._meshesForIntersections.pushNoDuplicate(mesh);
  918. }
  919. // Switch to current LOD
  920. var meshLOD = mesh.getLOD(this.activeCamera);
  921. if (!meshLOD) {
  922. continue;
  923. }
  924. mesh._preActivate();
  925. if (mesh.isEnabled() && mesh.isVisible && mesh.visibility > 0 && ((mesh.layerMask & this.activeCamera.layerMask) !== 0) && mesh.isInFrustum(this._frustumPlanes)) {
  926. this._activeMeshes.push(mesh);
  927. this.activeCamera._activeMeshes.push(mesh);
  928. mesh._activate(this._renderId);
  929. this._activeMesh(meshLOD);
  930. }
  931. }
  932. // Particle systems
  933. var beforeParticlesDate = BABYLON.Tools.Now;
  934. if (this.particlesEnabled) {
  935. BABYLON.Tools.StartPerformanceCounter("Particles", this.particleSystems.length > 0);
  936. for (var particleIndex = 0; particleIndex < this.particleSystems.length; particleIndex++) {
  937. var particleSystem = this.particleSystems[particleIndex];
  938. if (!particleSystem.isStarted()) {
  939. continue;
  940. }
  941. if (!particleSystem.emitter.position || (particleSystem.emitter && particleSystem.emitter.isEnabled())) {
  942. this._activeParticleSystems.push(particleSystem);
  943. particleSystem.animate();
  944. }
  945. }
  946. BABYLON.Tools.EndPerformanceCounter("Particles", this.particleSystems.length > 0);
  947. }
  948. this._particlesDuration += BABYLON.Tools.Now - beforeParticlesDate;
  949. };
  950. Scene.prototype._activeMesh = function (mesh) {
  951. if (mesh.skeleton && this.skeletonsEnabled) {
  952. this._activeSkeletons.pushNoDuplicate(mesh.skeleton);
  953. }
  954. if (mesh.showBoundingBox || this.forceShowBoundingBoxes) {
  955. this._boundingBoxRenderer.renderList.push(mesh.getBoundingInfo().boundingBox);
  956. }
  957. if (mesh && mesh.subMeshes) {
  958. // Submeshes Octrees
  959. var len;
  960. var subMeshes;
  961. if (mesh._submeshesOctree && mesh.useOctreeForRenderingSelection) {
  962. var intersections = mesh._submeshesOctree.select(this._frustumPlanes);
  963. len = intersections.length;
  964. subMeshes = intersections.data;
  965. }
  966. else {
  967. subMeshes = mesh.subMeshes;
  968. len = subMeshes.length;
  969. }
  970. for (var subIndex = 0; subIndex < len; subIndex++) {
  971. var subMesh = subMeshes[subIndex];
  972. this._evaluateSubMesh(subMesh, mesh);
  973. }
  974. }
  975. };
  976. Scene.prototype.updateTransformMatrix = function (force) {
  977. this.setTransformMatrix(this.activeCamera.getViewMatrix(), this.activeCamera.getProjectionMatrix(force));
  978. };
  979. Scene.prototype._renderForCamera = function (camera) {
  980. var engine = this._engine;
  981. this.activeCamera = camera;
  982. if (!this.activeCamera)
  983. throw new Error("Active camera not set");
  984. BABYLON.Tools.StartPerformanceCounter("Rendering camera " + this.activeCamera.name);
  985. // Viewport
  986. engine.setViewport(this.activeCamera.viewport);
  987. // Camera
  988. this._renderId++;
  989. this.updateTransformMatrix();
  990. if (this.beforeCameraRender) {
  991. this.beforeCameraRender(this.activeCamera);
  992. }
  993. // Meshes
  994. var beforeEvaluateActiveMeshesDate = BABYLON.Tools.Now;
  995. BABYLON.Tools.StartPerformanceCounter("Active meshes evaluation");
  996. this._evaluateActiveMeshes();
  997. this._evaluateActiveMeshesDuration += BABYLON.Tools.Now - beforeEvaluateActiveMeshesDate;
  998. BABYLON.Tools.EndPerformanceCounter("Active meshes evaluation");
  999. for (var skeletonIndex = 0; skeletonIndex < this._activeSkeletons.length; skeletonIndex++) {
  1000. var skeleton = this._activeSkeletons.data[skeletonIndex];
  1001. skeleton.prepare();
  1002. }
  1003. // Render targets
  1004. var beforeRenderTargetDate = BABYLON.Tools.Now;
  1005. if (this.renderTargetsEnabled) {
  1006. BABYLON.Tools.StartPerformanceCounter("Render targets", this._renderTargets.length > 0);
  1007. for (var renderIndex = 0; renderIndex < this._renderTargets.length; renderIndex++) {
  1008. var renderTarget = this._renderTargets.data[renderIndex];
  1009. if (renderTarget._shouldRender()) {
  1010. this._renderId++;
  1011. var hasSpecialRenderTargetCamera = renderTarget.activeCamera && renderTarget.activeCamera !== this.activeCamera;
  1012. renderTarget.render(hasSpecialRenderTargetCamera, this.dumpNextRenderTargets);
  1013. }
  1014. }
  1015. BABYLON.Tools.EndPerformanceCounter("Render targets", this._renderTargets.length > 0);
  1016. this._renderId++;
  1017. }
  1018. if (this._renderTargets.length > 0) {
  1019. engine.restoreDefaultFramebuffer();
  1020. }
  1021. this._renderTargetsDuration += BABYLON.Tools.Now - beforeRenderTargetDate;
  1022. // Prepare Frame
  1023. this.postProcessManager._prepareFrame();
  1024. var beforeRenderDate = BABYLON.Tools.Now;
  1025. // Backgrounds
  1026. if (this.layers.length) {
  1027. engine.setDepthBuffer(false);
  1028. var layerIndex;
  1029. var layer;
  1030. for (layerIndex = 0; layerIndex < this.layers.length; layerIndex++) {
  1031. layer = this.layers[layerIndex];
  1032. if (layer.isBackground) {
  1033. layer.render();
  1034. }
  1035. }
  1036. engine.setDepthBuffer(true);
  1037. }
  1038. // Render
  1039. BABYLON.Tools.StartPerformanceCounter("Main render");
  1040. this._renderingManager.render(null, null, true, true);
  1041. BABYLON.Tools.EndPerformanceCounter("Main render");
  1042. // Bounding boxes
  1043. this._boundingBoxRenderer.render();
  1044. // Lens flares
  1045. if (this.lensFlaresEnabled) {
  1046. BABYLON.Tools.StartPerformanceCounter("Lens flares", this.lensFlareSystems.length > 0);
  1047. for (var lensFlareSystemIndex = 0; lensFlareSystemIndex < this.lensFlareSystems.length; lensFlareSystemIndex++) {
  1048. this.lensFlareSystems[lensFlareSystemIndex].render();
  1049. }
  1050. BABYLON.Tools.EndPerformanceCounter("Lens flares", this.lensFlareSystems.length > 0);
  1051. }
  1052. // Foregrounds
  1053. if (this.layers.length) {
  1054. engine.setDepthBuffer(false);
  1055. for (layerIndex = 0; layerIndex < this.layers.length; layerIndex++) {
  1056. layer = this.layers[layerIndex];
  1057. if (!layer.isBackground) {
  1058. layer.render();
  1059. }
  1060. }
  1061. engine.setDepthBuffer(true);
  1062. }
  1063. this._renderDuration += BABYLON.Tools.Now - beforeRenderDate;
  1064. // Finalize frame
  1065. this.postProcessManager._finalizeFrame(camera.isIntermediate);
  1066. // Update camera
  1067. this.activeCamera._updateFromScene();
  1068. // Reset some special arrays
  1069. this._renderTargets.reset();
  1070. if (this.afterCameraRender) {
  1071. this.afterCameraRender(this.activeCamera);
  1072. }
  1073. BABYLON.Tools.EndPerformanceCounter("Rendering camera " + this.activeCamera.name);
  1074. };
  1075. Scene.prototype._processSubCameras = function (camera) {
  1076. if (camera.subCameras.length === 0) {
  1077. this._renderForCamera(camera);
  1078. return;
  1079. }
  1080. for (var index = 0; index < camera.subCameras.length; index++) {
  1081. this._renderForCamera(camera.subCameras[index]);
  1082. }
  1083. this.activeCamera = camera;
  1084. this.setTransformMatrix(this.activeCamera.getViewMatrix(), this.activeCamera.getProjectionMatrix());
  1085. // Update camera
  1086. this.activeCamera._updateFromScene();
  1087. };
  1088. Scene.prototype._checkIntersections = function () {
  1089. for (var index = 0; index < this._meshesForIntersections.length; index++) {
  1090. var sourceMesh = this._meshesForIntersections.data[index];
  1091. for (var actionIndex = 0; actionIndex < sourceMesh.actionManager.actions.length; actionIndex++) {
  1092. var action = sourceMesh.actionManager.actions[actionIndex];
  1093. if (action.trigger === BABYLON.ActionManager.OnIntersectionEnterTrigger || action.trigger === BABYLON.ActionManager.OnIntersectionExitTrigger) {
  1094. var parameters = action.getTriggerParameter();
  1095. var otherMesh = parameters instanceof BABYLON.AbstractMesh ? parameters : parameters.mesh;
  1096. var areIntersecting = otherMesh.intersectsMesh(sourceMesh, parameters.usePreciseIntersection);
  1097. var currentIntersectionInProgress = sourceMesh._intersectionsInProgress.indexOf(otherMesh);
  1098. if (areIntersecting && currentIntersectionInProgress === -1) {
  1099. if (action.trigger === BABYLON.ActionManager.OnIntersectionEnterTrigger) {
  1100. action._executeCurrent(BABYLON.ActionEvent.CreateNew(sourceMesh));
  1101. sourceMesh._intersectionsInProgress.push(otherMesh);
  1102. }
  1103. else if (action.trigger === BABYLON.ActionManager.OnIntersectionExitTrigger) {
  1104. sourceMesh._intersectionsInProgress.push(otherMesh);
  1105. }
  1106. }
  1107. else if (!areIntersecting && currentIntersectionInProgress > -1 && action.trigger === BABYLON.ActionManager.OnIntersectionExitTrigger) {
  1108. action._executeCurrent(BABYLON.ActionEvent.CreateNew(sourceMesh));
  1109. var indexOfOther = sourceMesh._intersectionsInProgress.indexOf(otherMesh);
  1110. if (indexOfOther > -1) {
  1111. sourceMesh._intersectionsInProgress.splice(indexOfOther, 1);
  1112. }
  1113. }
  1114. }
  1115. }
  1116. }
  1117. };
  1118. Scene.prototype.render = function () {
  1119. var startDate = BABYLON.Tools.Now;
  1120. this._particlesDuration = 0;
  1121. this._spritesDuration = 0;
  1122. this._activeParticles = 0;
  1123. this._renderDuration = 0;
  1124. this._renderTargetsDuration = 0;
  1125. this._evaluateActiveMeshesDuration = 0;
  1126. this._totalVertices = 0;
  1127. this._activeVertices = 0;
  1128. this._activeBones = 0;
  1129. this.getEngine().resetDrawCalls();
  1130. this._meshesForIntersections.reset();
  1131. this.resetCachedMaterial();
  1132. BABYLON.Tools.StartPerformanceCounter("Scene rendering");
  1133. // Actions
  1134. if (this.actionManager) {
  1135. this.actionManager.processTrigger(BABYLON.ActionManager.OnEveryFrameTrigger, null);
  1136. }
  1137. //Simplification Queue
  1138. if (!this.simplificationQueue.running) {
  1139. this.simplificationQueue.executeNext();
  1140. }
  1141. // Before render
  1142. if (this.beforeRender) {
  1143. this.beforeRender();
  1144. }
  1145. for (var callbackIndex = 0; callbackIndex < this._onBeforeRenderCallbacks.length; callbackIndex++) {
  1146. this._onBeforeRenderCallbacks[callbackIndex]();
  1147. }
  1148. // Animations
  1149. var deltaTime = Math.max(Scene.MinDeltaTime, Math.min(this._engine.getDeltaTime(), Scene.MaxDeltaTime));
  1150. this._animationRatio = deltaTime * (60.0 / 1000.0);
  1151. this._animate();
  1152. // Physics
  1153. if (this._physicsEngine) {
  1154. BABYLON.Tools.StartPerformanceCounter("Physics");
  1155. this._physicsEngine._runOneStep(deltaTime / 1000.0);
  1156. BABYLON.Tools.EndPerformanceCounter("Physics");
  1157. }
  1158. // Customs render targets
  1159. var beforeRenderTargetDate = BABYLON.Tools.Now;
  1160. var engine = this.getEngine();
  1161. var currentActiveCamera = this.activeCamera;
  1162. if (this.renderTargetsEnabled) {
  1163. BABYLON.Tools.StartPerformanceCounter("Custom render targets", this.customRenderTargets.length > 0);
  1164. for (var customIndex = 0; customIndex < this.customRenderTargets.length; customIndex++) {
  1165. var renderTarget = this.customRenderTargets[customIndex];
  1166. if (renderTarget._shouldRender()) {
  1167. this._renderId++;
  1168. this.activeCamera = renderTarget.activeCamera || this.activeCamera;
  1169. if (!this.activeCamera)
  1170. throw new Error("Active camera not set");
  1171. // Viewport
  1172. engine.setViewport(this.activeCamera.viewport);
  1173. // Camera
  1174. this.updateTransformMatrix();
  1175. renderTarget.render(currentActiveCamera !== this.activeCamera, this.dumpNextRenderTargets);
  1176. }
  1177. }
  1178. BABYLON.Tools.EndPerformanceCounter("Custom render targets", this.customRenderTargets.length > 0);
  1179. this._renderId++;
  1180. }
  1181. if (this.customRenderTargets.length > 0) {
  1182. engine.restoreDefaultFramebuffer();
  1183. }
  1184. this._renderTargetsDuration += BABYLON.Tools.Now - beforeRenderTargetDate;
  1185. this.activeCamera = currentActiveCamera;
  1186. // Procedural textures
  1187. if (this.proceduralTexturesEnabled) {
  1188. BABYLON.Tools.StartPerformanceCounter("Procedural textures", this._proceduralTextures.length > 0);
  1189. for (var proceduralIndex = 0; proceduralIndex < this._proceduralTextures.length; proceduralIndex++) {
  1190. var proceduralTexture = this._proceduralTextures[proceduralIndex];
  1191. if (proceduralTexture._shouldRender()) {
  1192. proceduralTexture.render();
  1193. }
  1194. }
  1195. BABYLON.Tools.EndPerformanceCounter("Procedural textures", this._proceduralTextures.length > 0);
  1196. }
  1197. // Clear
  1198. this._engine.clear(this.clearColor, this.autoClear || this.forceWireframe || this.forcePointsCloud, true);
  1199. // Shadows
  1200. if (this.shadowsEnabled) {
  1201. for (var lightIndex = 0; lightIndex < this.lights.length; lightIndex++) {
  1202. var light = this.lights[lightIndex];
  1203. var shadowGenerator = light.getShadowGenerator();
  1204. if (light.isEnabled() && shadowGenerator && shadowGenerator.getShadowMap().getScene().textures.indexOf(shadowGenerator.getShadowMap()) !== -1) {
  1205. this._renderTargets.push(shadowGenerator.getShadowMap());
  1206. }
  1207. }
  1208. }
  1209. // Depth renderer
  1210. if (this._depthRenderer) {
  1211. this._renderTargets.push(this._depthRenderer.getDepthMap());
  1212. }
  1213. // RenderPipeline
  1214. this.postProcessRenderPipelineManager.update();
  1215. // Multi-cameras?
  1216. if (this.activeCameras.length > 0) {
  1217. var currentRenderId = this._renderId;
  1218. for (var cameraIndex = 0; cameraIndex < this.activeCameras.length; cameraIndex++) {
  1219. this._renderId = currentRenderId;
  1220. this._processSubCameras(this.activeCameras[cameraIndex]);
  1221. }
  1222. }
  1223. else {
  1224. if (!this.activeCamera) {
  1225. throw new Error("No camera defined");
  1226. }
  1227. this._processSubCameras(this.activeCamera);
  1228. }
  1229. // Intersection checks
  1230. this._checkIntersections();
  1231. // Update the audio listener attached to the camera
  1232. this._updateAudioParameters();
  1233. // After render
  1234. if (this.afterRender) {
  1235. this.afterRender();
  1236. }
  1237. for (callbackIndex = 0; callbackIndex < this._onAfterRenderCallbacks.length; callbackIndex++) {
  1238. this._onAfterRenderCallbacks[callbackIndex]();
  1239. }
  1240. for (var index = 0; index < this._toBeDisposed.length; index++) {
  1241. this._toBeDisposed.data[index].dispose();
  1242. this._toBeDisposed[index] = null;
  1243. }
  1244. this._toBeDisposed.reset();
  1245. if (this.dumpNextRenderTargets) {
  1246. this.dumpNextRenderTargets = false;
  1247. }
  1248. BABYLON.Tools.EndPerformanceCounter("Scene rendering");
  1249. this._lastFrameDuration = BABYLON.Tools.Now - startDate;
  1250. };
  1251. Scene.prototype._updateAudioParameters = function () {
  1252. if (!this.audioEnabled || (this.mainSoundTrack.soundCollection.length === 0 && this.soundTracks.length === 0)) {
  1253. return;
  1254. }
  1255. var listeningCamera;
  1256. var audioEngine = BABYLON.Engine.audioEngine;
  1257. if (this.activeCameras.length > 0) {
  1258. listeningCamera = this.activeCameras[0];
  1259. }
  1260. else {
  1261. listeningCamera = this.activeCamera;
  1262. }
  1263. if (listeningCamera && audioEngine.canUseWebAudio) {
  1264. audioEngine.audioContext.listener.setPosition(listeningCamera.position.x, listeningCamera.position.y, listeningCamera.position.z);
  1265. var mat = BABYLON.Matrix.Invert(listeningCamera.getViewMatrix());
  1266. var cameraDirection = BABYLON.Vector3.TransformNormal(new BABYLON.Vector3(0, 0, -1), mat);
  1267. cameraDirection.normalize();
  1268. audioEngine.audioContext.listener.setOrientation(cameraDirection.x, cameraDirection.y, cameraDirection.z, 0, 1, 0);
  1269. for (var i = 0; i < this.mainSoundTrack.soundCollection.length; i++) {
  1270. var sound = this.mainSoundTrack.soundCollection[i];
  1271. if (sound.useCustomAttenuation) {
  1272. sound.updateDistanceFromListener();
  1273. }
  1274. }
  1275. for (i = 0; i < this.soundTracks.length; i++) {
  1276. for (var j = 0; j < this.soundTracks[i].soundCollection.length; j++) {
  1277. sound = this.soundTracks[i].soundCollection[j];
  1278. if (sound.useCustomAttenuation) {
  1279. sound.updateDistanceFromListener();
  1280. }
  1281. }
  1282. }
  1283. }
  1284. };
  1285. Object.defineProperty(Scene.prototype, "audioEnabled", {
  1286. // Audio
  1287. get: function () {
  1288. return this._audioEnabled;
  1289. },
  1290. set: function (value) {
  1291. this._audioEnabled = value;
  1292. if (this._audioEnabled) {
  1293. this._enableAudio();
  1294. }
  1295. else {
  1296. this._disableAudio();
  1297. }
  1298. },
  1299. enumerable: true,
  1300. configurable: true
  1301. });
  1302. Scene.prototype._disableAudio = function () {
  1303. for (var i = 0; i < this.mainSoundTrack.soundCollection.length; i++) {
  1304. this.mainSoundTrack.soundCollection[i].pause();
  1305. }
  1306. for (i = 0; i < this.soundTracks.length; i++) {
  1307. for (var j = 0; j < this.soundTracks[i].soundCollection.length; j++) {
  1308. this.soundTracks[i].soundCollection[j].pause();
  1309. }
  1310. }
  1311. };
  1312. Scene.prototype._enableAudio = function () {
  1313. for (var i = 0; i < this.mainSoundTrack.soundCollection.length; i++) {
  1314. if (this.mainSoundTrack.soundCollection[i].isPaused) {
  1315. this.mainSoundTrack.soundCollection[i].play();
  1316. }
  1317. }
  1318. for (i = 0; i < this.soundTracks.length; i++) {
  1319. for (var j = 0; j < this.soundTracks[i].soundCollection.length; j++) {
  1320. if (this.soundTracks[i].soundCollection[j].isPaused) {
  1321. this.soundTracks[i].soundCollection[j].play();
  1322. }
  1323. }
  1324. }
  1325. };
  1326. Object.defineProperty(Scene.prototype, "headphone", {
  1327. get: function () {
  1328. return this._headphone;
  1329. },
  1330. set: function (value) {
  1331. this._headphone = value;
  1332. if (this._headphone) {
  1333. this._switchAudioModeForHeadphones();
  1334. }
  1335. else {
  1336. this._switchAudioModeForNormalSpeakers();
  1337. }
  1338. },
  1339. enumerable: true,
  1340. configurable: true
  1341. });
  1342. Scene.prototype._switchAudioModeForHeadphones = function () {
  1343. this.mainSoundTrack.switchPanningModelToHRTF();
  1344. for (var i = 0; i < this.soundTracks.length; i++) {
  1345. this.soundTracks[i].switchPanningModelToHRTF();
  1346. }
  1347. };
  1348. Scene.prototype._switchAudioModeForNormalSpeakers = function () {
  1349. this.mainSoundTrack.switchPanningModelToEqualPower();
  1350. for (var i = 0; i < this.soundTracks.length; i++) {
  1351. this.soundTracks[i].switchPanningModelToEqualPower();
  1352. }
  1353. };
  1354. Scene.prototype.enableDepthRenderer = function () {
  1355. if (this._depthRenderer) {
  1356. return this._depthRenderer;
  1357. }
  1358. this._depthRenderer = new BABYLON.DepthRenderer(this);
  1359. return this._depthRenderer;
  1360. };
  1361. Scene.prototype.disableDepthRenderer = function () {
  1362. if (!this._depthRenderer) {
  1363. return;
  1364. }
  1365. this._depthRenderer.dispose();
  1366. this._depthRenderer = null;
  1367. };
  1368. Scene.prototype.dispose = function () {
  1369. this.beforeRender = null;
  1370. this.afterRender = null;
  1371. this.skeletons = [];
  1372. this._boundingBoxRenderer.dispose();
  1373. if (this._depthRenderer) {
  1374. this._depthRenderer.dispose();
  1375. }
  1376. // Debug layer
  1377. this.debugLayer.hide();
  1378. // Events
  1379. if (this.onDispose) {
  1380. this.onDispose();
  1381. }
  1382. this._onBeforeRenderCallbacks = [];
  1383. this._onAfterRenderCallbacks = [];
  1384. this.detachControl();
  1385. // Release sounds & sounds tracks
  1386. this.disposeSounds();
  1387. // Detach cameras
  1388. var canvas = this._engine.getRenderingCanvas();
  1389. var index;
  1390. for (index = 0; index < this.cameras.length; index++) {
  1391. this.cameras[index].detachControl(canvas);
  1392. }
  1393. while (this.lights.length) {
  1394. this.lights[0].dispose();
  1395. }
  1396. while (this.meshes.length) {
  1397. this.meshes[0].dispose(true);
  1398. }
  1399. while (this.cameras.length) {
  1400. this.cameras[0].dispose();
  1401. }
  1402. while (this.materials.length) {
  1403. this.materials[0].dispose();
  1404. }
  1405. while (this.particleSystems.length) {
  1406. this.particleSystems[0].dispose();
  1407. }
  1408. while (this.spriteManagers.length) {
  1409. this.spriteManagers[0].dispose();
  1410. }
  1411. while (this.layers.length) {
  1412. this.layers[0].dispose();
  1413. }
  1414. while (this.textures.length) {
  1415. this.textures[0].dispose();
  1416. }
  1417. // Post-processes
  1418. this.postProcessManager.dispose();
  1419. // Physics
  1420. if (this._physicsEngine) {
  1421. this.disablePhysicsEngine();
  1422. }
  1423. // Remove from engine
  1424. index = this._engine.scenes.indexOf(this);
  1425. if (index > -1) {
  1426. this._engine.scenes.splice(index, 1);
  1427. }
  1428. this._engine.wipeCaches();
  1429. };
  1430. // Release sounds & sounds tracks
  1431. Scene.prototype.disposeSounds = function () {
  1432. this.mainSoundTrack.dispose();
  1433. for (var scIndex = 0; scIndex < this.soundTracks.length; scIndex++) {
  1434. this.soundTracks[scIndex].dispose();
  1435. }
  1436. };
  1437. // Collisions
  1438. Scene.prototype._getNewPosition = function (position, velocity, collider, maximumRetry, finalPosition, excludedMesh) {
  1439. if (excludedMesh === void 0) { excludedMesh = null; }
  1440. position.divideToRef(collider.radius, this._scaledPosition);
  1441. velocity.divideToRef(collider.radius, this._scaledVelocity);
  1442. collider.retry = 0;
  1443. collider.initialVelocity = this._scaledVelocity;
  1444. collider.initialPosition = this._scaledPosition;
  1445. this._collideWithWorld(this._scaledPosition, this._scaledVelocity, collider, maximumRetry, finalPosition, excludedMesh);
  1446. finalPosition.multiplyInPlace(collider.radius);
  1447. };
  1448. Scene.prototype._collideWithWorld = function (position, velocity, collider, maximumRetry, finalPosition, excludedMesh) {
  1449. if (excludedMesh === void 0) { excludedMesh = null; }
  1450. var closeDistance = BABYLON.Engine.CollisionsEpsilon * 10.0;
  1451. if (collider.retry >= maximumRetry) {
  1452. finalPosition.copyFrom(position);
  1453. return;
  1454. }
  1455. collider._initialize(position, velocity, closeDistance);
  1456. for (var index = 0; index < this.meshes.length; index++) {
  1457. var mesh = this.meshes[index];
  1458. if (mesh.isEnabled() && mesh.checkCollisions && mesh.subMeshes && mesh !== excludedMesh) {
  1459. mesh._checkCollision(collider);
  1460. }
  1461. }
  1462. if (!collider.collisionFound) {
  1463. position.addToRef(velocity, finalPosition);
  1464. return;
  1465. }
  1466. if (velocity.x !== 0 || velocity.y !== 0 || velocity.z !== 0) {
  1467. collider._getResponse(position, velocity);
  1468. }
  1469. if (velocity.length() <= closeDistance) {
  1470. finalPosition.copyFrom(position);
  1471. return;
  1472. }
  1473. collider.retry++;
  1474. this._collideWithWorld(position, velocity, collider, maximumRetry, finalPosition, excludedMesh);
  1475. };
  1476. // Octrees
  1477. Scene.prototype.getWorldExtends = function () {
  1478. var min = new BABYLON.Vector3(Number.MAX_VALUE, Number.MAX_VALUE, Number.MAX_VALUE);
  1479. var max = new BABYLON.Vector3(-Number.MAX_VALUE, -Number.MAX_VALUE, -Number.MAX_VALUE);
  1480. for (var index = 0; index < this.meshes.length; index++) {
  1481. var mesh = this.meshes[index];
  1482. mesh.computeWorldMatrix(true);
  1483. var minBox = mesh.getBoundingInfo().boundingBox.minimumWorld;
  1484. var maxBox = mesh.getBoundingInfo().boundingBox.maximumWorld;
  1485. BABYLON.Tools.CheckExtends(minBox, min, max);
  1486. BABYLON.Tools.CheckExtends(maxBox, min, max);
  1487. }
  1488. return {
  1489. min: min,
  1490. max: max
  1491. };
  1492. };
  1493. Scene.prototype.createOrUpdateSelectionOctree = function (maxCapacity, maxDepth) {
  1494. if (maxCapacity === void 0) { maxCapacity = 64; }
  1495. if (maxDepth === void 0) { maxDepth = 2; }
  1496. if (!this._selectionOctree) {
  1497. this._selectionOctree = new BABYLON.Octree(BABYLON.Octree.CreationFuncForMeshes, maxCapacity, maxDepth);
  1498. }
  1499. var worldExtends = this.getWorldExtends();
  1500. // Update octree
  1501. this._selectionOctree.update(worldExtends.min, worldExtends.max, this.meshes);
  1502. return this._selectionOctree;
  1503. };
  1504. // Picking
  1505. Scene.prototype.createPickingRay = function (x, y, world, camera) {
  1506. var engine = this._engine;
  1507. if (!camera) {
  1508. if (!this.activeCamera)
  1509. throw new Error("Active camera not set");
  1510. camera = this.activeCamera;
  1511. }
  1512. var cameraViewport = camera.viewport;
  1513. var viewport = cameraViewport.toGlobal(engine);
  1514. // Moving coordinates to local viewport world
  1515. x = x / this._engine.getHardwareScalingLevel() - viewport.x;
  1516. y = y / this._engine.getHardwareScalingLevel() - (this._engine.getRenderHeight() - viewport.y - viewport.height);
  1517. return BABYLON.Ray.CreateNew(x, y, viewport.width, viewport.height, world ? world : BABYLON.Matrix.Identity(), camera.getViewMatrix(), camera.getProjectionMatrix());
  1518. // return BABYLON.Ray.CreateNew(x / window.devicePixelRatio, y / window.devicePixelRatio, viewport.width, viewport.height, world ? world : BABYLON.Matrix.Identity(), camera.getViewMatrix(), camera.getProjectionMatrix());
  1519. };
  1520. Scene.prototype._internalPick = function (rayFunction, predicate, fastCheck) {
  1521. var pickingInfo = null;
  1522. for (var meshIndex = 0; meshIndex < this.meshes.length; meshIndex++) {
  1523. var mesh = this.meshes[meshIndex];
  1524. if (predicate) {
  1525. if (!predicate(mesh)) {
  1526. continue;
  1527. }
  1528. }
  1529. else if (!mesh.isEnabled() || !mesh.isVisible || !mesh.isPickable) {
  1530. continue;
  1531. }
  1532. var world = mesh.getWorldMatrix();
  1533. var ray = rayFunction(world);
  1534. var result = mesh.intersects(ray, fastCheck);
  1535. if (!result || !result.hit)
  1536. continue;
  1537. if (!fastCheck && pickingInfo != null && result.distance >= pickingInfo.distance)
  1538. continue;
  1539. pickingInfo = result;
  1540. if (fastCheck) {
  1541. break;
  1542. }
  1543. }
  1544. return pickingInfo || new BABYLON.PickingInfo();
  1545. };
  1546. Scene.prototype.pick = function (x, y, predicate, fastCheck, camera) {
  1547. var _this = this;
  1548. /// <summary>Launch a ray to try to pick a mesh in the scene</summary>
  1549. /// <param name="x">X position on screen</param>
  1550. /// <param name="y">Y position on screen</param>
  1551. /// <param name="predicate">Predicate function used to determine eligible meshes. Can be set to null. In this case, a mesh must be enabled, visible and with isPickable set to true</param>
  1552. /// <param name="fastCheck">Launch a fast check only using the bounding boxes. Can be set to null.</param>
  1553. /// <param name="camera">camera to use for computing the picking ray. Can be set to null. In this case, the scene.activeCamera will be used</param>
  1554. return this._internalPick(function (world) { return _this.createPickingRay(x, y, world, camera); }, predicate, fastCheck);
  1555. };
  1556. Scene.prototype.pickWithRay = function (ray, predicate, fastCheck) {
  1557. var _this = this;
  1558. return this._internalPick(function (world) {
  1559. if (!_this._pickWithRayInverseMatrix) {
  1560. _this._pickWithRayInverseMatrix = BABYLON.Matrix.Identity();
  1561. }
  1562. world.invertToRef(_this._pickWithRayInverseMatrix);
  1563. return BABYLON.Ray.Transform(ray, _this._pickWithRayInverseMatrix);
  1564. }, predicate, fastCheck);
  1565. };
  1566. Scene.prototype.setPointerOverMesh = function (mesh) {
  1567. if (this._pointerOverMesh === mesh) {
  1568. return;
  1569. }
  1570. if (this._pointerOverMesh && this._pointerOverMesh.actionManager) {
  1571. this._pointerOverMesh.actionManager.processTrigger(BABYLON.ActionManager.OnPointerOutTrigger, BABYLON.ActionEvent.CreateNew(this._pointerOverMesh));
  1572. }
  1573. this._pointerOverMesh = mesh;
  1574. if (this._pointerOverMesh && this._pointerOverMesh.actionManager) {
  1575. this._pointerOverMesh.actionManager.processTrigger(BABYLON.ActionManager.OnPointerOverTrigger, BABYLON.ActionEvent.CreateNew(this._pointerOverMesh));
  1576. }
  1577. };
  1578. Scene.prototype.getPointerOverMesh = function () {
  1579. return this._pointerOverMesh;
  1580. };
  1581. // Physics
  1582. Scene.prototype.getPhysicsEngine = function () {
  1583. return this._physicsEngine;
  1584. };
  1585. Scene.prototype.enablePhysics = function (gravity, plugin) {
  1586. if (this._physicsEngine) {
  1587. return true;
  1588. }
  1589. this._physicsEngine = new BABYLON.PhysicsEngine(plugin);
  1590. if (!this._physicsEngine.isSupported()) {
  1591. this._physicsEngine = null;
  1592. return false;
  1593. }
  1594. this._physicsEngine._initialize(gravity);
  1595. return true;
  1596. };
  1597. Scene.prototype.disablePhysicsEngine = function () {
  1598. if (!this._physicsEngine) {
  1599. return;
  1600. }
  1601. this._physicsEngine.dispose();
  1602. this._physicsEngine = undefined;
  1603. };
  1604. Scene.prototype.isPhysicsEnabled = function () {
  1605. return this._physicsEngine !== undefined;
  1606. };
  1607. Scene.prototype.setGravity = function (gravity) {
  1608. if (!this._physicsEngine) {
  1609. return;
  1610. }
  1611. this._physicsEngine._setGravity(gravity);
  1612. };
  1613. Scene.prototype.createCompoundImpostor = function (parts, options) {
  1614. if (parts.parts) {
  1615. options = parts;
  1616. parts = parts.parts;
  1617. }
  1618. if (!this._physicsEngine) {
  1619. return null;
  1620. }
  1621. for (var index = 0; index < parts.length; index++) {
  1622. var mesh = parts[index].mesh;
  1623. mesh._physicImpostor = parts[index].impostor;
  1624. mesh._physicsMass = options.mass / parts.length;
  1625. mesh._physicsFriction = options.friction;
  1626. mesh._physicRestitution = options.restitution;
  1627. }
  1628. return this._physicsEngine._registerMeshesAsCompound(parts, options);
  1629. };
  1630. Scene.prototype.deleteCompoundImpostor = function (compound) {
  1631. for (var index = 0; index < compound.parts.length; index++) {
  1632. var mesh = compound.parts[index].mesh;
  1633. mesh._physicImpostor = BABYLON.PhysicsEngine.NoImpostor;
  1634. this._physicsEngine._unregisterMesh(mesh);
  1635. }
  1636. };
  1637. // Misc.
  1638. Scene.prototype.createDefaultCameraOrLight = function () {
  1639. // Light
  1640. if (this.lights.length === 0) {
  1641. new BABYLON.HemisphericLight("default light", BABYLON.Vector3.Up(), this);
  1642. }
  1643. // Camera
  1644. if (!this.activeCamera) {
  1645. var camera = new BABYLON.FreeCamera("default camera", BABYLON.Vector3.Zero(), this);
  1646. // Compute position
  1647. var worldExtends = this.getWorldExtends();
  1648. var worldCenter = worldExtends.min.add(worldExtends.max.subtract(worldExtends.min).scale(0.5));
  1649. camera.position = new BABYLON.Vector3(worldCenter.x, worldCenter.y, worldExtends.min.z - (worldExtends.max.z - worldExtends.min.z));
  1650. camera.setTarget(worldCenter);
  1651. this.activeCamera = camera;
  1652. }
  1653. };
  1654. // Tags
  1655. Scene.prototype._getByTags = function (list, tagsQuery, forEach) {
  1656. if (tagsQuery === undefined) {
  1657. // returns the complete list (could be done with BABYLON.Tags.MatchesQuery but no need to have a for-loop here)
  1658. return list;
  1659. }
  1660. var listByTags = [];
  1661. forEach = forEach || (function (item) {
  1662. return;
  1663. });
  1664. for (var i in list) {
  1665. var item = list[i];
  1666. if (BABYLON.Tags.MatchesQuery(item, tagsQuery)) {
  1667. listByTags.push(item);
  1668. forEach(item);
  1669. }
  1670. }
  1671. return listByTags;
  1672. };
  1673. Scene.prototype.getMeshesByTags = function (tagsQuery, forEach) {
  1674. return this._getByTags(this.meshes, tagsQuery, forEach);
  1675. };
  1676. Scene.prototype.getCamerasByTags = function (tagsQuery, forEach) {
  1677. return this._getByTags(this.cameras, tagsQuery, forEach);
  1678. };
  1679. Scene.prototype.getLightsByTags = function (tagsQuery, forEach) {
  1680. return this._getByTags(this.lights, tagsQuery, forEach);
  1681. };
  1682. Scene.prototype.getMaterialByTags = function (tagsQuery, forEach) {
  1683. return this._getByTags(this.materials, tagsQuery, forEach).concat(this._getByTags(this.multiMaterials, tagsQuery, forEach));
  1684. };
  1685. // Statics
  1686. Scene._FOGMODE_NONE = 0;
  1687. Scene._FOGMODE_EXP = 1;
  1688. Scene._FOGMODE_EXP2 = 2;
  1689. Scene._FOGMODE_LINEAR = 3;
  1690. Scene.MinDeltaTime = 1.0;
  1691. Scene.MaxDeltaTime = 1000.0;
  1692. return Scene;
  1693. })();
  1694. BABYLON.Scene = Scene;
  1695. })(BABYLON || (BABYLON = {}));
  1696. //# sourceMappingURL=babylon.scene.js.map