babylon.scene.js 45 KB

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