babylon.scene.js 115 KB

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