babylon.scene.js 34 KB

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