babylon.scene.js 56 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504
  1. var BABYLON;
  2. (function (BABYLON) {
  3. var Scene = (function () {
  4. // Constructor
  5. function Scene(engine) {
  6. // Members
  7. this.autoClear = true;
  8. this.clearColor = new BABYLON.Color3(0.2, 0.2, 0.3);
  9. this.ambientColor = new BABYLON.Color3(0, 0, 0);
  10. this.forceWireframe = false;
  11. this.cameraToUseForPointers = null;
  12. // Fog
  13. this.fogMode = BABYLON.Scene.FOGMODE_NONE;
  14. this.fogColor = new BABYLON.Color3(0.2, 0.2, 0.3);
  15. this.fogDensity = 0.1;
  16. this.fogStart = 0;
  17. this.fogEnd = 1000.0;
  18. // Lights
  19. this.shadowsEnabled = true;
  20. this.lightsEnabled = true;
  21. this.lights = new Array();
  22. // Cameras
  23. this.cameras = new Array();
  24. this.activeCameras = new Array();
  25. // Meshes
  26. this.meshes = new Array();
  27. // Geometries
  28. this._geometries = new Array();
  29. this.materials = new Array();
  30. this.multiMaterials = new Array();
  31. this.defaultMaterial = new BABYLON.StandardMaterial("default material", this);
  32. // Textures
  33. this.texturesEnabled = true;
  34. this.textures = new Array();
  35. // Particles
  36. this.particlesEnabled = true;
  37. this.particleSystems = new Array();
  38. // Sprites
  39. this.spriteManagers = new Array();
  40. // Layers
  41. this.layers = new Array();
  42. // Skeletons
  43. this.skeletons = new Array();
  44. // Lens flares
  45. this.lensFlaresEnabled = true;
  46. this.lensFlareSystems = new Array();
  47. // Collisions
  48. this.collisionsEnabled = true;
  49. this.gravity = new BABYLON.Vector3(0, -9.0, 0);
  50. // Postprocesses
  51. this.postProcessesEnabled = true;
  52. // Customs render targets
  53. this.renderTargetsEnabled = true;
  54. this.customRenderTargets = new Array();
  55. // Imported meshes
  56. this.importedMeshesFiles = new Array();
  57. this._actionManagers = new Array();
  58. this._meshesForIntersections = new BABYLON.SmartArray(256);
  59. // Procedural textures
  60. this.proceduralTexturesEnabled = true;
  61. this._proceduralTextures = new Array();
  62. this._totalVertices = 0;
  63. this._activeVertices = 0;
  64. this._activeParticles = 0;
  65. this._lastFrameDuration = 0;
  66. this._evaluateActiveMeshesDuration = 0;
  67. this._renderTargetsDuration = 0;
  68. this._particlesDuration = 0;
  69. this._renderDuration = 0;
  70. this._spritesDuration = 0;
  71. this._animationRatio = 0;
  72. this._renderId = 0;
  73. this._executeWhenReadyTimeoutId = -1;
  74. this._toBeDisposed = new BABYLON.SmartArray(256);
  75. this._onReadyCallbacks = new Array();
  76. this._pendingData = [];
  77. this._onBeforeRenderCallbacks = new Array();
  78. this._activeMeshes = new BABYLON.SmartArray(256);
  79. this._processedMaterials = new BABYLON.SmartArray(256);
  80. this._renderTargets = new BABYLON.SmartArray(256);
  81. this._activeParticleSystems = new BABYLON.SmartArray(256);
  82. this._activeSkeletons = new BABYLON.SmartArray(32);
  83. this._activeAnimatables = new Array();
  84. this._transformMatrix = BABYLON.Matrix.Zero();
  85. this._scaledPosition = BABYLON.Vector3.Zero();
  86. this._scaledVelocity = BABYLON.Vector3.Zero();
  87. this._engine = engine;
  88. engine.scenes.push(this);
  89. this._renderingManager = new BABYLON.RenderingManager(this);
  90. this.postProcessManager = new BABYLON.PostProcessManager(this);
  91. this.postProcessRenderPipelineManager = new BABYLON.PostProcessRenderPipelineManager();
  92. this._boundingBoxRenderer = new BABYLON.BoundingBoxRenderer(this);
  93. this._outlineRenderer = new BABYLON.OutlineRenderer(this);
  94. this.attachControl();
  95. }
  96. Object.defineProperty(Scene.prototype, "meshUnderPointer", {
  97. // Properties
  98. get: function () {
  99. return this._meshUnderPointer;
  100. },
  101. enumerable: true,
  102. configurable: true
  103. });
  104. Object.defineProperty(Scene.prototype, "pointerX", {
  105. get: function () {
  106. return this._pointerX;
  107. },
  108. enumerable: true,
  109. configurable: true
  110. });
  111. Object.defineProperty(Scene.prototype, "pointerY", {
  112. get: function () {
  113. return this._pointerY;
  114. },
  115. enumerable: true,
  116. configurable: true
  117. });
  118. Scene.prototype.getBoundingBoxRenderer = function () {
  119. return this._boundingBoxRenderer;
  120. };
  121. Scene.prototype.getOutlineRenderer = function () {
  122. return this._outlineRenderer;
  123. };
  124. Scene.prototype.getEngine = function () {
  125. return this._engine;
  126. };
  127. Scene.prototype.getTotalVertices = function () {
  128. return this._totalVertices;
  129. };
  130. Scene.prototype.getActiveVertices = function () {
  131. return this._activeVertices;
  132. };
  133. Scene.prototype.getActiveParticles = function () {
  134. return this._activeParticles;
  135. };
  136. // Stats
  137. Scene.prototype.getLastFrameDuration = function () {
  138. return this._lastFrameDuration;
  139. };
  140. Scene.prototype.getEvaluateActiveMeshesDuration = function () {
  141. return this._evaluateActiveMeshesDuration;
  142. };
  143. Scene.prototype.getActiveMeshes = function () {
  144. return this._activeMeshes;
  145. };
  146. Scene.prototype.getRenderTargetsDuration = function () {
  147. return this._renderTargetsDuration;
  148. };
  149. Scene.prototype.getRenderDuration = function () {
  150. return this._renderDuration;
  151. };
  152. Scene.prototype.getParticlesDuration = function () {
  153. return this._particlesDuration;
  154. };
  155. Scene.prototype.getSpritesDuration = function () {
  156. return this._spritesDuration;
  157. };
  158. Scene.prototype.getAnimationRatio = function () {
  159. return this._animationRatio;
  160. };
  161. Scene.prototype.getRenderId = function () {
  162. return this._renderId;
  163. };
  164. Scene.prototype._updatePointerPosition = function (evt) {
  165. var canvasRect = this._engine.getRenderingCanvasClientRect();
  166. this._pointerX = evt.clientX - canvasRect.left;
  167. this._pointerY = evt.clientY - canvasRect.top;
  168. if (this.cameraToUseForPointers) {
  169. this._pointerX = this._pointerX - this.cameraToUseForPointers.viewport.x * this._engine.getRenderWidth();
  170. this._pointerY = this._pointerY - this.cameraToUseForPointers.viewport.y * this._engine.getRenderHeight();
  171. }
  172. };
  173. // Pointers handling
  174. Scene.prototype.attachControl = function () {
  175. var _this = this;
  176. this._onPointerMove = function (evt) {
  177. var canvas = _this._engine.getRenderingCanvas();
  178. _this._updatePointerPosition(evt);
  179. var pickResult = _this.pick(_this._pointerX, _this._pointerY, function (mesh) {
  180. return mesh.isPickable && mesh.isVisible && mesh.isReady() && mesh.actionManager && mesh.actionManager.hasPointerTriggers;
  181. }, false, _this.cameraToUseForPointers);
  182. if (pickResult.hit) {
  183. _this._meshUnderPointer = pickResult.pickedMesh;
  184. _this.setPointerOverMesh(pickResult.pickedMesh);
  185. canvas.style.cursor = "pointer";
  186. } else {
  187. _this.setPointerOverMesh(null);
  188. canvas.style.cursor = "";
  189. _this._meshUnderPointer = null;
  190. }
  191. };
  192. this._onPointerDown = function (evt) {
  193. var predicate = null;
  194. if (!_this.onPointerDown) {
  195. predicate = function (mesh) {
  196. return mesh.isPickable && mesh.isVisible && mesh.isReady() && mesh.actionManager && mesh.actionManager.hasPickTriggers;
  197. };
  198. }
  199. _this._updatePointerPosition(evt);
  200. var pickResult = _this.pick(_this._pointerX, _this._pointerY, predicate, false, _this.cameraToUseForPointers);
  201. if (pickResult.hit) {
  202. if (pickResult.pickedMesh.actionManager) {
  203. switch (evt.button) {
  204. case 0:
  205. pickResult.pickedMesh.actionManager.processTrigger(BABYLON.ActionManager.OnLeftPickTrigger, BABYLON.ActionEvent.CreateNew(pickResult.pickedMesh));
  206. break;
  207. case 1:
  208. pickResult.pickedMesh.actionManager.processTrigger(BABYLON.ActionManager.OnCenterPickTrigger, BABYLON.ActionEvent.CreateNew(pickResult.pickedMesh));
  209. break;
  210. case 2:
  211. pickResult.pickedMesh.actionManager.processTrigger(BABYLON.ActionManager.OnRightPickTrigger, BABYLON.ActionEvent.CreateNew(pickResult.pickedMesh));
  212. break;
  213. }
  214. pickResult.pickedMesh.actionManager.processTrigger(BABYLON.ActionManager.OnPickTrigger, BABYLON.ActionEvent.CreateNew(pickResult.pickedMesh));
  215. }
  216. }
  217. if (_this.onPointerDown) {
  218. _this.onPointerDown(evt, pickResult);
  219. }
  220. };
  221. this._onKeyDown = function (evt) {
  222. if (_this.actionManager) {
  223. _this.actionManager.processTrigger(BABYLON.ActionManager.OnKeyDownTrigger, BABYLON.ActionEvent.CreateNewFromScene(_this, evt));
  224. }
  225. };
  226. this._onKeyUp = function (evt) {
  227. if (_this.actionManager) {
  228. _this.actionManager.processTrigger(BABYLON.ActionManager.OnKeyUpTrigger, BABYLON.ActionEvent.CreateNewFromScene(_this, evt));
  229. }
  230. };
  231. var eventPrefix = BABYLON.Tools.GetPointerPrefix();
  232. this._engine.getRenderingCanvas().addEventListener(eventPrefix + "move", this._onPointerMove, false);
  233. this._engine.getRenderingCanvas().addEventListener(eventPrefix + "down", this._onPointerDown, false);
  234. window.addEventListener("keydown", this._onKeyDown, false);
  235. window.addEventListener("keyup", this._onKeyUp, false);
  236. };
  237. Scene.prototype.detachControl = function () {
  238. var eventPrefix = BABYLON.Tools.GetPointerPrefix();
  239. this._engine.getRenderingCanvas().removeEventListener(eventPrefix + "move", this._onPointerMove);
  240. this._engine.getRenderingCanvas().removeEventListener(eventPrefix + "down", this._onPointerDown);
  241. window.removeEventListener("keydown", this._onKeyDown);
  242. window.removeEventListener("keyup", this._onKeyUp);
  243. };
  244. // Ready
  245. Scene.prototype.isReady = function () {
  246. if (this._pendingData.length > 0) {
  247. return false;
  248. }
  249. for (var index = 0; index < this._geometries.length; index++) {
  250. var geometry = this._geometries[index];
  251. if (geometry.delayLoadState === BABYLON.Engine.DELAYLOADSTATE_LOADING) {
  252. return false;
  253. }
  254. }
  255. for (index = 0; index < this.meshes.length; index++) {
  256. var mesh = this.meshes[index];
  257. if (!mesh.isReady()) {
  258. return false;
  259. }
  260. var mat = mesh.material;
  261. if (mat) {
  262. if (!mat.isReady(mesh)) {
  263. return false;
  264. }
  265. }
  266. }
  267. return true;
  268. };
  269. Scene.prototype.registerBeforeRender = function (func) {
  270. this._onBeforeRenderCallbacks.push(func);
  271. };
  272. Scene.prototype.unregisterBeforeRender = function (func) {
  273. var index = this._onBeforeRenderCallbacks.indexOf(func);
  274. if (index > -1) {
  275. this._onBeforeRenderCallbacks.splice(index, 1);
  276. }
  277. };
  278. Scene.prototype._addPendingData = function (data) {
  279. this._pendingData.push(data);
  280. };
  281. Scene.prototype._removePendingData = function (data) {
  282. var index = this._pendingData.indexOf(data);
  283. if (index !== -1) {
  284. this._pendingData.splice(index, 1);
  285. }
  286. };
  287. Scene.prototype.getWaitingItemsCount = function () {
  288. return this._pendingData.length;
  289. };
  290. Scene.prototype.executeWhenReady = function (func) {
  291. var _this = this;
  292. this._onReadyCallbacks.push(func);
  293. if (this._executeWhenReadyTimeoutId !== -1) {
  294. return;
  295. }
  296. this._executeWhenReadyTimeoutId = setTimeout(function () {
  297. _this._checkIsReady();
  298. }, 150);
  299. };
  300. Scene.prototype._checkIsReady = function () {
  301. var _this = this;
  302. if (this.isReady()) {
  303. this._onReadyCallbacks.forEach(function (func) {
  304. func();
  305. });
  306. this._onReadyCallbacks = [];
  307. this._executeWhenReadyTimeoutId = -1;
  308. return;
  309. }
  310. this._executeWhenReadyTimeoutId = setTimeout(function () {
  311. _this._checkIsReady();
  312. }, 150);
  313. };
  314. // Animations
  315. Scene.prototype.beginAnimation = function (target, from, to, loop, speedRatio, onAnimationEnd, animatable) {
  316. if (speedRatio === undefined) {
  317. speedRatio = 1.0;
  318. }
  319. this.stopAnimation(target);
  320. if (!animatable) {
  321. animatable = new BABYLON.Animatable(this, target, from, to, loop, speedRatio, onAnimationEnd);
  322. }
  323. // Local animations
  324. if (target.animations) {
  325. animatable.appendAnimations(target, target.animations);
  326. }
  327. // Children animations
  328. if (target.getAnimatables) {
  329. var animatables = target.getAnimatables();
  330. for (var index = 0; index < animatables.length; index++) {
  331. this.beginAnimation(animatables[index], from, to, loop, speedRatio, onAnimationEnd, animatable);
  332. }
  333. }
  334. return animatable;
  335. };
  336. Scene.prototype.beginDirectAnimation = function (target, animations, from, to, loop, speedRatio, onAnimationEnd) {
  337. if (speedRatio === undefined) {
  338. speedRatio = 1.0;
  339. }
  340. var animatable = new BABYLON.Animatable(this, target, from, to, loop, speedRatio, onAnimationEnd, animations);
  341. return animatable;
  342. };
  343. Scene.prototype.getAnimatableByTarget = function (target) {
  344. for (var index = 0; index < this._activeAnimatables.length; index++) {
  345. if (this._activeAnimatables[index].target === target) {
  346. return this._activeAnimatables[index];
  347. }
  348. }
  349. return null;
  350. };
  351. Scene.prototype.stopAnimation = function (target) {
  352. var animatable = this.getAnimatableByTarget(target);
  353. if (animatable) {
  354. animatable.stop();
  355. }
  356. };
  357. Scene.prototype._animate = function () {
  358. if (!this._animationStartDate) {
  359. this._animationStartDate = BABYLON.Tools.Now;
  360. }
  361. // Getting time
  362. var now = BABYLON.Tools.Now;
  363. var delay = now - this._animationStartDate;
  364. for (var index = 0; index < this._activeAnimatables.length; index++) {
  365. if (!this._activeAnimatables[index]._animate(delay)) {
  366. this._activeAnimatables.splice(index, 1);
  367. index--;
  368. }
  369. }
  370. };
  371. // Matrix
  372. Scene.prototype.getViewMatrix = function () {
  373. return this._viewMatrix;
  374. };
  375. Scene.prototype.getProjectionMatrix = function () {
  376. return this._projectionMatrix;
  377. };
  378. Scene.prototype.getTransformMatrix = function () {
  379. return this._transformMatrix;
  380. };
  381. Scene.prototype.setTransformMatrix = function (view, projection) {
  382. this._viewMatrix = view;
  383. this._projectionMatrix = projection;
  384. this._viewMatrix.multiplyToRef(this._projectionMatrix, this._transformMatrix);
  385. };
  386. // Methods
  387. Scene.prototype.setActiveCameraByID = function (id) {
  388. var camera = this.getCameraByID(id);
  389. if (camera) {
  390. this.activeCamera = camera;
  391. return camera;
  392. }
  393. return null;
  394. };
  395. Scene.prototype.setActiveCameraByName = function (name) {
  396. var camera = this.getCameraByName(name);
  397. if (camera) {
  398. this.activeCamera = camera;
  399. return camera;
  400. }
  401. return null;
  402. };
  403. Scene.prototype.getMaterialByID = function (id) {
  404. for (var index = 0; index < this.materials.length; index++) {
  405. if (this.materials[index].id === id) {
  406. return this.materials[index];
  407. }
  408. }
  409. return null;
  410. };
  411. Scene.prototype.getMaterialByName = function (name) {
  412. for (var index = 0; index < this.materials.length; index++) {
  413. if (this.materials[index].name === name) {
  414. return this.materials[index];
  415. }
  416. }
  417. return null;
  418. };
  419. Scene.prototype.getCameraByID = function (id) {
  420. for (var index = 0; index < this.cameras.length; index++) {
  421. if (this.cameras[index].id === id) {
  422. return this.cameras[index];
  423. }
  424. }
  425. return null;
  426. };
  427. Scene.prototype.getCameraByName = function (name) {
  428. for (var index = 0; index < this.cameras.length; index++) {
  429. if (this.cameras[index].name === name) {
  430. return this.cameras[index];
  431. }
  432. }
  433. return null;
  434. };
  435. Scene.prototype.getLightByName = function (name) {
  436. for (var index = 0; index < this.lights.length; index++) {
  437. if (this.lights[index].name === name) {
  438. return this.lights[index];
  439. }
  440. }
  441. return null;
  442. };
  443. Scene.prototype.getLightByID = function (id) {
  444. for (var index = 0; index < this.lights.length; index++) {
  445. if (this.lights[index].id === id) {
  446. return this.lights[index];
  447. }
  448. }
  449. return null;
  450. };
  451. Scene.prototype.getGeometryByID = function (id) {
  452. for (var index = 0; index < this._geometries.length; index++) {
  453. if (this._geometries[index].id === id) {
  454. return this._geometries[index];
  455. }
  456. }
  457. return null;
  458. };
  459. Scene.prototype.pushGeometry = function (geometry, force) {
  460. if (!force && this.getGeometryByID(geometry.id)) {
  461. return false;
  462. }
  463. this._geometries.push(geometry);
  464. return true;
  465. };
  466. Scene.prototype.getGeometries = function () {
  467. return this._geometries;
  468. };
  469. Scene.prototype.getMeshByID = function (id) {
  470. for (var index = 0; index < this.meshes.length; index++) {
  471. if (this.meshes[index].id === id) {
  472. return this.meshes[index];
  473. }
  474. }
  475. return null;
  476. };
  477. Scene.prototype.getLastMeshByID = function (id) {
  478. for (var index = this.meshes.length - 1; index >= 0; index--) {
  479. if (this.meshes[index].id === id) {
  480. return this.meshes[index];
  481. }
  482. }
  483. return null;
  484. };
  485. Scene.prototype.getLastEntryByID = function (id) {
  486. for (var index = this.meshes.length - 1; index >= 0; index--) {
  487. if (this.meshes[index].id === id) {
  488. return this.meshes[index];
  489. }
  490. }
  491. for (index = this.cameras.length - 1; index >= 0; index--) {
  492. if (this.cameras[index].id === id) {
  493. return this.cameras[index];
  494. }
  495. }
  496. for (index = this.lights.length - 1; index >= 0; index--) {
  497. if (this.lights[index].id === id) {
  498. return this.lights[index];
  499. }
  500. }
  501. return null;
  502. };
  503. Scene.prototype.getMeshByName = function (name) {
  504. for (var index = 0; index < this.meshes.length; index++) {
  505. if (this.meshes[index].name === name) {
  506. return this.meshes[index];
  507. }
  508. }
  509. return null;
  510. };
  511. Scene.prototype.getLastSkeletonByID = function (id) {
  512. for (var index = this.skeletons.length - 1; index >= 0; index--) {
  513. if (this.skeletons[index].id === id) {
  514. return this.skeletons[index];
  515. }
  516. }
  517. return null;
  518. };
  519. Scene.prototype.getSkeletonById = function (id) {
  520. for (var index = 0; index < this.skeletons.length; index++) {
  521. if (this.skeletons[index].id === id) {
  522. return this.skeletons[index];
  523. }
  524. }
  525. return null;
  526. };
  527. Scene.prototype.getSkeletonByName = function (name) {
  528. for (var index = 0; index < this.skeletons.length; index++) {
  529. if (this.skeletons[index].name === name) {
  530. return this.skeletons[index];
  531. }
  532. }
  533. return null;
  534. };
  535. Scene.prototype.isActiveMesh = function (mesh) {
  536. return (this._activeMeshes.indexOf(mesh) !== -1);
  537. };
  538. Scene.prototype._evaluateSubMesh = function (subMesh, mesh) {
  539. if (mesh.subMeshes.length == 1 || subMesh.isInFrustum(this._frustumPlanes)) {
  540. var material = subMesh.getMaterial();
  541. if (mesh.showSubMeshesBoundingBox) {
  542. this._boundingBoxRenderer.renderList.push(subMesh.getBoundingInfo().boundingBox);
  543. }
  544. if (material) {
  545. // Render targets
  546. if (material.getRenderTargetTextures) {
  547. if (this._processedMaterials.indexOf(material) === -1) {
  548. this._processedMaterials.push(material);
  549. this._renderTargets.concat(material.getRenderTargetTextures());
  550. }
  551. }
  552. // Dispatch
  553. this._activeVertices += subMesh.verticesCount;
  554. this._renderingManager.dispatch(subMesh);
  555. }
  556. }
  557. };
  558. Scene.prototype._evaluateActiveMeshes = function () {
  559. this._activeMeshes.reset();
  560. this._renderingManager.reset();
  561. this._processedMaterials.reset();
  562. this._activeParticleSystems.reset();
  563. this._activeSkeletons.reset();
  564. this._boundingBoxRenderer.reset();
  565. if (!this._frustumPlanes) {
  566. this._frustumPlanes = BABYLON.Frustum.GetPlanes(this._transformMatrix);
  567. } else {
  568. BABYLON.Frustum.GetPlanesToRef(this._transformMatrix, this._frustumPlanes);
  569. }
  570. // Meshes
  571. var meshes;
  572. var len;
  573. if (this._selectionOctree) {
  574. var selection = this._selectionOctree.select(this._frustumPlanes);
  575. meshes = selection.data;
  576. len = selection.length;
  577. } else {
  578. len = this.meshes.length;
  579. meshes = this.meshes;
  580. }
  581. for (var meshIndex = 0; meshIndex < len; meshIndex++) {
  582. var mesh = meshes[meshIndex];
  583. if (mesh.isBlocked) {
  584. continue;
  585. }
  586. this._totalVertices += mesh.getTotalVertices();
  587. if (!mesh.isReady()) {
  588. continue;
  589. }
  590. mesh.computeWorldMatrix();
  591. // Intersections
  592. if (mesh.actionManager && mesh.actionManager.hasSpecificTriggers([BABYLON.ActionManager.OnIntersectionEnterTrigger, BABYLON.ActionManager.OnIntersectionExitTrigger])) {
  593. this._meshesForIntersections.pushNoDuplicate(mesh);
  594. }
  595. // Switch to current LOD
  596. var meshLOD = mesh.getLOD(this.activeCamera);
  597. if (!meshLOD) {
  598. continue;
  599. }
  600. mesh._preActivate();
  601. if (mesh.isEnabled() && mesh.isVisible && mesh.visibility > 0 && ((mesh.layerMask & this.activeCamera.layerMask) != 0) && mesh.isInFrustum(this._frustumPlanes)) {
  602. this._activeMeshes.push(mesh);
  603. mesh._activate(this._renderId);
  604. this._activeMesh(meshLOD);
  605. }
  606. }
  607. // Particle systems
  608. var beforeParticlesDate = BABYLON.Tools.Now;
  609. if (this.particlesEnabled) {
  610. BABYLON.Tools.StartPerformanceCounter("Particles", this.particleSystems.length > 0);
  611. for (var particleIndex = 0; particleIndex < this.particleSystems.length; particleIndex++) {
  612. var particleSystem = this.particleSystems[particleIndex];
  613. if (!particleSystem.isStarted()) {
  614. continue;
  615. }
  616. if (!particleSystem.emitter.position || (particleSystem.emitter && particleSystem.emitter.isEnabled())) {
  617. this._activeParticleSystems.push(particleSystem);
  618. particleSystem.animate();
  619. }
  620. }
  621. BABYLON.Tools.EndPerformanceCounter("Particles", this.particleSystems.length > 0);
  622. }
  623. this._particlesDuration += BABYLON.Tools.Now - beforeParticlesDate;
  624. };
  625. Scene.prototype._activeMesh = function (mesh) {
  626. if (mesh.skeleton) {
  627. this._activeSkeletons.pushNoDuplicate(mesh.skeleton);
  628. }
  629. if (mesh.showBoundingBox) {
  630. this._boundingBoxRenderer.renderList.push(mesh.getBoundingInfo().boundingBox);
  631. }
  632. if (mesh && mesh.subMeshes) {
  633. // Submeshes Octrees
  634. var len;
  635. var subMeshes;
  636. if (mesh._submeshesOctree && mesh.useOctreeForRenderingSelection) {
  637. var intersections = mesh._submeshesOctree.select(this._frustumPlanes);
  638. len = intersections.length;
  639. subMeshes = intersections.data;
  640. } else {
  641. subMeshes = mesh.subMeshes;
  642. len = subMeshes.length;
  643. }
  644. for (var subIndex = 0; subIndex < len; subIndex++) {
  645. var subMesh = subMeshes[subIndex];
  646. this._evaluateSubMesh(subMesh, mesh);
  647. }
  648. }
  649. };
  650. Scene.prototype.updateTransformMatrix = function (force) {
  651. this.setTransformMatrix(this.activeCamera.getViewMatrix(), this.activeCamera.getProjectionMatrix(force));
  652. };
  653. Scene.prototype._renderForCamera = function (camera) {
  654. var engine = this._engine;
  655. this.activeCamera = camera;
  656. if (!this.activeCamera)
  657. throw new Error("Active camera not set");
  658. BABYLON.Tools.StartPerformanceCounter("Rendering camera " + this.activeCamera.name);
  659. // Viewport
  660. engine.setViewport(this.activeCamera.viewport);
  661. // Camera
  662. this._renderId++;
  663. this.updateTransformMatrix();
  664. if (this.beforeCameraRender) {
  665. this.beforeCameraRender(this.activeCamera);
  666. }
  667. // Meshes
  668. var beforeEvaluateActiveMeshesDate = BABYLON.Tools.Now;
  669. BABYLON.Tools.StartPerformanceCounter("Active meshes evaluation");
  670. this._evaluateActiveMeshes();
  671. this._evaluateActiveMeshesDuration += BABYLON.Tools.Now - beforeEvaluateActiveMeshesDate;
  672. BABYLON.Tools.EndPerformanceCounter("Active meshes evaluation");
  673. for (var skeletonIndex = 0; skeletonIndex < this._activeSkeletons.length; skeletonIndex++) {
  674. var skeleton = this._activeSkeletons.data[skeletonIndex];
  675. skeleton.prepare();
  676. }
  677. // Render targets
  678. var beforeRenderTargetDate = BABYLON.Tools.Now;
  679. if (this.renderTargetsEnabled) {
  680. BABYLON.Tools.StartPerformanceCounter("Render targets", this._renderTargets.length > 0);
  681. for (var renderIndex = 0; renderIndex < this._renderTargets.length; renderIndex++) {
  682. var renderTarget = this._renderTargets.data[renderIndex];
  683. if (renderTarget._shouldRender()) {
  684. this._renderId++;
  685. renderTarget.render();
  686. }
  687. }
  688. BABYLON.Tools.EndPerformanceCounter("Render targets", this._renderTargets.length > 0);
  689. this._renderId++;
  690. }
  691. if (this._renderTargets.length > 0) {
  692. engine.restoreDefaultFramebuffer();
  693. }
  694. this._renderTargetsDuration += BABYLON.Tools.Now - beforeRenderTargetDate;
  695. // Prepare Frame
  696. this.postProcessManager._prepareFrame();
  697. var beforeRenderDate = BABYLON.Tools.Now;
  698. // Backgrounds
  699. if (this.layers.length) {
  700. engine.setDepthBuffer(false);
  701. var layerIndex;
  702. var layer;
  703. for (layerIndex = 0; layerIndex < this.layers.length; layerIndex++) {
  704. layer = this.layers[layerIndex];
  705. if (layer.isBackground) {
  706. layer.render();
  707. }
  708. }
  709. engine.setDepthBuffer(true);
  710. }
  711. // Render
  712. BABYLON.Tools.StartPerformanceCounter("Main render");
  713. this._renderingManager.render(null, null, true, true);
  714. BABYLON.Tools.EndPerformanceCounter("Main render");
  715. // Bounding boxes
  716. this._boundingBoxRenderer.render();
  717. // Lens flares
  718. if (this.lensFlaresEnabled) {
  719. BABYLON.Tools.StartPerformanceCounter("Lens flares", this.lensFlareSystems.length > 0);
  720. for (var lensFlareSystemIndex = 0; lensFlareSystemIndex < this.lensFlareSystems.length; lensFlareSystemIndex++) {
  721. this.lensFlareSystems[lensFlareSystemIndex].render();
  722. }
  723. BABYLON.Tools.EndPerformanceCounter("Lens flares", this.lensFlareSystems.length > 0);
  724. }
  725. // Foregrounds
  726. if (this.layers.length) {
  727. engine.setDepthBuffer(false);
  728. for (layerIndex = 0; layerIndex < this.layers.length; layerIndex++) {
  729. layer = this.layers[layerIndex];
  730. if (!layer.isBackground) {
  731. layer.render();
  732. }
  733. }
  734. engine.setDepthBuffer(true);
  735. }
  736. this._renderDuration += BABYLON.Tools.Now - beforeRenderDate;
  737. // Finalize frame
  738. this.postProcessManager._finalizeFrame(camera.isIntermediate);
  739. // Update camera
  740. this.activeCamera._updateFromScene();
  741. // Reset some special arrays
  742. this._renderTargets.reset();
  743. if (this.afterCameraRender) {
  744. this.afterCameraRender(this.activeCamera);
  745. }
  746. BABYLON.Tools.EndPerformanceCounter("Rendering camera " + this.activeCamera.name);
  747. };
  748. Scene.prototype._processSubCameras = function (camera) {
  749. if (camera.subCameras.length == 0) {
  750. this._renderForCamera(camera);
  751. return;
  752. }
  753. for (var index = 0; index < camera.subCameras.length; index++) {
  754. this._renderForCamera(camera.subCameras[index]);
  755. }
  756. this.activeCamera = camera;
  757. this.setTransformMatrix(this.activeCamera.getViewMatrix(), this.activeCamera.getProjectionMatrix());
  758. // Update camera
  759. this.activeCamera._updateFromScene();
  760. };
  761. Scene.prototype._checkIntersections = function () {
  762. for (var index = 0; index < this._meshesForIntersections.length; index++) {
  763. var sourceMesh = this._meshesForIntersections.data[index];
  764. for (var actionIndex = 0; actionIndex < sourceMesh.actionManager.actions.length; actionIndex++) {
  765. var action = sourceMesh.actionManager.actions[actionIndex];
  766. if (action.trigger == BABYLON.ActionManager.OnIntersectionEnterTrigger || action.trigger == BABYLON.ActionManager.OnIntersectionExitTrigger) {
  767. var otherMesh = action.getTriggerParameter();
  768. var areIntersecting = otherMesh.intersectsMesh(sourceMesh, false);
  769. var currentIntersectionInProgress = sourceMesh._intersectionsInProgress.indexOf(otherMesh);
  770. if (areIntersecting && currentIntersectionInProgress === -1 && action.trigger == BABYLON.ActionManager.OnIntersectionEnterTrigger) {
  771. sourceMesh.actionManager.processTrigger(BABYLON.ActionManager.OnIntersectionEnterTrigger, BABYLON.ActionEvent.CreateNew(sourceMesh));
  772. sourceMesh._intersectionsInProgress.push(otherMesh);
  773. } else if (!areIntersecting && currentIntersectionInProgress > -1 && action.trigger == BABYLON.ActionManager.OnIntersectionExitTrigger) {
  774. sourceMesh.actionManager.processTrigger(BABYLON.ActionManager.OnIntersectionExitTrigger, BABYLON.ActionEvent.CreateNew(sourceMesh));
  775. var indexOfOther = sourceMesh._intersectionsInProgress.indexOf(otherMesh);
  776. if (indexOfOther > -1) {
  777. sourceMesh._intersectionsInProgress.splice(indexOfOther, 1);
  778. }
  779. }
  780. }
  781. }
  782. }
  783. };
  784. Scene.prototype.render = function () {
  785. var startDate = BABYLON.Tools.Now;
  786. this._particlesDuration = 0;
  787. this._spritesDuration = 0;
  788. this._activeParticles = 0;
  789. this._renderDuration = 0;
  790. this._evaluateActiveMeshesDuration = 0;
  791. this._totalVertices = 0;
  792. this._activeVertices = 0;
  793. this._meshesForIntersections.reset();
  794. BABYLON.Tools.StartPerformanceCounter("Scene rendering");
  795. // Actions
  796. if (this.actionManager) {
  797. this.actionManager.processTrigger(BABYLON.ActionManager.OnEveryFrameTrigger, null);
  798. }
  799. // Before render
  800. if (this.beforeRender) {
  801. this.beforeRender();
  802. }
  803. for (var callbackIndex = 0; callbackIndex < this._onBeforeRenderCallbacks.length; callbackIndex++) {
  804. this._onBeforeRenderCallbacks[callbackIndex]();
  805. }
  806. // Animations
  807. var deltaTime = Math.max(Scene.MinDeltaTime, Math.min(BABYLON.Tools.GetDeltaTime(), Scene.MaxDeltaTime));
  808. this._animationRatio = deltaTime * (60.0 / 1000.0);
  809. this._animate();
  810. // Physics
  811. if (this._physicsEngine) {
  812. BABYLON.Tools.StartPerformanceCounter("Physics");
  813. this._physicsEngine._runOneStep(deltaTime / 1000.0);
  814. BABYLON.Tools.EndPerformanceCounter("Physics");
  815. }
  816. // Customs render targets
  817. var beforeRenderTargetDate = BABYLON.Tools.Now;
  818. var engine = this.getEngine();
  819. if (this.renderTargetsEnabled) {
  820. BABYLON.Tools.StartPerformanceCounter("Custom render targets", this.customRenderTargets.length > 0);
  821. for (var customIndex = 0; customIndex < this.customRenderTargets.length; customIndex++) {
  822. var renderTarget = this.customRenderTargets[customIndex];
  823. if (renderTarget._shouldRender()) {
  824. this._renderId++;
  825. this.activeCamera = renderTarget.activeCamera || this.activeCamera;
  826. if (!this.activeCamera)
  827. throw new Error("Active camera not set");
  828. // Viewport
  829. engine.setViewport(this.activeCamera.viewport);
  830. // Camera
  831. this.updateTransformMatrix();
  832. renderTarget.render();
  833. }
  834. }
  835. BABYLON.Tools.EndPerformanceCounter("Custom render targets", this.customRenderTargets.length > 0);
  836. this._renderId++;
  837. }
  838. if (this.customRenderTargets.length > 0) {
  839. engine.restoreDefaultFramebuffer();
  840. }
  841. this._renderTargetsDuration += BABYLON.Tools.Now - beforeRenderTargetDate;
  842. // Procedural textures
  843. if (this.proceduralTexturesEnabled) {
  844. BABYLON.Tools.StartPerformanceCounter("Procedural textures", this._proceduralTextures.length > 0);
  845. for (var proceduralIndex = 0; proceduralIndex < this._proceduralTextures.length; proceduralIndex++) {
  846. var proceduralTexture = this._proceduralTextures[proceduralIndex];
  847. if (proceduralTexture._shouldRender()) {
  848. proceduralTexture.render();
  849. }
  850. }
  851. BABYLON.Tools.EndPerformanceCounter("Procedural textures", this._proceduralTextures.length > 0);
  852. }
  853. // Clear
  854. this._engine.clear(this.clearColor, this.autoClear || this.forceWireframe, true);
  855. // Shadows
  856. if (this.shadowsEnabled) {
  857. for (var lightIndex = 0; lightIndex < this.lights.length; lightIndex++) {
  858. var light = this.lights[lightIndex];
  859. var shadowGenerator = light.getShadowGenerator();
  860. if (light.isEnabled() && shadowGenerator && shadowGenerator.getShadowMap().getScene().textures.indexOf(shadowGenerator.getShadowMap()) !== -1) {
  861. this._renderTargets.push(shadowGenerator.getShadowMap());
  862. }
  863. }
  864. }
  865. // RenderPipeline
  866. this.postProcessRenderPipelineManager.update();
  867. // Multi-cameras?
  868. if (this.activeCameras.length > 0) {
  869. var currentRenderId = this._renderId;
  870. for (var cameraIndex = 0; cameraIndex < this.activeCameras.length; cameraIndex++) {
  871. this._renderId = currentRenderId;
  872. this._processSubCameras(this.activeCameras[cameraIndex]);
  873. }
  874. } else {
  875. if (!this.activeCamera) {
  876. throw new Error("No camera defined");
  877. }
  878. this._processSubCameras(this.activeCamera);
  879. }
  880. // Intersection checks
  881. this._checkIntersections();
  882. // Update the audio listener attached to the camera
  883. this._updateAudioParameters();
  884. // After render
  885. if (this.afterRender) {
  886. this.afterRender();
  887. }
  888. for (var index = 0; index < this._toBeDisposed.length; index++) {
  889. this._toBeDisposed.data[index].dispose();
  890. this._toBeDisposed[index] = null;
  891. }
  892. this._toBeDisposed.reset();
  893. BABYLON.Tools.EndPerformanceCounter("Scene rendering");
  894. this._lastFrameDuration = BABYLON.Tools.Now - startDate;
  895. };
  896. Scene.prototype._updateAudioParameters = function () {
  897. var listeningCamera;
  898. var audioEngine = this._engine.getAudioEngine();
  899. if (this.activeCameras.length > 0) {
  900. listeningCamera = this.activeCameras[0];
  901. } else {
  902. listeningCamera = this.activeCamera;
  903. }
  904. if (listeningCamera && audioEngine.canUseWebAudio) {
  905. audioEngine.audioContext.listener.setPosition(listeningCamera.position.x, listeningCamera.position.y, listeningCamera.position.z);
  906. var mat = BABYLON.Matrix.Invert(listeningCamera.getViewMatrix());
  907. var cameraDirection = BABYLON.Vector3.TransformNormal(new BABYLON.Vector3(0, 0, -1), mat);
  908. cameraDirection.normalize();
  909. audioEngine.audioContext.listener.setOrientation(cameraDirection.x, cameraDirection.y, cameraDirection.z, 0, 1, 0);
  910. }
  911. };
  912. Scene.prototype.dispose = function () {
  913. this.beforeRender = null;
  914. this.afterRender = null;
  915. this.skeletons = [];
  916. this._boundingBoxRenderer.dispose();
  917. // Events
  918. if (this.onDispose) {
  919. this.onDispose();
  920. }
  921. this._onBeforeRenderCallbacks = [];
  922. this.detachControl();
  923. // Detach cameras
  924. var canvas = this._engine.getRenderingCanvas();
  925. var index;
  926. for (index = 0; index < this.cameras.length; index++) {
  927. this.cameras[index].detachControl(canvas);
  928. }
  929. while (this.lights.length) {
  930. this.lights[0].dispose();
  931. }
  932. while (this.meshes.length) {
  933. this.meshes[0].dispose(true);
  934. }
  935. while (this.cameras.length) {
  936. this.cameras[0].dispose();
  937. }
  938. while (this.materials.length) {
  939. this.materials[0].dispose();
  940. }
  941. while (this.particleSystems.length) {
  942. this.particleSystems[0].dispose();
  943. }
  944. while (this.spriteManagers.length) {
  945. this.spriteManagers[0].dispose();
  946. }
  947. while (this.layers.length) {
  948. this.layers[0].dispose();
  949. }
  950. while (this.textures.length) {
  951. this.textures[0].dispose();
  952. }
  953. // Post-processes
  954. this.postProcessManager.dispose();
  955. // Physics
  956. if (this._physicsEngine) {
  957. this.disablePhysicsEngine();
  958. }
  959. // Remove from engine
  960. index = this._engine.scenes.indexOf(this);
  961. if (index > -1) {
  962. this._engine.scenes.splice(index, 1);
  963. }
  964. this._engine.wipeCaches();
  965. };
  966. // Collisions
  967. Scene.prototype._getNewPosition = function (position, velocity, collider, maximumRetry, finalPosition, excludedMesh) {
  968. if (typeof excludedMesh === "undefined") { excludedMesh = null; }
  969. position.divideToRef(collider.radius, this._scaledPosition);
  970. velocity.divideToRef(collider.radius, this._scaledVelocity);
  971. collider.retry = 0;
  972. collider.initialVelocity = this._scaledVelocity;
  973. collider.initialPosition = this._scaledPosition;
  974. this._collideWithWorld(this._scaledPosition, this._scaledVelocity, collider, maximumRetry, finalPosition, excludedMesh);
  975. finalPosition.multiplyInPlace(collider.radius);
  976. };
  977. Scene.prototype._collideWithWorld = function (position, velocity, collider, maximumRetry, finalPosition, excludedMesh) {
  978. if (typeof excludedMesh === "undefined") { excludedMesh = null; }
  979. var closeDistance = BABYLON.Engine.CollisionsEpsilon * 10.0;
  980. if (collider.retry >= maximumRetry) {
  981. finalPosition.copyFrom(position);
  982. return;
  983. }
  984. collider._initialize(position, velocity, closeDistance);
  985. for (var index = 0; index < this.meshes.length; index++) {
  986. var mesh = this.meshes[index];
  987. if (mesh.isEnabled() && mesh.checkCollisions && mesh.subMeshes && mesh !== excludedMesh) {
  988. mesh._checkCollision(collider);
  989. }
  990. }
  991. if (!collider.collisionFound) {
  992. position.addToRef(velocity, finalPosition);
  993. return;
  994. }
  995. if (velocity.x != 0 || velocity.y != 0 || velocity.z != 0) {
  996. collider._getResponse(position, velocity);
  997. }
  998. if (velocity.length() <= closeDistance) {
  999. finalPosition.copyFrom(position);
  1000. return;
  1001. }
  1002. collider.retry++;
  1003. this._collideWithWorld(position, velocity, collider, maximumRetry, finalPosition, excludedMesh);
  1004. };
  1005. // Octrees
  1006. Scene.prototype.createOrUpdateSelectionOctree = function (maxCapacity, maxDepth) {
  1007. if (typeof maxCapacity === "undefined") { maxCapacity = 64; }
  1008. if (typeof maxDepth === "undefined") { maxDepth = 2; }
  1009. if (!this._selectionOctree) {
  1010. this._selectionOctree = new BABYLON.Octree(BABYLON.Octree.CreationFuncForMeshes, maxCapacity, maxDepth);
  1011. }
  1012. var min = new BABYLON.Vector3(Number.MAX_VALUE, Number.MAX_VALUE, Number.MAX_VALUE);
  1013. var max = new BABYLON.Vector3(-Number.MAX_VALUE, -Number.MAX_VALUE, -Number.MAX_VALUE);
  1014. for (var index = 0; index < this.meshes.length; index++) {
  1015. var mesh = this.meshes[index];
  1016. mesh.computeWorldMatrix(true);
  1017. var minBox = mesh.getBoundingInfo().boundingBox.minimumWorld;
  1018. var maxBox = mesh.getBoundingInfo().boundingBox.maximumWorld;
  1019. BABYLON.Tools.CheckExtends(minBox, min, max);
  1020. BABYLON.Tools.CheckExtends(maxBox, min, max);
  1021. }
  1022. // Update octree
  1023. this._selectionOctree.update(min, max, this.meshes);
  1024. return this._selectionOctree;
  1025. };
  1026. // Picking
  1027. Scene.prototype.createPickingRay = function (x, y, world, camera) {
  1028. var engine = this._engine;
  1029. if (!camera) {
  1030. if (!this.activeCamera)
  1031. throw new Error("Active camera not set");
  1032. camera = this.activeCamera;
  1033. }
  1034. var cameraViewport = camera.viewport;
  1035. var viewport = cameraViewport.toGlobal(engine);
  1036. // Moving coordinates to local viewport world
  1037. x = x / this._engine.getHardwareScalingLevel() - viewport.x;
  1038. y = y / this._engine.getHardwareScalingLevel() - (this._engine.getRenderHeight() - viewport.y - viewport.height);
  1039. return BABYLON.Ray.CreateNew(x, y, viewport.width, viewport.height, world ? world : BABYLON.Matrix.Identity(), camera.getViewMatrix(), camera.getProjectionMatrix());
  1040. // return BABYLON.Ray.CreateNew(x / window.devicePixelRatio, y / window.devicePixelRatio, viewport.width, viewport.height, world ? world : BABYLON.Matrix.Identity(), camera.getViewMatrix(), camera.getProjectionMatrix());
  1041. };
  1042. Scene.prototype._internalPick = function (rayFunction, predicate, fastCheck) {
  1043. var pickingInfo = null;
  1044. for (var meshIndex = 0; meshIndex < this.meshes.length; meshIndex++) {
  1045. var mesh = this.meshes[meshIndex];
  1046. if (predicate) {
  1047. if (!predicate(mesh)) {
  1048. continue;
  1049. }
  1050. } else if (!mesh.isEnabled() || !mesh.isVisible || !mesh.isPickable) {
  1051. continue;
  1052. }
  1053. var world = mesh.getWorldMatrix();
  1054. var ray = rayFunction(world);
  1055. var result = mesh.intersects(ray, fastCheck);
  1056. if (!result || !result.hit)
  1057. continue;
  1058. if (!fastCheck && pickingInfo != null && result.distance >= pickingInfo.distance)
  1059. continue;
  1060. pickingInfo = result;
  1061. if (fastCheck) {
  1062. break;
  1063. }
  1064. }
  1065. return pickingInfo || new BABYLON.PickingInfo();
  1066. };
  1067. Scene.prototype.pick = function (x, y, predicate, fastCheck, camera) {
  1068. var _this = this;
  1069. /// <summary>Launch a ray to try to pick a mesh in the scene</summary>
  1070. /// <param name="x">X position on screen</param>
  1071. /// <param name="y">Y position on screen</param>
  1072. /// <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>
  1073. /// <param name="fastCheck">Launch a fast check only using the bounding boxes. Can be set to null.</param>
  1074. /// <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>
  1075. return this._internalPick(function (world) {
  1076. return _this.createPickingRay(x, y, world, camera);
  1077. }, predicate, fastCheck);
  1078. };
  1079. Scene.prototype.pickWithRay = function (ray, predicate, fastCheck) {
  1080. var _this = this;
  1081. return this._internalPick(function (world) {
  1082. if (!_this._pickWithRayInverseMatrix) {
  1083. _this._pickWithRayInverseMatrix = BABYLON.Matrix.Identity();
  1084. }
  1085. world.invertToRef(_this._pickWithRayInverseMatrix);
  1086. return BABYLON.Ray.Transform(ray, _this._pickWithRayInverseMatrix);
  1087. }, predicate, fastCheck);
  1088. };
  1089. Scene.prototype.setPointerOverMesh = function (mesh) {
  1090. if (this._pointerOverMesh === mesh) {
  1091. return;
  1092. }
  1093. if (this._pointerOverMesh && this._pointerOverMesh.actionManager) {
  1094. this._pointerOverMesh.actionManager.processTrigger(BABYLON.ActionManager.OnPointerOutTrigger, BABYLON.ActionEvent.CreateNew(this._pointerOverMesh));
  1095. }
  1096. this._pointerOverMesh = mesh;
  1097. if (this._pointerOverMesh && this._pointerOverMesh.actionManager) {
  1098. this._pointerOverMesh.actionManager.processTrigger(BABYLON.ActionManager.OnPointerOverTrigger, BABYLON.ActionEvent.CreateNew(this._pointerOverMesh));
  1099. }
  1100. };
  1101. Scene.prototype.getPointerOverMesh = function () {
  1102. return this._pointerOverMesh;
  1103. };
  1104. // Physics
  1105. Scene.prototype.getPhysicsEngine = function () {
  1106. return this._physicsEngine;
  1107. };
  1108. Scene.prototype.enablePhysics = function (gravity, plugin) {
  1109. if (this._physicsEngine) {
  1110. return true;
  1111. }
  1112. this._physicsEngine = new BABYLON.PhysicsEngine(plugin);
  1113. if (!this._physicsEngine.isSupported()) {
  1114. this._physicsEngine = null;
  1115. return false;
  1116. }
  1117. this._physicsEngine._initialize(gravity);
  1118. return true;
  1119. };
  1120. Scene.prototype.disablePhysicsEngine = function () {
  1121. if (!this._physicsEngine) {
  1122. return;
  1123. }
  1124. this._physicsEngine.dispose();
  1125. this._physicsEngine = undefined;
  1126. };
  1127. Scene.prototype.isPhysicsEnabled = function () {
  1128. return this._physicsEngine !== undefined;
  1129. };
  1130. Scene.prototype.setGravity = function (gravity) {
  1131. if (!this._physicsEngine) {
  1132. return;
  1133. }
  1134. this._physicsEngine._setGravity(gravity);
  1135. };
  1136. Scene.prototype.createCompoundImpostor = function (parts, options) {
  1137. if (parts.parts) {
  1138. options = parts;
  1139. parts = parts.parts;
  1140. }
  1141. if (!this._physicsEngine) {
  1142. return null;
  1143. }
  1144. for (var index = 0; index < parts.length; index++) {
  1145. var mesh = parts[index].mesh;
  1146. mesh._physicImpostor = parts[index].impostor;
  1147. mesh._physicsMass = options.mass / parts.length;
  1148. mesh._physicsFriction = options.friction;
  1149. mesh._physicRestitution = options.restitution;
  1150. }
  1151. return this._physicsEngine._registerMeshesAsCompound(parts, options);
  1152. };
  1153. //ANY
  1154. Scene.prototype.deleteCompoundImpostor = function (compound) {
  1155. for (var index = 0; index < compound.parts.length; index++) {
  1156. var mesh = compound.parts[index].mesh;
  1157. mesh._physicImpostor = BABYLON.PhysicsEngine.NoImpostor;
  1158. this._physicsEngine._unregisterMesh(mesh);
  1159. }
  1160. };
  1161. // Tags
  1162. Scene.prototype._getByTags = function (list, tagsQuery) {
  1163. if (tagsQuery === undefined) {
  1164. // returns the complete list (could be done with BABYLON.Tags.MatchesQuery but no need to have a for-loop here)
  1165. return list;
  1166. }
  1167. var listByTags = [];
  1168. for (var i in list) {
  1169. var item = list[i];
  1170. if (BABYLON.Tags.MatchesQuery(item, tagsQuery)) {
  1171. listByTags.push(item);
  1172. }
  1173. }
  1174. return listByTags;
  1175. };
  1176. Scene.prototype.getMeshesByTags = function (tagsQuery) {
  1177. return this._getByTags(this.meshes, tagsQuery);
  1178. };
  1179. Scene.prototype.getCamerasByTags = function (tagsQuery) {
  1180. return this._getByTags(this.cameras, tagsQuery);
  1181. };
  1182. Scene.prototype.getLightsByTags = function (tagsQuery) {
  1183. return this._getByTags(this.lights, tagsQuery);
  1184. };
  1185. Scene.prototype.getMaterialByTags = function (tagsQuery) {
  1186. return this._getByTags(this.materials, tagsQuery).concat(this._getByTags(this.multiMaterials, tagsQuery));
  1187. };
  1188. Scene.FOGMODE_NONE = 0;
  1189. Scene.FOGMODE_EXP = 1;
  1190. Scene.FOGMODE_EXP2 = 2;
  1191. Scene.FOGMODE_LINEAR = 3;
  1192. Scene.MinDeltaTime = 1.0;
  1193. Scene.MaxDeltaTime = 1000.0;
  1194. return Scene;
  1195. })();
  1196. BABYLON.Scene = Scene;
  1197. })(BABYLON || (BABYLON = {}));
  1198. //# sourceMappingURL=babylon.scene.js.map