babylon.scene.js 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003
  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.lightsEnabled = true;
  31. this.lights = [];
  32. // Cameras
  33. this.cameras = [];
  34. this.activeCamera = null;
  35. // Meshes
  36. this.meshes = [];
  37. // Internal smart arrays
  38. this._activeMeshes = new BABYLON.Tools.SmartArray(256);
  39. this._processedMaterials = new BABYLON.Tools.SmartArray(256);
  40. this._renderTargets = new BABYLON.Tools.SmartArray(256);
  41. this._activeParticleSystems = new BABYLON.Tools.SmartArray(256);
  42. this._activeSkeletons = new BABYLON.Tools.SmartArray(32);
  43. // Rendering groups
  44. this._renderingManager = new BABYLON.RenderingManager(this);
  45. // Materials
  46. this.materials = [];
  47. this.multiMaterials = [];
  48. this.defaultMaterial = new BABYLON.StandardMaterial("default material", this);
  49. // Textures
  50. this.texturesEnabled = true;
  51. this.textures = [];
  52. // Particles
  53. this.particlesEnabled = true;
  54. this.particleSystems = [];
  55. // Sprites
  56. this.spriteManagers = [];
  57. // Layers
  58. this.layers = [];
  59. // Skeletons
  60. this.skeletons = [];
  61. // Lens flares
  62. this.lensFlareSystems = [];
  63. // Collisions
  64. this.collisionsEnabled = true;
  65. this.gravity = new BABYLON.Vector3(0, -9.0, 0);
  66. // Animations
  67. this._activeAnimatables = [];
  68. // Matrices
  69. this._transformMatrix = BABYLON.Matrix.Zero();
  70. // Internals
  71. this._scaledPosition = BABYLON.Vector3.Zero();
  72. this._scaledVelocity = BABYLON.Vector3.Zero();
  73. // Postprocesses
  74. this.postProcessesEnabled = true;
  75. this.postProcessManager = new BABYLON.PostProcessManager(this);
  76. // Customs render targets
  77. this.renderTargetsEnabled = true;
  78. this.customRenderTargets = [];
  79. // Multi-cameras
  80. this.activeCameras = [];
  81. };
  82. // Properties
  83. BABYLON.Scene.prototype.getEngine = function () {
  84. return this._engine;
  85. };
  86. BABYLON.Scene.prototype.getTotalVertices = function () {
  87. return this._totalVertices;
  88. };
  89. BABYLON.Scene.prototype.getActiveVertices = function () {
  90. return this._activeVertices;
  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. if (this.renderTargetsEnabled) {
  474. for (var renderIndex = 0; renderIndex < this._renderTargets.length; renderIndex++) {
  475. var renderTarget = this._renderTargets.data[renderIndex];
  476. this._renderId++;
  477. renderTarget.render();
  478. }
  479. }
  480. if (this._renderTargets.length > 0) { // Restore back buffer
  481. engine.restoreDefaultFramebuffer();
  482. }
  483. this._renderTargetsDuration = new Date() - beforeRenderTargetDate;
  484. // Prepare Frame
  485. this.postProcessManager._prepareFrame();
  486. var beforeRenderDate = new Date();
  487. // Backgrounds
  488. if (this.layers.length) {
  489. engine.setDepthBuffer(false);
  490. var layerIndex;
  491. var layer;
  492. for (layerIndex = 0; layerIndex < this.layers.length; layerIndex++) {
  493. layer = this.layers[layerIndex];
  494. if (layer.isBackground) {
  495. layer.render();
  496. }
  497. }
  498. engine.setDepthBuffer(true);
  499. }
  500. // Render
  501. this._renderingManager.render(null, null, true, true);
  502. // Lens flares
  503. for (var lensFlareSystemIndex = 0; lensFlareSystemIndex < this.lensFlareSystems.length; lensFlareSystemIndex++) {
  504. this.lensFlareSystems[lensFlareSystemIndex].render();
  505. }
  506. // Foregrounds
  507. if (this.layers.length) {
  508. engine.setDepthBuffer(false);
  509. for (layerIndex = 0; layerIndex < this.layers.length; layerIndex++) {
  510. layer = this.layers[layerIndex];
  511. if (!layer.isBackground) {
  512. layer.render();
  513. }
  514. }
  515. engine.setDepthBuffer(true);
  516. }
  517. this._renderDuration += new Date() - beforeRenderDate;
  518. // Finalize frame
  519. this.postProcessManager._finalizeFrame();
  520. // Update camera
  521. this.activeCamera._update();
  522. // Reset some special arrays
  523. this._renderTargets.reset();
  524. };
  525. BABYLON.Scene.prototype.render = function () {
  526. var startDate = new Date();
  527. this._particlesDuration = 0;
  528. this._spritesDuration = 0;
  529. this._activeParticles = 0;
  530. this._renderDuration = 0;
  531. this._evaluateActiveMeshesDuration = 0;
  532. this._totalVertices = 0;
  533. this._activeVertices = 0;
  534. // Before render
  535. if (this.beforeRender) {
  536. this.beforeRender();
  537. }
  538. for (var callbackIndex = 0; callbackIndex < this._onBeforeRenderCallbacks.length; callbackIndex++) {
  539. this._onBeforeRenderCallbacks[callbackIndex]();
  540. }
  541. // Animations
  542. var deltaTime = BABYLON.Tools.GetDeltaTime();
  543. this._animationRatio = deltaTime * (60.0 / 1000.0);
  544. this._animate();
  545. // Physics
  546. if (this._physicsEngine) {
  547. this._physicsEngine._runOneStep(deltaTime / 1000.0);
  548. }
  549. // Clear
  550. this._engine.clear(this.clearColor, this.autoClear || this.forceWireframe, true);
  551. // Shadows
  552. for (var lightIndex = 0; lightIndex < this.lights.length; lightIndex++) {
  553. var light = this.lights[lightIndex];
  554. var shadowGenerator = light.getShadowGenerator();
  555. if (light.isEnabled() && shadowGenerator) {
  556. this._renderTargets.push(shadowGenerator.getShadowMap());
  557. }
  558. }
  559. // Multi-cameras?
  560. if (this.activeCameras.length > 0) {
  561. var currentRenderId = this._renderId;
  562. for (var cameraIndex = 0; cameraIndex < this.activeCameras.length; cameraIndex++) {
  563. this._renderId = currentRenderId;
  564. this._renderForCamera(this.activeCameras[cameraIndex]);
  565. }
  566. } else {
  567. this._renderForCamera(this.activeCamera);
  568. }
  569. // After render
  570. if (this.afterRender) {
  571. this.afterRender();
  572. }
  573. // Cleaning
  574. for (var index = 0; index < this._toBeDisposed.length; index++) {
  575. this._toBeDisposed.data[index].dispose();
  576. this._toBeDisposed[index] = null;
  577. }
  578. this._toBeDisposed.reset();
  579. this._lastFrameDuration = new Date() - startDate;
  580. };
  581. BABYLON.Scene.prototype.dispose = function () {
  582. this.beforeRender = null;
  583. this.afterRender = null;
  584. this.skeletons = [];
  585. // Detach cameras
  586. var canvas = this._engine.getRenderingCanvas();
  587. var index;
  588. for (index = 0; index < this.cameras.length; index++) {
  589. this.cameras[index].detachControl(canvas);
  590. }
  591. // Release lights
  592. while (this.lights.length) {
  593. this.lights[0].dispose(true);
  594. }
  595. // Release meshes
  596. while (this.meshes.length) {
  597. this.meshes[0].dispose(true);
  598. }
  599. // Release cameras
  600. while (this.cameras.length) {
  601. this.cameras[0].dispose();
  602. }
  603. // Release materials
  604. while (this.materials.length) {
  605. this.materials[0].dispose();
  606. }
  607. // Release particles
  608. while (this.particleSystems.length) {
  609. this.particleSystems[0].dispose();
  610. }
  611. // Release sprites
  612. while (this.spriteManagers.length) {
  613. this.spriteManagers[0].dispose();
  614. }
  615. // Release layers
  616. while (this.layers.length) {
  617. this.layers[0].dispose();
  618. }
  619. // Release textures
  620. while (this.textures.length) {
  621. this.textures[0].dispose();
  622. }
  623. // Post-processes
  624. this.postProcessManager.dispose();
  625. // Physics
  626. if (this._physicsEngine) {
  627. this.disablePhysicsEngine();
  628. }
  629. // Remove from engine
  630. index = this._engine.scenes.indexOf(this);
  631. this._engine.scenes.splice(index, 1);
  632. this._engine.wipeCaches();
  633. };
  634. // Collisions
  635. BABYLON.Scene.prototype._getNewPosition = function (position, velocity, collider, maximumRetry, finalPosition) {
  636. position.divideToRef(collider.radius, this._scaledPosition);
  637. velocity.divideToRef(collider.radius, this._scaledVelocity);
  638. collider.retry = 0;
  639. collider.initialVelocity = this._scaledVelocity;
  640. collider.initialPosition = this._scaledPosition;
  641. this._collideWithWorld(this._scaledPosition, this._scaledVelocity, collider, maximumRetry, finalPosition);
  642. finalPosition.multiplyInPlace(collider.radius);
  643. };
  644. BABYLON.Scene.prototype._collideWithWorld = function (position, velocity, collider, maximumRetry, finalPosition) {
  645. var closeDistance = BABYLON.Engine.collisionsEpsilon * 10.0;
  646. if (collider.retry >= maximumRetry) {
  647. finalPosition.copyFrom(position);
  648. return;
  649. }
  650. collider._initialize(position, velocity, closeDistance);
  651. // Check all meshes
  652. for (var index = 0; index < this.meshes.length; index++) {
  653. var mesh = this.meshes[index];
  654. if (mesh.isEnabled() && mesh.checkCollisions) {
  655. mesh._checkCollision(collider);
  656. }
  657. }
  658. if (!collider.collisionFound) {
  659. position.addToRef(velocity, finalPosition);
  660. return;
  661. }
  662. if (velocity.x != 0 || velocity.y != 0 || velocity.z != 0) {
  663. collider._getResponse(position, velocity);
  664. }
  665. if (velocity.length() <= closeDistance) {
  666. finalPosition.copyFrom(position);
  667. return;
  668. }
  669. collider.retry++;
  670. this._collideWithWorld(position, velocity, collider, maximumRetry, finalPosition);
  671. };
  672. // Octrees
  673. BABYLON.Scene.prototype.createOrUpdateSelectionOctree = function () {
  674. if (!this._selectionOctree) {
  675. this._selectionOctree = new BABYLON.Octree();
  676. }
  677. // World limits
  678. var checkExtends = function (v, min, max) {
  679. if (v.x < min.x)
  680. min.x = v.x;
  681. if (v.y < min.y)
  682. min.y = v.y;
  683. if (v.z < min.z)
  684. min.z = v.z;
  685. if (v.x > max.x)
  686. max.x = v.x;
  687. if (v.y > max.y)
  688. max.y = v.y;
  689. if (v.z > max.z)
  690. max.z = v.z;
  691. };
  692. var min = new BABYLON.Vector3(Number.MAX_VALUE, Number.MAX_VALUE, Number.MAX_VALUE);
  693. var max = new BABYLON.Vector3(-Number.MAX_VALUE, -Number.MAX_VALUE, -Number.MAX_VALUE);
  694. for (var index = 0; index < this.meshes.length; index++) {
  695. var mesh = this.meshes[index];
  696. mesh.computeWorldMatrix();
  697. var minBox = mesh.getBoundingInfo().boundingBox.minimumWorld;
  698. var maxBox = mesh.getBoundingInfo().boundingBox.maximumWorld;
  699. checkExtends(minBox, min, max);
  700. checkExtends(maxBox, min, max);
  701. }
  702. // Update octree
  703. this._selectionOctree.update(min, max, this.meshes);
  704. };
  705. // Picking
  706. BABYLON.Scene.prototype.createPickingRay = function (x, y, world) {
  707. var engine = this._engine;
  708. if (!this._viewMatrix) {
  709. if (!this.activeCamera)
  710. throw new Error("Active camera not set");
  711. this.setTransformMatrix(this.activeCamera.getViewMatrix(), this.activeCamera.getProjectionMatrix());
  712. }
  713. var viewport = this.activeCamera.viewport.toGlobal(engine);
  714. return BABYLON.Ray.CreateNew(x, y, viewport.width, viewport.height, world ? world : BABYLON.Matrix.Identity(), this._viewMatrix, this._projectionMatrix);
  715. };
  716. BABYLON.Scene.prototype._internalPick = function (rayFunction, predicate, fastCheck) {
  717. var pickingInfo = null;
  718. for (var meshIndex = 0; meshIndex < this.meshes.length; meshIndex++) {
  719. var mesh = this.meshes[meshIndex];
  720. if (predicate) {
  721. if (!predicate(mesh)) {
  722. continue;
  723. }
  724. } else if (!mesh.isEnabled() || !mesh.isVisible || !mesh.isPickable) {
  725. continue;
  726. }
  727. var world = mesh.getWorldMatrix();
  728. var ray = rayFunction(world);
  729. var result = mesh.intersects(ray, fastCheck);
  730. if (!result.hit)
  731. continue;
  732. if (!fastCheck && pickingInfo != null && result.distance >= pickingInfo.distance)
  733. continue;
  734. pickingInfo = result;
  735. if (fastCheck) {
  736. break;
  737. }
  738. }
  739. return pickingInfo || new BABYLON.PickingInfo();
  740. };
  741. BABYLON.Scene.prototype.pick = function (x, y, predicate, fastCheck) {
  742. var that = this;
  743. return this._internalPick(function(world) {
  744. return that.createPickingRay(x, y, world);
  745. }, predicate, fastCheck);
  746. };
  747. BABYLON.Scene.prototype.pickWithRay = function (ray, predicate, fastCheck) {
  748. var that = this;
  749. return this._internalPick(function (world) {
  750. if (!that._pickWithRayInverseMatrix) {
  751. that._pickWithRayInverseMatrix = BABYLON.Matrix.Identity();
  752. }
  753. world.invertToRef(that._pickWithRayInverseMatrix);
  754. return BABYLON.Ray.Transform(ray, that._pickWithRayInverseMatrix);
  755. }, predicate, fastCheck);
  756. };
  757. // Physics
  758. BABYLON.Scene.prototype.enablePhysics = function(gravity) {
  759. if (this._physicsEngine) {
  760. return true;
  761. }
  762. if (!BABYLON.PhysicsEngine.IsSupported()) {
  763. return false;
  764. }
  765. this._physicsEngine = new BABYLON.PhysicsEngine(gravity);
  766. return true;
  767. };
  768. BABYLON.Scene.prototype.disablePhysicsEngine = function() {
  769. if (!this._physicsEngine) {
  770. return;
  771. }
  772. this._physicsEngine.dispose();
  773. this._physicsEngine = undefined;
  774. };
  775. BABYLON.Scene.prototype.isPhysicsEnabled = function() {
  776. return this._physicsEngine !== undefined;
  777. };
  778. BABYLON.Scene.prototype.setGravity = function (gravity) {
  779. if (!this._physicsEngine) {
  780. return;
  781. }
  782. this._physicsEngine._setGravity(gravity);
  783. };
  784. // Statics
  785. BABYLON.Scene.FOGMODE_NONE = 0;
  786. BABYLON.Scene.FOGMODE_EXP = 1;
  787. BABYLON.Scene.FOGMODE_EXP2 = 2;
  788. BABYLON.Scene.FOGMODE_LINEAR = 3;
  789. })();