babylon.scene.js 77 KB

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