babylon.scene.js 55 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468
  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. this._totalVertices += mesh.getTotalVertices();
  584. if (!mesh.isReady()) {
  585. continue;
  586. }
  587. mesh.computeWorldMatrix();
  588. mesh._preActivate();
  589. // Intersections
  590. if (mesh.actionManager && mesh.actionManager.hasSpecificTriggers([BABYLON.ActionManager.OnIntersectionEnterTrigger, BABYLON.ActionManager.OnIntersectionExitTrigger])) {
  591. this._meshesForIntersections.pushNoDuplicate(mesh);
  592. }
  593. if (mesh.isEnabled() && mesh.isVisible && mesh.visibility > 0 && ((mesh.layerMask & this.activeCamera.layerMask) != 0) && mesh.isInFrustum(this._frustumPlanes)) {
  594. this._activeMeshes.push(mesh);
  595. mesh._activate(this._renderId);
  596. this._activeMesh(mesh);
  597. }
  598. }
  599. // Particle systems
  600. var beforeParticlesDate = BABYLON.Tools.Now;
  601. if (this.particlesEnabled) {
  602. BABYLON.Tools.StartPerformanceCounter("Particles", this.particleSystems.length > 0);
  603. for (var particleIndex = 0; particleIndex < this.particleSystems.length; particleIndex++) {
  604. var particleSystem = this.particleSystems[particleIndex];
  605. if (!particleSystem.isStarted()) {
  606. continue;
  607. }
  608. if (!particleSystem.emitter.position || (particleSystem.emitter && particleSystem.emitter.isEnabled())) {
  609. this._activeParticleSystems.push(particleSystem);
  610. particleSystem.animate();
  611. }
  612. }
  613. BABYLON.Tools.EndPerformanceCounter("Particles", this.particleSystems.length > 0);
  614. }
  615. this._particlesDuration += BABYLON.Tools.Now - beforeParticlesDate;
  616. };
  617. Scene.prototype._activeMesh = function (mesh) {
  618. if (mesh.skeleton) {
  619. this._activeSkeletons.pushNoDuplicate(mesh.skeleton);
  620. }
  621. if (mesh.showBoundingBox) {
  622. this._boundingBoxRenderer.renderList.push(mesh.getBoundingInfo().boundingBox);
  623. }
  624. if (mesh.subMeshes) {
  625. // Submeshes Octrees
  626. var len;
  627. var subMeshes;
  628. if (mesh._submeshesOctree && mesh.useOctreeForRenderingSelection) {
  629. var intersections = mesh._submeshesOctree.select(this._frustumPlanes);
  630. len = intersections.length;
  631. subMeshes = intersections.data;
  632. } else {
  633. subMeshes = mesh.subMeshes;
  634. len = subMeshes.length;
  635. }
  636. for (var subIndex = 0; subIndex < len; subIndex++) {
  637. var subMesh = subMeshes[subIndex];
  638. this._evaluateSubMesh(subMesh, mesh);
  639. }
  640. }
  641. };
  642. Scene.prototype.updateTransformMatrix = function (force) {
  643. this.setTransformMatrix(this.activeCamera.getViewMatrix(), this.activeCamera.getProjectionMatrix(force));
  644. };
  645. Scene.prototype._renderForCamera = function (camera) {
  646. var engine = this._engine;
  647. this.activeCamera = camera;
  648. if (!this.activeCamera)
  649. throw new Error("Active camera not set");
  650. BABYLON.Tools.StartPerformanceCounter("Rendering camera " + this.activeCamera.name);
  651. // Viewport
  652. engine.setViewport(this.activeCamera.viewport);
  653. // Camera
  654. this._renderId++;
  655. this.updateTransformMatrix();
  656. if (this.beforeCameraRender) {
  657. this.beforeCameraRender(this.activeCamera);
  658. }
  659. // Meshes
  660. var beforeEvaluateActiveMeshesDate = BABYLON.Tools.Now;
  661. BABYLON.Tools.StartPerformanceCounter("Active meshes evaluation");
  662. this._evaluateActiveMeshes();
  663. this._evaluateActiveMeshesDuration += BABYLON.Tools.Now - beforeEvaluateActiveMeshesDate;
  664. BABYLON.Tools.EndPerformanceCounter("Active meshes evaluation");
  665. for (var skeletonIndex = 0; skeletonIndex < this._activeSkeletons.length; skeletonIndex++) {
  666. var skeleton = this._activeSkeletons.data[skeletonIndex];
  667. skeleton.prepare();
  668. }
  669. // Render targets
  670. var beforeRenderTargetDate = BABYLON.Tools.Now;
  671. if (this.renderTargetsEnabled) {
  672. BABYLON.Tools.StartPerformanceCounter("Render targets", this._renderTargets.length > 0);
  673. for (var renderIndex = 0; renderIndex < this._renderTargets.length; renderIndex++) {
  674. var renderTarget = this._renderTargets.data[renderIndex];
  675. if (renderTarget._shouldRender()) {
  676. this._renderId++;
  677. renderTarget.render();
  678. }
  679. }
  680. BABYLON.Tools.EndPerformanceCounter("Render targets", this._renderTargets.length > 0);
  681. this._renderId++;
  682. }
  683. if (this._renderTargets.length > 0) {
  684. engine.restoreDefaultFramebuffer();
  685. }
  686. this._renderTargetsDuration += BABYLON.Tools.Now - beforeRenderTargetDate;
  687. // Prepare Frame
  688. this.postProcessManager._prepareFrame();
  689. var beforeRenderDate = BABYLON.Tools.Now;
  690. // Backgrounds
  691. if (this.layers.length) {
  692. engine.setDepthBuffer(false);
  693. var layerIndex;
  694. var layer;
  695. for (layerIndex = 0; layerIndex < this.layers.length; layerIndex++) {
  696. layer = this.layers[layerIndex];
  697. if (layer.isBackground) {
  698. layer.render();
  699. }
  700. }
  701. engine.setDepthBuffer(true);
  702. }
  703. // Render
  704. BABYLON.Tools.StartPerformanceCounter("Main render");
  705. this._renderingManager.render(null, null, true, true);
  706. BABYLON.Tools.EndPerformanceCounter("Main render");
  707. // Bounding boxes
  708. this._boundingBoxRenderer.render();
  709. // Lens flares
  710. if (this.lensFlaresEnabled) {
  711. BABYLON.Tools.StartPerformanceCounter("Lens flares", this.lensFlareSystems.length > 0);
  712. for (var lensFlareSystemIndex = 0; lensFlareSystemIndex < this.lensFlareSystems.length; lensFlareSystemIndex++) {
  713. this.lensFlareSystems[lensFlareSystemIndex].render();
  714. }
  715. BABYLON.Tools.EndPerformanceCounter("Lens flares", this.lensFlareSystems.length > 0);
  716. }
  717. // Foregrounds
  718. if (this.layers.length) {
  719. engine.setDepthBuffer(false);
  720. for (layerIndex = 0; layerIndex < this.layers.length; layerIndex++) {
  721. layer = this.layers[layerIndex];
  722. if (!layer.isBackground) {
  723. layer.render();
  724. }
  725. }
  726. engine.setDepthBuffer(true);
  727. }
  728. this._renderDuration += BABYLON.Tools.Now - beforeRenderDate;
  729. // Finalize frame
  730. this.postProcessManager._finalizeFrame(camera.isIntermediate);
  731. // Update camera
  732. this.activeCamera._updateFromScene();
  733. // Reset some special arrays
  734. this._renderTargets.reset();
  735. if (this.afterCameraRender) {
  736. this.afterCameraRender(this.activeCamera);
  737. }
  738. BABYLON.Tools.EndPerformanceCounter("Rendering camera " + this.activeCamera.name);
  739. };
  740. Scene.prototype._processSubCameras = function (camera) {
  741. if (camera.subCameras.length == 0) {
  742. this._renderForCamera(camera);
  743. return;
  744. }
  745. for (var index = 0; index < camera.subCameras.length; index++) {
  746. this._renderForCamera(camera.subCameras[index]);
  747. }
  748. this.activeCamera = camera;
  749. this.setTransformMatrix(this.activeCamera.getViewMatrix(), this.activeCamera.getProjectionMatrix());
  750. // Update camera
  751. this.activeCamera._updateFromScene();
  752. };
  753. Scene.prototype._checkIntersections = function () {
  754. for (var index = 0; index < this._meshesForIntersections.length; index++) {
  755. var sourceMesh = this._meshesForIntersections.data[index];
  756. for (var actionIndex = 0; actionIndex < sourceMesh.actionManager.actions.length; actionIndex++) {
  757. var action = sourceMesh.actionManager.actions[actionIndex];
  758. if (action.trigger == BABYLON.ActionManager.OnIntersectionEnterTrigger || action.trigger == BABYLON.ActionManager.OnIntersectionExitTrigger) {
  759. var otherMesh = action.getTriggerParameter();
  760. var areIntersecting = otherMesh.intersectsMesh(sourceMesh, false);
  761. var currentIntersectionInProgress = sourceMesh._intersectionsInProgress.indexOf(otherMesh);
  762. if (areIntersecting && currentIntersectionInProgress === -1 && action.trigger == BABYLON.ActionManager.OnIntersectionEnterTrigger) {
  763. sourceMesh.actionManager.processTrigger(BABYLON.ActionManager.OnIntersectionEnterTrigger, BABYLON.ActionEvent.CreateNew(sourceMesh));
  764. sourceMesh._intersectionsInProgress.push(otherMesh);
  765. } else if (!areIntersecting && currentIntersectionInProgress > -1 && action.trigger == BABYLON.ActionManager.OnIntersectionExitTrigger) {
  766. sourceMesh.actionManager.processTrigger(BABYLON.ActionManager.OnIntersectionExitTrigger, BABYLON.ActionEvent.CreateNew(sourceMesh));
  767. var indexOfOther = sourceMesh._intersectionsInProgress.indexOf(otherMesh);
  768. if (indexOfOther > -1) {
  769. sourceMesh._intersectionsInProgress.splice(indexOfOther, 1);
  770. }
  771. }
  772. }
  773. }
  774. }
  775. };
  776. Scene.prototype.render = function () {
  777. var startDate = BABYLON.Tools.Now;
  778. this._particlesDuration = 0;
  779. this._spritesDuration = 0;
  780. this._activeParticles = 0;
  781. this._renderDuration = 0;
  782. this._evaluateActiveMeshesDuration = 0;
  783. this._totalVertices = 0;
  784. this._activeVertices = 0;
  785. this._meshesForIntersections.reset();
  786. BABYLON.Tools.StartPerformanceCounter("Scene rendering");
  787. // Actions
  788. if (this.actionManager) {
  789. this.actionManager.processTrigger(BABYLON.ActionManager.OnEveryFrameTrigger, null);
  790. }
  791. // Before render
  792. if (this.beforeRender) {
  793. this.beforeRender();
  794. }
  795. for (var callbackIndex = 0; callbackIndex < this._onBeforeRenderCallbacks.length; callbackIndex++) {
  796. this._onBeforeRenderCallbacks[callbackIndex]();
  797. }
  798. // Animations
  799. var deltaTime = Math.max(Scene.MinDeltaTime, Math.min(BABYLON.Tools.GetDeltaTime(), Scene.MaxDeltaTime));
  800. this._animationRatio = deltaTime * (60.0 / 1000.0);
  801. this._animate();
  802. // Physics
  803. if (this._physicsEngine) {
  804. BABYLON.Tools.StartPerformanceCounter("Physics");
  805. this._physicsEngine._runOneStep(deltaTime / 1000.0);
  806. BABYLON.Tools.EndPerformanceCounter("Physics");
  807. }
  808. // Customs render targets
  809. var beforeRenderTargetDate = BABYLON.Tools.Now;
  810. var engine = this.getEngine();
  811. if (this.renderTargetsEnabled) {
  812. BABYLON.Tools.StartPerformanceCounter("Custom render targets", this.customRenderTargets.length > 0);
  813. for (var customIndex = 0; customIndex < this.customRenderTargets.length; customIndex++) {
  814. var renderTarget = this.customRenderTargets[customIndex];
  815. if (renderTarget._shouldRender()) {
  816. this._renderId++;
  817. this.activeCamera = renderTarget.activeCamera || this.activeCamera;
  818. if (!this.activeCamera)
  819. throw new Error("Active camera not set");
  820. // Viewport
  821. engine.setViewport(this.activeCamera.viewport);
  822. // Camera
  823. this.updateTransformMatrix();
  824. renderTarget.render();
  825. }
  826. }
  827. BABYLON.Tools.EndPerformanceCounter("Custom render targets", this.customRenderTargets.length > 0);
  828. this._renderId++;
  829. }
  830. if (this.customRenderTargets.length > 0) {
  831. engine.restoreDefaultFramebuffer();
  832. }
  833. this._renderTargetsDuration += BABYLON.Tools.Now - beforeRenderTargetDate;
  834. // Procedural textures
  835. if (this.proceduralTexturesEnabled) {
  836. BABYLON.Tools.StartPerformanceCounter("Procedural textures", this._proceduralTextures.length > 0);
  837. for (var proceduralIndex = 0; proceduralIndex < this._proceduralTextures.length; proceduralIndex++) {
  838. var proceduralTexture = this._proceduralTextures[proceduralIndex];
  839. if (proceduralTexture._shouldRender()) {
  840. proceduralTexture.render();
  841. }
  842. }
  843. BABYLON.Tools.EndPerformanceCounter("Procedural textures", this._proceduralTextures.length > 0);
  844. }
  845. // Clear
  846. this._engine.clear(this.clearColor, this.autoClear || this.forceWireframe, true);
  847. // Shadows
  848. if (this.shadowsEnabled) {
  849. for (var lightIndex = 0; lightIndex < this.lights.length; lightIndex++) {
  850. var light = this.lights[lightIndex];
  851. var shadowGenerator = light.getShadowGenerator();
  852. if (light.isEnabled() && shadowGenerator && shadowGenerator.getShadowMap().getScene().textures.indexOf(shadowGenerator.getShadowMap()) !== -1) {
  853. this._renderTargets.push(shadowGenerator.getShadowMap());
  854. }
  855. }
  856. }
  857. // RenderPipeline
  858. this.postProcessRenderPipelineManager.update();
  859. // Multi-cameras?
  860. if (this.activeCameras.length > 0) {
  861. var currentRenderId = this._renderId;
  862. for (var cameraIndex = 0; cameraIndex < this.activeCameras.length; cameraIndex++) {
  863. this._renderId = currentRenderId;
  864. this._processSubCameras(this.activeCameras[cameraIndex]);
  865. }
  866. } else {
  867. if (!this.activeCamera) {
  868. throw new Error("No camera defined");
  869. }
  870. this._processSubCameras(this.activeCamera);
  871. }
  872. // Intersection checks
  873. this._checkIntersections();
  874. // After render
  875. if (this.afterRender) {
  876. this.afterRender();
  877. }
  878. for (var index = 0; index < this._toBeDisposed.length; index++) {
  879. this._toBeDisposed.data[index].dispose();
  880. this._toBeDisposed[index] = null;
  881. }
  882. this._toBeDisposed.reset();
  883. BABYLON.Tools.EndPerformanceCounter("Scene rendering");
  884. this._lastFrameDuration = BABYLON.Tools.Now - startDate;
  885. };
  886. Scene.prototype.dispose = function () {
  887. this.beforeRender = null;
  888. this.afterRender = null;
  889. this.skeletons = [];
  890. this._boundingBoxRenderer.dispose();
  891. // Events
  892. if (this.onDispose) {
  893. this.onDispose();
  894. }
  895. this.detachControl();
  896. // Detach cameras
  897. var canvas = this._engine.getRenderingCanvas();
  898. var index;
  899. for (index = 0; index < this.cameras.length; index++) {
  900. this.cameras[index].detachControl(canvas);
  901. }
  902. while (this.lights.length) {
  903. this.lights[0].dispose();
  904. }
  905. while (this.meshes.length) {
  906. this.meshes[0].dispose(true);
  907. }
  908. while (this.cameras.length) {
  909. this.cameras[0].dispose();
  910. }
  911. while (this.materials.length) {
  912. this.materials[0].dispose();
  913. }
  914. while (this.particleSystems.length) {
  915. this.particleSystems[0].dispose();
  916. }
  917. while (this.spriteManagers.length) {
  918. this.spriteManagers[0].dispose();
  919. }
  920. while (this.layers.length) {
  921. this.layers[0].dispose();
  922. }
  923. while (this.textures.length) {
  924. this.textures[0].dispose();
  925. }
  926. // Post-processes
  927. this.postProcessManager.dispose();
  928. // Physics
  929. if (this._physicsEngine) {
  930. this.disablePhysicsEngine();
  931. }
  932. // Remove from engine
  933. index = this._engine.scenes.indexOf(this);
  934. if (index > -1) {
  935. this._engine.scenes.splice(index, 1);
  936. }
  937. this._engine.wipeCaches();
  938. };
  939. // Collisions
  940. Scene.prototype._getNewPosition = function (position, velocity, collider, maximumRetry, finalPosition, excludedMesh) {
  941. if (typeof excludedMesh === "undefined") { excludedMesh = null; }
  942. position.divideToRef(collider.radius, this._scaledPosition);
  943. velocity.divideToRef(collider.radius, this._scaledVelocity);
  944. collider.retry = 0;
  945. collider.initialVelocity = this._scaledVelocity;
  946. collider.initialPosition = this._scaledPosition;
  947. this._collideWithWorld(this._scaledPosition, this._scaledVelocity, collider, maximumRetry, finalPosition, excludedMesh);
  948. finalPosition.multiplyInPlace(collider.radius);
  949. };
  950. Scene.prototype._collideWithWorld = function (position, velocity, collider, maximumRetry, finalPosition, excludedMesh) {
  951. if (typeof excludedMesh === "undefined") { excludedMesh = null; }
  952. var closeDistance = BABYLON.Engine.CollisionsEpsilon * 10.0;
  953. if (collider.retry >= maximumRetry) {
  954. finalPosition.copyFrom(position);
  955. return;
  956. }
  957. collider._initialize(position, velocity, closeDistance);
  958. for (var index = 0; index < this.meshes.length; index++) {
  959. var mesh = this.meshes[index];
  960. if (mesh.isEnabled() && mesh.checkCollisions && mesh.subMeshes && mesh !== excludedMesh) {
  961. mesh._checkCollision(collider);
  962. }
  963. }
  964. if (!collider.collisionFound) {
  965. position.addToRef(velocity, finalPosition);
  966. return;
  967. }
  968. if (velocity.x != 0 || velocity.y != 0 || velocity.z != 0) {
  969. collider._getResponse(position, velocity);
  970. }
  971. if (velocity.length() <= closeDistance) {
  972. finalPosition.copyFrom(position);
  973. return;
  974. }
  975. collider.retry++;
  976. this._collideWithWorld(position, velocity, collider, maximumRetry, finalPosition, excludedMesh);
  977. };
  978. // Octrees
  979. Scene.prototype.createOrUpdateSelectionOctree = function (maxCapacity, maxDepth) {
  980. if (typeof maxCapacity === "undefined") { maxCapacity = 64; }
  981. if (typeof maxDepth === "undefined") { maxDepth = 2; }
  982. if (!this._selectionOctree) {
  983. this._selectionOctree = new BABYLON.Octree(BABYLON.Octree.CreationFuncForMeshes, maxCapacity, maxDepth);
  984. }
  985. var min = new BABYLON.Vector3(Number.MAX_VALUE, Number.MAX_VALUE, Number.MAX_VALUE);
  986. var max = new BABYLON.Vector3(-Number.MAX_VALUE, -Number.MAX_VALUE, -Number.MAX_VALUE);
  987. for (var index = 0; index < this.meshes.length; index++) {
  988. var mesh = this.meshes[index];
  989. mesh.computeWorldMatrix(true);
  990. var minBox = mesh.getBoundingInfo().boundingBox.minimumWorld;
  991. var maxBox = mesh.getBoundingInfo().boundingBox.maximumWorld;
  992. BABYLON.Tools.CheckExtends(minBox, min, max);
  993. BABYLON.Tools.CheckExtends(maxBox, min, max);
  994. }
  995. // Update octree
  996. this._selectionOctree.update(min, max, this.meshes);
  997. return this._selectionOctree;
  998. };
  999. // Picking
  1000. Scene.prototype.createPickingRay = function (x, y, world, camera) {
  1001. var engine = this._engine;
  1002. if (!camera) {
  1003. if (!this.activeCamera)
  1004. throw new Error("Active camera not set");
  1005. camera = this.activeCamera;
  1006. }
  1007. var cameraViewport = camera.viewport;
  1008. var viewport = cameraViewport.toGlobal(engine);
  1009. // Moving coordinates to local viewport world
  1010. x = x / this._engine.getHardwareScalingLevel() - viewport.x;
  1011. y = y / this._engine.getHardwareScalingLevel() - (this._engine.getRenderHeight() - viewport.y - viewport.height);
  1012. return BABYLON.Ray.CreateNew(x, y, viewport.width, viewport.height, world ? world : BABYLON.Matrix.Identity(), camera.getViewMatrix(), camera.getProjectionMatrix());
  1013. // return BABYLON.Ray.CreateNew(x / window.devicePixelRatio, y / window.devicePixelRatio, viewport.width, viewport.height, world ? world : BABYLON.Matrix.Identity(), camera.getViewMatrix(), camera.getProjectionMatrix());
  1014. };
  1015. Scene.prototype._internalPick = function (rayFunction, predicate, fastCheck) {
  1016. var pickingInfo = null;
  1017. for (var meshIndex = 0; meshIndex < this.meshes.length; meshIndex++) {
  1018. var mesh = this.meshes[meshIndex];
  1019. if (predicate) {
  1020. if (!predicate(mesh)) {
  1021. continue;
  1022. }
  1023. } else if (!mesh.isEnabled() || !mesh.isVisible || !mesh.isPickable) {
  1024. continue;
  1025. }
  1026. var world = mesh.getWorldMatrix();
  1027. var ray = rayFunction(world);
  1028. var result = mesh.intersects(ray, fastCheck);
  1029. if (!result || !result.hit)
  1030. continue;
  1031. if (!fastCheck && pickingInfo != null && result.distance >= pickingInfo.distance)
  1032. continue;
  1033. pickingInfo = result;
  1034. if (fastCheck) {
  1035. break;
  1036. }
  1037. }
  1038. return pickingInfo || new BABYLON.PickingInfo();
  1039. };
  1040. Scene.prototype.pick = function (x, y, predicate, fastCheck, camera) {
  1041. var _this = this;
  1042. /// <summary>Launch a ray to try to pick a mesh in the scene</summary>
  1043. /// <param name="x">X position on screen</param>
  1044. /// <param name="y">Y position on screen</param>
  1045. /// <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>
  1046. /// <param name="fastCheck">Launch a fast check only using the bounding boxes. Can be set to null.</param>
  1047. /// <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>
  1048. return this._internalPick(function (world) {
  1049. return _this.createPickingRay(x, y, world, camera);
  1050. }, predicate, fastCheck);
  1051. };
  1052. Scene.prototype.pickWithRay = function (ray, predicate, fastCheck) {
  1053. var _this = this;
  1054. return this._internalPick(function (world) {
  1055. if (!_this._pickWithRayInverseMatrix) {
  1056. _this._pickWithRayInverseMatrix = BABYLON.Matrix.Identity();
  1057. }
  1058. world.invertToRef(_this._pickWithRayInverseMatrix);
  1059. return BABYLON.Ray.Transform(ray, _this._pickWithRayInverseMatrix);
  1060. }, predicate, fastCheck);
  1061. };
  1062. Scene.prototype.setPointerOverMesh = function (mesh) {
  1063. if (this._pointerOverMesh === mesh) {
  1064. return;
  1065. }
  1066. if (this._pointerOverMesh && this._pointerOverMesh.actionManager) {
  1067. this._pointerOverMesh.actionManager.processTrigger(BABYLON.ActionManager.OnPointerOutTrigger, BABYLON.ActionEvent.CreateNew(this._pointerOverMesh));
  1068. }
  1069. this._pointerOverMesh = mesh;
  1070. if (this._pointerOverMesh && this._pointerOverMesh.actionManager) {
  1071. this._pointerOverMesh.actionManager.processTrigger(BABYLON.ActionManager.OnPointerOverTrigger, BABYLON.ActionEvent.CreateNew(this._pointerOverMesh));
  1072. }
  1073. };
  1074. Scene.prototype.getPointerOverMesh = function () {
  1075. return this._pointerOverMesh;
  1076. };
  1077. // Physics
  1078. Scene.prototype.getPhysicsEngine = function () {
  1079. return this._physicsEngine;
  1080. };
  1081. Scene.prototype.enablePhysics = function (gravity, plugin) {
  1082. if (this._physicsEngine) {
  1083. return true;
  1084. }
  1085. this._physicsEngine = new BABYLON.PhysicsEngine(plugin);
  1086. if (!this._physicsEngine.isSupported()) {
  1087. this._physicsEngine = null;
  1088. return false;
  1089. }
  1090. this._physicsEngine._initialize(gravity);
  1091. return true;
  1092. };
  1093. Scene.prototype.disablePhysicsEngine = function () {
  1094. if (!this._physicsEngine) {
  1095. return;
  1096. }
  1097. this._physicsEngine.dispose();
  1098. this._physicsEngine = undefined;
  1099. };
  1100. Scene.prototype.isPhysicsEnabled = function () {
  1101. return this._physicsEngine !== undefined;
  1102. };
  1103. Scene.prototype.setGravity = function (gravity) {
  1104. if (!this._physicsEngine) {
  1105. return;
  1106. }
  1107. this._physicsEngine._setGravity(gravity);
  1108. };
  1109. Scene.prototype.createCompoundImpostor = function (parts, options) {
  1110. if (parts.parts) {
  1111. options = parts;
  1112. parts = parts.parts;
  1113. }
  1114. if (!this._physicsEngine) {
  1115. return null;
  1116. }
  1117. for (var index = 0; index < parts.length; index++) {
  1118. var mesh = parts[index].mesh;
  1119. mesh._physicImpostor = parts[index].impostor;
  1120. mesh._physicsMass = options.mass / parts.length;
  1121. mesh._physicsFriction = options.friction;
  1122. mesh._physicRestitution = options.restitution;
  1123. }
  1124. return this._physicsEngine._registerMeshesAsCompound(parts, options);
  1125. };
  1126. //ANY
  1127. Scene.prototype.deleteCompoundImpostor = function (compound) {
  1128. for (var index = 0; index < compound.parts.length; index++) {
  1129. var mesh = compound.parts[index].mesh;
  1130. mesh._physicImpostor = BABYLON.PhysicsEngine.NoImpostor;
  1131. this._physicsEngine._unregisterMesh(mesh);
  1132. }
  1133. };
  1134. // Tags
  1135. Scene.prototype._getByTags = function (list, tagsQuery) {
  1136. if (tagsQuery === undefined) {
  1137. // returns the complete list (could be done with BABYLON.Tags.MatchesQuery but no need to have a for-loop here)
  1138. return list;
  1139. }
  1140. var listByTags = [];
  1141. for (var i in list) {
  1142. var item = list[i];
  1143. if (BABYLON.Tags.MatchesQuery(item, tagsQuery)) {
  1144. listByTags.push(item);
  1145. }
  1146. }
  1147. return listByTags;
  1148. };
  1149. Scene.prototype.getMeshesByTags = function (tagsQuery) {
  1150. return this._getByTags(this.meshes, tagsQuery);
  1151. };
  1152. Scene.prototype.getCamerasByTags = function (tagsQuery) {
  1153. return this._getByTags(this.cameras, tagsQuery);
  1154. };
  1155. Scene.prototype.getLightsByTags = function (tagsQuery) {
  1156. return this._getByTags(this.lights, tagsQuery);
  1157. };
  1158. Scene.prototype.getMaterialByTags = function (tagsQuery) {
  1159. return this._getByTags(this.materials, tagsQuery).concat(this._getByTags(this.multiMaterials, tagsQuery));
  1160. };
  1161. Scene.FOGMODE_NONE = 0;
  1162. Scene.FOGMODE_EXP = 1;
  1163. Scene.FOGMODE_EXP2 = 2;
  1164. Scene.FOGMODE_LINEAR = 3;
  1165. Scene.MinDeltaTime = 1.0;
  1166. Scene.MaxDeltaTime = 1000.0;
  1167. return Scene;
  1168. })();
  1169. BABYLON.Scene = Scene;
  1170. })(BABYLON || (BABYLON = {}));
  1171. //# sourceMappingURL=babylon.scene.js.map