babylon.scene.js 77 KB

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