babylon.scene.js 76 KB

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