babylon.scene.js 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143
  1. var BABYLON;
  2. (function (BABYLON) {
  3. var checkExtends = function (v, min, max) {
  4. if (v.x < min.x)
  5. min.x = v.x;
  6. if (v.y < min.y)
  7. min.y = v.y;
  8. if (v.z < min.z)
  9. min.z = v.z;
  10. if (v.x > max.x)
  11. max.x = v.x;
  12. if (v.y > max.y)
  13. max.y = v.y;
  14. if (v.z > max.z)
  15. max.z = v.z;
  16. };
  17. var Scene = (function () {
  18. // Constructor
  19. function Scene(engine) {
  20. // Members
  21. this.autoClear = true;
  22. this.clearColor = new BABYLON.Color3(0.2, 0.2, 0.3);
  23. this.ambientColor = new BABYLON.Color3(0, 0, 0);
  24. this.forceWireframe = false;
  25. // Fog
  26. this.fogMode = BABYLON.Scene.FOGMODE_NONE;
  27. this.fogColor = new BABYLON.Color3(0.2, 0.2, 0.3);
  28. this.fogDensity = 0.1;
  29. this.fogStart = 0;
  30. this.fogEnd = 1000.0;
  31. // Lights
  32. this.lightsEnabled = true;
  33. this.lights = new Array();
  34. // Cameras
  35. this.cameras = new Array();
  36. this.activeCameras = new Array();
  37. // Meshes
  38. this.meshes = new Array();
  39. this.materials = new Array();
  40. this.multiMaterials = new Array();
  41. this.defaultMaterial = new BABYLON.StandardMaterial("default material", this);
  42. // Textures
  43. this.texturesEnabled = true;
  44. this.textures = new Array();
  45. // Particles
  46. this.particlesEnabled = true;
  47. this.particleSystems = new Array();
  48. // Sprites
  49. this.spriteManagers = new Array();
  50. // Layers
  51. this.layers = new Array();
  52. // Skeletons
  53. this.skeletons = new Array();
  54. // Lens flares
  55. this.lensFlareSystems = new Array();
  56. // Collisions
  57. this.collisionsEnabled = true;
  58. this.gravity = new BABYLON.Vector3(0, -9.0, 0);
  59. // Postprocesses
  60. this.postProcessesEnabled = true;
  61. // Customs render targets
  62. this.renderTargetsEnabled = true;
  63. this.customRenderTargets = [];
  64. this._totalVertices = 0;
  65. this._activeVertices = 0;
  66. this._activeParticles = 0;
  67. this._lastFrameDuration = 0;
  68. this._evaluateActiveMeshesDuration = 0;
  69. this._renderTargetsDuration = 0;
  70. this._particlesDuration = 0;
  71. this._renderDuration = 0;
  72. this._spritesDuration = 0;
  73. this._animationRatio = 0;
  74. this._renderId = 0;
  75. this._executeWhenReadyTimeoutId = -1;
  76. this._toBeDisposed = new BABYLON.SmartArray(256);
  77. this._onReadyCallbacks = new Array();
  78. this._pendingData = [];
  79. this._onBeforeRenderCallbacks = new Array();
  80. this._activeMeshes = new BABYLON.SmartArray(256);
  81. this._processedMaterials = new BABYLON.SmartArray(256);
  82. this._renderTargets = new BABYLON.SmartArray(256);
  83. this._activeParticleSystems = new BABYLON.SmartArray(256);
  84. this._activeSkeletons = new BABYLON.SmartArray(32);
  85. this._activeAnimatables = new Array();
  86. this._transformMatrix = BABYLON.Matrix.Zero();
  87. this._scaledPosition = BABYLON.Vector3.Zero();
  88. this._scaledVelocity = BABYLON.Vector3.Zero();
  89. this._engine = engine;
  90. engine.scenes.push(this);
  91. this._renderingManager = new BABYLON.RenderingManager(this);
  92. this.postProcessManager = new BABYLON.PostProcessManager(this);
  93. this._boundingBoxRenderer = new BABYLON.BoundingBoxRenderer(this);
  94. }
  95. // Properties
  96. Scene.prototype.getBoundingBoxRenderer = function () {
  97. return this._boundingBoxRenderer;
  98. };
  99. Scene.prototype.getEngine = function () {
  100. return this._engine;
  101. };
  102. Scene.prototype.getTotalVertices = function () {
  103. return this._totalVertices;
  104. };
  105. Scene.prototype.getActiveVertices = function () {
  106. return this._activeVertices;
  107. };
  108. Scene.prototype.getActiveParticles = function () {
  109. return this._activeParticles;
  110. };
  111. // Stats
  112. Scene.prototype.getLastFrameDuration = function () {
  113. return this._lastFrameDuration;
  114. };
  115. Scene.prototype.getEvaluateActiveMeshesDuration = function () {
  116. return this._evaluateActiveMeshesDuration;
  117. };
  118. Scene.prototype.getActiveMeshes = function () {
  119. return this._activeMeshes;
  120. };
  121. Scene.prototype.getRenderTargetsDuration = function () {
  122. return this._renderTargetsDuration;
  123. };
  124. Scene.prototype.getRenderDuration = function () {
  125. return this._renderDuration;
  126. };
  127. Scene.prototype.getParticlesDuration = function () {
  128. return this._particlesDuration;
  129. };
  130. Scene.prototype.getSpritesDuration = function () {
  131. return this._spritesDuration;
  132. };
  133. Scene.prototype.getAnimationRatio = function () {
  134. return this._animationRatio;
  135. };
  136. Scene.prototype.getRenderId = function () {
  137. return this._renderId;
  138. };
  139. // Ready
  140. Scene.prototype.isReady = function () {
  141. if (this._pendingData.length > 0) {
  142. return false;
  143. }
  144. for (var index = 0; index < this.meshes.length; index++) {
  145. var mesh = this.meshes[index];
  146. var mat = mesh.material;
  147. if (mesh.delayLoadState === BABYLON.Engine.DELAYLOADSTATE_LOADING) {
  148. return false;
  149. }
  150. if (mat) {
  151. if (!mat.isReady(mesh)) {
  152. return false;
  153. }
  154. }
  155. }
  156. return true;
  157. };
  158. Scene.prototype.registerBeforeRender = function (func) {
  159. this._onBeforeRenderCallbacks.push(func);
  160. };
  161. Scene.prototype.unregisterBeforeRender = function (func) {
  162. var index = this._onBeforeRenderCallbacks.indexOf(func);
  163. if (index > -1) {
  164. this._onBeforeRenderCallbacks.splice(index, 1);
  165. }
  166. };
  167. Scene.prototype._addPendingData = function (data) {
  168. this._pendingData.push(data);
  169. };
  170. Scene.prototype._removePendingData = function (data) {
  171. var index = this._pendingData.indexOf(data);
  172. if (index !== -1) {
  173. this._pendingData.splice(index, 1);
  174. }
  175. };
  176. Scene.prototype.getWaitingItemsCount = function () {
  177. return this._pendingData.length;
  178. };
  179. Scene.prototype.executeWhenReady = function (func) {
  180. var _this = this;
  181. this._onReadyCallbacks.push(func);
  182. if (this._executeWhenReadyTimeoutId !== -1) {
  183. return;
  184. }
  185. this._executeWhenReadyTimeoutId = setTimeout(function () {
  186. _this._checkIsReady();
  187. }, 150);
  188. };
  189. Scene.prototype._checkIsReady = function () {
  190. var _this = this;
  191. if (this.isReady()) {
  192. this._onReadyCallbacks.forEach(function (func) {
  193. func();
  194. });
  195. this._onReadyCallbacks = [];
  196. this._executeWhenReadyTimeoutId = -1;
  197. return;
  198. }
  199. this._executeWhenReadyTimeoutId = setTimeout(function () {
  200. _this._checkIsReady();
  201. }, 150);
  202. };
  203. // Animations
  204. Scene.prototype.beginAnimation = function (target, from, to, loop, speedRatio, onAnimationEnd) {
  205. if (speedRatio === undefined) {
  206. speedRatio = 1.0;
  207. }
  208. // Local animations
  209. if (target.animations) {
  210. this.stopAnimation(target);
  211. var animatable = new BABYLON.Internals.Animatable(target, from, to, loop, speedRatio, onAnimationEnd);
  212. this._activeAnimatables.push(animatable);
  213. }
  214. // Children animations
  215. if (target.getAnimatables) {
  216. var animatables = target.getAnimatables();
  217. for (var index = 0; index < animatables.length; index++) {
  218. this.beginAnimation(animatables[index], from, to, loop, speedRatio, onAnimationEnd);
  219. }
  220. }
  221. };
  222. Scene.prototype.beginDirectAnimation = function (target, animations, from, to, loop, speedRatio, onAnimationEnd) {
  223. if (speedRatio === undefined) {
  224. speedRatio = 1.0;
  225. }
  226. var animatable = new BABYLON.Internals.Animatable(target, from, to, loop, speedRatio, onAnimationEnd, animations);
  227. this._activeAnimatables.push(animatable);
  228. };
  229. Scene.prototype.stopAnimation = function (target) {
  230. // Local animations
  231. if (target.animations) {
  232. for (var index = 0; index < this._activeAnimatables.length; index++) {
  233. if (this._activeAnimatables[index].target === target) {
  234. this._activeAnimatables.splice(index, 1);
  235. return;
  236. }
  237. }
  238. }
  239. // Children animations
  240. if (target.getAnimatables) {
  241. var animatables = target.getAnimatables();
  242. for (index = 0; index < animatables.length; index++) {
  243. this.stopAnimation(animatables[index]);
  244. }
  245. }
  246. };
  247. Scene.prototype._animate = function () {
  248. if (!this._animationStartDate) {
  249. this._animationStartDate = new Date().getTime();
  250. }
  251. // Getting time
  252. var now = new Date().getTime();
  253. var delay = now - this._animationStartDate;
  254. for (var index = 0; index < this._activeAnimatables.length; index++) {
  255. if (!this._activeAnimatables[index]._animate(delay)) {
  256. this._activeAnimatables.splice(index, 1);
  257. index--;
  258. }
  259. }
  260. };
  261. // Matrix
  262. Scene.prototype.getViewMatrix = function () {
  263. return this._viewMatrix;
  264. };
  265. Scene.prototype.getProjectionMatrix = function () {
  266. return this._projectionMatrix;
  267. };
  268. Scene.prototype.getTransformMatrix = function () {
  269. return this._transformMatrix;
  270. };
  271. Scene.prototype.setTransformMatrix = function (view, projection) {
  272. this._viewMatrix = view;
  273. this._projectionMatrix = projection;
  274. this._viewMatrix.multiplyToRef(this._projectionMatrix, this._transformMatrix);
  275. };
  276. // Methods
  277. Scene.prototype.setActiveCameraByID = function (id) {
  278. var camera = this.getCameraByID(id);
  279. if (camera) {
  280. this.activeCamera = camera;
  281. return camera;
  282. }
  283. return null;
  284. };
  285. Scene.prototype.setActiveCameraByName = function (name) {
  286. var camera = this.getCameraByName(name);
  287. if (camera) {
  288. this.activeCamera = camera;
  289. return camera;
  290. }
  291. return null;
  292. };
  293. Scene.prototype.getMaterialByID = function (id) {
  294. for (var index = 0; index < this.materials.length; index++) {
  295. if (this.materials[index].id === id) {
  296. return this.materials[index];
  297. }
  298. }
  299. return null;
  300. };
  301. Scene.prototype.getMaterialByName = function (name) {
  302. for (var index = 0; index < this.materials.length; index++) {
  303. if (this.materials[index].name === name) {
  304. return this.materials[index];
  305. }
  306. }
  307. return null;
  308. };
  309. Scene.prototype.getCameraByID = function (id) {
  310. for (var index = 0; index < this.cameras.length; index++) {
  311. if (this.cameras[index].id === id) {
  312. return this.cameras[index];
  313. }
  314. }
  315. return null;
  316. };
  317. Scene.prototype.getCameraByName = function (name) {
  318. for (var index = 0; index < this.cameras.length; index++) {
  319. if (this.cameras[index].name === name) {
  320. return this.cameras[index];
  321. }
  322. }
  323. return null;
  324. };
  325. Scene.prototype.getLightByName = function (name) {
  326. for (var index = 0; index < this.lights.length; index++) {
  327. if (this.lights[index].name === name) {
  328. return this.lights[index];
  329. }
  330. }
  331. return null;
  332. };
  333. Scene.prototype.getLightByID = function (id) {
  334. for (var index = 0; index < this.lights.length; index++) {
  335. if (this.lights[index].id === id) {
  336. return this.lights[index];
  337. }
  338. }
  339. return null;
  340. };
  341. Scene.prototype.getMeshByID = function (id) {
  342. for (var index = 0; index < this.meshes.length; index++) {
  343. if (this.meshes[index].id === id) {
  344. return this.meshes[index];
  345. }
  346. }
  347. return null;
  348. };
  349. Scene.prototype.getLastMeshByID = function (id) {
  350. for (var index = this.meshes.length - 1; index >= 0; index--) {
  351. if (this.meshes[index].id === id) {
  352. return this.meshes[index];
  353. }
  354. }
  355. return null;
  356. };
  357. Scene.prototype.getLastEntryByID = function (id) {
  358. for (var index = this.meshes.length - 1; index >= 0; index--) {
  359. if (this.meshes[index].id === id) {
  360. return this.meshes[index];
  361. }
  362. }
  363. for (index = this.cameras.length - 1; index >= 0; index--) {
  364. if (this.cameras[index].id === id) {
  365. return this.cameras[index];
  366. }
  367. }
  368. for (index = this.lights.length - 1; index >= 0; index--) {
  369. if (this.lights[index].id === id) {
  370. return this.lights[index];
  371. }
  372. }
  373. return null;
  374. };
  375. Scene.prototype.getMeshByName = function (name) {
  376. for (var index = 0; index < this.meshes.length; index++) {
  377. if (this.meshes[index].name === name) {
  378. return this.meshes[index];
  379. }
  380. }
  381. return null;
  382. };
  383. Scene.prototype.getLastSkeletonByID = function (id) {
  384. for (var index = this.skeletons.length - 1; index >= 0; index--) {
  385. if (this.skeletons[index].id === id) {
  386. return this.skeletons[index];
  387. }
  388. }
  389. return null;
  390. };
  391. Scene.prototype.getSkeletonById = function (id) {
  392. for (var index = 0; index < this.skeletons.length; index++) {
  393. if (this.skeletons[index].id === id) {
  394. return this.skeletons[index];
  395. }
  396. }
  397. return null;
  398. };
  399. Scene.prototype.getSkeletonByName = function (name) {
  400. for (var index = 0; index < this.skeletons.length; index++) {
  401. if (this.skeletons[index].name === name) {
  402. return this.skeletons[index];
  403. }
  404. }
  405. return null;
  406. };
  407. Scene.prototype.isActiveMesh = function (mesh) {
  408. return (this._activeMeshes.indexOf(mesh) !== -1);
  409. };
  410. Scene.prototype._evaluateSubMesh = function (subMesh, mesh) {
  411. if (mesh.subMeshes.length == 1 || subMesh.isInFrustum(this._frustumPlanes)) {
  412. var material = subMesh.getMaterial();
  413. if (material) {
  414. // Render targets
  415. if (material.getRenderTargetTextures) {
  416. if (this._processedMaterials.indexOf(material) === -1) {
  417. this._processedMaterials.push(material);
  418. this._renderTargets.concat(material.getRenderTargetTextures());
  419. }
  420. }
  421. // Dispatch
  422. this._activeVertices += subMesh.verticesCount;
  423. this._renderingManager.dispatch(subMesh);
  424. }
  425. }
  426. };
  427. Scene.prototype._evaluateActiveMeshes = function () {
  428. this._activeMeshes.reset();
  429. this._renderingManager.reset();
  430. this._processedMaterials.reset();
  431. this._activeParticleSystems.reset();
  432. this._activeSkeletons.reset();
  433. this._boundingBoxRenderer.reset();
  434. if (!this._frustumPlanes) {
  435. this._frustumPlanes = BABYLON.Frustum.GetPlanes(this._transformMatrix);
  436. } else {
  437. BABYLON.Frustum.GetPlanesToRef(this._transformMatrix, this._frustumPlanes);
  438. }
  439. // Meshes
  440. if (this._selectionOctree) {
  441. var selection = this._selectionOctree.select(this._frustumPlanes);
  442. for (var blockIndex = 0; blockIndex < selection.length; blockIndex++) {
  443. var block = selection.data[blockIndex];
  444. for (var meshIndex = 0; meshIndex < block.meshes.length; meshIndex++) {
  445. var mesh = block.meshes[meshIndex];
  446. if (Math.abs(mesh._renderId) !== this._renderId) {
  447. this._totalVertices += mesh.getTotalVertices();
  448. if (!mesh.isReady()) {
  449. continue;
  450. }
  451. mesh.computeWorldMatrix();
  452. mesh._renderId = 0;
  453. }
  454. if (mesh._renderId === this._renderId || (mesh._renderId === 0 && mesh.isEnabled() && mesh.isVisible && mesh.visibility > 0 && mesh.isInFrustum(this._frustumPlanes))) {
  455. if (mesh._renderId === 0) {
  456. this._activeMeshes.push(mesh);
  457. }
  458. mesh._renderId = this._renderId;
  459. if (mesh.showBoundingBox) {
  460. this._boundingBoxRenderer.renderList.push(mesh);
  461. }
  462. if (mesh.skeleton) {
  463. this._activeSkeletons.pushNoDuplicate(mesh.skeleton);
  464. }
  465. var subMeshes = block.subMeshes[meshIndex];
  466. for (subIndex = 0; subIndex < subMeshes.length; subIndex++) {
  467. subMesh = subMeshes[subIndex];
  468. if (subMesh._renderId === this._renderId) {
  469. continue;
  470. }
  471. subMesh._renderId = this._renderId;
  472. this._evaluateSubMesh(subMesh, mesh);
  473. }
  474. } else {
  475. mesh._renderId = -this._renderId;
  476. }
  477. }
  478. }
  479. } else {
  480. for (meshIndex = 0; meshIndex < this.meshes.length; meshIndex++) {
  481. mesh = this.meshes[meshIndex];
  482. this._totalVertices += mesh.getTotalVertices();
  483. if (!mesh.isReady()) {
  484. continue;
  485. }
  486. mesh.computeWorldMatrix();
  487. if (mesh.isEnabled() && mesh.isVisible && mesh.visibility > 0 && mesh.isInFrustum(this._frustumPlanes)) {
  488. this._activeMeshes.push(mesh);
  489. if (mesh.skeleton) {
  490. this._activeSkeletons.pushNoDuplicate(mesh.skeleton);
  491. }
  492. if (mesh.showBoundingBox) {
  493. this._boundingBoxRenderer.renderList.push(mesh);
  494. }
  495. if (mesh.subMeshes) {
  496. for (var subIndex = 0; subIndex < mesh.subMeshes.length; subIndex++) {
  497. var subMesh = mesh.subMeshes[subIndex];
  498. this._evaluateSubMesh(subMesh, mesh);
  499. }
  500. }
  501. }
  502. }
  503. }
  504. // Particle systems
  505. var beforeParticlesDate = new Date().getTime();
  506. if (this.particlesEnabled) {
  507. for (var particleIndex = 0; particleIndex < this.particleSystems.length; particleIndex++) {
  508. var particleSystem = this.particleSystems[particleIndex];
  509. if (!particleSystem.emitter.position || (particleSystem.emitter && particleSystem.emitter.isEnabled())) {
  510. this._activeParticleSystems.push(particleSystem);
  511. particleSystem.animate();
  512. }
  513. }
  514. }
  515. this._particlesDuration += new Date().getTime() - beforeParticlesDate;
  516. };
  517. Scene.prototype.updateTransformMatrix = function (force) {
  518. this.setTransformMatrix(this.activeCamera.getViewMatrix(), this.activeCamera.getProjectionMatrix(force));
  519. };
  520. Scene.prototype._renderForCamera = function (camera) {
  521. var engine = this._engine;
  522. this.activeCamera = camera;
  523. if (!this.activeCamera)
  524. throw new Error("Active camera not set");
  525. // Viewport
  526. engine.setViewport(this.activeCamera.viewport);
  527. // Camera
  528. this._renderId++;
  529. this.updateTransformMatrix();
  530. // Meshes
  531. var beforeEvaluateActiveMeshesDate = new Date().getTime();
  532. this._evaluateActiveMeshes();
  533. this._evaluateActiveMeshesDuration += new Date().getTime() - beforeEvaluateActiveMeshesDate;
  534. for (var skeletonIndex = 0; skeletonIndex < this._activeSkeletons.length; skeletonIndex++) {
  535. var skeleton = this._activeSkeletons.data[skeletonIndex];
  536. skeleton.prepare();
  537. }
  538. for (var customIndex = 0; customIndex < this.customRenderTargets.length; customIndex++) {
  539. this._renderTargets.push(this.customRenderTargets[customIndex]);
  540. }
  541. // Render targets
  542. var beforeRenderTargetDate = new Date().getTime();
  543. if (this.renderTargetsEnabled) {
  544. for (var renderIndex = 0; renderIndex < this._renderTargets.length; renderIndex++) {
  545. var renderTarget = this._renderTargets.data[renderIndex];
  546. this._renderId++;
  547. renderTarget.render();
  548. }
  549. }
  550. if (this._renderTargets.length > 0) {
  551. engine.restoreDefaultFramebuffer();
  552. }
  553. this._renderTargetsDuration = new Date().getTime() - beforeRenderTargetDate;
  554. // Prepare Frame
  555. this.postProcessManager._prepareFrame();
  556. var beforeRenderDate = new Date().getTime();
  557. // Backgrounds
  558. if (this.layers.length) {
  559. engine.setDepthBuffer(false);
  560. var layerIndex;
  561. var layer;
  562. for (layerIndex = 0; layerIndex < this.layers.length; layerIndex++) {
  563. layer = this.layers[layerIndex];
  564. if (layer.isBackground) {
  565. layer.render();
  566. }
  567. }
  568. engine.setDepthBuffer(true);
  569. }
  570. // Render
  571. this._renderingManager.render(null, null, true, true);
  572. // Bounding boxes
  573. this._boundingBoxRenderer.render();
  574. for (var lensFlareSystemIndex = 0; lensFlareSystemIndex < this.lensFlareSystems.length; lensFlareSystemIndex++) {
  575. this.lensFlareSystems[lensFlareSystemIndex].render();
  576. }
  577. // Foregrounds
  578. if (this.layers.length) {
  579. engine.setDepthBuffer(false);
  580. for (layerIndex = 0; layerIndex < this.layers.length; layerIndex++) {
  581. layer = this.layers[layerIndex];
  582. if (!layer.isBackground) {
  583. layer.render();
  584. }
  585. }
  586. engine.setDepthBuffer(true);
  587. }
  588. this._renderDuration += new Date().getTime() - beforeRenderDate;
  589. // Finalize frame
  590. this.postProcessManager._finalizeFrame(camera.isIntermediate);
  591. // Update camera
  592. this.activeCamera._updateFromScene();
  593. // Reset some special arrays
  594. this._renderTargets.reset();
  595. };
  596. Scene.prototype._processSubCameras = function (camera) {
  597. if (camera.subCameras.length == 0) {
  598. this._renderForCamera(camera);
  599. return;
  600. }
  601. for (var index = 0; index < camera.subCameras.length; index++) {
  602. this._renderForCamera(camera.subCameras[index]);
  603. }
  604. this.activeCamera = camera;
  605. this.setTransformMatrix(this.activeCamera.getViewMatrix(), this.activeCamera.getProjectionMatrix());
  606. // Update camera
  607. this.activeCamera._updateFromScene();
  608. };
  609. Scene.prototype.render = function () {
  610. var startDate = new Date().getTime();
  611. this._particlesDuration = 0;
  612. this._spritesDuration = 0;
  613. this._activeParticles = 0;
  614. this._renderDuration = 0;
  615. this._evaluateActiveMeshesDuration = 0;
  616. this._totalVertices = 0;
  617. this._activeVertices = 0;
  618. // Before render
  619. if (this.beforeRender) {
  620. this.beforeRender();
  621. }
  622. for (var callbackIndex = 0; callbackIndex < this._onBeforeRenderCallbacks.length; callbackIndex++) {
  623. this._onBeforeRenderCallbacks[callbackIndex]();
  624. }
  625. // Animations
  626. var deltaTime = BABYLON.Tools.GetDeltaTime();
  627. this._animationRatio = deltaTime * (60.0 / 1000.0);
  628. this._animate();
  629. // Physics
  630. if (this._physicsEngine) {
  631. this._physicsEngine._runOneStep(deltaTime / 1000.0);
  632. }
  633. // Clear
  634. this._engine.clear(this.clearColor, this.autoClear || this.forceWireframe, true);
  635. for (var lightIndex = 0; lightIndex < this.lights.length; lightIndex++) {
  636. var light = this.lights[lightIndex];
  637. var shadowGenerator = light.getShadowGenerator();
  638. if (light.isEnabled() && shadowGenerator && shadowGenerator.getShadowMap().getScene().textures.indexOf(shadowGenerator.getShadowMap()) !== -1) {
  639. this._renderTargets.push(shadowGenerator.getShadowMap());
  640. }
  641. }
  642. // Multi-cameras?
  643. if (this.activeCameras.length > 0) {
  644. var currentRenderId = this._renderId;
  645. for (var cameraIndex = 0; cameraIndex < this.activeCameras.length; cameraIndex++) {
  646. this._renderId = currentRenderId;
  647. this._processSubCameras(this.activeCameras[cameraIndex]);
  648. }
  649. } else {
  650. this._processSubCameras(this.activeCamera);
  651. }
  652. // After render
  653. if (this.afterRender) {
  654. this.afterRender();
  655. }
  656. for (var index = 0; index < this._toBeDisposed.length; index++) {
  657. this._toBeDisposed.data[index].dispose();
  658. this._toBeDisposed[index] = null;
  659. }
  660. this._toBeDisposed.reset();
  661. this._lastFrameDuration = new Date().getTime() - startDate;
  662. };
  663. Scene.prototype.dispose = function () {
  664. this.beforeRender = null;
  665. this.afterRender = null;
  666. this.skeletons = [];
  667. this._boundingBoxRenderer.dispose();
  668. // Detach cameras
  669. var canvas = this._engine.getRenderingCanvas();
  670. var index;
  671. for (index = 0; index < this.cameras.length; index++) {
  672. this.cameras[index].detachControl(canvas);
  673. }
  674. while (this.lights.length) {
  675. this.lights[0].dispose();
  676. }
  677. while (this.meshes.length) {
  678. this.meshes[0].dispose(true);
  679. }
  680. while (this.cameras.length) {
  681. this.cameras[0].dispose();
  682. }
  683. while (this.materials.length) {
  684. this.materials[0].dispose();
  685. }
  686. while (this.particleSystems.length) {
  687. this.particleSystems[0].dispose();
  688. }
  689. while (this.spriteManagers.length) {
  690. this.spriteManagers[0].dispose();
  691. }
  692. while (this.layers.length) {
  693. this.layers[0].dispose();
  694. }
  695. while (this.textures.length) {
  696. this.textures[0].dispose();
  697. }
  698. // Post-processes
  699. this.postProcessManager.dispose();
  700. // Physics
  701. if (this._physicsEngine) {
  702. this.disablePhysicsEngine();
  703. }
  704. // Remove from engine
  705. index = this._engine.scenes.indexOf(this);
  706. this._engine.scenes.splice(index, 1);
  707. this._engine.wipeCaches();
  708. };
  709. // Collisions
  710. Scene.prototype._getNewPosition = function (position, velocity, collider, maximumRetry, finalPosition) {
  711. position.divideToRef(collider.radius, this._scaledPosition);
  712. velocity.divideToRef(collider.radius, this._scaledVelocity);
  713. collider.retry = 0;
  714. collider.initialVelocity = this._scaledVelocity;
  715. collider.initialPosition = this._scaledPosition;
  716. this._collideWithWorld(this._scaledPosition, this._scaledVelocity, collider, maximumRetry, finalPosition);
  717. finalPosition.multiplyInPlace(collider.radius);
  718. };
  719. Scene.prototype._collideWithWorld = function (position, velocity, collider, maximumRetry, finalPosition) {
  720. var closeDistance = BABYLON.Engine.CollisionsEpsilon * 10.0;
  721. if (collider.retry >= maximumRetry) {
  722. finalPosition.copyFrom(position);
  723. return;
  724. }
  725. collider._initialize(position, velocity, closeDistance);
  726. for (var index = 0; index < this.meshes.length; index++) {
  727. var mesh = this.meshes[index];
  728. if (mesh.isEnabled() && mesh.checkCollisions) {
  729. mesh._checkCollision(collider);
  730. }
  731. }
  732. if (!collider.collisionFound) {
  733. position.addToRef(velocity, finalPosition);
  734. return;
  735. }
  736. if (velocity.x != 0 || velocity.y != 0 || velocity.z != 0) {
  737. collider._getResponse(position, velocity);
  738. }
  739. if (velocity.length() <= closeDistance) {
  740. finalPosition.copyFrom(position);
  741. return;
  742. }
  743. collider.retry++;
  744. this._collideWithWorld(position, velocity, collider, maximumRetry, finalPosition);
  745. };
  746. // Octrees
  747. Scene.prototype.createOrUpdateSelectionOctree = function () {
  748. if (!this._selectionOctree) {
  749. this._selectionOctree = new BABYLON.Octree();
  750. }
  751. var min = new BABYLON.Vector3(Number.MAX_VALUE, Number.MAX_VALUE, Number.MAX_VALUE);
  752. var max = new BABYLON.Vector3(-Number.MAX_VALUE, -Number.MAX_VALUE, -Number.MAX_VALUE);
  753. for (var index = 0; index < this.meshes.length; index++) {
  754. var mesh = this.meshes[index];
  755. mesh.computeWorldMatrix(true);
  756. var minBox = mesh.getBoundingInfo().boundingBox.minimumWorld;
  757. var maxBox = mesh.getBoundingInfo().boundingBox.maximumWorld;
  758. checkExtends(minBox, min, max);
  759. checkExtends(maxBox, min, max);
  760. }
  761. // Update octree
  762. this._selectionOctree.update(min, max, this.meshes);
  763. };
  764. // Picking
  765. Scene.prototype.createPickingRay = function (x, y, world, camera) {
  766. var engine = this._engine;
  767. if (!camera) {
  768. if (!this.activeCamera)
  769. throw new Error("Active camera not set");
  770. camera = this.activeCamera;
  771. }
  772. var viewport = camera.viewport.toGlobal(engine);
  773. return BABYLON.Ray.CreateNew(x * this._engine.getHardwareScalingLevel(), y * this._engine.getHardwareScalingLevel(), viewport.width, viewport.height, world ? world : BABYLON.Matrix.Identity(), camera.getViewMatrix(), camera.getProjectionMatrix());
  774. };
  775. Scene.prototype._internalPick = function (rayFunction, predicate, fastCheck) {
  776. var pickingInfo = null;
  777. for (var meshIndex = 0; meshIndex < this.meshes.length; meshIndex++) {
  778. var mesh = this.meshes[meshIndex];
  779. if (predicate) {
  780. if (!predicate(mesh)) {
  781. continue;
  782. }
  783. } else if (!mesh.isEnabled() || !mesh.isVisible || !mesh.isPickable) {
  784. continue;
  785. }
  786. var world = mesh.getWorldMatrix();
  787. var ray = rayFunction(world);
  788. var result = mesh.intersects(ray, fastCheck);
  789. if (!result.hit)
  790. continue;
  791. if (!fastCheck && pickingInfo != null && result.distance >= pickingInfo.distance)
  792. continue;
  793. pickingInfo = result;
  794. if (fastCheck) {
  795. break;
  796. }
  797. }
  798. return pickingInfo || new BABYLON.PickingInfo();
  799. };
  800. Scene.prototype.pick = function (x, y, predicate, fastCheck, camera) {
  801. var _this = this;
  802. /// <summary>Launch a ray to try to pick a mesh in the scene</summary>
  803. /// <param name="x">X position on screen</param>
  804. /// <param name="y">Y position on screen</param>
  805. /// <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>
  806. /// <param name="fastCheck">Launch a fast check only using the bounding boxes. Can be set to null.</param>
  807. /// <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>
  808. return this._internalPick(function (world) {
  809. return _this.createPickingRay(x, y, world, camera);
  810. }, predicate, fastCheck);
  811. };
  812. Scene.prototype.pickWithRay = function (ray, predicate, fastCheck) {
  813. var _this = this;
  814. return this._internalPick(function (world) {
  815. if (!_this._pickWithRayInverseMatrix) {
  816. _this._pickWithRayInverseMatrix = BABYLON.Matrix.Identity();
  817. }
  818. world.invertToRef(_this._pickWithRayInverseMatrix);
  819. return BABYLON.Ray.Transform(ray, _this._pickWithRayInverseMatrix);
  820. }, predicate, fastCheck);
  821. };
  822. // Physics
  823. Scene.prototype.getPhysicsEngine = function () {
  824. return this._physicsEngine;
  825. };
  826. Scene.prototype.enablePhysics = function (gravity, plugin) {
  827. if (this._physicsEngine) {
  828. return true;
  829. }
  830. this._physicsEngine = new BABYLON.PhysicsEngine(plugin);
  831. if (!this._physicsEngine.isSupported()) {
  832. this._physicsEngine = null;
  833. return false;
  834. }
  835. this._physicsEngine._initialize(gravity);
  836. return true;
  837. };
  838. Scene.prototype.disablePhysicsEngine = function () {
  839. if (!this._physicsEngine) {
  840. return;
  841. }
  842. this._physicsEngine.dispose();
  843. this._physicsEngine = undefined;
  844. };
  845. Scene.prototype.isPhysicsEnabled = function () {
  846. return this._physicsEngine !== undefined;
  847. };
  848. Scene.prototype.setGravity = function (gravity) {
  849. if (!this._physicsEngine) {
  850. return;
  851. }
  852. this._physicsEngine._setGravity(gravity);
  853. };
  854. Scene.prototype.createCompoundImpostor = function (parts, options) {
  855. if (parts.parts) {
  856. options = parts;
  857. parts = parts.parts;
  858. }
  859. if (!this._physicsEngine) {
  860. return null;
  861. }
  862. for (var index = 0; index < parts.length; index++) {
  863. var mesh = parts[index].mesh;
  864. mesh._physicImpostor = parts[index].impostor;
  865. mesh._physicsMass = options.mass / parts.length;
  866. mesh._physicsFriction = options.friction;
  867. mesh._physicRestitution = options.restitution;
  868. }
  869. return this._physicsEngine._registerMeshesAsCompound(parts, options);
  870. };
  871. //ANY
  872. Scene.prototype.deleteCompoundImpostor = function (compound) {
  873. for (var index = 0; index < compound.parts.length; index++) {
  874. var mesh = compound.parts[index].mesh;
  875. mesh._physicImpostor = BABYLON.PhysicsEngine.NoImpostor;
  876. this._physicsEngine._unregisterMesh(mesh);
  877. }
  878. };
  879. // Tags
  880. Scene.prototype._getByTags = function (list, tagsQuery) {
  881. if (tagsQuery === undefined) {
  882. // returns the complete list (could be done with BABYLON.Tags.MatchesQuery but no need to have a for-loop here)
  883. return list;
  884. }
  885. var listByTags = [];
  886. for (var i in list) {
  887. var item = list[i];
  888. if (BABYLON.Tags.MatchesQuery(item, tagsQuery)) {
  889. listByTags.push(item);
  890. }
  891. }
  892. return listByTags;
  893. };
  894. Scene.prototype.getMeshesByTags = function (tagsQuery) {
  895. return this._getByTags(this.meshes, tagsQuery);
  896. };
  897. Scene.prototype.getCamerasByTags = function (tagsQuery) {
  898. return this._getByTags(this.cameras, tagsQuery);
  899. };
  900. Scene.prototype.getLightsByTags = function (tagsQuery) {
  901. return this._getByTags(this.lights, tagsQuery);
  902. };
  903. Scene.prototype.getMaterialByTags = function (tagsQuery) {
  904. return this._getByTags(this.materials, tagsQuery).concat(this._getByTags(this.multiMaterials, tagsQuery));
  905. };
  906. Scene.FOGMODE_NONE = 0;
  907. Scene.FOGMODE_EXP = 1;
  908. Scene.FOGMODE_EXP2 = 2;
  909. Scene.FOGMODE_LINEAR = 3;
  910. return Scene;
  911. })();
  912. BABYLON.Scene = Scene;
  913. })(BABYLON || (BABYLON = {}));
  914. //# sourceMappingURL=babylon.scene.js.map