babylon.scene.ts 71 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019
  1. module BABYLON {
  2. export interface IDisposable {
  3. dispose(): void;
  4. }
  5. /**
  6. * Represents a scene to be rendered by the engine.
  7. * @see http://doc.babylonjs.com/page.php?p=21911
  8. */
  9. export class Scene {
  10. // Statics
  11. private static _FOGMODE_NONE = 0;
  12. private static _FOGMODE_EXP = 1;
  13. private static _FOGMODE_EXP2 = 2;
  14. private static _FOGMODE_LINEAR = 3;
  15. public static MinDeltaTime = 1.0;
  16. public static MaxDeltaTime = 1000.0;
  17. public static get FOGMODE_NONE(): number {
  18. return Scene._FOGMODE_NONE;
  19. }
  20. public static get FOGMODE_EXP(): number {
  21. return Scene._FOGMODE_EXP;
  22. }
  23. public static get FOGMODE_EXP2(): number {
  24. return Scene._FOGMODE_EXP2;
  25. }
  26. public static get FOGMODE_LINEAR(): number {
  27. return Scene._FOGMODE_LINEAR;
  28. }
  29. // Members
  30. public autoClear = true;
  31. public clearColor: any = new Color3(0.2, 0.2, 0.3);
  32. public ambientColor = new Color3(0, 0, 0);
  33. /**
  34. * A function to be executed before rendering this scene
  35. * @type {Function}
  36. */
  37. public beforeRender: () => void;
  38. /**
  39. * A function to be executed after rendering this scene
  40. * @type {Function}
  41. */
  42. public afterRender: () => void;
  43. /**
  44. * A function to be executed when this scene is disposed.
  45. * @type {Function}
  46. */
  47. public onDispose: () => void;
  48. public beforeCameraRender: (camera: Camera) => void;
  49. public afterCameraRender: (camera: Camera) => void;
  50. public forceWireframe = false;
  51. public forcePointsCloud = false;
  52. public forceShowBoundingBoxes = false;
  53. public clipPlane: Plane;
  54. public animationsEnabled = true;
  55. // Pointers
  56. private _onPointerMove: (evt: PointerEvent) => void;
  57. private _onPointerDown: (evt: PointerEvent) => void;
  58. public onPointerDown: (evt: PointerEvent, pickInfo: PickingInfo) => void;
  59. public cameraToUseForPointers: Camera = null; // Define this parameter if you are using multiple cameras and you want to specify which one should be used for pointer position
  60. private _pointerX: number;
  61. private _pointerY: number;
  62. private _meshUnderPointer: AbstractMesh;
  63. // Keyboard
  64. private _onKeyDown: (evt: Event) => void;
  65. private _onKeyUp: (evt: Event) => void;
  66. // Fog
  67. /**
  68. * is fog enabled on this scene.
  69. * @type {boolean}
  70. */
  71. public fogEnabled = true;
  72. public fogMode = Scene.FOGMODE_NONE;
  73. public fogColor = new Color3(0.2, 0.2, 0.3);
  74. public fogDensity = 0.1;
  75. public fogStart = 0;
  76. public fogEnd = 1000.0;
  77. // Lights
  78. /**
  79. * is shadow enabled on this scene.
  80. * @type {boolean}
  81. */
  82. public shadowsEnabled = true;
  83. /**
  84. * is light enabled on this scene.
  85. * @type {boolean}
  86. */
  87. public lightsEnabled = true;
  88. /**
  89. * All of the lights added to this scene.
  90. * @see BABYLON.Light
  91. * @type {BABYLON.Light[]}
  92. */
  93. public lights = new Array<Light>();
  94. // Cameras
  95. /**
  96. * All of the cameras added to this scene.
  97. * @see BABYLON.Camera
  98. * @type {BABYLON.Camera[]}
  99. */
  100. public cameras = new Array<Camera>();
  101. public activeCameras = new Array<Camera>();
  102. public activeCamera: Camera;
  103. // Meshes
  104. /**
  105. * All of the (abstract) meshes added to this scene.
  106. * @see BABYLON.AbstractMesh
  107. * @type {BABYLON.AbstractMesh[]}
  108. */
  109. public meshes = new Array<AbstractMesh>();
  110. // Geometries
  111. private _geometries = new Array<Geometry>();
  112. public materials = new Array<Material>();
  113. public multiMaterials = new Array<MultiMaterial>();
  114. public defaultMaterial = new StandardMaterial("default material", this);
  115. // Textures
  116. public texturesEnabled = true;
  117. public textures = new Array<BaseTexture>();
  118. // Particles
  119. public particlesEnabled = true;
  120. public particleSystems = new Array<ParticleSystem>();
  121. // Sprites
  122. public spritesEnabled = true;
  123. public spriteManagers = new Array<SpriteManager>();
  124. // Layers
  125. public layers = new Array<Layer>();
  126. // Skeletons
  127. public skeletonsEnabled = true;
  128. public skeletons = new Array<Skeleton>();
  129. // Lens flares
  130. public lensFlaresEnabled = true;
  131. public lensFlareSystems = new Array<LensFlareSystem>();
  132. // Collisions
  133. public collisionsEnabled = true;
  134. public gravity = new Vector3(0, -9.0, 0);
  135. // Postprocesses
  136. public postProcessesEnabled = true;
  137. public postProcessManager: PostProcessManager;
  138. public postProcessRenderPipelineManager: PostProcessRenderPipelineManager;
  139. // Customs render targets
  140. public renderTargetsEnabled = true;
  141. public dumpNextRenderTargets = false;
  142. public customRenderTargets = new Array<RenderTargetTexture>();
  143. // Delay loading
  144. public useDelayedTextureLoading: boolean;
  145. // Imported meshes
  146. public importedMeshesFiles = new Array<String>();
  147. // Database
  148. public database; //ANY
  149. // Actions
  150. /**
  151. * This scene's action manager
  152. * @type {BABYLON.ActionManager}
  153. */
  154. public actionManager: ActionManager;
  155. public _actionManagers = new Array<ActionManager>();
  156. private _meshesForIntersections = new SmartArray<AbstractMesh>(256);
  157. // Procedural textures
  158. public proceduralTexturesEnabled = true;
  159. public _proceduralTextures = new Array<ProceduralTexture>();
  160. // Sound Tracks
  161. public mainSoundTrack: SoundTrack;
  162. public soundTracks = new Array<SoundTrack>();
  163. private _audioEnabled = true;
  164. //Simplification Queue
  165. public simplificationQueue: SimplificationQueue;
  166. // Private
  167. private _engine: Engine;
  168. private _totalVertices = 0;
  169. public _activeVertices = 0;
  170. public _activeParticles = 0;
  171. private _lastFrameDuration = 0;
  172. private _evaluateActiveMeshesDuration = 0;
  173. private _renderTargetsDuration = 0;
  174. public _particlesDuration = 0;
  175. private _renderDuration = 0;
  176. public _spritesDuration = 0;
  177. private _animationRatio = 0;
  178. private _animationStartDate: number;
  179. public _cachedMaterial: Material;
  180. private _renderId = 0;
  181. private _executeWhenReadyTimeoutId = -1;
  182. public _toBeDisposed = new SmartArray<IDisposable>(256);
  183. private _onReadyCallbacks = new Array<() => void>();
  184. private _pendingData = [];//ANY
  185. private _onBeforeRenderCallbacks = new Array<() => void>();
  186. private _onAfterRenderCallbacks = new Array<() => void>();
  187. private _activeMeshes = new SmartArray<Mesh>(256);
  188. private _processedMaterials = new SmartArray<Material>(256);
  189. private _renderTargets = new SmartArray<RenderTargetTexture>(256);
  190. public _activeParticleSystems = new SmartArray<ParticleSystem>(256);
  191. private _activeSkeletons = new SmartArray<Skeleton>(32);
  192. public _activeBones = 0;
  193. private _renderingManager: RenderingManager;
  194. private _physicsEngine: PhysicsEngine;
  195. public _activeAnimatables = new Array<Animatable>();
  196. private _transformMatrix = Matrix.Zero();
  197. private _pickWithRayInverseMatrix: Matrix;
  198. private _scaledPosition = Vector3.Zero();
  199. private _scaledVelocity = Vector3.Zero();
  200. private _boundingBoxRenderer: BoundingBoxRenderer;
  201. private _outlineRenderer: OutlineRenderer;
  202. private _viewMatrix: Matrix;
  203. private _projectionMatrix: Matrix;
  204. private _frustumPlanes: Plane[];
  205. private _selectionOctree: Octree<AbstractMesh>;
  206. private _pointerOverMesh: AbstractMesh;
  207. private _debugLayer: DebugLayer;
  208. private _depthRenderer: DepthRenderer;
  209. /**
  210. * @constructor
  211. * @param {BABYLON.Engine} engine - the engine to be used to render this scene.
  212. */
  213. constructor(engine: Engine) {
  214. this._engine = engine;
  215. engine.scenes.push(this);
  216. this._renderingManager = new RenderingManager(this);
  217. this.postProcessManager = new PostProcessManager(this);
  218. this.postProcessRenderPipelineManager = new PostProcessRenderPipelineManager();
  219. this._boundingBoxRenderer = new BoundingBoxRenderer(this);
  220. this._outlineRenderer = new OutlineRenderer(this);
  221. this.attachControl();
  222. this._debugLayer = new DebugLayer(this);
  223. this.mainSoundTrack = new SoundTrack(this, { mainTrack: true });
  224. //simplification queue
  225. this.simplificationQueue = new SimplificationQueue();
  226. }
  227. // Properties
  228. public get debugLayer(): DebugLayer {
  229. return this._debugLayer;
  230. }
  231. /**
  232. * The mesh that is currently under the pointer.
  233. * @return {BABYLON.AbstractMesh} mesh under the pointer/mouse cursor or null if none.
  234. */
  235. public get meshUnderPointer(): AbstractMesh {
  236. return this._meshUnderPointer;
  237. }
  238. /**
  239. * Current on-screen X position of the pointer
  240. * @return {number} X position of the pointer
  241. */
  242. public get pointerX(): number {
  243. return this._pointerX;
  244. }
  245. /**
  246. * Current on-screen Y position of the pointer
  247. * @return {number} Y position of the pointer
  248. */
  249. public get pointerY(): number {
  250. return this._pointerY;
  251. }
  252. public getCachedMaterial(): Material {
  253. return this._cachedMaterial;
  254. }
  255. public getBoundingBoxRenderer(): BoundingBoxRenderer {
  256. return this._boundingBoxRenderer;
  257. }
  258. public getOutlineRenderer(): OutlineRenderer {
  259. return this._outlineRenderer;
  260. }
  261. public getEngine(): Engine {
  262. return this._engine;
  263. }
  264. public getTotalVertices(): number {
  265. return this._totalVertices;
  266. }
  267. public getActiveVertices(): number {
  268. return this._activeVertices;
  269. }
  270. public getActiveParticles(): number {
  271. return this._activeParticles;
  272. }
  273. public getActiveBones(): number {
  274. return this._activeBones;
  275. }
  276. // Stats
  277. public getLastFrameDuration(): number {
  278. return this._lastFrameDuration;
  279. }
  280. public getEvaluateActiveMeshesDuration(): number {
  281. return this._evaluateActiveMeshesDuration;
  282. }
  283. public getActiveMeshes(): SmartArray<Mesh> {
  284. return this._activeMeshes;
  285. }
  286. public getRenderTargetsDuration(): number {
  287. return this._renderTargetsDuration;
  288. }
  289. public getRenderDuration(): number {
  290. return this._renderDuration;
  291. }
  292. public getParticlesDuration(): number {
  293. return this._particlesDuration;
  294. }
  295. public getSpritesDuration(): number {
  296. return this._spritesDuration;
  297. }
  298. public getAnimationRatio(): number {
  299. return this._animationRatio;
  300. }
  301. public getRenderId(): number {
  302. return this._renderId;
  303. }
  304. public incrementRenderId(): void {
  305. this._renderId++;
  306. }
  307. private _updatePointerPosition(evt: PointerEvent): void {
  308. var canvasRect = this._engine.getRenderingCanvasClientRect();
  309. this._pointerX = evt.clientX - canvasRect.left;
  310. this._pointerY = evt.clientY - canvasRect.top;
  311. if (this.cameraToUseForPointers) {
  312. this._pointerX = this._pointerX - this.cameraToUseForPointers.viewport.x * this._engine.getRenderWidth();
  313. this._pointerY = this._pointerY - this.cameraToUseForPointers.viewport.y * this._engine.getRenderHeight();
  314. }
  315. }
  316. // Pointers handling
  317. public attachControl() {
  318. this._onPointerMove = (evt: PointerEvent) => {
  319. var canvas = this._engine.getRenderingCanvas();
  320. this._updatePointerPosition(evt);
  321. var pickResult = this.pick(this._pointerX, this._pointerY,
  322. (mesh: AbstractMesh): boolean => mesh.isPickable && mesh.isVisible && mesh.isReady() && mesh.actionManager && mesh.actionManager.hasPointerTriggers,
  323. false,
  324. this.cameraToUseForPointers);
  325. if (pickResult.hit) {
  326. this._meshUnderPointer = pickResult.pickedMesh;
  327. this.setPointerOverMesh(pickResult.pickedMesh);
  328. canvas.style.cursor = "pointer";
  329. } else {
  330. this.setPointerOverMesh(null);
  331. canvas.style.cursor = "";
  332. this._meshUnderPointer = null;
  333. }
  334. };
  335. this._onPointerDown = (evt: PointerEvent) => {
  336. var predicate = null;
  337. if (!this.onPointerDown) {
  338. predicate = (mesh: AbstractMesh): boolean => {
  339. return mesh.isPickable && mesh.isVisible && mesh.isReady() && mesh.actionManager && mesh.actionManager.hasPickTriggers;
  340. };
  341. }
  342. this._updatePointerPosition(evt);
  343. var pickResult = this.pick(this._pointerX, this._pointerY, predicate, false, this.cameraToUseForPointers);
  344. if (pickResult.hit) {
  345. if (pickResult.pickedMesh.actionManager) {
  346. switch (evt.button) {
  347. case 0:
  348. pickResult.pickedMesh.actionManager.processTrigger(ActionManager.OnLeftPickTrigger, ActionEvent.CreateNew(pickResult.pickedMesh, evt));
  349. break;
  350. case 1:
  351. pickResult.pickedMesh.actionManager.processTrigger(ActionManager.OnCenterPickTrigger, ActionEvent.CreateNew(pickResult.pickedMesh, evt));
  352. break;
  353. case 2:
  354. pickResult.pickedMesh.actionManager.processTrigger(ActionManager.OnRightPickTrigger, ActionEvent.CreateNew(pickResult.pickedMesh, evt));
  355. break;
  356. }
  357. pickResult.pickedMesh.actionManager.processTrigger(ActionManager.OnPickTrigger, ActionEvent.CreateNew(pickResult.pickedMesh, evt));
  358. }
  359. }
  360. if (this.onPointerDown) {
  361. this.onPointerDown(evt, pickResult);
  362. }
  363. };
  364. this._onKeyDown = (evt: Event) => {
  365. if (this.actionManager) {
  366. this.actionManager.processTrigger(ActionManager.OnKeyDownTrigger, ActionEvent.CreateNewFromScene(this, evt));
  367. }
  368. };
  369. this._onKeyUp = (evt: Event) => {
  370. if (this.actionManager) {
  371. this.actionManager.processTrigger(ActionManager.OnKeyUpTrigger, ActionEvent.CreateNewFromScene(this, evt));
  372. }
  373. };
  374. var eventPrefix = Tools.GetPointerPrefix();
  375. this._engine.getRenderingCanvas().addEventListener(eventPrefix + "move", this._onPointerMove, false);
  376. this._engine.getRenderingCanvas().addEventListener(eventPrefix + "down", this._onPointerDown, false);
  377. Tools.RegisterTopRootEvents([
  378. { name: "keydown", handler: this._onKeyDown },
  379. { name: "keyup", handler: this._onKeyUp }
  380. ]);
  381. }
  382. public detachControl() {
  383. var eventPrefix = Tools.GetPointerPrefix();
  384. this._engine.getRenderingCanvas().removeEventListener(eventPrefix + "move", this._onPointerMove);
  385. this._engine.getRenderingCanvas().removeEventListener(eventPrefix + "down", this._onPointerDown);
  386. Tools.UnregisterTopRootEvents([
  387. { name: "keydown", handler: this._onKeyDown },
  388. { name: "keyup", handler: this._onKeyUp }
  389. ]);
  390. }
  391. // Ready
  392. public isReady(): boolean {
  393. if (this._pendingData.length > 0) {
  394. return false;
  395. }
  396. for (var index = 0; index < this._geometries.length; index++) {
  397. var geometry = this._geometries[index];
  398. if (geometry.delayLoadState === Engine.DELAYLOADSTATE_LOADING) {
  399. return false;
  400. }
  401. }
  402. for (index = 0; index < this.meshes.length; index++) {
  403. var mesh = this.meshes[index];
  404. if (!mesh.isReady()) {
  405. return false;
  406. }
  407. var mat = mesh.material;
  408. if (mat) {
  409. if (!mat.isReady(mesh)) {
  410. return false;
  411. }
  412. }
  413. }
  414. return true;
  415. }
  416. public resetCachedMaterial(): void {
  417. this._cachedMaterial = null;
  418. }
  419. public registerBeforeRender(func: () => void): void {
  420. this._onBeforeRenderCallbacks.push(func);
  421. }
  422. public unregisterBeforeRender(func: () => void): void {
  423. var index = this._onBeforeRenderCallbacks.indexOf(func);
  424. if (index > -1) {
  425. this._onBeforeRenderCallbacks.splice(index, 1);
  426. }
  427. }
  428. public registerAfterRender(func: () => void): void {
  429. this._onAfterRenderCallbacks.push(func);
  430. }
  431. public unregisterAfterRender(func: () => void): void {
  432. var index = this._onAfterRenderCallbacks.indexOf(func);
  433. if (index > -1) {
  434. this._onAfterRenderCallbacks.splice(index, 1);
  435. }
  436. }
  437. public _addPendingData(data): void {
  438. this._pendingData.push(data);
  439. }
  440. public _removePendingData(data): void {
  441. var index = this._pendingData.indexOf(data);
  442. if (index !== -1) {
  443. this._pendingData.splice(index, 1);
  444. }
  445. }
  446. public getWaitingItemsCount(): number {
  447. return this._pendingData.length;
  448. }
  449. /**
  450. * Registers a function to be executed when the scene is ready.
  451. * @param {Function} func - the function to be executed.
  452. */
  453. public executeWhenReady(func: () => void): void {
  454. this._onReadyCallbacks.push(func);
  455. if (this._executeWhenReadyTimeoutId !== -1) {
  456. return;
  457. }
  458. this._executeWhenReadyTimeoutId = setTimeout(() => {
  459. this._checkIsReady();
  460. }, 150);
  461. }
  462. public _checkIsReady() {
  463. if (this.isReady()) {
  464. this._onReadyCallbacks.forEach(func => {
  465. func();
  466. });
  467. this._onReadyCallbacks = [];
  468. this._executeWhenReadyTimeoutId = -1;
  469. return;
  470. }
  471. this._executeWhenReadyTimeoutId = setTimeout(() => {
  472. this._checkIsReady();
  473. }, 150);
  474. }
  475. // Animations
  476. /**
  477. * Will start the animation sequence of a given target
  478. * @param target - the target
  479. * @param {number} from - from which frame should animation start
  480. * @param {number} to - till which frame should animation run.
  481. * @param {boolean} [loop] - should the animation loop
  482. * @param {number} [speedRatio] - the speed in which to run the animation
  483. * @param {Function} [onAnimationEnd] function to be executed when the animation ended.
  484. * @param {BABYLON.Animatable} [animatable] an animatable object. If not provided a new one will be created from the given params.
  485. * @return {BABYLON.Animatable} the animatable object created for this animation
  486. * @see BABYLON.Animatable
  487. * @see http://doc.babylonjs.com/page.php?p=22081
  488. */
  489. public beginAnimation(target: any, from: number, to: number, loop?: boolean, speedRatio?: number, onAnimationEnd?: () => void, animatable?: Animatable): Animatable {
  490. if (speedRatio === undefined) {
  491. speedRatio = 1.0;
  492. }
  493. this.stopAnimation(target);
  494. if (!animatable) {
  495. animatable = new Animatable(this, target, from, to, loop, speedRatio, onAnimationEnd);
  496. }
  497. // Local animations
  498. if (target.animations) {
  499. animatable.appendAnimations(target, target.animations);
  500. }
  501. // Children animations
  502. if (target.getAnimatables) {
  503. var animatables = target.getAnimatables();
  504. for (var index = 0; index < animatables.length; index++) {
  505. this.beginAnimation(animatables[index], from, to, loop, speedRatio, onAnimationEnd, animatable);
  506. }
  507. }
  508. return animatable;
  509. }
  510. public beginDirectAnimation(target: any, animations: Animation[], from: number, to: number, loop?: boolean, speedRatio?: number, onAnimationEnd?: () => void): Animatable {
  511. if (speedRatio === undefined) {
  512. speedRatio = 1.0;
  513. }
  514. var animatable = new Animatable(this, target, from, to, loop, speedRatio, onAnimationEnd, animations);
  515. return animatable;
  516. }
  517. public getAnimatableByTarget(target: any): Animatable {
  518. for (var index = 0; index < this._activeAnimatables.length; index++) {
  519. if (this._activeAnimatables[index].target === target) {
  520. return this._activeAnimatables[index];
  521. }
  522. }
  523. return null;
  524. }
  525. /**
  526. * Will stop the animation of the given target
  527. * @param target - the target
  528. * @see beginAnimation
  529. */
  530. public stopAnimation(target: any): void {
  531. var animatable = this.getAnimatableByTarget(target);
  532. if (animatable) {
  533. animatable.stop();
  534. }
  535. }
  536. private _animate(): void {
  537. if (!this.animationsEnabled) {
  538. return;
  539. }
  540. if (!this._animationStartDate) {
  541. this._animationStartDate = Tools.Now;
  542. }
  543. // Getting time
  544. var now = Tools.Now;
  545. var delay = now - this._animationStartDate;
  546. for (var index = 0; index < this._activeAnimatables.length; index++) {
  547. if (!this._activeAnimatables[index]._animate(delay)) {
  548. this._activeAnimatables.splice(index, 1);
  549. index--;
  550. }
  551. }
  552. }
  553. // Matrix
  554. public getViewMatrix(): Matrix {
  555. return this._viewMatrix;
  556. }
  557. public getProjectionMatrix(): Matrix {
  558. return this._projectionMatrix;
  559. }
  560. public getTransformMatrix(): Matrix {
  561. return this._transformMatrix;
  562. }
  563. public setTransformMatrix(view: Matrix, projection: Matrix): void {
  564. this._viewMatrix = view;
  565. this._projectionMatrix = projection;
  566. this._viewMatrix.multiplyToRef(this._projectionMatrix, this._transformMatrix);
  567. }
  568. // Methods
  569. /**
  570. * sets the active camera of the scene using its ID
  571. * @param {string} id - the camera's ID
  572. * @return {BABYLON.Camera|null} the new active camera or null if none found.
  573. * @see activeCamera
  574. */
  575. public setActiveCameraByID(id: string): Camera {
  576. var camera = this.getCameraByID(id);
  577. if (camera) {
  578. this.activeCamera = camera;
  579. return camera;
  580. }
  581. return null;
  582. }
  583. /**
  584. * sets the active camera of the scene using its name
  585. * @param {string} name - the camera's name
  586. * @return {BABYLON.Camera|null} the new active camera or null if none found.
  587. * @see activeCamera
  588. */
  589. public setActiveCameraByName(name: string): Camera {
  590. var camera = this.getCameraByName(name);
  591. if (camera) {
  592. this.activeCamera = camera;
  593. return camera;
  594. }
  595. return null;
  596. }
  597. /**
  598. * get a material using its id
  599. * @param {string} the material's ID
  600. * @return {BABYLON.Material|null} the material or null if none found.
  601. */
  602. public getMaterialByID(id: string): Material {
  603. for (var index = 0; index < this.materials.length; index++) {
  604. if (this.materials[index].id === id) {
  605. return this.materials[index];
  606. }
  607. }
  608. return null;
  609. }
  610. /**
  611. * get a material using its name
  612. * @param {string} the material's name
  613. * @return {BABYLON.Material|null} the material or null if none found.
  614. */
  615. public getMaterialByName(name: string): Material {
  616. for (var index = 0; index < this.materials.length; index++) {
  617. if (this.materials[index].name === name) {
  618. return this.materials[index];
  619. }
  620. }
  621. return null;
  622. }
  623. public getCameraByID(id: string): Camera {
  624. for (var index = 0; index < this.cameras.length; index++) {
  625. if (this.cameras[index].id === id) {
  626. return this.cameras[index];
  627. }
  628. }
  629. return null;
  630. }
  631. /**
  632. * get a camera using its name
  633. * @param {string} the camera's name
  634. * @return {BABYLON.Camera|null} the camera or null if none found.
  635. */
  636. public getCameraByName(name: string): Camera {
  637. for (var index = 0; index < this.cameras.length; index++) {
  638. if (this.cameras[index].name === name) {
  639. return this.cameras[index];
  640. }
  641. }
  642. return null;
  643. }
  644. /**
  645. * get a light node using its name
  646. * @param {string} the light's name
  647. * @return {BABYLON.Light|null} the light or null if none found.
  648. */
  649. public getLightByName(name: string): Light {
  650. for (var index = 0; index < this.lights.length; index++) {
  651. if (this.lights[index].name === name) {
  652. return this.lights[index];
  653. }
  654. }
  655. return null;
  656. }
  657. /**
  658. * get a light node using its ID
  659. * @param {string} the light's id
  660. * @return {BABYLON.Light|null} the light or null if none found.
  661. */
  662. public getLightByID(id: string): Light {
  663. for (var index = 0; index < this.lights.length; index++) {
  664. if (this.lights[index].id === id) {
  665. return this.lights[index];
  666. }
  667. }
  668. return null;
  669. }
  670. /**
  671. * get a geometry using its ID
  672. * @param {string} the geometry's id
  673. * @return {BABYLON.Geometry|null} the geometry or null if none found.
  674. */
  675. public getGeometryByID(id: string): Geometry {
  676. for (var index = 0; index < this._geometries.length; index++) {
  677. if (this._geometries[index].id === id) {
  678. return this._geometries[index];
  679. }
  680. }
  681. return null;
  682. }
  683. /**
  684. * add a new geometry to this scene.
  685. * @param {BABYLON.Geometry} geometry - the geometry to be added to the scene.
  686. * @param {boolean} [force] - force addition, even if a geometry with this ID already exists
  687. * @return {boolean} was the geometry added or not
  688. */
  689. public pushGeometry(geometry: Geometry, force?: boolean): boolean {
  690. if (!force && this.getGeometryByID(geometry.id)) {
  691. return false;
  692. }
  693. this._geometries.push(geometry);
  694. return true;
  695. }
  696. public getGeometries(): Geometry[] {
  697. return this._geometries;
  698. }
  699. /**
  700. * Get a the first added mesh found of a given ID
  701. * @param {string} id - the id to search for
  702. * @return {BABYLON.AbstractMesh|null} the mesh found or null if not found at all.
  703. */
  704. public getMeshByID(id: string): AbstractMesh {
  705. for (var index = 0; index < this.meshes.length; index++) {
  706. if (this.meshes[index].id === id) {
  707. return this.meshes[index];
  708. }
  709. }
  710. return null;
  711. }
  712. /**
  713. * Get a the last added mesh found of a given ID
  714. * @param {string} id - the id to search for
  715. * @return {BABYLON.AbstractMesh|null} the mesh found or null if not found at all.
  716. */
  717. public getLastMeshByID(id: string): AbstractMesh {
  718. for (var index = this.meshes.length - 1; index >= 0; index--) {
  719. if (this.meshes[index].id === id) {
  720. return this.meshes[index];
  721. }
  722. }
  723. return null;
  724. }
  725. /**
  726. * Get a the last added node (Mesh, Camera, Light) found of a given ID
  727. * @param {string} id - the id to search for
  728. * @return {BABYLON.Node|null} the node found or null if not found at all.
  729. */
  730. public getLastEntryByID(id: string): Node {
  731. for (var index = this.meshes.length - 1; index >= 0; index--) {
  732. if (this.meshes[index].id === id) {
  733. return this.meshes[index];
  734. }
  735. }
  736. for (index = this.cameras.length - 1; index >= 0; index--) {
  737. if (this.cameras[index].id === id) {
  738. return this.cameras[index];
  739. }
  740. }
  741. for (index = this.lights.length - 1; index >= 0; index--) {
  742. if (this.lights[index].id === id) {
  743. return this.lights[index];
  744. }
  745. }
  746. return null;
  747. }
  748. public getNodeByName(name: string): Node {
  749. var mesh = this.getMeshByName(name);
  750. if (mesh) {
  751. return mesh;
  752. }
  753. var light = this.getLightByName(name);
  754. if (light) {
  755. return light;
  756. }
  757. return this.getCameraByName(name);
  758. }
  759. public getMeshByName(name: string): AbstractMesh {
  760. for (var index = 0; index < this.meshes.length; index++) {
  761. if (this.meshes[index].name === name) {
  762. return this.meshes[index];
  763. }
  764. }
  765. return null;
  766. }
  767. public getSoundByName(name: string): Sound {
  768. for (var index = 0; index < this.mainSoundTrack.soundCollection.length; index++) {
  769. if (this.mainSoundTrack.soundCollection[index].name === name) {
  770. return this.mainSoundTrack.soundCollection[index];
  771. }
  772. }
  773. for (var sdIndex = 0; sdIndex < this.soundTracks.length; sdIndex++) {
  774. for (index = 0; index < this.soundTracks[sdIndex].soundCollection.length; index++) {
  775. if (this.soundTracks[sdIndex].soundCollection[index].name === name) {
  776. return this.soundTracks[sdIndex].soundCollection[index];
  777. }
  778. }
  779. }
  780. return null;
  781. }
  782. public getLastSkeletonByID(id: string): Skeleton {
  783. for (var index = this.skeletons.length - 1; index >= 0; index--) {
  784. if (this.skeletons[index].id === id) {
  785. return this.skeletons[index];
  786. }
  787. }
  788. return null;
  789. }
  790. public getSkeletonById(id: string): Skeleton {
  791. for (var index = 0; index < this.skeletons.length; index++) {
  792. if (this.skeletons[index].id === id) {
  793. return this.skeletons[index];
  794. }
  795. }
  796. return null;
  797. }
  798. public getSkeletonByName(name: string): Skeleton {
  799. for (var index = 0; index < this.skeletons.length; index++) {
  800. if (this.skeletons[index].name === name) {
  801. return this.skeletons[index];
  802. }
  803. }
  804. return null;
  805. }
  806. public isActiveMesh(mesh: Mesh): boolean {
  807. return (this._activeMeshes.indexOf(mesh) !== -1);
  808. }
  809. private _evaluateSubMesh(subMesh: SubMesh, mesh: AbstractMesh): void {
  810. if (mesh.subMeshes.length === 1 || subMesh.isInFrustum(this._frustumPlanes)) {
  811. var material = subMesh.getMaterial();
  812. if (mesh.showSubMeshesBoundingBox) {
  813. this._boundingBoxRenderer.renderList.push(subMesh.getBoundingInfo().boundingBox);
  814. }
  815. if (material) {
  816. // Render targets
  817. if (material.getRenderTargetTextures) {
  818. if (this._processedMaterials.indexOf(material) === -1) {
  819. this._processedMaterials.push(material);
  820. this._renderTargets.concat(material.getRenderTargetTextures());
  821. }
  822. }
  823. // Dispatch
  824. this._activeVertices += subMesh.indexCount;
  825. this._renderingManager.dispatch(subMesh);
  826. }
  827. }
  828. }
  829. private _evaluateActiveMeshes(): void {
  830. this.activeCamera._activeMeshes.reset();
  831. this._activeMeshes.reset();
  832. this._renderingManager.reset();
  833. this._processedMaterials.reset();
  834. this._activeParticleSystems.reset();
  835. this._activeSkeletons.reset();
  836. this._boundingBoxRenderer.reset();
  837. if (!this._frustumPlanes) {
  838. this._frustumPlanes = Frustum.GetPlanes(this._transformMatrix);
  839. } else {
  840. Frustum.GetPlanesToRef(this._transformMatrix, this._frustumPlanes);
  841. }
  842. // Meshes
  843. var meshes: AbstractMesh[];
  844. var len: number;
  845. if (this._selectionOctree) { // Octree
  846. var selection = this._selectionOctree.select(this._frustumPlanes);
  847. meshes = selection.data;
  848. len = selection.length;
  849. } else { // Full scene traversal
  850. len = this.meshes.length;
  851. meshes = this.meshes;
  852. }
  853. for (var meshIndex = 0; meshIndex < len; meshIndex++) {
  854. var mesh = meshes[meshIndex];
  855. if (mesh.isBlocked) {
  856. continue;
  857. }
  858. this._totalVertices += mesh.getTotalVertices();
  859. if (!mesh.isReady()) {
  860. continue;
  861. }
  862. mesh.computeWorldMatrix();
  863. // Intersections
  864. if (mesh.actionManager && mesh.actionManager.hasSpecificTriggers([ActionManager.OnIntersectionEnterTrigger, ActionManager.OnIntersectionExitTrigger])) {
  865. this._meshesForIntersections.pushNoDuplicate(mesh);
  866. }
  867. // Switch to current LOD
  868. var meshLOD = mesh.getLOD(this.activeCamera);
  869. if (!meshLOD) {
  870. continue;
  871. }
  872. mesh._preActivate();
  873. if (mesh.isEnabled() && mesh.isVisible && mesh.visibility > 0 && ((mesh.layerMask & this.activeCamera.layerMask) !== 0) && mesh.isInFrustum(this._frustumPlanes)) {
  874. this._activeMeshes.push(mesh);
  875. this.activeCamera._activeMeshes.push(mesh);
  876. mesh._activate(this._renderId);
  877. this._activeMesh(meshLOD);
  878. }
  879. }
  880. // Particle systems
  881. var beforeParticlesDate = Tools.Now;
  882. if (this.particlesEnabled) {
  883. Tools.StartPerformanceCounter("Particles", this.particleSystems.length > 0);
  884. for (var particleIndex = 0; particleIndex < this.particleSystems.length; particleIndex++) {
  885. var particleSystem = this.particleSystems[particleIndex];
  886. if (!particleSystem.isStarted()) {
  887. continue;
  888. }
  889. if (!particleSystem.emitter.position || (particleSystem.emitter && particleSystem.emitter.isEnabled())) {
  890. this._activeParticleSystems.push(particleSystem);
  891. particleSystem.animate();
  892. }
  893. }
  894. Tools.EndPerformanceCounter("Particles", this.particleSystems.length > 0);
  895. }
  896. this._particlesDuration += Tools.Now - beforeParticlesDate;
  897. }
  898. private _activeMesh(mesh: AbstractMesh): void {
  899. if (mesh.skeleton && this.skeletonsEnabled) {
  900. this._activeSkeletons.pushNoDuplicate(mesh.skeleton);
  901. }
  902. if (mesh.showBoundingBox || this.forceShowBoundingBoxes) {
  903. this._boundingBoxRenderer.renderList.push(mesh.getBoundingInfo().boundingBox);
  904. }
  905. if (mesh && mesh.subMeshes) {
  906. // Submeshes Octrees
  907. var len: number;
  908. var subMeshes: SubMesh[];
  909. if (mesh._submeshesOctree && mesh.useOctreeForRenderingSelection) {
  910. var intersections = mesh._submeshesOctree.select(this._frustumPlanes);
  911. len = intersections.length;
  912. subMeshes = intersections.data;
  913. } else {
  914. subMeshes = mesh.subMeshes;
  915. len = subMeshes.length;
  916. }
  917. for (var subIndex = 0; subIndex < len; subIndex++) {
  918. var subMesh = subMeshes[subIndex];
  919. this._evaluateSubMesh(subMesh, mesh);
  920. }
  921. }
  922. }
  923. public updateTransformMatrix(force?: boolean): void {
  924. this.setTransformMatrix(this.activeCamera.getViewMatrix(), this.activeCamera.getProjectionMatrix(force));
  925. }
  926. private _renderForCamera(camera: Camera): void {
  927. var engine = this._engine;
  928. this.activeCamera = camera;
  929. if (!this.activeCamera)
  930. throw new Error("Active camera not set");
  931. Tools.StartPerformanceCounter("Rendering camera " + this.activeCamera.name);
  932. // Viewport
  933. engine.setViewport(this.activeCamera.viewport);
  934. // Camera
  935. this._renderId++;
  936. this.updateTransformMatrix();
  937. if (this.beforeCameraRender) {
  938. this.beforeCameraRender(this.activeCamera);
  939. }
  940. // Meshes
  941. var beforeEvaluateActiveMeshesDate = Tools.Now;
  942. Tools.StartPerformanceCounter("Active meshes evaluation");
  943. this._evaluateActiveMeshes();
  944. this._evaluateActiveMeshesDuration += Tools.Now - beforeEvaluateActiveMeshesDate;
  945. Tools.EndPerformanceCounter("Active meshes evaluation");
  946. // Skeletons
  947. for (var skeletonIndex = 0; skeletonIndex < this._activeSkeletons.length; skeletonIndex++) {
  948. var skeleton = this._activeSkeletons.data[skeletonIndex];
  949. skeleton.prepare();
  950. }
  951. // Render targets
  952. var beforeRenderTargetDate = Tools.Now;
  953. if (this.renderTargetsEnabled) {
  954. Tools.StartPerformanceCounter("Render targets", this._renderTargets.length > 0);
  955. for (var renderIndex = 0; renderIndex < this._renderTargets.length; renderIndex++) {
  956. var renderTarget = this._renderTargets.data[renderIndex];
  957. if (renderTarget._shouldRender()) {
  958. this._renderId++;
  959. renderTarget.render(false, this.dumpNextRenderTargets);
  960. }
  961. }
  962. Tools.EndPerformanceCounter("Render targets", this._renderTargets.length > 0);
  963. this._renderId++;
  964. }
  965. if (this._renderTargets.length > 0) { // Restore back buffer
  966. engine.restoreDefaultFramebuffer();
  967. }
  968. this._renderTargetsDuration += Tools.Now - beforeRenderTargetDate;
  969. // Prepare Frame
  970. this.postProcessManager._prepareFrame();
  971. var beforeRenderDate = Tools.Now;
  972. // Backgrounds
  973. if (this.layers.length) {
  974. engine.setDepthBuffer(false);
  975. var layerIndex;
  976. var layer;
  977. for (layerIndex = 0; layerIndex < this.layers.length; layerIndex++) {
  978. layer = this.layers[layerIndex];
  979. if (layer.isBackground) {
  980. layer.render();
  981. }
  982. }
  983. engine.setDepthBuffer(true);
  984. }
  985. // Render
  986. Tools.StartPerformanceCounter("Main render");
  987. this._renderingManager.render(null, null, true, true);
  988. Tools.EndPerformanceCounter("Main render");
  989. // Bounding boxes
  990. this._boundingBoxRenderer.render();
  991. // Lens flares
  992. if (this.lensFlaresEnabled) {
  993. Tools.StartPerformanceCounter("Lens flares", this.lensFlareSystems.length > 0);
  994. for (var lensFlareSystemIndex = 0; lensFlareSystemIndex < this.lensFlareSystems.length; lensFlareSystemIndex++) {
  995. this.lensFlareSystems[lensFlareSystemIndex].render();
  996. }
  997. Tools.EndPerformanceCounter("Lens flares", this.lensFlareSystems.length > 0);
  998. }
  999. // Foregrounds
  1000. if (this.layers.length) {
  1001. engine.setDepthBuffer(false);
  1002. for (layerIndex = 0; layerIndex < this.layers.length; layerIndex++) {
  1003. layer = this.layers[layerIndex];
  1004. if (!layer.isBackground) {
  1005. layer.render();
  1006. }
  1007. }
  1008. engine.setDepthBuffer(true);
  1009. }
  1010. this._renderDuration += Tools.Now - beforeRenderDate;
  1011. // Finalize frame
  1012. this.postProcessManager._finalizeFrame(camera.isIntermediate);
  1013. // Update camera
  1014. this.activeCamera._updateFromScene();
  1015. // Reset some special arrays
  1016. this._renderTargets.reset();
  1017. if (this.afterCameraRender) {
  1018. this.afterCameraRender(this.activeCamera);
  1019. }
  1020. Tools.EndPerformanceCounter("Rendering camera " + this.activeCamera.name);
  1021. }
  1022. private _processSubCameras(camera: Camera): void {
  1023. if (camera.subCameras.length === 0) {
  1024. this._renderForCamera(camera);
  1025. return;
  1026. }
  1027. // Sub-cameras
  1028. for (var index = 0; index < camera.subCameras.length; index++) {
  1029. this._renderForCamera(camera.subCameras[index]);
  1030. }
  1031. this.activeCamera = camera;
  1032. this.setTransformMatrix(this.activeCamera.getViewMatrix(), this.activeCamera.getProjectionMatrix());
  1033. // Update camera
  1034. this.activeCamera._updateFromScene();
  1035. }
  1036. private _checkIntersections(): void {
  1037. for (var index = 0; index < this._meshesForIntersections.length; index++) {
  1038. var sourceMesh = this._meshesForIntersections.data[index];
  1039. for (var actionIndex = 0; actionIndex < sourceMesh.actionManager.actions.length; actionIndex++) {
  1040. var action = sourceMesh.actionManager.actions[actionIndex];
  1041. if (action.trigger === ActionManager.OnIntersectionEnterTrigger || action.trigger === ActionManager.OnIntersectionExitTrigger) {
  1042. var parameters = action.getTriggerParameter();
  1043. var otherMesh = parameters instanceof AbstractMesh ? parameters : parameters.mesh;
  1044. var areIntersecting = otherMesh.intersectsMesh(sourceMesh, parameters.usePreciseIntersection);
  1045. var currentIntersectionInProgress = sourceMesh._intersectionsInProgress.indexOf(otherMesh);
  1046. if (areIntersecting && currentIntersectionInProgress === -1) {
  1047. if (action.trigger === ActionManager.OnIntersectionEnterTrigger) {
  1048. action._executeCurrent(ActionEvent.CreateNew(sourceMesh));
  1049. sourceMesh._intersectionsInProgress.push(otherMesh);
  1050. } else if (action.trigger === ActionManager.OnIntersectionExitTrigger) {
  1051. sourceMesh._intersectionsInProgress.push(otherMesh);
  1052. }
  1053. } else if (!areIntersecting && currentIntersectionInProgress > -1 && action.trigger === ActionManager.OnIntersectionExitTrigger) {
  1054. action._executeCurrent(ActionEvent.CreateNew(sourceMesh));
  1055. var indexOfOther = sourceMesh._intersectionsInProgress.indexOf(otherMesh);
  1056. if (indexOfOther > -1) {
  1057. sourceMesh._intersectionsInProgress.splice(indexOfOther, 1);
  1058. }
  1059. }
  1060. }
  1061. }
  1062. }
  1063. }
  1064. public render(): void {
  1065. var startDate = Tools.Now;
  1066. this._particlesDuration = 0;
  1067. this._spritesDuration = 0;
  1068. this._activeParticles = 0;
  1069. this._renderDuration = 0;
  1070. this._renderTargetsDuration = 0;
  1071. this._evaluateActiveMeshesDuration = 0;
  1072. this._totalVertices = 0;
  1073. this._activeVertices = 0;
  1074. this._activeBones = 0;
  1075. this.getEngine().resetDrawCalls();
  1076. this._meshesForIntersections.reset();
  1077. this.resetCachedMaterial();
  1078. Tools.StartPerformanceCounter("Scene rendering");
  1079. // Actions
  1080. if (this.actionManager) {
  1081. this.actionManager.processTrigger(ActionManager.OnEveryFrameTrigger, null);
  1082. }
  1083. //Simplification Queue
  1084. if (!this.simplificationQueue.running) {
  1085. this.simplificationQueue.executeNext();
  1086. }
  1087. // Before render
  1088. if (this.beforeRender) {
  1089. this.beforeRender();
  1090. }
  1091. for (var callbackIndex = 0; callbackIndex < this._onBeforeRenderCallbacks.length; callbackIndex++) {
  1092. this._onBeforeRenderCallbacks[callbackIndex]();
  1093. }
  1094. // Animations
  1095. var deltaTime = Math.max(Scene.MinDeltaTime, Math.min(this._engine.getDeltaTime(), Scene.MaxDeltaTime));
  1096. this._animationRatio = deltaTime * (60.0 / 1000.0);
  1097. this._animate();
  1098. // Physics
  1099. if (this._physicsEngine) {
  1100. Tools.StartPerformanceCounter("Physics");
  1101. this._physicsEngine._runOneStep(deltaTime / 1000.0);
  1102. Tools.EndPerformanceCounter("Physics");
  1103. }
  1104. // Customs render targets
  1105. var beforeRenderTargetDate = Tools.Now;
  1106. var engine = this.getEngine();
  1107. var currentActiveCamera = this.activeCamera;
  1108. if (this.renderTargetsEnabled) {
  1109. Tools.StartPerformanceCounter("Custom render targets", this.customRenderTargets.length > 0);
  1110. for (var customIndex = 0; customIndex < this.customRenderTargets.length; customIndex++) {
  1111. var renderTarget = this.customRenderTargets[customIndex];
  1112. if (renderTarget._shouldRender()) {
  1113. this._renderId++;
  1114. this.activeCamera = renderTarget.activeCamera || this.activeCamera;
  1115. if (!this.activeCamera)
  1116. throw new Error("Active camera not set");
  1117. // Viewport
  1118. engine.setViewport(this.activeCamera.viewport);
  1119. // Camera
  1120. this.updateTransformMatrix();
  1121. renderTarget.render(false, this.dumpNextRenderTargets);
  1122. }
  1123. }
  1124. Tools.EndPerformanceCounter("Custom render targets", this.customRenderTargets.length > 0);
  1125. this._renderId++;
  1126. }
  1127. if (this.customRenderTargets.length > 0) { // Restore back buffer
  1128. engine.restoreDefaultFramebuffer();
  1129. }
  1130. this._renderTargetsDuration += Tools.Now - beforeRenderTargetDate;
  1131. this.activeCamera = currentActiveCamera;
  1132. // Procedural textures
  1133. if (this.proceduralTexturesEnabled) {
  1134. Tools.StartPerformanceCounter("Procedural textures", this._proceduralTextures.length > 0);
  1135. for (var proceduralIndex = 0; proceduralIndex < this._proceduralTextures.length; proceduralIndex++) {
  1136. var proceduralTexture = this._proceduralTextures[proceduralIndex];
  1137. if (proceduralTexture._shouldRender()) {
  1138. proceduralTexture.render();
  1139. }
  1140. }
  1141. Tools.EndPerformanceCounter("Procedural textures", this._proceduralTextures.length > 0);
  1142. }
  1143. // Clear
  1144. this._engine.clear(this.clearColor, this.autoClear || this.forceWireframe || this.forcePointsCloud, true);
  1145. // Shadows
  1146. if (this.shadowsEnabled) {
  1147. for (var lightIndex = 0; lightIndex < this.lights.length; lightIndex++) {
  1148. var light = this.lights[lightIndex];
  1149. var shadowGenerator = light.getShadowGenerator();
  1150. if (light.isEnabled() && shadowGenerator && shadowGenerator.getShadowMap().getScene().textures.indexOf(shadowGenerator.getShadowMap()) !== -1) {
  1151. this._renderTargets.push(shadowGenerator.getShadowMap());
  1152. }
  1153. }
  1154. }
  1155. // Depth renderer
  1156. if (this._depthRenderer) {
  1157. this._renderTargets.push(this._depthRenderer.getDepthMap());
  1158. }
  1159. // RenderPipeline
  1160. this.postProcessRenderPipelineManager.update();
  1161. // Multi-cameras?
  1162. if (this.activeCameras.length > 0) {
  1163. var currentRenderId = this._renderId;
  1164. for (var cameraIndex = 0; cameraIndex < this.activeCameras.length; cameraIndex++) {
  1165. this._renderId = currentRenderId;
  1166. this._processSubCameras(this.activeCameras[cameraIndex]);
  1167. }
  1168. } else {
  1169. if (!this.activeCamera) {
  1170. throw new Error("No camera defined");
  1171. }
  1172. this._processSubCameras(this.activeCamera);
  1173. }
  1174. // Intersection checks
  1175. this._checkIntersections();
  1176. // Update the audio listener attached to the camera
  1177. this._updateAudioParameters();
  1178. // After render
  1179. if (this.afterRender) {
  1180. this.afterRender();
  1181. }
  1182. for (callbackIndex = 0; callbackIndex < this._onAfterRenderCallbacks.length; callbackIndex++) {
  1183. this._onAfterRenderCallbacks[callbackIndex]();
  1184. }
  1185. // Cleaning
  1186. for (var index = 0; index < this._toBeDisposed.length; index++) {
  1187. this._toBeDisposed.data[index].dispose();
  1188. this._toBeDisposed[index] = null;
  1189. }
  1190. this._toBeDisposed.reset();
  1191. if (this.dumpNextRenderTargets) {
  1192. this.dumpNextRenderTargets = false;
  1193. }
  1194. Tools.EndPerformanceCounter("Scene rendering");
  1195. this._lastFrameDuration = Tools.Now - startDate;
  1196. }
  1197. private _updateAudioParameters() {
  1198. if (!this.audioEnabled || (this.mainSoundTrack.soundCollection.length === 0 && this.soundTracks.length === 0)) {
  1199. return;
  1200. }
  1201. var listeningCamera: Camera;
  1202. var audioEngine = Engine.audioEngine;
  1203. if (this.activeCameras.length > 0) {
  1204. listeningCamera = this.activeCameras[0];
  1205. } else {
  1206. listeningCamera = this.activeCamera;
  1207. }
  1208. if (listeningCamera && audioEngine.canUseWebAudio) {
  1209. audioEngine.audioContext.listener.setPosition(listeningCamera.position.x, listeningCamera.position.y, listeningCamera.position.z);
  1210. var mat = Matrix.Invert(listeningCamera.getViewMatrix());
  1211. var cameraDirection = Vector3.TransformNormal(new Vector3(0, 0, -1), mat);
  1212. cameraDirection.normalize();
  1213. audioEngine.audioContext.listener.setOrientation(cameraDirection.x, cameraDirection.y, cameraDirection.z, 0, 1, 0);
  1214. for (var i = 0; i < this.mainSoundTrack.soundCollection.length; i++) {
  1215. var sound = this.mainSoundTrack.soundCollection[i];
  1216. if (sound.useCustomAttenuation) {
  1217. sound.updateDistanceFromListener();
  1218. }
  1219. }
  1220. for (i = 0; i < this.soundTracks.length; i++) {
  1221. for (var j = 0; j < this.soundTracks[i].soundCollection.length; j++) {
  1222. sound = this.soundTracks[i].soundCollection[j];
  1223. if (sound.useCustomAttenuation) {
  1224. sound.updateDistanceFromListener();
  1225. }
  1226. }
  1227. }
  1228. }
  1229. }
  1230. public get audioEnabled(): boolean {
  1231. return this._audioEnabled;
  1232. }
  1233. public set audioEnabled(value: boolean) {
  1234. this._audioEnabled = value;
  1235. if (this._audioEnabled) {
  1236. this._enableAudio();
  1237. }
  1238. else {
  1239. this._disableAudio();
  1240. }
  1241. }
  1242. private _disableAudio() {
  1243. for (var i = 0; i < this.mainSoundTrack.soundCollection.length; i++) {
  1244. this.mainSoundTrack.soundCollection[i].pause();
  1245. }
  1246. for (i = 0; i < this.soundTracks.length; i++) {
  1247. for (var j = 0; j < this.soundTracks[i].soundCollection.length; j++) {
  1248. this.soundTracks[i].soundCollection[j].pause();
  1249. }
  1250. }
  1251. }
  1252. private _enableAudio() {
  1253. for (var i = 0; i < this.mainSoundTrack.soundCollection.length; i++) {
  1254. if (this.mainSoundTrack.soundCollection[i].isPaused) {
  1255. this.mainSoundTrack.soundCollection[i].play();
  1256. }
  1257. }
  1258. for (i = 0; i < this.soundTracks.length; i++) {
  1259. for (var j = 0; j < this.soundTracks[i].soundCollection.length; j++) {
  1260. if (this.soundTracks[i].soundCollection[j].isPaused) {
  1261. this.soundTracks[i].soundCollection[j].play();
  1262. }
  1263. }
  1264. }
  1265. }
  1266. public enableDepthRenderer(): DepthRenderer {
  1267. if (this._depthRenderer) {
  1268. return this._depthRenderer;
  1269. }
  1270. this._depthRenderer = new DepthRenderer(this);
  1271. return this._depthRenderer;
  1272. }
  1273. public disableDepthRenderer(): void {
  1274. if (!this._depthRenderer) {
  1275. return;
  1276. }
  1277. this._depthRenderer.dispose();
  1278. this._depthRenderer = null;
  1279. }
  1280. public dispose(): void {
  1281. this.beforeRender = null;
  1282. this.afterRender = null;
  1283. this.skeletons = [];
  1284. this._boundingBoxRenderer.dispose();
  1285. if (this._depthRenderer) {
  1286. this._depthRenderer.dispose();
  1287. }
  1288. // Debug layer
  1289. this.debugLayer.hide();
  1290. // Events
  1291. if (this.onDispose) {
  1292. this.onDispose();
  1293. }
  1294. this._onBeforeRenderCallbacks = [];
  1295. this._onAfterRenderCallbacks = [];
  1296. this.detachControl();
  1297. // Release sounds & sounds tracks
  1298. this.disposeSounds();
  1299. // Detach cameras
  1300. var canvas = this._engine.getRenderingCanvas();
  1301. var index;
  1302. for (index = 0; index < this.cameras.length; index++) {
  1303. this.cameras[index].detachControl(canvas);
  1304. }
  1305. // Release lights
  1306. while (this.lights.length) {
  1307. this.lights[0].dispose();
  1308. }
  1309. // Release meshes
  1310. while (this.meshes.length) {
  1311. this.meshes[0].dispose(true);
  1312. }
  1313. // Release cameras
  1314. while (this.cameras.length) {
  1315. this.cameras[0].dispose();
  1316. }
  1317. // Release materials
  1318. while (this.materials.length) {
  1319. this.materials[0].dispose();
  1320. }
  1321. // Release particles
  1322. while (this.particleSystems.length) {
  1323. this.particleSystems[0].dispose();
  1324. }
  1325. // Release sprites
  1326. while (this.spriteManagers.length) {
  1327. this.spriteManagers[0].dispose();
  1328. }
  1329. // Release layers
  1330. while (this.layers.length) {
  1331. this.layers[0].dispose();
  1332. }
  1333. // Release textures
  1334. while (this.textures.length) {
  1335. this.textures[0].dispose();
  1336. }
  1337. // Post-processes
  1338. this.postProcessManager.dispose();
  1339. // Physics
  1340. if (this._physicsEngine) {
  1341. this.disablePhysicsEngine();
  1342. }
  1343. // Remove from engine
  1344. index = this._engine.scenes.indexOf(this);
  1345. if (index > -1) {
  1346. this._engine.scenes.splice(index, 1);
  1347. }
  1348. this._engine.wipeCaches();
  1349. }
  1350. // Release sounds & sounds tracks
  1351. public disposeSounds() {
  1352. this.mainSoundTrack.dispose();
  1353. for (var scIndex = 0; scIndex < this.soundTracks.length; scIndex++) {
  1354. this.soundTracks[scIndex].dispose();
  1355. }
  1356. }
  1357. // Collisions
  1358. public _getNewPosition(position: Vector3, velocity: Vector3, collider: Collider, maximumRetry: number, finalPosition: Vector3, excludedMesh: AbstractMesh = null): void {
  1359. position.divideToRef(collider.radius, this._scaledPosition);
  1360. velocity.divideToRef(collider.radius, this._scaledVelocity);
  1361. collider.retry = 0;
  1362. collider.initialVelocity = this._scaledVelocity;
  1363. collider.initialPosition = this._scaledPosition;
  1364. this._collideWithWorld(this._scaledPosition, this._scaledVelocity, collider, maximumRetry, finalPosition, excludedMesh);
  1365. finalPosition.multiplyInPlace(collider.radius);
  1366. }
  1367. private _collideWithWorld(position: Vector3, velocity: Vector3, collider: Collider, maximumRetry: number, finalPosition: Vector3, excludedMesh: AbstractMesh = null): void {
  1368. var closeDistance = Engine.CollisionsEpsilon * 10.0;
  1369. if (collider.retry >= maximumRetry) {
  1370. finalPosition.copyFrom(position);
  1371. return;
  1372. }
  1373. collider._initialize(position, velocity, closeDistance);
  1374. // Check all meshes
  1375. for (var index = 0; index < this.meshes.length; index++) {
  1376. var mesh = this.meshes[index];
  1377. if (mesh.isEnabled() && mesh.checkCollisions && mesh.subMeshes && mesh !== excludedMesh) {
  1378. mesh._checkCollision(collider);
  1379. }
  1380. }
  1381. if (!collider.collisionFound) {
  1382. position.addToRef(velocity, finalPosition);
  1383. return;
  1384. }
  1385. if (velocity.x !== 0 || velocity.y !== 0 || velocity.z !== 0) {
  1386. collider._getResponse(position, velocity);
  1387. }
  1388. if (velocity.length() <= closeDistance) {
  1389. finalPosition.copyFrom(position);
  1390. return;
  1391. }
  1392. collider.retry++;
  1393. this._collideWithWorld(position, velocity, collider, maximumRetry, finalPosition, excludedMesh);
  1394. }
  1395. // Octrees
  1396. public getWorldExtends(): { min: Vector3; max: Vector3 } {
  1397. var min = new Vector3(Number.MAX_VALUE, Number.MAX_VALUE, Number.MAX_VALUE);
  1398. var max = new Vector3(-Number.MAX_VALUE, -Number.MAX_VALUE, -Number.MAX_VALUE);
  1399. for (var index = 0; index < this.meshes.length; index++) {
  1400. var mesh = this.meshes[index];
  1401. mesh.computeWorldMatrix(true);
  1402. var minBox = mesh.getBoundingInfo().boundingBox.minimumWorld;
  1403. var maxBox = mesh.getBoundingInfo().boundingBox.maximumWorld;
  1404. Tools.CheckExtends(minBox, min, max);
  1405. Tools.CheckExtends(maxBox, min, max);
  1406. }
  1407. return {
  1408. min: min,
  1409. max: max
  1410. };
  1411. }
  1412. public createOrUpdateSelectionOctree(maxCapacity = 64, maxDepth = 2): Octree<AbstractMesh> {
  1413. if (!this._selectionOctree) {
  1414. this._selectionOctree = new Octree<AbstractMesh>(Octree.CreationFuncForMeshes, maxCapacity, maxDepth);
  1415. }
  1416. var worldExtends = this.getWorldExtends();
  1417. // Update octree
  1418. this._selectionOctree.update(worldExtends.min, worldExtends.max, this.meshes);
  1419. return this._selectionOctree;
  1420. }
  1421. // Picking
  1422. public createPickingRay(x: number, y: number, world: Matrix, camera: Camera): Ray {
  1423. var engine = this._engine;
  1424. if (!camera) {
  1425. if (!this.activeCamera)
  1426. throw new Error("Active camera not set");
  1427. camera = this.activeCamera;
  1428. }
  1429. var cameraViewport = camera.viewport;
  1430. var viewport = cameraViewport.toGlobal(engine);
  1431. // Moving coordinates to local viewport world
  1432. x = x / this._engine.getHardwareScalingLevel() - viewport.x;
  1433. y = y / this._engine.getHardwareScalingLevel() - (this._engine.getRenderHeight() - viewport.y - viewport.height);
  1434. return Ray.CreateNew(x, y, viewport.width, viewport.height, world ? world : Matrix.Identity(), camera.getViewMatrix(), camera.getProjectionMatrix());
  1435. // return BABYLON.Ray.CreateNew(x / window.devicePixelRatio, y / window.devicePixelRatio, viewport.width, viewport.height, world ? world : BABYLON.Matrix.Identity(), camera.getViewMatrix(), camera.getProjectionMatrix());
  1436. }
  1437. private _internalPick(rayFunction: (world: Matrix) => Ray, predicate: (mesh: AbstractMesh) => boolean, fastCheck?: boolean): PickingInfo {
  1438. var pickingInfo = null;
  1439. for (var meshIndex = 0; meshIndex < this.meshes.length; meshIndex++) {
  1440. var mesh = this.meshes[meshIndex];
  1441. if (predicate) {
  1442. if (!predicate(mesh)) {
  1443. continue;
  1444. }
  1445. } else if (!mesh.isEnabled() || !mesh.isVisible || !mesh.isPickable) {
  1446. continue;
  1447. }
  1448. var world = mesh.getWorldMatrix();
  1449. var ray = rayFunction(world);
  1450. var result = mesh.intersects(ray, fastCheck);
  1451. if (!result || !result.hit)
  1452. continue;
  1453. if (!fastCheck && pickingInfo != null && result.distance >= pickingInfo.distance)
  1454. continue;
  1455. pickingInfo = result;
  1456. if (fastCheck) {
  1457. break;
  1458. }
  1459. }
  1460. return pickingInfo || new PickingInfo();
  1461. }
  1462. public pick(x: number, y: number, predicate?: (mesh: AbstractMesh) => boolean, fastCheck?: boolean, camera?: Camera): PickingInfo {
  1463. /// <summary>Launch a ray to try to pick a mesh in the scene</summary>
  1464. /// <param name="x">X position on screen</param>
  1465. /// <param name="y">Y position on screen</param>
  1466. /// <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>
  1467. /// <param name="fastCheck">Launch a fast check only using the bounding boxes. Can be set to null.</param>
  1468. /// <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>
  1469. return this._internalPick(world => this.createPickingRay(x, y, world, camera), predicate, fastCheck);
  1470. }
  1471. public pickWithRay(ray: Ray, predicate: (mesh: Mesh) => boolean, fastCheck?: boolean) {
  1472. return this._internalPick(world => {
  1473. if (!this._pickWithRayInverseMatrix) {
  1474. this._pickWithRayInverseMatrix = Matrix.Identity();
  1475. }
  1476. world.invertToRef(this._pickWithRayInverseMatrix);
  1477. return Ray.Transform(ray, this._pickWithRayInverseMatrix);
  1478. }, predicate, fastCheck);
  1479. }
  1480. public setPointerOverMesh(mesh: AbstractMesh): void {
  1481. if (this._pointerOverMesh === mesh) {
  1482. return;
  1483. }
  1484. if (this._pointerOverMesh && this._pointerOverMesh.actionManager) {
  1485. this._pointerOverMesh.actionManager.processTrigger(ActionManager.OnPointerOutTrigger, ActionEvent.CreateNew(this._pointerOverMesh));
  1486. }
  1487. this._pointerOverMesh = mesh;
  1488. if (this._pointerOverMesh && this._pointerOverMesh.actionManager) {
  1489. this._pointerOverMesh.actionManager.processTrigger(ActionManager.OnPointerOverTrigger, ActionEvent.CreateNew(this._pointerOverMesh));
  1490. }
  1491. }
  1492. public getPointerOverMesh(): AbstractMesh {
  1493. return this._pointerOverMesh;
  1494. }
  1495. // Physics
  1496. public getPhysicsEngine(): PhysicsEngine {
  1497. return this._physicsEngine;
  1498. }
  1499. public enablePhysics(gravity: Vector3, plugin?: IPhysicsEnginePlugin): boolean {
  1500. if (this._physicsEngine) {
  1501. return true;
  1502. }
  1503. this._physicsEngine = new PhysicsEngine(plugin);
  1504. if (!this._physicsEngine.isSupported()) {
  1505. this._physicsEngine = null;
  1506. return false;
  1507. }
  1508. this._physicsEngine._initialize(gravity);
  1509. return true;
  1510. }
  1511. public disablePhysicsEngine(): void {
  1512. if (!this._physicsEngine) {
  1513. return;
  1514. }
  1515. this._physicsEngine.dispose();
  1516. this._physicsEngine = undefined;
  1517. }
  1518. public isPhysicsEnabled(): boolean {
  1519. return this._physicsEngine !== undefined;
  1520. }
  1521. public setGravity(gravity: Vector3): void {
  1522. if (!this._physicsEngine) {
  1523. return;
  1524. }
  1525. this._physicsEngine._setGravity(gravity);
  1526. }
  1527. public createCompoundImpostor(parts: any, options: PhysicsBodyCreationOptions): any {
  1528. if (parts.parts) { // Old API
  1529. options = parts;
  1530. parts = parts.parts;
  1531. }
  1532. if (!this._physicsEngine) {
  1533. return null;
  1534. }
  1535. for (var index = 0; index < parts.length; index++) {
  1536. var mesh = parts[index].mesh;
  1537. mesh._physicImpostor = parts[index].impostor;
  1538. mesh._physicsMass = options.mass / parts.length;
  1539. mesh._physicsFriction = options.friction;
  1540. mesh._physicRestitution = options.restitution;
  1541. }
  1542. return this._physicsEngine._registerMeshesAsCompound(parts, options);
  1543. }
  1544. public deleteCompoundImpostor(compound: any): void {
  1545. for (var index = 0; index < compound.parts.length; index++) {
  1546. var mesh = compound.parts[index].mesh;
  1547. mesh._physicImpostor = PhysicsEngine.NoImpostor;
  1548. this._physicsEngine._unregisterMesh(mesh);
  1549. }
  1550. }
  1551. // Misc.
  1552. public createDefaultCameraOrLight() {
  1553. // Light
  1554. if (this.lights.length === 0) {
  1555. new HemisphericLight("default light", Vector3.Up(), this);
  1556. }
  1557. // Camera
  1558. if (!this.activeCamera) {
  1559. var camera = new FreeCamera("default camera", Vector3.Zero(), this);
  1560. // Compute position
  1561. var worldExtends = this.getWorldExtends();
  1562. var worldCenter = worldExtends.min.add(worldExtends.max.subtract(worldExtends.min).scale(0.5));
  1563. camera.position = new Vector3(worldCenter.x, worldCenter.y, worldExtends.min.z - (worldExtends.max.z - worldExtends.min.z));
  1564. camera.setTarget(worldCenter);
  1565. this.activeCamera = camera;
  1566. }
  1567. }
  1568. // Tags
  1569. private _getByTags(list: any[], tagsQuery: string, forEach?: (item: any) => void): any[] {
  1570. if (tagsQuery === undefined) {
  1571. // returns the complete list (could be done with BABYLON.Tags.MatchesQuery but no need to have a for-loop here)
  1572. return list;
  1573. }
  1574. var listByTags = [];
  1575. forEach = forEach || ((item: any) => { return; });
  1576. for (var i in list) {
  1577. var item = list[i];
  1578. if (Tags.MatchesQuery(item, tagsQuery)) {
  1579. listByTags.push(item);
  1580. forEach(item);
  1581. }
  1582. }
  1583. return listByTags;
  1584. }
  1585. public getMeshesByTags(tagsQuery: string, forEach?: (mesh: AbstractMesh) => void): Mesh[] {
  1586. return this._getByTags(this.meshes, tagsQuery, forEach);
  1587. }
  1588. public getCamerasByTags(tagsQuery: string, forEach?: (camera: Camera) => void): Camera[] {
  1589. return this._getByTags(this.cameras, tagsQuery, forEach);
  1590. }
  1591. public getLightsByTags(tagsQuery: string, forEach?: (light: Light) => void): Light[] {
  1592. return this._getByTags(this.lights, tagsQuery, forEach);
  1593. }
  1594. public getMaterialByTags(tagsQuery: string, forEach?: (material: Material) => void): Material[] {
  1595. return this._getByTags(this.materials, tagsQuery, forEach).concat(this._getByTags(this.multiMaterials, tagsQuery, forEach));
  1596. }
  1597. // Audio
  1598. public switchAudioModeForHeadphones() {
  1599. this.mainSoundTrack.switchPanningModelToHRTF();
  1600. for (var i = 0; i < this.soundTracks.length; i++) {
  1601. this.soundTracks[i].switchPanningModelToHRTF();
  1602. }
  1603. }
  1604. public switchAudioModeForNormalSpeakers() {
  1605. this.mainSoundTrack.switchPanningModelToEqualPower();
  1606. for (var i = 0; i < this.soundTracks.length; i++) {
  1607. this.soundTracks[i].switchPanningModelToEqualPower();
  1608. }
  1609. }
  1610. }
  1611. }