babylon.scene.js 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955
  1. "use strict";
  2. var BABYLON = BABYLON || {};
  3. (function () {
  4. BABYLON.Scene = function (engine) {
  5. this._engine = engine;
  6. this.autoClear = true;
  7. this.clearColor = new BABYLON.Color3(0.2, 0.2, 0.3);
  8. this.ambientColor = new BABYLON.Color3(0, 0, 0);
  9. engine.scenes.push(this);
  10. this._totalVertices = 0;
  11. this._activeVertices = 0;
  12. this._activeParticles = 0;
  13. this._lastFrameDuration = 0;
  14. this._evaluateActiveMeshesDuration = 0;
  15. this._renderTargetsDuration = 0;
  16. this._renderDuration = 0;
  17. this._renderId = 0;
  18. this._executeWhenReadyTimeoutId = -1;
  19. this._toBeDisposed = new BABYLON.Tools.SmartArray(256);
  20. this._onReadyCallbacks = [];
  21. this._pendingData = [];
  22. this._onBeforeRenderCallbacks = [];
  23. // Fog
  24. this.fogMode = BABYLON.Scene.FOGMODE_NONE;
  25. this.fogColor = new BABYLON.Color3(0.2, 0.2, 0.3);
  26. this.fogDensity = 0.1;
  27. this.fogStart = 0;
  28. this.fogEnd = 1000.0;
  29. // Lights
  30. this.lights = [];
  31. // Cameras
  32. this.cameras = [];
  33. this.activeCamera = null;
  34. // Meshes
  35. this.meshes = [];
  36. // Internal smart arrays
  37. this._activeMeshes = new BABYLON.Tools.SmartArray(256);
  38. this._processedMaterials = new BABYLON.Tools.SmartArray(256);
  39. this._renderTargets = new BABYLON.Tools.SmartArray(256);
  40. this._activeParticleSystems = new BABYLON.Tools.SmartArray(256);
  41. this._activeSkeletons = new BABYLON.Tools.SmartArray(32);
  42. // Rendering groups
  43. this._renderingManager = new BABYLON.RenderingManager(this);
  44. // Materials
  45. this.materials = [];
  46. this.multiMaterials = [];
  47. this.defaultMaterial = new BABYLON.StandardMaterial("default material", this);
  48. // Textures
  49. this.textures = [];
  50. // Particles
  51. this.particlesEnabled = true;
  52. this.particleSystems = [];
  53. // Sprites
  54. this.spriteManagers = [];
  55. // Layers
  56. this.layers = [];
  57. // Skeletons
  58. this.skeletons = [];
  59. // Lens flares
  60. this.lensFlareSystems = [];
  61. // Collisions
  62. this.collisionsEnabled = true;
  63. this.gravity = new BABYLON.Vector3(0, -9.0, 0);
  64. // Animations
  65. this._activeAnimatables = [];
  66. // Matrices
  67. this._transformMatrix = BABYLON.Matrix.Zero();
  68. // Internals
  69. this._scaledPosition = BABYLON.Vector3.Zero();
  70. this._scaledVelocity = BABYLON.Vector3.Zero();
  71. // Postprocesses
  72. this.postProcessesEnabled = true;
  73. this.postProcessManager = new BABYLON.PostProcessManager(this);
  74. // Customs render targets
  75. this.customRenderTargets = [];
  76. // Multi-cameras
  77. this.activeCameras = [];
  78. };
  79. // Properties
  80. BABYLON.Scene.prototype.getEngine = function () {
  81. return this._engine;
  82. };
  83. BABYLON.Scene.prototype.getTotalVertices = function () {
  84. return this._totalVertices;
  85. };
  86. BABYLON.Scene.prototype.getActiveVertices = function () {
  87. return this._activeVertices;
  88. };
  89. BABYLON.Scene.prototype.getTotalVertices = function () {
  90. return this._totalVertices;
  91. };
  92. BABYLON.Scene.prototype.getActiveParticles = function () {
  93. return this._activeParticles;
  94. };
  95. // Stats
  96. BABYLON.Scene.prototype.getLastFrameDuration = function () {
  97. return this._lastFrameDuration;
  98. };
  99. BABYLON.Scene.prototype.getEvaluateActiveMeshesDuration = function () {
  100. return this._evaluateActiveMeshesDuration;
  101. };
  102. BABYLON.Scene.prototype.getRenderTargetsDuration = function () {
  103. return this._renderTargetsDuration;
  104. };
  105. BABYLON.Scene.prototype.getRenderDuration = function () {
  106. return this._renderDuration;
  107. };
  108. BABYLON.Scene.prototype.getParticlesDuration = function () {
  109. return this._particlesDuration;
  110. };
  111. BABYLON.Scene.prototype.getSpritesDuration = function () {
  112. return this._spritesDuration;
  113. };
  114. BABYLON.Scene.prototype.getAnimationRatio = function () {
  115. return this._animationRatio;
  116. };
  117. BABYLON.Scene.prototype.getRenderId = function () {
  118. return this._renderId;
  119. };
  120. // Ready
  121. BABYLON.Scene.prototype.isReady = function () {
  122. if (this._pendingData.length > 0) {
  123. return false;
  124. }
  125. for (var index = 0; index < this.meshes.length; index++) {
  126. var mesh = this.meshes[index];
  127. var mat = mesh.material;
  128. if (mesh.delayLoadState === BABYLON.Engine.DELAYLOADSTATE_LOADING) {
  129. return false;
  130. }
  131. if (mat) {
  132. if (!mat.isReady(mesh)) {
  133. return false;
  134. }
  135. }
  136. }
  137. return true;
  138. };
  139. BABYLON.Scene.prototype.registerBeforeRender = function (func) {
  140. this._onBeforeRenderCallbacks.push(func);
  141. };
  142. BABYLON.Scene.prototype.unregisterBeforeRender = function (func) {
  143. var index = this._onBeforeRenderCallbacks.indexOf(func);
  144. if (index > -1) {
  145. this._onBeforeRenderCallbacks.splice(index, 1);
  146. }
  147. };
  148. BABYLON.Scene.prototype._addPendingData = function (data) {
  149. this._pendingData.push(data);
  150. };
  151. BABYLON.Scene.prototype._removePendingData = function (data) {
  152. var index = this._pendingData.indexOf(data);
  153. if (index !== -1) {
  154. this._pendingData.splice(index, 1);
  155. }
  156. };
  157. BABYLON.Scene.prototype.getWaitingItemsCount = function () {
  158. return this._pendingData.length;
  159. };
  160. BABYLON.Scene.prototype.executeWhenReady = function (func) {
  161. this._onReadyCallbacks.push(func);
  162. if (this._executeWhenReadyTimeoutId !== -1) {
  163. return;
  164. }
  165. var that = this;
  166. this._executeWhenReadyTimeoutId = setTimeout(function () {
  167. that._checkIsReady();
  168. }, 150);
  169. };
  170. BABYLON.Scene.prototype._checkIsReady = function () {
  171. if (this.isReady()) {
  172. this._onReadyCallbacks.forEach(function (func) {
  173. func();
  174. });
  175. this._onReadyCallbacks = [];
  176. this._executeWhenReadyTimeoutId = -1;
  177. return;
  178. }
  179. var that = this;
  180. this._executeWhenReadyTimeoutId = setTimeout(function () {
  181. that._checkIsReady();
  182. }, 150);
  183. };
  184. // Animations
  185. BABYLON.Scene.prototype.beginAnimation = function (target, from, to, loop, speedRatio, onAnimationEnd) {
  186. if (speedRatio === undefined) {
  187. speedRatio = 1.0;
  188. }
  189. // Local animations
  190. if (target.animations) {
  191. this.stopAnimation(target);
  192. var animatable = new BABYLON._Animatable(target, from, to, loop, speedRatio, onAnimationEnd);
  193. this._activeAnimatables.push(animatable);
  194. }
  195. // Children animations
  196. if (target.getAnimatables) {
  197. var animatables = target.getAnimatables();
  198. for (var index = 0; index < animatables.length; index++) {
  199. this.beginAnimation(animatables[index], from, to, loop, speedRatio, onAnimationEnd);
  200. }
  201. }
  202. };
  203. BABYLON.Scene.prototype.stopAnimation = function (target) {
  204. for (var index = 0; index < this._activeAnimatables.length; index++) {
  205. if (this._activeAnimatables[index].target === target) {
  206. this._activeAnimatables.splice(index, 1);
  207. return;
  208. }
  209. }
  210. };
  211. BABYLON.Scene.prototype._animate = function () {
  212. if (!this._animationStartDate) {
  213. this._animationStartDate = new Date();
  214. }
  215. // Getting time
  216. var now = new Date();
  217. var delay = now - this._animationStartDate;
  218. for (var index = 0; index < this._activeAnimatables.length; index++) {
  219. if (!this._activeAnimatables[index]._animate(delay)) {
  220. this._activeAnimatables.splice(index, 1);
  221. index--;
  222. }
  223. }
  224. };
  225. // Matrix
  226. BABYLON.Scene.prototype.getViewMatrix = function () {
  227. return this._viewMatrix;
  228. };
  229. BABYLON.Scene.prototype.getProjectionMatrix = function () {
  230. return this._projectionMatrix;
  231. };
  232. BABYLON.Scene.prototype.getTransformMatrix = function () {
  233. return this._transformMatrix;
  234. };
  235. BABYLON.Scene.prototype.setTransformMatrix = function (view, projection) {
  236. this._viewMatrix = view;
  237. this._projectionMatrix = projection;
  238. this._viewMatrix.multiplyToRef(this._projectionMatrix, this._transformMatrix);
  239. };
  240. // Methods
  241. BABYLON.Scene.prototype.activeCameraByID = function (id) {
  242. for (var index = 0; index < this.cameras.length; index++) {
  243. if (this.cameras[index].id === id) {
  244. this.activeCamera = this.cameras[index];
  245. return;
  246. }
  247. }
  248. };
  249. BABYLON.Scene.prototype.getMaterialByID = function (id) {
  250. for (var index = 0; index < this.materials.length; index++) {
  251. if (this.materials[index].id === id) {
  252. return this.materials[index];
  253. }
  254. }
  255. return null;
  256. };
  257. BABYLON.Scene.prototype.getMaterialByName = function (name) {
  258. for (var index = 0; index < this.materials.length; index++) {
  259. if (this.materials[index].name === name) {
  260. return this.materials[index];
  261. }
  262. }
  263. return null;
  264. };
  265. BABYLON.Scene.prototype.getCameraByName = function (name) {
  266. for (var index = 0; index < this.cameras.length; index++) {
  267. if (this.cameras[index].name === name) {
  268. return this.cameras[index];
  269. }
  270. }
  271. return null;
  272. };
  273. BABYLON.Scene.prototype.getLightByID = function (id) {
  274. for (var index = 0; index < this.lights.length; index++) {
  275. if (this.lights[index].id === id) {
  276. return this.lights[index];
  277. }
  278. }
  279. return null;
  280. };
  281. BABYLON.Scene.prototype.getMeshByID = function (id) {
  282. for (var index = 0; index < this.meshes.length; index++) {
  283. if (this.meshes[index].id === id) {
  284. return this.meshes[index];
  285. }
  286. }
  287. return null;
  288. };
  289. BABYLON.Scene.prototype.getLastMeshByID = function (id) {
  290. for (var index = this.meshes.length - 1; index >= 0 ; index--) {
  291. if (this.meshes[index].id === id) {
  292. return this.meshes[index];
  293. }
  294. }
  295. return null;
  296. };
  297. BABYLON.Scene.prototype.getLastEntryByID = function (id) {
  298. for (var index = this.meshes.length - 1; index >= 0 ; index--) {
  299. if (this.meshes[index].id === id) {
  300. return this.meshes[index];
  301. }
  302. }
  303. for (var index = this.cameras.length - 1; index >= 0 ; index--) {
  304. if (this.cameras[index].id === id) {
  305. return this.cameras[index];
  306. }
  307. }
  308. for (var index = this.lights.length - 1; index >= 0 ; index--) {
  309. if (this.lights[index].id === id) {
  310. return this.lights[index];
  311. }
  312. }
  313. return null;
  314. };
  315. BABYLON.Scene.prototype.getMeshByName = function (name) {
  316. for (var index = 0; index < this.meshes.length; index++) {
  317. if (this.meshes[index].name === name) {
  318. return this.meshes[index];
  319. }
  320. }
  321. return null;
  322. };
  323. BABYLON.Scene.prototype.getLastSkeletonByID = function (id) {
  324. for (var index = this.skeletons.length - 1; index >= 0 ; index--) {
  325. if (this.skeletons[index].id === id) {
  326. return this.skeletons[index];
  327. }
  328. }
  329. return null;
  330. };
  331. BABYLON.Scene.prototype.getSkeletonById = function (id) {
  332. for (var index = 0; index < this.skeletons.length; index++) {
  333. if (this.skeletons[index].id === id) {
  334. return this.skeletons[index];
  335. }
  336. }
  337. return null;
  338. };
  339. BABYLON.Scene.prototype.getSkeletonByName = function (name) {
  340. for (var index = 0; index < this.skeleton.length; index++) {
  341. if (this.skeletons[index].name === name) {
  342. return this.skeletons[index];
  343. }
  344. }
  345. return null;
  346. };
  347. BABYLON.Scene.prototype.isActiveMesh = function (mesh) {
  348. return (this._activeMeshes.indexOf(mesh) !== -1);
  349. };
  350. BABYLON.Scene.prototype._evaluateSubMesh = function (subMesh, mesh) {
  351. if (mesh.subMeshes.length == 1 || subMesh.isInFrustrum(this._frustumPlanes)) {
  352. var material = subMesh.getMaterial();
  353. if (material) {
  354. // Render targets
  355. if (material.getRenderTargetTextures) {
  356. if (this._processedMaterials.indexOf(material) === -1) {
  357. this._processedMaterials.push(material);
  358. this._renderTargets.concat(material.getRenderTargetTextures());
  359. }
  360. }
  361. // Dispatch
  362. this._activeVertices += subMesh.verticesCount;
  363. this._renderingManager.dispatch(subMesh);
  364. }
  365. }
  366. };
  367. BABYLON.Scene.prototype._evaluateActiveMeshes = function () {
  368. this._activeMeshes.reset();
  369. this._renderingManager.reset();
  370. this._processedMaterials.reset();
  371. this._activeParticleSystems.reset();
  372. this._activeSkeletons.reset();
  373. if (!this._frustumPlanes) {
  374. this._frustumPlanes = BABYLON.Frustum.GetPlanes(this._transformMatrix);
  375. } else {
  376. BABYLON.Frustum.GetPlanesToRef(this._transformMatrix, this._frustumPlanes);
  377. }
  378. // Meshes
  379. if (this._selectionOctree) { // Octree
  380. var selection = this._selectionOctree.select(this._frustumPlanes);
  381. for (var blockIndex = 0; blockIndex < selection.length; blockIndex++) {
  382. var block = selection.data[blockIndex];
  383. for (var meshIndex = 0; meshIndex < block.meshes.length; meshIndex++) {
  384. var mesh = block.meshes[meshIndex];
  385. if (Math.abs(mesh._renderId) !== this._renderId) {
  386. this._totalVertices += mesh.getTotalVertices();
  387. if (!mesh.isReady()) {
  388. continue;
  389. }
  390. mesh.computeWorldMatrix();
  391. mesh._renderId = 0;
  392. }
  393. if (mesh._renderId === this._renderId || (mesh._renderId === 0 && mesh.isEnabled() && mesh.isVisible && mesh.visibility > 0 && mesh.isInFrustrum(this._frustumPlanes))) {
  394. if (mesh._renderId === 0) {
  395. this._activeMeshes.push(mesh);
  396. }
  397. mesh._renderId = this._renderId;
  398. if (mesh.skeleton) {
  399. this._activeSkeletons.pushNoDuplicate(mesh.skeleton);
  400. }
  401. var subMeshes = block.subMeshes[meshIndex];
  402. for (var subIndex = 0; subIndex < subMeshes.length; subIndex++) {
  403. var subMesh = subMeshes[subIndex];
  404. if (subMesh._renderId === this._renderId) {
  405. continue;
  406. }
  407. subMesh._renderId = this._renderId;
  408. this._evaluateSubMesh(subMesh, mesh);
  409. }
  410. } else {
  411. mesh._renderId = -this._renderId;
  412. }
  413. }
  414. }
  415. } else { // Full scene traversal
  416. for (var meshIndex = 0; meshIndex < this.meshes.length; meshIndex++) {
  417. var mesh = this.meshes[meshIndex];
  418. this._totalVertices += mesh.getTotalVertices();
  419. if (!mesh.isReady()) {
  420. continue;
  421. }
  422. mesh.computeWorldMatrix();
  423. if (mesh.isEnabled() && mesh.isVisible && mesh.visibility > 0 && mesh.isInFrustrum(this._frustumPlanes)) {
  424. this._activeMeshes.push(mesh);
  425. if (mesh.skeleton) {
  426. this._activeSkeletons.pushNoDuplicate(mesh.skeleton);
  427. }
  428. for (var subIndex = 0; subIndex < mesh.subMeshes.length; subIndex++) {
  429. var subMesh = mesh.subMeshes[subIndex];
  430. this._evaluateSubMesh(subMesh, mesh);
  431. }
  432. }
  433. }
  434. }
  435. // Particle systems
  436. var beforeParticlesDate = new Date();
  437. if (this.particlesEnabled) {
  438. for (var particleIndex = 0; particleIndex < this.particleSystems.length; particleIndex++) {
  439. var particleSystem = this.particleSystems[particleIndex];
  440. if (!particleSystem.emitter.position || (particleSystem.emitter && particleSystem.emitter.isEnabled())) {
  441. this._activeParticleSystems.push(particleSystem);
  442. particleSystem.animate();
  443. }
  444. }
  445. }
  446. this._particlesDuration += new Date() - beforeParticlesDate;
  447. };
  448. BABYLON.Scene.prototype._renderForCamera = function (camera) {
  449. var engine = this._engine;
  450. this.activeCamera = camera;
  451. if (!this.activeCamera)
  452. throw new Error("Active camera not set");
  453. // Viewport
  454. engine.setViewport(this.activeCamera.viewport);
  455. // Camera
  456. this._renderId++;
  457. this.setTransformMatrix(this.activeCamera.getViewMatrix(), this.activeCamera.getProjectionMatrix());
  458. // Meshes
  459. var beforeEvaluateActiveMeshesDate = new Date();
  460. this._evaluateActiveMeshes();
  461. this._evaluateActiveMeshesDuration += new Date() - beforeEvaluateActiveMeshesDate;
  462. // Skeletons
  463. for (var skeletonIndex = 0; skeletonIndex < this._activeSkeletons.length; skeletonIndex++) {
  464. var skeleton = this._activeSkeletons.data[skeletonIndex];
  465. skeleton.prepare();
  466. }
  467. // Customs render targets registration
  468. for (var customIndex = 0; customIndex < this.customRenderTargets.length; customIndex++) {
  469. this._renderTargets.push(this.customRenderTargets[customIndex]);
  470. }
  471. // Render targets
  472. var beforeRenderTargetDate = new Date();
  473. for (var renderIndex = 0; renderIndex < this._renderTargets.length; renderIndex++) {
  474. var renderTarget = this._renderTargets.data[renderIndex];
  475. this._renderId++;
  476. renderTarget.render();
  477. }
  478. if (this._renderTargets.length > 0) { // Restore back buffer
  479. engine.restoreDefaultFramebuffer();
  480. }
  481. this._renderTargetsDuration = new Date() - beforeRenderTargetDate;
  482. // Prepare Frame
  483. this.postProcessManager._prepareFrame();
  484. var beforeRenderDate = new Date();
  485. // Backgrounds
  486. if (this.layers.length) {
  487. engine.setDepthBuffer(false);
  488. var layerIndex;
  489. var layer;
  490. for (layerIndex = 0; layerIndex < this.layers.length; layerIndex++) {
  491. layer = this.layers[layerIndex];
  492. if (layer.isBackground) {
  493. layer.render();
  494. }
  495. }
  496. engine.setDepthBuffer(true);
  497. }
  498. // Render
  499. this._renderingManager.render(null, null, true, true);
  500. // Lens flares
  501. for (var lensFlareSystemIndex = 0; lensFlareSystemIndex < this.lensFlareSystems.length; lensFlareSystemIndex++) {
  502. this.lensFlareSystems[lensFlareSystemIndex].render();
  503. }
  504. // Foregrounds
  505. if (this.layers.length) {
  506. engine.setDepthBuffer(false);
  507. for (layerIndex = 0; layerIndex < this.layers.length; layerIndex++) {
  508. layer = this.layers[layerIndex];
  509. if (!layer.isBackground) {
  510. layer.render();
  511. }
  512. }
  513. engine.setDepthBuffer(true);
  514. }
  515. this._renderDuration += new Date() - beforeRenderDate;
  516. // Finalize frame
  517. this.postProcessManager._finalizeFrame();
  518. // Update camera
  519. this.activeCamera._update();
  520. // Reset some special arrays
  521. this._renderTargets.reset();
  522. };
  523. BABYLON.Scene.prototype.render = function () {
  524. var startDate = new Date();
  525. this._particlesDuration = 0;
  526. this._spritesDuration = 0;
  527. this._activeParticles = 0;
  528. this._renderDuration = 0;
  529. this._evaluateActiveMeshesDuration = 0;
  530. this._totalVertices = 0;
  531. this._activeVertices = 0;
  532. // Before render
  533. if (this.beforeRender) {
  534. this.beforeRender();
  535. }
  536. for (var callbackIndex = 0; callbackIndex < this._onBeforeRenderCallbacks.length; callbackIndex++) {
  537. this._onBeforeRenderCallbacks[callbackIndex]();
  538. }
  539. // Animations
  540. this._animationRatio = BABYLON.Tools.GetDeltaTime() * (60.0 / 1000.0);
  541. this._animate();
  542. // Clear
  543. this._engine.clear(this.clearColor, this.autoClear || this.forceWireframe, true);
  544. // Shadows
  545. for (var lightIndex = 0; lightIndex < this.lights.length; lightIndex++) {
  546. var light = this.lights[lightIndex];
  547. var shadowGenerator = light.getShadowGenerator();
  548. if (light.isEnabled() && shadowGenerator) {
  549. this._renderTargets.push(shadowGenerator.getShadowMap());
  550. }
  551. }
  552. // Multi-cameras?
  553. if (this.activeCameras.length > 0) {
  554. var currentRenderId = this._renderId;
  555. for (var cameraIndex = 0; cameraIndex < this.activeCameras.length; cameraIndex++) {
  556. this._renderId = currentRenderId;
  557. this._renderForCamera(this.activeCameras[cameraIndex]);
  558. }
  559. } else {
  560. this._renderForCamera(this.activeCamera);
  561. }
  562. // After render
  563. if (this.afterRender) {
  564. this.afterRender();
  565. }
  566. // Cleaning
  567. for (var index = 0; index < this._toBeDisposed.length; index++) {
  568. this._toBeDisposed.data[index].dispose();
  569. this._toBeDisposed[index] = null;
  570. }
  571. this._toBeDisposed.reset();
  572. this._lastFrameDuration = new Date() - startDate;
  573. };
  574. BABYLON.Scene.prototype.dispose = function () {
  575. this.beforeRender = null;
  576. this.afterRender = null;
  577. this.skeletons = [];
  578. // Detach cameras
  579. var canvas = this._engine.getRenderingCanvas();
  580. var index;
  581. for (index = 0; index < this.cameras.length; index++) {
  582. this.cameras[index].detachControl(canvas);
  583. }
  584. // Release lights
  585. while (this.lights.length) {
  586. this.lights[0].dispose(true);
  587. }
  588. // Release meshes
  589. while (this.meshes.length) {
  590. this.meshes[0].dispose(true);
  591. }
  592. // Release cameras
  593. while (this.cameras.length) {
  594. this.cameras[0].dispose();
  595. }
  596. // Release materials
  597. while (this.materials.length) {
  598. this.materials[0].dispose();
  599. }
  600. // Release particles
  601. while (this.particleSystems.length) {
  602. this.particleSystems[0].dispose();
  603. }
  604. // Release sprites
  605. while (this.spriteManagers.length) {
  606. this.spriteManagers[0].dispose();
  607. }
  608. // Release layers
  609. while (this.layers.length) {
  610. this.layers[0].dispose();
  611. }
  612. // Release textures
  613. while (this.textures.length) {
  614. this.textures[0].dispose();
  615. }
  616. // Post-processes
  617. this.postProcessManager.dispose();
  618. // Remove from engine
  619. index = this._engine.scenes.indexOf(this);
  620. this._engine.scenes.splice(index, 1);
  621. this._engine.wipeCaches();
  622. };
  623. // Collisions
  624. BABYLON.Scene.prototype._getNewPosition = function (position, velocity, collider, maximumRetry, finalPosition) {
  625. position.divideToRef(collider.radius, this._scaledPosition);
  626. velocity.divideToRef(collider.radius, this._scaledVelocity);
  627. collider.retry = 0;
  628. collider.initialVelocity = this._scaledVelocity;
  629. collider.initialPosition = this._scaledPosition;
  630. this._collideWithWorld(this._scaledPosition, this._scaledVelocity, collider, maximumRetry, finalPosition);
  631. finalPosition.multiplyInPlace(collider.radius);
  632. };
  633. BABYLON.Scene.prototype._collideWithWorld = function (position, velocity, collider, maximumRetry, finalPosition) {
  634. var closeDistance = BABYLON.Engine.collisionsEpsilon * 10.0;
  635. if (collider.retry >= maximumRetry) {
  636. finalPosition.copyFrom(position);
  637. return;
  638. }
  639. collider._initialize(position, velocity, closeDistance);
  640. // Check all meshes
  641. for (var index = 0; index < this.meshes.length; index++) {
  642. var mesh = this.meshes[index];
  643. if (mesh.isEnabled() && mesh.checkCollisions) {
  644. mesh._checkCollision(collider);
  645. }
  646. }
  647. if (!collider.collisionFound) {
  648. position.addToRef(velocity, finalPosition);
  649. return;
  650. }
  651. if (velocity.x != 0 || velocity.y != 0 || velocity.z != 0) {
  652. collider._getResponse(position, velocity);
  653. }
  654. if (velocity.length() <= closeDistance) {
  655. finalPosition.copyFrom(position);
  656. return;
  657. }
  658. collider.retry++;
  659. this._collideWithWorld(position, velocity, collider, maximumRetry, finalPosition);
  660. };
  661. // Octrees
  662. BABYLON.Scene.prototype.createOrUpdateSelectionOctree = function () {
  663. if (!this._selectionOctree) {
  664. this._selectionOctree = new BABYLON.Octree();
  665. }
  666. // World limits
  667. var checkExtends = function (v, min, max) {
  668. if (v.x < min.x)
  669. min.x = v.x;
  670. if (v.y < min.y)
  671. min.y = v.y;
  672. if (v.z < min.z)
  673. min.z = v.z;
  674. if (v.x > max.x)
  675. max.x = v.x;
  676. if (v.y > max.y)
  677. max.y = v.y;
  678. if (v.z > max.z)
  679. max.z = v.z;
  680. };
  681. var min = new BABYLON.Vector3(Number.MAX_VALUE, Number.MAX_VALUE, Number.MAX_VALUE);
  682. var max = new BABYLON.Vector3(-Number.MAX_VALUE, -Number.MAX_VALUE, -Number.MAX_VALUE);
  683. for (var index = 0; index < this.meshes.length; index++) {
  684. var mesh = this.meshes[index];
  685. mesh.computeWorldMatrix();
  686. var minBox = mesh.getBoundingInfo().boundingBox.minimumWorld;
  687. var maxBox = mesh.getBoundingInfo().boundingBox.maximumWorld;
  688. checkExtends(minBox, min, max);
  689. checkExtends(maxBox, min, max);
  690. }
  691. // Update octree
  692. this._selectionOctree.update(min, max, this.meshes);
  693. };
  694. // Picking
  695. BABYLON.Scene.prototype.createPickingRay = function (x, y, world) {
  696. var engine = this._engine;
  697. if (!this._viewMatrix) {
  698. if (!this.activeCamera)
  699. throw new Error("Active camera not set");
  700. this.setTransformMatrix(this.activeCamera.getViewMatrix(), this.activeCamera.getProjectionMatrix());
  701. }
  702. var viewport = this.activeCamera.viewport.toGlobal(engine);
  703. return BABYLON.Ray.CreateNew(x, y, viewport.width, viewport.height, world ? world : BABYLON.Matrix.Identity(), this._viewMatrix, this._projectionMatrix);
  704. };
  705. BABYLON.Scene.prototype._internalPick = function (rayFunction, predicate, fastCheck) {
  706. var pickingInfo = null;
  707. for (var meshIndex = 0; meshIndex < this.meshes.length; meshIndex++) {
  708. var mesh = this.meshes[meshIndex];
  709. if (predicate) {
  710. if (!predicate(mesh)) {
  711. continue;
  712. }
  713. } else if (!mesh.isEnabled() || !mesh.isVisible || !mesh.isPickable) {
  714. continue;
  715. }
  716. var world = mesh.getWorldMatrix();
  717. var ray = rayFunction(world);
  718. var result = mesh.intersects(ray, fastCheck);
  719. if (!result.hit)
  720. continue;
  721. if (!fastCheck && pickingInfo != null && result.distance >= pickingInfo.distance)
  722. continue;
  723. pickingInfo = result;
  724. if (fastCheck) {
  725. break;
  726. }
  727. }
  728. return pickingInfo || new BABYLON.PickingInfo();
  729. };
  730. BABYLON.Scene.prototype.pick = function (x, y, predicate, fastCheck) {
  731. var that = this;
  732. return this._internalPick(function(world) {
  733. return that.createPickingRay(x, y, world);
  734. }, predicate, fastCheck);
  735. };
  736. BABYLON.Scene.prototype.pickWithRay = function (ray, predicate, fastCheck) {
  737. var that = this;
  738. return this._internalPick(function (world) {
  739. if (!that._pickWithRayInverseMatrix) {
  740. that._pickWithRayInverseMatrix = BABYLON.Matrix.Identity();
  741. }
  742. world.invertToRef(that._pickWithRayInverseMatrix);
  743. return BABYLON.Ray.Transform(ray, that._pickWithRayInverseMatrix);
  744. }, predicate, fastCheck);
  745. };
  746. // Statics
  747. BABYLON.Scene.FOGMODE_NONE = 0;
  748. BABYLON.Scene.FOGMODE_EXP = 1;
  749. BABYLON.Scene.FOGMODE_EXP2 = 2;
  750. BABYLON.Scene.FOGMODE_LINEAR = 3;
  751. })();