babylon.scene.js 43 KB

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