babylon.scene.js 97 KB

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