babylon.scene.js 98 KB

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