babylon.scene.js 46 KB

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