babylon.scene.js 77 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749
  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.resetCachedMaterial();
  1067. this._renderId++;
  1068. this.updateTransformMatrix();
  1069. if (this.beforeCameraRender) {
  1070. this.beforeCameraRender(this.activeCamera);
  1071. }
  1072. // Meshes
  1073. var beforeEvaluateActiveMeshesDate = BABYLON.Tools.Now;
  1074. BABYLON.Tools.StartPerformanceCounter("Active meshes evaluation");
  1075. this._evaluateActiveMeshes();
  1076. this._evaluateActiveMeshesDuration += BABYLON.Tools.Now - beforeEvaluateActiveMeshesDate;
  1077. BABYLON.Tools.EndPerformanceCounter("Active meshes evaluation");
  1078. // Skeletons
  1079. for (var skeletonIndex = 0; skeletonIndex < this._activeSkeletons.length; skeletonIndex++) {
  1080. var skeleton = this._activeSkeletons.data[skeletonIndex];
  1081. skeleton.prepare();
  1082. }
  1083. // Render targets
  1084. var beforeRenderTargetDate = BABYLON.Tools.Now;
  1085. if (this.renderTargetsEnabled) {
  1086. BABYLON.Tools.StartPerformanceCounter("Render targets", this._renderTargets.length > 0);
  1087. for (var renderIndex = 0; renderIndex < this._renderTargets.length; renderIndex++) {
  1088. var renderTarget = this._renderTargets.data[renderIndex];
  1089. if (renderTarget._shouldRender()) {
  1090. this._renderId++;
  1091. var hasSpecialRenderTargetCamera = renderTarget.activeCamera && renderTarget.activeCamera !== this.activeCamera;
  1092. renderTarget.render(hasSpecialRenderTargetCamera, this.dumpNextRenderTargets);
  1093. }
  1094. }
  1095. BABYLON.Tools.EndPerformanceCounter("Render targets", this._renderTargets.length > 0);
  1096. this._renderId++;
  1097. }
  1098. if (this._renderTargets.length > 0) {
  1099. engine.restoreDefaultFramebuffer();
  1100. }
  1101. this._renderTargetsDuration += BABYLON.Tools.Now - beforeRenderTargetDate;
  1102. // Prepare Frame
  1103. this.postProcessManager._prepareFrame();
  1104. var beforeRenderDate = BABYLON.Tools.Now;
  1105. // Backgrounds
  1106. if (this.layers.length) {
  1107. engine.setDepthBuffer(false);
  1108. var layerIndex;
  1109. var layer;
  1110. for (layerIndex = 0; layerIndex < this.layers.length; layerIndex++) {
  1111. layer = this.layers[layerIndex];
  1112. if (layer.isBackground) {
  1113. layer.render();
  1114. }
  1115. }
  1116. engine.setDepthBuffer(true);
  1117. }
  1118. // Render
  1119. BABYLON.Tools.StartPerformanceCounter("Main render");
  1120. this._renderingManager.render(null, null, true, true);
  1121. BABYLON.Tools.EndPerformanceCounter("Main render");
  1122. // Bounding boxes
  1123. this._boundingBoxRenderer.render();
  1124. // Lens flares
  1125. if (this.lensFlaresEnabled) {
  1126. BABYLON.Tools.StartPerformanceCounter("Lens flares", this.lensFlareSystems.length > 0);
  1127. for (var lensFlareSystemIndex = 0; lensFlareSystemIndex < this.lensFlareSystems.length; lensFlareSystemIndex++) {
  1128. this.lensFlareSystems[lensFlareSystemIndex].render();
  1129. }
  1130. BABYLON.Tools.EndPerformanceCounter("Lens flares", this.lensFlareSystems.length > 0);
  1131. }
  1132. // Foregrounds
  1133. if (this.layers.length) {
  1134. engine.setDepthBuffer(false);
  1135. for (layerIndex = 0; layerIndex < this.layers.length; layerIndex++) {
  1136. layer = this.layers[layerIndex];
  1137. if (!layer.isBackground) {
  1138. layer.render();
  1139. }
  1140. }
  1141. engine.setDepthBuffer(true);
  1142. }
  1143. this._renderDuration += BABYLON.Tools.Now - beforeRenderDate;
  1144. // Finalize frame
  1145. this.postProcessManager._finalizeFrame(camera.isIntermediate);
  1146. // Update camera
  1147. this.activeCamera._updateFromScene();
  1148. // Reset some special arrays
  1149. this._renderTargets.reset();
  1150. if (this.afterCameraRender) {
  1151. this.afterCameraRender(this.activeCamera);
  1152. }
  1153. BABYLON.Tools.EndPerformanceCounter("Rendering camera " + this.activeCamera.name);
  1154. };
  1155. Scene.prototype._processSubCameras = function (camera) {
  1156. if (camera.subCameras.length === 0) {
  1157. this._renderForCamera(camera);
  1158. return;
  1159. }
  1160. // Sub-cameras
  1161. for (var index = 0; index < camera.subCameras.length; index++) {
  1162. this._renderForCamera(camera.subCameras[index]);
  1163. }
  1164. this.activeCamera = camera;
  1165. this.setTransformMatrix(this.activeCamera.getViewMatrix(), this.activeCamera.getProjectionMatrix());
  1166. // Update camera
  1167. this.activeCamera._updateFromScene();
  1168. };
  1169. Scene.prototype._checkIntersections = function () {
  1170. for (var index = 0; index < this._meshesForIntersections.length; index++) {
  1171. var sourceMesh = this._meshesForIntersections.data[index];
  1172. for (var actionIndex = 0; actionIndex < sourceMesh.actionManager.actions.length; actionIndex++) {
  1173. var action = sourceMesh.actionManager.actions[actionIndex];
  1174. if (action.trigger === BABYLON.ActionManager.OnIntersectionEnterTrigger || action.trigger === BABYLON.ActionManager.OnIntersectionExitTrigger) {
  1175. var parameters = action.getTriggerParameter();
  1176. var otherMesh = parameters instanceof BABYLON.AbstractMesh ? parameters : parameters.mesh;
  1177. var areIntersecting = otherMesh.intersectsMesh(sourceMesh, parameters.usePreciseIntersection);
  1178. var currentIntersectionInProgress = sourceMesh._intersectionsInProgress.indexOf(otherMesh);
  1179. if (areIntersecting && currentIntersectionInProgress === -1) {
  1180. if (action.trigger === BABYLON.ActionManager.OnIntersectionEnterTrigger) {
  1181. action._executeCurrent(BABYLON.ActionEvent.CreateNew(sourceMesh));
  1182. sourceMesh._intersectionsInProgress.push(otherMesh);
  1183. }
  1184. else if (action.trigger === BABYLON.ActionManager.OnIntersectionExitTrigger) {
  1185. sourceMesh._intersectionsInProgress.push(otherMesh);
  1186. }
  1187. }
  1188. else if (!areIntersecting && currentIntersectionInProgress > -1) {
  1189. //They intersected, and now they don't.
  1190. //is this trigger an exit trigger? execute an event.
  1191. if (action.trigger === BABYLON.ActionManager.OnIntersectionExitTrigger) {
  1192. action._executeCurrent(BABYLON.ActionEvent.CreateNew(sourceMesh));
  1193. }
  1194. //if this is an exit trigger, or no exit trigger exists, remove the id from the intersection in progress array.
  1195. if (!sourceMesh.actionManager.hasSpecificTrigger(BABYLON.ActionManager.OnIntersectionExitTrigger) || action.trigger === BABYLON.ActionManager.OnIntersectionExitTrigger) {
  1196. sourceMesh._intersectionsInProgress.splice(currentIntersectionInProgress, 1);
  1197. }
  1198. }
  1199. }
  1200. }
  1201. }
  1202. };
  1203. Scene.prototype.render = function () {
  1204. var startDate = BABYLON.Tools.Now;
  1205. this._particlesDuration = 0;
  1206. this._spritesDuration = 0;
  1207. this._activeParticles = 0;
  1208. this._renderDuration = 0;
  1209. this._renderTargetsDuration = 0;
  1210. this._evaluateActiveMeshesDuration = 0;
  1211. this._totalVertices = 0;
  1212. this._activeIndices = 0;
  1213. this._activeBones = 0;
  1214. this.getEngine().resetDrawCalls();
  1215. this._meshesForIntersections.reset();
  1216. this.resetCachedMaterial();
  1217. BABYLON.Tools.StartPerformanceCounter("Scene rendering");
  1218. // Actions
  1219. if (this.actionManager) {
  1220. this.actionManager.processTrigger(BABYLON.ActionManager.OnEveryFrameTrigger, null);
  1221. }
  1222. //Simplification Queue
  1223. if (!this.simplificationQueue.running) {
  1224. this.simplificationQueue.executeNext();
  1225. }
  1226. // Before render
  1227. if (this.beforeRender) {
  1228. this.beforeRender();
  1229. }
  1230. for (var callbackIndex = 0; callbackIndex < this._onBeforeRenderCallbacks.length; callbackIndex++) {
  1231. this._onBeforeRenderCallbacks[callbackIndex]();
  1232. }
  1233. // Animations
  1234. var deltaTime = Math.max(Scene.MinDeltaTime, Math.min(this._engine.getDeltaTime(), Scene.MaxDeltaTime));
  1235. this._animationRatio = deltaTime * (60.0 / 1000.0);
  1236. this._animate();
  1237. // Physics
  1238. if (this._physicsEngine) {
  1239. BABYLON.Tools.StartPerformanceCounter("Physics");
  1240. this._physicsEngine._runOneStep(deltaTime / 1000.0);
  1241. BABYLON.Tools.EndPerformanceCounter("Physics");
  1242. }
  1243. // Customs render targets
  1244. var beforeRenderTargetDate = BABYLON.Tools.Now;
  1245. var engine = this.getEngine();
  1246. var currentActiveCamera = this.activeCamera;
  1247. if (this.renderTargetsEnabled) {
  1248. BABYLON.Tools.StartPerformanceCounter("Custom render targets", this.customRenderTargets.length > 0);
  1249. for (var customIndex = 0; customIndex < this.customRenderTargets.length; customIndex++) {
  1250. var renderTarget = this.customRenderTargets[customIndex];
  1251. if (renderTarget._shouldRender()) {
  1252. this._renderId++;
  1253. this.activeCamera = renderTarget.activeCamera || this.activeCamera;
  1254. if (!this.activeCamera)
  1255. throw new Error("Active camera not set");
  1256. // Viewport
  1257. engine.setViewport(this.activeCamera.viewport);
  1258. // Camera
  1259. this.updateTransformMatrix();
  1260. renderTarget.render(currentActiveCamera !== this.activeCamera, this.dumpNextRenderTargets);
  1261. }
  1262. }
  1263. BABYLON.Tools.EndPerformanceCounter("Custom render targets", this.customRenderTargets.length > 0);
  1264. this._renderId++;
  1265. }
  1266. if (this.customRenderTargets.length > 0) {
  1267. engine.restoreDefaultFramebuffer();
  1268. }
  1269. this._renderTargetsDuration += BABYLON.Tools.Now - beforeRenderTargetDate;
  1270. this.activeCamera = currentActiveCamera;
  1271. // Procedural textures
  1272. if (this.proceduralTexturesEnabled) {
  1273. BABYLON.Tools.StartPerformanceCounter("Procedural textures", this._proceduralTextures.length > 0);
  1274. for (var proceduralIndex = 0; proceduralIndex < this._proceduralTextures.length; proceduralIndex++) {
  1275. var proceduralTexture = this._proceduralTextures[proceduralIndex];
  1276. if (proceduralTexture._shouldRender()) {
  1277. proceduralTexture.render();
  1278. }
  1279. }
  1280. BABYLON.Tools.EndPerformanceCounter("Procedural textures", this._proceduralTextures.length > 0);
  1281. }
  1282. // Clear
  1283. this._engine.clear(this.clearColor, this.autoClear || this.forceWireframe || this.forcePointsCloud, true);
  1284. // Shadows
  1285. if (this.shadowsEnabled) {
  1286. for (var lightIndex = 0; lightIndex < this.lights.length; lightIndex++) {
  1287. var light = this.lights[lightIndex];
  1288. var shadowGenerator = light.getShadowGenerator();
  1289. if (light.isEnabled() && shadowGenerator && shadowGenerator.getShadowMap().getScene().textures.indexOf(shadowGenerator.getShadowMap()) !== -1) {
  1290. this._renderTargets.push(shadowGenerator.getShadowMap());
  1291. }
  1292. }
  1293. }
  1294. // Depth renderer
  1295. if (this._depthRenderer) {
  1296. this._renderTargets.push(this._depthRenderer.getDepthMap());
  1297. }
  1298. // RenderPipeline
  1299. this.postProcessRenderPipelineManager.update();
  1300. // Multi-cameras?
  1301. if (this.activeCameras.length > 0) {
  1302. var currentRenderId = this._renderId;
  1303. for (var cameraIndex = 0; cameraIndex < this.activeCameras.length; cameraIndex++) {
  1304. this._renderId = currentRenderId;
  1305. this._processSubCameras(this.activeCameras[cameraIndex]);
  1306. }
  1307. }
  1308. else {
  1309. if (!this.activeCamera) {
  1310. throw new Error("No camera defined");
  1311. }
  1312. this._processSubCameras(this.activeCamera);
  1313. }
  1314. // Intersection checks
  1315. this._checkIntersections();
  1316. // Update the audio listener attached to the camera
  1317. this._updateAudioParameters();
  1318. // After render
  1319. if (this.afterRender) {
  1320. this.afterRender();
  1321. }
  1322. for (callbackIndex = 0; callbackIndex < this._onAfterRenderCallbacks.length; callbackIndex++) {
  1323. this._onAfterRenderCallbacks[callbackIndex]();
  1324. }
  1325. // Cleaning
  1326. for (var index = 0; index < this._toBeDisposed.length; index++) {
  1327. this._toBeDisposed.data[index].dispose();
  1328. this._toBeDisposed[index] = null;
  1329. }
  1330. this._toBeDisposed.reset();
  1331. if (this.dumpNextRenderTargets) {
  1332. this.dumpNextRenderTargets = false;
  1333. }
  1334. BABYLON.Tools.EndPerformanceCounter("Scene rendering");
  1335. this._lastFrameDuration = BABYLON.Tools.Now - startDate;
  1336. };
  1337. Scene.prototype._updateAudioParameters = function () {
  1338. if (!this.audioEnabled || (this.mainSoundTrack.soundCollection.length === 0 && this.soundTracks.length === 0)) {
  1339. return;
  1340. }
  1341. var listeningCamera;
  1342. var audioEngine = BABYLON.Engine.audioEngine;
  1343. if (this.activeCameras.length > 0) {
  1344. listeningCamera = this.activeCameras[0];
  1345. }
  1346. else {
  1347. listeningCamera = this.activeCamera;
  1348. }
  1349. if (listeningCamera && audioEngine.canUseWebAudio) {
  1350. audioEngine.audioContext.listener.setPosition(listeningCamera.position.x, listeningCamera.position.y, listeningCamera.position.z);
  1351. var mat = BABYLON.Matrix.Invert(listeningCamera.getViewMatrix());
  1352. var cameraDirection = BABYLON.Vector3.TransformNormal(new BABYLON.Vector3(0, 0, -1), mat);
  1353. cameraDirection.normalize();
  1354. audioEngine.audioContext.listener.setOrientation(cameraDirection.x, cameraDirection.y, cameraDirection.z, 0, 1, 0);
  1355. for (var i = 0; i < this.mainSoundTrack.soundCollection.length; i++) {
  1356. var sound = this.mainSoundTrack.soundCollection[i];
  1357. if (sound.useCustomAttenuation) {
  1358. sound.updateDistanceFromListener();
  1359. }
  1360. }
  1361. for (i = 0; i < this.soundTracks.length; i++) {
  1362. for (var j = 0; j < this.soundTracks[i].soundCollection.length; j++) {
  1363. sound = this.soundTracks[i].soundCollection[j];
  1364. if (sound.useCustomAttenuation) {
  1365. sound.updateDistanceFromListener();
  1366. }
  1367. }
  1368. }
  1369. }
  1370. };
  1371. Object.defineProperty(Scene.prototype, "audioEnabled", {
  1372. // Audio
  1373. get: function () {
  1374. return this._audioEnabled;
  1375. },
  1376. set: function (value) {
  1377. this._audioEnabled = value;
  1378. if (this._audioEnabled) {
  1379. this._enableAudio();
  1380. }
  1381. else {
  1382. this._disableAudio();
  1383. }
  1384. },
  1385. enumerable: true,
  1386. configurable: true
  1387. });
  1388. Scene.prototype._disableAudio = function () {
  1389. for (var i = 0; i < this.mainSoundTrack.soundCollection.length; i++) {
  1390. this.mainSoundTrack.soundCollection[i].pause();
  1391. }
  1392. for (i = 0; i < this.soundTracks.length; i++) {
  1393. for (var j = 0; j < this.soundTracks[i].soundCollection.length; j++) {
  1394. this.soundTracks[i].soundCollection[j].pause();
  1395. }
  1396. }
  1397. };
  1398. Scene.prototype._enableAudio = function () {
  1399. for (var i = 0; i < this.mainSoundTrack.soundCollection.length; i++) {
  1400. if (this.mainSoundTrack.soundCollection[i].isPaused) {
  1401. this.mainSoundTrack.soundCollection[i].play();
  1402. }
  1403. }
  1404. for (i = 0; i < this.soundTracks.length; i++) {
  1405. for (var j = 0; j < this.soundTracks[i].soundCollection.length; j++) {
  1406. if (this.soundTracks[i].soundCollection[j].isPaused) {
  1407. this.soundTracks[i].soundCollection[j].play();
  1408. }
  1409. }
  1410. }
  1411. };
  1412. Object.defineProperty(Scene.prototype, "headphone", {
  1413. get: function () {
  1414. return this._headphone;
  1415. },
  1416. set: function (value) {
  1417. this._headphone = value;
  1418. if (this._headphone) {
  1419. this._switchAudioModeForHeadphones();
  1420. }
  1421. else {
  1422. this._switchAudioModeForNormalSpeakers();
  1423. }
  1424. },
  1425. enumerable: true,
  1426. configurable: true
  1427. });
  1428. Scene.prototype._switchAudioModeForHeadphones = function () {
  1429. this.mainSoundTrack.switchPanningModelToHRTF();
  1430. for (var i = 0; i < this.soundTracks.length; i++) {
  1431. this.soundTracks[i].switchPanningModelToHRTF();
  1432. }
  1433. };
  1434. Scene.prototype._switchAudioModeForNormalSpeakers = function () {
  1435. this.mainSoundTrack.switchPanningModelToEqualPower();
  1436. for (var i = 0; i < this.soundTracks.length; i++) {
  1437. this.soundTracks[i].switchPanningModelToEqualPower();
  1438. }
  1439. };
  1440. Scene.prototype.enableDepthRenderer = function () {
  1441. if (this._depthRenderer) {
  1442. return this._depthRenderer;
  1443. }
  1444. this._depthRenderer = new BABYLON.DepthRenderer(this);
  1445. return this._depthRenderer;
  1446. };
  1447. Scene.prototype.disableDepthRenderer = function () {
  1448. if (!this._depthRenderer) {
  1449. return;
  1450. }
  1451. this._depthRenderer.dispose();
  1452. this._depthRenderer = null;
  1453. };
  1454. Scene.prototype.dispose = function () {
  1455. this.beforeRender = null;
  1456. this.afterRender = null;
  1457. this.skeletons = [];
  1458. this._boundingBoxRenderer.dispose();
  1459. if (this._depthRenderer) {
  1460. this._depthRenderer.dispose();
  1461. }
  1462. // Debug layer
  1463. this.debugLayer.hide();
  1464. // Events
  1465. if (this.onDispose) {
  1466. this.onDispose();
  1467. }
  1468. this._onBeforeRenderCallbacks = [];
  1469. this._onAfterRenderCallbacks = [];
  1470. this.detachControl();
  1471. // Release sounds & sounds tracks
  1472. this.disposeSounds();
  1473. // Detach cameras
  1474. var canvas = this._engine.getRenderingCanvas();
  1475. var index;
  1476. for (index = 0; index < this.cameras.length; index++) {
  1477. this.cameras[index].detachControl(canvas);
  1478. }
  1479. // Release lights
  1480. while (this.lights.length) {
  1481. this.lights[0].dispose();
  1482. }
  1483. // Release meshes
  1484. while (this.meshes.length) {
  1485. this.meshes[0].dispose(true);
  1486. }
  1487. // Release cameras
  1488. while (this.cameras.length) {
  1489. this.cameras[0].dispose();
  1490. }
  1491. // Release materials
  1492. while (this.materials.length) {
  1493. this.materials[0].dispose();
  1494. }
  1495. // Release particles
  1496. while (this.particleSystems.length) {
  1497. this.particleSystems[0].dispose();
  1498. }
  1499. // Release sprites
  1500. while (this.spriteManagers.length) {
  1501. this.spriteManagers[0].dispose();
  1502. }
  1503. // Release layers
  1504. while (this.layers.length) {
  1505. this.layers[0].dispose();
  1506. }
  1507. // Release textures
  1508. while (this.textures.length) {
  1509. this.textures[0].dispose();
  1510. }
  1511. // Post-processes
  1512. this.postProcessManager.dispose();
  1513. // Physics
  1514. if (this._physicsEngine) {
  1515. this.disablePhysicsEngine();
  1516. }
  1517. // Remove from engine
  1518. index = this._engine.scenes.indexOf(this);
  1519. if (index > -1) {
  1520. this._engine.scenes.splice(index, 1);
  1521. }
  1522. this._engine.wipeCaches();
  1523. };
  1524. // Release sounds & sounds tracks
  1525. Scene.prototype.disposeSounds = function () {
  1526. this.mainSoundTrack.dispose();
  1527. for (var scIndex = 0; scIndex < this.soundTracks.length; scIndex++) {
  1528. this.soundTracks[scIndex].dispose();
  1529. }
  1530. };
  1531. // Octrees
  1532. Scene.prototype.getWorldExtends = function () {
  1533. var min = new BABYLON.Vector3(Number.MAX_VALUE, Number.MAX_VALUE, Number.MAX_VALUE);
  1534. var max = new BABYLON.Vector3(-Number.MAX_VALUE, -Number.MAX_VALUE, -Number.MAX_VALUE);
  1535. for (var index = 0; index < this.meshes.length; index++) {
  1536. var mesh = this.meshes[index];
  1537. mesh.computeWorldMatrix(true);
  1538. var minBox = mesh.getBoundingInfo().boundingBox.minimumWorld;
  1539. var maxBox = mesh.getBoundingInfo().boundingBox.maximumWorld;
  1540. BABYLON.Tools.CheckExtends(minBox, min, max);
  1541. BABYLON.Tools.CheckExtends(maxBox, min, max);
  1542. }
  1543. return {
  1544. min: min,
  1545. max: max
  1546. };
  1547. };
  1548. Scene.prototype.createOrUpdateSelectionOctree = function (maxCapacity, maxDepth) {
  1549. if (maxCapacity === void 0) { maxCapacity = 64; }
  1550. if (maxDepth === void 0) { maxDepth = 2; }
  1551. if (!this._selectionOctree) {
  1552. this._selectionOctree = new BABYLON.Octree(BABYLON.Octree.CreationFuncForMeshes, maxCapacity, maxDepth);
  1553. }
  1554. var worldExtends = this.getWorldExtends();
  1555. // Update octree
  1556. this._selectionOctree.update(worldExtends.min, worldExtends.max, this.meshes);
  1557. return this._selectionOctree;
  1558. };
  1559. // Picking
  1560. Scene.prototype.createPickingRay = function (x, y, world, camera) {
  1561. var engine = this._engine;
  1562. if (!camera) {
  1563. if (!this.activeCamera)
  1564. throw new Error("Active camera not set");
  1565. camera = this.activeCamera;
  1566. }
  1567. var cameraViewport = camera.viewport;
  1568. var viewport = cameraViewport.toGlobal(engine);
  1569. // Moving coordinates to local viewport world
  1570. x = x / this._engine.getHardwareScalingLevel() - viewport.x;
  1571. y = y / this._engine.getHardwareScalingLevel() - (this._engine.getRenderHeight() - viewport.y - viewport.height);
  1572. return BABYLON.Ray.CreateNew(x, y, viewport.width, viewport.height, world ? world : BABYLON.Matrix.Identity(), camera.getViewMatrix(), camera.getProjectionMatrix());
  1573. // return BABYLON.Ray.CreateNew(x / window.devicePixelRatio, y / window.devicePixelRatio, viewport.width, viewport.height, world ? world : BABYLON.Matrix.Identity(), camera.getViewMatrix(), camera.getProjectionMatrix());
  1574. };
  1575. Scene.prototype._internalPick = function (rayFunction, predicate, fastCheck) {
  1576. var pickingInfo = null;
  1577. for (var meshIndex = 0; meshIndex < this.meshes.length; meshIndex++) {
  1578. var mesh = this.meshes[meshIndex];
  1579. if (predicate) {
  1580. if (!predicate(mesh)) {
  1581. continue;
  1582. }
  1583. }
  1584. else if (!mesh.isEnabled() || !mesh.isVisible || !mesh.isPickable) {
  1585. continue;
  1586. }
  1587. var world = mesh.getWorldMatrix();
  1588. var ray = rayFunction(world);
  1589. var result = mesh.intersects(ray, fastCheck);
  1590. if (!result || !result.hit)
  1591. continue;
  1592. if (!fastCheck && pickingInfo != null && result.distance >= pickingInfo.distance)
  1593. continue;
  1594. pickingInfo = result;
  1595. if (fastCheck) {
  1596. break;
  1597. }
  1598. }
  1599. return pickingInfo || new BABYLON.PickingInfo();
  1600. };
  1601. Scene.prototype.pick = function (x, y, predicate, fastCheck, camera) {
  1602. var _this = this;
  1603. /// <summary>Launch a ray to try to pick a mesh in the scene</summary>
  1604. /// <param name="x">X position on screen</param>
  1605. /// <param name="y">Y position on screen</param>
  1606. /// <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>
  1607. /// <param name="fastCheck">Launch a fast check only using the bounding boxes. Can be set to null.</param>
  1608. /// <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>
  1609. return this._internalPick(function (world) { return _this.createPickingRay(x, y, world, camera); }, predicate, fastCheck);
  1610. };
  1611. Scene.prototype.pickWithRay = function (ray, predicate, fastCheck) {
  1612. var _this = this;
  1613. return this._internalPick(function (world) {
  1614. if (!_this._pickWithRayInverseMatrix) {
  1615. _this._pickWithRayInverseMatrix = BABYLON.Matrix.Identity();
  1616. }
  1617. world.invertToRef(_this._pickWithRayInverseMatrix);
  1618. return BABYLON.Ray.Transform(ray, _this._pickWithRayInverseMatrix);
  1619. }, predicate, fastCheck);
  1620. };
  1621. Scene.prototype.setPointerOverMesh = function (mesh) {
  1622. if (this._pointerOverMesh === mesh) {
  1623. return;
  1624. }
  1625. if (this._pointerOverMesh && this._pointerOverMesh.actionManager) {
  1626. this._pointerOverMesh.actionManager.processTrigger(BABYLON.ActionManager.OnPointerOutTrigger, BABYLON.ActionEvent.CreateNew(this._pointerOverMesh));
  1627. }
  1628. this._pointerOverMesh = mesh;
  1629. if (this._pointerOverMesh && this._pointerOverMesh.actionManager) {
  1630. this._pointerOverMesh.actionManager.processTrigger(BABYLON.ActionManager.OnPointerOverTrigger, BABYLON.ActionEvent.CreateNew(this._pointerOverMesh));
  1631. }
  1632. };
  1633. Scene.prototype.getPointerOverMesh = function () {
  1634. return this._pointerOverMesh;
  1635. };
  1636. // Physics
  1637. Scene.prototype.getPhysicsEngine = function () {
  1638. return this._physicsEngine;
  1639. };
  1640. Scene.prototype.enablePhysics = function (gravity, plugin) {
  1641. if (this._physicsEngine) {
  1642. return true;
  1643. }
  1644. this._physicsEngine = new BABYLON.PhysicsEngine(plugin);
  1645. if (!this._physicsEngine.isSupported()) {
  1646. this._physicsEngine = null;
  1647. return false;
  1648. }
  1649. this._physicsEngine._initialize(gravity);
  1650. return true;
  1651. };
  1652. Scene.prototype.disablePhysicsEngine = function () {
  1653. if (!this._physicsEngine) {
  1654. return;
  1655. }
  1656. this._physicsEngine.dispose();
  1657. this._physicsEngine = undefined;
  1658. };
  1659. Scene.prototype.isPhysicsEnabled = function () {
  1660. return this._physicsEngine !== undefined;
  1661. };
  1662. Scene.prototype.setGravity = function (gravity) {
  1663. if (!this._physicsEngine) {
  1664. return;
  1665. }
  1666. this._physicsEngine._setGravity(gravity);
  1667. };
  1668. Scene.prototype.createCompoundImpostor = function (parts, options) {
  1669. if (parts.parts) {
  1670. options = parts;
  1671. parts = parts.parts;
  1672. }
  1673. if (!this._physicsEngine) {
  1674. return null;
  1675. }
  1676. for (var index = 0; index < parts.length; index++) {
  1677. var mesh = parts[index].mesh;
  1678. mesh._physicImpostor = parts[index].impostor;
  1679. mesh._physicsMass = options.mass / parts.length;
  1680. mesh._physicsFriction = options.friction;
  1681. mesh._physicRestitution = options.restitution;
  1682. }
  1683. return this._physicsEngine._registerMeshesAsCompound(parts, options);
  1684. };
  1685. Scene.prototype.deleteCompoundImpostor = function (compound) {
  1686. for (var index = 0; index < compound.parts.length; index++) {
  1687. var mesh = compound.parts[index].mesh;
  1688. mesh._physicImpostor = BABYLON.PhysicsEngine.NoImpostor;
  1689. this._physicsEngine._unregisterMesh(mesh);
  1690. }
  1691. };
  1692. // Misc.
  1693. Scene.prototype.createDefaultCameraOrLight = function () {
  1694. // Light
  1695. if (this.lights.length === 0) {
  1696. new BABYLON.HemisphericLight("default light", BABYLON.Vector3.Up(), this);
  1697. }
  1698. // Camera
  1699. if (!this.activeCamera) {
  1700. var camera = new BABYLON.FreeCamera("default camera", BABYLON.Vector3.Zero(), this);
  1701. // Compute position
  1702. var worldExtends = this.getWorldExtends();
  1703. var worldCenter = worldExtends.min.add(worldExtends.max.subtract(worldExtends.min).scale(0.5));
  1704. camera.position = new BABYLON.Vector3(worldCenter.x, worldCenter.y, worldExtends.min.z - (worldExtends.max.z - worldExtends.min.z));
  1705. camera.setTarget(worldCenter);
  1706. this.activeCamera = camera;
  1707. }
  1708. };
  1709. // Tags
  1710. Scene.prototype._getByTags = function (list, tagsQuery, forEach) {
  1711. if (tagsQuery === undefined) {
  1712. // returns the complete list (could be done with BABYLON.Tags.MatchesQuery but no need to have a for-loop here)
  1713. return list;
  1714. }
  1715. var listByTags = [];
  1716. forEach = forEach || (function (item) { return; });
  1717. for (var i in list) {
  1718. var item = list[i];
  1719. if (BABYLON.Tags.MatchesQuery(item, tagsQuery)) {
  1720. listByTags.push(item);
  1721. forEach(item);
  1722. }
  1723. }
  1724. return listByTags;
  1725. };
  1726. Scene.prototype.getMeshesByTags = function (tagsQuery, forEach) {
  1727. return this._getByTags(this.meshes, tagsQuery, forEach);
  1728. };
  1729. Scene.prototype.getCamerasByTags = function (tagsQuery, forEach) {
  1730. return this._getByTags(this.cameras, tagsQuery, forEach);
  1731. };
  1732. Scene.prototype.getLightsByTags = function (tagsQuery, forEach) {
  1733. return this._getByTags(this.lights, tagsQuery, forEach);
  1734. };
  1735. Scene.prototype.getMaterialByTags = function (tagsQuery, forEach) {
  1736. return this._getByTags(this.materials, tagsQuery, forEach).concat(this._getByTags(this.multiMaterials, tagsQuery, forEach));
  1737. };
  1738. // Statics
  1739. Scene._FOGMODE_NONE = 0;
  1740. Scene._FOGMODE_EXP = 1;
  1741. Scene._FOGMODE_EXP2 = 2;
  1742. Scene._FOGMODE_LINEAR = 3;
  1743. Scene.MinDeltaTime = 1.0;
  1744. Scene.MaxDeltaTime = 1000.0;
  1745. return Scene;
  1746. })();
  1747. BABYLON.Scene = Scene;
  1748. })(BABYLON || (BABYLON = {}));
  1749. //# sourceMappingURL=babylon.scene.js.map