babylon.scene.js 33 KB

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