babylon.scene.js 97 KB

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