babylon.scene.js 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043
  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.getActiveMeshes = 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, mustClearDepth) {
  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. // Clear
  459. if (mustClearDepth) {
  460. this._engine.clear(this.clearColor, false, true);
  461. }
  462. // Camera
  463. this._renderId++;
  464. this.setTransformMatrix(this.activeCamera.getViewMatrix(), this.activeCamera.getProjectionMatrix());
  465. // Meshes
  466. var beforeEvaluateActiveMeshesDate = new Date();
  467. this._evaluateActiveMeshes();
  468. this._evaluateActiveMeshesDuration += new Date() - beforeEvaluateActiveMeshesDate;
  469. // Skeletons
  470. for (var skeletonIndex = 0; skeletonIndex < this._activeSkeletons.length; skeletonIndex++) {
  471. var skeleton = this._activeSkeletons.data[skeletonIndex];
  472. skeleton.prepare();
  473. }
  474. // Customs render targets registration
  475. for (var customIndex = 0; customIndex < this.customRenderTargets.length; customIndex++) {
  476. this._renderTargets.push(this.customRenderTargets[customIndex]);
  477. }
  478. // Render targets
  479. var beforeRenderTargetDate = new Date();
  480. if (this.renderTargetsEnabled) {
  481. for (var renderIndex = 0; renderIndex < this._renderTargets.length; renderIndex++) {
  482. var renderTarget = this._renderTargets.data[renderIndex];
  483. this._renderId++;
  484. renderTarget.render();
  485. }
  486. }
  487. if (this._renderTargets.length > 0) { // Restore back buffer
  488. engine.restoreDefaultFramebuffer();
  489. }
  490. this._renderTargetsDuration = new Date() - beforeRenderTargetDate;
  491. // Prepare Frame
  492. this.postProcessManager._prepareFrame();
  493. var beforeRenderDate = new Date();
  494. // Backgrounds
  495. if (this.layers.length) {
  496. engine.setDepthBuffer(false);
  497. var layerIndex;
  498. var layer;
  499. for (layerIndex = 0; layerIndex < this.layers.length; layerIndex++) {
  500. layer = this.layers[layerIndex];
  501. if (layer.isBackground) {
  502. layer.render();
  503. }
  504. }
  505. engine.setDepthBuffer(true);
  506. }
  507. // Render
  508. this._renderingManager.render(null, null, true, true);
  509. // Lens flares
  510. for (var lensFlareSystemIndex = 0; lensFlareSystemIndex < this.lensFlareSystems.length; lensFlareSystemIndex++) {
  511. this.lensFlareSystems[lensFlareSystemIndex].render();
  512. }
  513. // Foregrounds
  514. if (this.layers.length) {
  515. engine.setDepthBuffer(false);
  516. for (layerIndex = 0; layerIndex < this.layers.length; layerIndex++) {
  517. layer = this.layers[layerIndex];
  518. if (!layer.isBackground) {
  519. layer.render();
  520. }
  521. }
  522. engine.setDepthBuffer(true);
  523. }
  524. this._renderDuration += new Date() - beforeRenderDate;
  525. // Finalize frame
  526. this.postProcessManager._finalizeFrame();
  527. // Update camera
  528. this.activeCamera._updateFromScene();
  529. // Reset some special arrays
  530. this._renderTargets.reset();
  531. };
  532. BABYLON.Scene.prototype.render = function () {
  533. var startDate = new Date();
  534. this._particlesDuration = 0;
  535. this._spritesDuration = 0;
  536. this._activeParticles = 0;
  537. this._renderDuration = 0;
  538. this._evaluateActiveMeshesDuration = 0;
  539. this._totalVertices = 0;
  540. this._activeVertices = 0;
  541. // Before render
  542. if (this.beforeRender) {
  543. this.beforeRender();
  544. }
  545. for (var callbackIndex = 0; callbackIndex < this._onBeforeRenderCallbacks.length; callbackIndex++) {
  546. this._onBeforeRenderCallbacks[callbackIndex]();
  547. }
  548. // Animations
  549. var deltaTime = BABYLON.Tools.GetDeltaTime();
  550. this._animationRatio = deltaTime * (60.0 / 1000.0);
  551. this._animate();
  552. // Physics
  553. if (this._physicsEngine) {
  554. this._physicsEngine._runOneStep(deltaTime / 1000.0);
  555. }
  556. // Clear
  557. this._engine.clear(this.clearColor, this.autoClear || this.forceWireframe, true);
  558. // Shadows
  559. for (var lightIndex = 0; lightIndex < this.lights.length; lightIndex++) {
  560. var light = this.lights[lightIndex];
  561. var shadowGenerator = light.getShadowGenerator();
  562. if (light.isEnabled() && shadowGenerator) {
  563. this._renderTargets.push(shadowGenerator.getShadowMap());
  564. }
  565. }
  566. // Multi-cameras?
  567. if (this.activeCameras.length > 0) {
  568. var currentRenderId = this._renderId;
  569. for (var cameraIndex = 0; cameraIndex < this.activeCameras.length; cameraIndex++) {
  570. this._renderId = currentRenderId;
  571. this._renderForCamera(this.activeCameras[cameraIndex], cameraIndex != 0);
  572. }
  573. } else {
  574. this._renderForCamera(this.activeCamera);
  575. }
  576. // After render
  577. if (this.afterRender) {
  578. this.afterRender();
  579. }
  580. // Cleaning
  581. for (var index = 0; index < this._toBeDisposed.length; index++) {
  582. this._toBeDisposed.data[index].dispose();
  583. this._toBeDisposed[index] = null;
  584. }
  585. this._toBeDisposed.reset();
  586. this._lastFrameDuration = new Date() - startDate;
  587. };
  588. BABYLON.Scene.prototype.dispose = function () {
  589. this.beforeRender = null;
  590. this.afterRender = null;
  591. this.skeletons = [];
  592. // Detach cameras
  593. var canvas = this._engine.getRenderingCanvas();
  594. var index;
  595. for (index = 0; index < this.cameras.length; index++) {
  596. this.cameras[index].detachControl(canvas);
  597. }
  598. // Release lights
  599. while (this.lights.length) {
  600. this.lights[0].dispose(true);
  601. }
  602. // Release meshes
  603. while (this.meshes.length) {
  604. this.meshes[0].dispose(true);
  605. }
  606. // Release cameras
  607. while (this.cameras.length) {
  608. this.cameras[0].dispose();
  609. }
  610. // Release materials
  611. while (this.materials.length) {
  612. this.materials[0].dispose();
  613. }
  614. // Release particles
  615. while (this.particleSystems.length) {
  616. this.particleSystems[0].dispose();
  617. }
  618. // Release sprites
  619. while (this.spriteManagers.length) {
  620. this.spriteManagers[0].dispose();
  621. }
  622. // Release layers
  623. while (this.layers.length) {
  624. this.layers[0].dispose();
  625. }
  626. // Release textures
  627. while (this.textures.length) {
  628. this.textures[0].dispose();
  629. }
  630. // Post-processes
  631. this.postProcessManager.dispose();
  632. // Physics
  633. if (this._physicsEngine) {
  634. this.disablePhysicsEngine();
  635. }
  636. // Remove from engine
  637. index = this._engine.scenes.indexOf(this);
  638. this._engine.scenes.splice(index, 1);
  639. this._engine.wipeCaches();
  640. };
  641. // Collisions
  642. BABYLON.Scene.prototype._getNewPosition = function (position, velocity, collider, maximumRetry, finalPosition) {
  643. position.divideToRef(collider.radius, this._scaledPosition);
  644. velocity.divideToRef(collider.radius, this._scaledVelocity);
  645. collider.retry = 0;
  646. collider.initialVelocity = this._scaledVelocity;
  647. collider.initialPosition = this._scaledPosition;
  648. this._collideWithWorld(this._scaledPosition, this._scaledVelocity, collider, maximumRetry, finalPosition);
  649. finalPosition.multiplyInPlace(collider.radius);
  650. };
  651. BABYLON.Scene.prototype._collideWithWorld = function (position, velocity, collider, maximumRetry, finalPosition) {
  652. var closeDistance = BABYLON.Engine.collisionsEpsilon * 10.0;
  653. if (collider.retry >= maximumRetry) {
  654. finalPosition.copyFrom(position);
  655. return;
  656. }
  657. collider._initialize(position, velocity, closeDistance);
  658. // Check all meshes
  659. for (var index = 0; index < this.meshes.length; index++) {
  660. var mesh = this.meshes[index];
  661. if (mesh.isEnabled() && mesh.checkCollisions) {
  662. mesh._checkCollision(collider);
  663. }
  664. }
  665. if (!collider.collisionFound) {
  666. position.addToRef(velocity, finalPosition);
  667. return;
  668. }
  669. if (velocity.x != 0 || velocity.y != 0 || velocity.z != 0) {
  670. collider._getResponse(position, velocity);
  671. }
  672. if (velocity.length() <= closeDistance) {
  673. finalPosition.copyFrom(position);
  674. return;
  675. }
  676. collider.retry++;
  677. this._collideWithWorld(position, velocity, collider, maximumRetry, finalPosition);
  678. };
  679. // Octrees
  680. BABYLON.Scene.prototype.createOrUpdateSelectionOctree = function () {
  681. if (!this._selectionOctree) {
  682. this._selectionOctree = new BABYLON.Octree();
  683. }
  684. // World limits
  685. var checkExtends = function (v, min, max) {
  686. if (v.x < min.x)
  687. min.x = v.x;
  688. if (v.y < min.y)
  689. min.y = v.y;
  690. if (v.z < min.z)
  691. min.z = v.z;
  692. if (v.x > max.x)
  693. max.x = v.x;
  694. if (v.y > max.y)
  695. max.y = v.y;
  696. if (v.z > max.z)
  697. max.z = v.z;
  698. };
  699. var min = new BABYLON.Vector3(Number.MAX_VALUE, Number.MAX_VALUE, Number.MAX_VALUE);
  700. var max = new BABYLON.Vector3(-Number.MAX_VALUE, -Number.MAX_VALUE, -Number.MAX_VALUE);
  701. for (var index = 0; index < this.meshes.length; index++) {
  702. var mesh = this.meshes[index];
  703. mesh.computeWorldMatrix(true);
  704. var minBox = mesh.getBoundingInfo().boundingBox.minimumWorld;
  705. var maxBox = mesh.getBoundingInfo().boundingBox.maximumWorld;
  706. checkExtends(minBox, min, max);
  707. checkExtends(maxBox, min, max);
  708. }
  709. // Update octree
  710. this._selectionOctree.update(min, max, this.meshes);
  711. };
  712. // Picking
  713. BABYLON.Scene.prototype.createPickingRay = function (x, y, world, camera) {
  714. var engine = this._engine;
  715. if (!camera) {
  716. if (!this.activeCamera)
  717. throw new Error("Active camera not set");
  718. camera = this.activeCamera;
  719. }
  720. var viewport = camera.viewport.toGlobal(engine);
  721. return BABYLON.Ray.CreateNew(x, y, viewport.width, viewport.height, world ? world : BABYLON.Matrix.Identity(), camera.getViewMatrix(), camera.getProjectionMatrix());
  722. };
  723. BABYLON.Scene.prototype._internalPick = function (rayFunction, predicate, fastCheck) {
  724. var pickingInfo = null;
  725. for (var meshIndex = 0; meshIndex < this.meshes.length; meshIndex++) {
  726. var mesh = this.meshes[meshIndex];
  727. if (predicate) {
  728. if (!predicate(mesh)) {
  729. continue;
  730. }
  731. } else if (!mesh.isEnabled() || !mesh.isVisible || !mesh.isPickable) {
  732. continue;
  733. }
  734. var world = mesh.getWorldMatrix();
  735. var ray = rayFunction(world);
  736. var result = mesh.intersects(ray, fastCheck);
  737. if (!result.hit)
  738. continue;
  739. if (!fastCheck && pickingInfo != null && result.distance >= pickingInfo.distance)
  740. continue;
  741. pickingInfo = result;
  742. if (fastCheck) {
  743. break;
  744. }
  745. }
  746. return pickingInfo || new BABYLON.PickingInfo();
  747. };
  748. BABYLON.Scene.prototype.pick = function (x, y, predicate, fastCheck, camera) {
  749. /// <summary>Launch a ray to try to pick a mesh in the scene</summary>
  750. /// <param name="x">X position on screen</param>
  751. /// <param name="y">Y position on screen</param>
  752. /// <param name="predicate">Predicate function used to determine eligible meshes. Can be set to null. In this case, a mesh must be enabled, visible and with isPickable set to true</param>
  753. /// <param name="fastCheck">Launch a fast check only using the bounding boxes. Can be set to null.</param>
  754. /// <param name="camera">camera to use for computing the picking ray. Can be set to null. In this case, the scene.activeCamera will be used</param>
  755. var that = this;
  756. return this._internalPick(function (world) {
  757. return that.createPickingRay(x, y, world, camera);
  758. }, predicate, fastCheck);
  759. };
  760. BABYLON.Scene.prototype.pickWithRay = function (ray, predicate, fastCheck) {
  761. var that = this;
  762. return this._internalPick(function (world) {
  763. if (!that._pickWithRayInverseMatrix) {
  764. that._pickWithRayInverseMatrix = BABYLON.Matrix.Identity();
  765. }
  766. world.invertToRef(that._pickWithRayInverseMatrix);
  767. return BABYLON.Ray.Transform(ray, that._pickWithRayInverseMatrix);
  768. }, predicate, fastCheck);
  769. };
  770. // Physics
  771. BABYLON.Scene.prototype.enablePhysics = function (gravity, iterations) {
  772. if (this._physicsEngine) {
  773. return true;
  774. }
  775. if (!BABYLON.PhysicsEngine.IsSupported()) {
  776. return false;
  777. }
  778. this._physicsEngine = new BABYLON.PhysicsEngine(gravity, iterations || 10);
  779. return true;
  780. };
  781. BABYLON.Scene.prototype.disablePhysicsEngine = function () {
  782. if (!this._physicsEngine) {
  783. return;
  784. }
  785. this._physicsEngine.dispose();
  786. this._physicsEngine = undefined;
  787. };
  788. BABYLON.Scene.prototype.isPhysicsEnabled = function () {
  789. return this._physicsEngine !== undefined;
  790. };
  791. BABYLON.Scene.prototype.setGravity = function (gravity) {
  792. if (!this._physicsEngine) {
  793. return;
  794. }
  795. this._physicsEngine._setGravity(gravity);
  796. };
  797. BABYLON.Scene.prototype.createCompoundImpostor = function (options) {
  798. if (!this._physicsEngine) {
  799. return null;
  800. }
  801. for (var index = 0; index < options.parts.length; index++) {
  802. var mesh = options.parts[index].mesh;
  803. mesh._physicImpostor = options.parts[index].impostor;
  804. mesh._physicsMass = options.mass / options.parts.length;
  805. mesh._physicsFriction = options.friction;
  806. mesh._physicRestitution = options.restitution;
  807. }
  808. return this._physicsEngine._registerCompound(options);
  809. };
  810. BABYLON.Scene.prototype.deleteCompoundImpostor = function (compound) {
  811. for (var index = 0; index < compound.parts.length; index++) {
  812. var mesh = compound.parts[index].mesh;
  813. mesh._physicImpostor = BABYLON.PhysicsEngine.NoImpostor;
  814. this._scene._physicsEngine._unregisterMesh(mesh);
  815. }
  816. };
  817. // Statics
  818. BABYLON.Scene.FOGMODE_NONE = 0;
  819. BABYLON.Scene.FOGMODE_EXP = 1;
  820. BABYLON.Scene.FOGMODE_EXP2 = 2;
  821. BABYLON.Scene.FOGMODE_LINEAR = 3;
  822. })();