babylon.scene.js 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842
  1. var BABYLON = BABYLON || {};
  2. (function () {
  3. BABYLON.Scene = function (engine) {
  4. this._engine = engine;
  5. this.autoClear = true;
  6. this.clearColor = new BABYLON.Color3(0.2, 0.2, 0.3);
  7. this.ambientColor = new BABYLON.Color3(0, 0, 0);
  8. engine.scenes.push(this);
  9. this._totalVertices = 0;
  10. this._activeVertices = 0;
  11. this._activeParticles = 0;
  12. this._lastFrameDuration = 0;
  13. this._evaluateActiveMeshesDuration = 0;
  14. this._renderTargetsDuration = 0;
  15. this._renderDuration = 0;
  16. this._renderId = 0;
  17. this._executeWhenReadyTimeoutId = -1;
  18. this._toBeDisposed = new BABYLON.Tools.SmartArray(256);
  19. this._onReadyCallbacks = [];
  20. this._pendingData = [];
  21. this._onBeforeRenderCallbacks = [];
  22. // Fog
  23. this.fogMode = BABYLON.Scene.FOGMODE_NONE;
  24. this.fogColor = new BABYLON.Color3(0.2, 0.2, 0.3);
  25. this.fogDensity = 0.1;
  26. this.fogStart = 0;
  27. this.fogEnd = 1000.0;
  28. // Lights
  29. this.lights = [];
  30. // Cameras
  31. this.cameras = [];
  32. this.activeCamera = null;
  33. // Meshes
  34. this.meshes = [];
  35. // Internal smart arrays
  36. this._activeMeshes = new BABYLON.Tools.SmartArray(256);
  37. this._processedMaterials = new BABYLON.Tools.SmartArray(256);
  38. this._renderTargets = new BABYLON.Tools.SmartArray(256);
  39. this._activeParticleSystems = new BABYLON.Tools.SmartArray(256);
  40. this._activeSkeletons = new BABYLON.Tools.SmartArray(32);
  41. // Rendering groups
  42. this._renderingManager = new BABYLON.RenderingManager(this);
  43. // Materials
  44. this.materials = [];
  45. this.multiMaterials = [];
  46. this.defaultMaterial = new BABYLON.StandardMaterial("default material", this);
  47. // Textures
  48. this.textures = [];
  49. // Particles
  50. this.particlesEnabled = true;
  51. this.particleSystems = [];
  52. // Sprites
  53. this.spriteManagers = [];
  54. // Layers
  55. this.layers = [];
  56. // Skeletons
  57. this.skeletons = [];
  58. // Collisions
  59. this.collisionsEnabled = true;
  60. this.gravity = new BABYLON.Vector3(0, -9.0, 0);
  61. // Animations
  62. this._activeAnimatables = [];
  63. // Matrices
  64. this._transformMatrix = BABYLON.Matrix.Zero();
  65. // Internals
  66. this._scaledPosition = BABYLON.Vector3.Zero();
  67. this._scaledVelocity = BABYLON.Vector3.Zero();
  68. // Postprocesses
  69. this.postProcessManager = new BABYLON.PostProcessManager();
  70. };
  71. // Properties
  72. BABYLON.Scene.prototype.getEngine = function () {
  73. return this._engine;
  74. };
  75. BABYLON.Scene.prototype.getTotalVertices = function () {
  76. return this._totalVertices;
  77. };
  78. BABYLON.Scene.prototype.getActiveVertices = function () {
  79. return this._activeVertices;
  80. };
  81. BABYLON.Scene.prototype.getTotalVertices = function () {
  82. return this._totalVertices;
  83. };
  84. BABYLON.Scene.prototype.getActiveParticles = function () {
  85. return this._activeParticles;
  86. };
  87. // Stats
  88. BABYLON.Scene.prototype.getLastFrameDuration = function () {
  89. return this._lastFrameDuration;
  90. };
  91. BABYLON.Scene.prototype.getEvaluateActiveMeshesDuration = function () {
  92. return this._evaluateActiveMeshesDuration;
  93. };
  94. BABYLON.Scene.prototype.getRenderTargetsDuration = function () {
  95. return this._renderTargetsDuration;
  96. };
  97. BABYLON.Scene.prototype.getRenderDuration = function () {
  98. return this._renderDuration;
  99. };
  100. BABYLON.Scene.prototype.getParticlesDuration = function () {
  101. return this._particlesDuration;
  102. };
  103. BABYLON.Scene.prototype.getSpritesDuration = function () {
  104. return this._spritesDuration;
  105. };
  106. BABYLON.Scene.prototype.getAnimationRatio = function () {
  107. return this._animationRatio;
  108. };
  109. BABYLON.Scene.prototype.getRenderId = function () {
  110. return this._renderId;
  111. };
  112. // Ready
  113. BABYLON.Scene.prototype.isReady = function () {
  114. if (this._pendingData.length > 0) {
  115. return false;
  116. }
  117. for (var index = 0; index < this.meshes.length; index++) {
  118. var mesh = this.meshes[index];
  119. var mat = mesh.material;
  120. if (mesh.delayLoadState === BABYLON.Engine.DELAYLOADSTATE_LOADING) {
  121. return false;
  122. }
  123. if (mat) {
  124. if (!mat.isReady(mesh, mesh.delayLoadState !== BABYLON.Engine.DELAYLOADSTATE_NOTLOADED)) {
  125. return false;
  126. }
  127. }
  128. }
  129. return true;
  130. };
  131. BABYLON.Scene.prototype.registerBeforeRender = function (func) {
  132. this._onBeforeRenderCallbacks.push(func);
  133. };
  134. BABYLON.Scene.prototype.unregisterBeforeRender = function (func) {
  135. var index = this._onBeforeRenderCallbacks.indexOf(func);
  136. if (index > -1) {
  137. this._onBeforeRenderCallbacks.splice(index, 1);
  138. }
  139. };
  140. BABYLON.Scene.prototype._addPendingData = function (data) {
  141. this._pendingData.push(data);
  142. };
  143. BABYLON.Scene.prototype._removePendingData = function (data) {
  144. var index = this._pendingData.indexOf(data);
  145. if (index !== -1) {
  146. this._pendingData.splice(index, 1);
  147. }
  148. };
  149. BABYLON.Scene.prototype.getWaitingItemsCount = function () {
  150. return this._pendingData.length;
  151. };
  152. BABYLON.Scene.prototype.executeWhenReady = function (func) {
  153. this._onReadyCallbacks.push(func);
  154. if (this._executeWhenReadyTimeoutId !== -1) {
  155. return;
  156. }
  157. var that = this;
  158. this._executeWhenReadyTimeoutId = setTimeout(function () {
  159. that._checkIsReady();
  160. }, 150);
  161. };
  162. BABYLON.Scene.prototype._checkIsReady = function () {
  163. if (this.isReady()) {
  164. this._onReadyCallbacks.forEach(function (func) {
  165. func();
  166. });
  167. this._onReadyCallbacks = [];
  168. this._executeWhenReadyTimeoutId = -1;
  169. return;
  170. }
  171. var that = this;
  172. this._executeWhenReadyTimeoutId = setTimeout(function () {
  173. that._checkIsReady();
  174. }, 150);
  175. };
  176. // Animations
  177. BABYLON.Scene.prototype.beginAnimation = function (target, from, to, loop, speedRatio, onAnimationEnd) {
  178. if (speedRatio === undefined) {
  179. speedRatio = 1.0;
  180. }
  181. // Local animations
  182. if (target.animations) {
  183. this.stopAnimation(target);
  184. var animatable = new BABYLON._Animatable(target, from, to, loop, speedRatio, onAnimationEnd);
  185. this._activeAnimatables.push(animatable);
  186. }
  187. // Children animations
  188. if (target.getAnimatables) {
  189. var animatables = target.getAnimatables();
  190. for (var index = 0; index < animatables.length; index++) {
  191. this.beginAnimation(animatables[index], from, to, loop, speedRatio, onAnimationEnd);
  192. }
  193. }
  194. };
  195. BABYLON.Scene.prototype.stopAnimation = function (target) {
  196. for (var index = 0; index < this._activeAnimatables.length; index++) {
  197. if (this._activeAnimatables[index].target === target) {
  198. this._activeAnimatables.splice(index, 1);
  199. return;
  200. }
  201. }
  202. };
  203. BABYLON.Scene.prototype._animate = function () {
  204. if (!this._animationStartDate) {
  205. this._animationStartDate = new Date();
  206. }
  207. // Getting time
  208. var now = new Date();
  209. var delay = now - this._animationStartDate;
  210. for (var index = 0; index < this._activeAnimatables.length; index++) {
  211. if (!this._activeAnimatables[index]._animate(delay)) {
  212. this._activeAnimatables.splice(index, 1);
  213. index--;
  214. }
  215. }
  216. };
  217. // Matrix
  218. BABYLON.Scene.prototype.getViewMatrix = function () {
  219. return this._viewMatrix;
  220. };
  221. BABYLON.Scene.prototype.getProjectionMatrix = function () {
  222. return this._projectionMatrix;
  223. };
  224. BABYLON.Scene.prototype.getTransformMatrix = function () {
  225. return this._transformMatrix;
  226. };
  227. BABYLON.Scene.prototype.setTransformMatrix = function (view, projection) {
  228. this._viewMatrix = view;
  229. this._projectionMatrix = projection;
  230. this._viewMatrix.multiplyToRef(this._projectionMatrix, this._transformMatrix);
  231. };
  232. // Methods
  233. BABYLON.Scene.prototype.activeCameraByID = function (id) {
  234. for (var index = 0; index < this.cameras.length; index++) {
  235. if (this.cameras[index].id === id) {
  236. this.activeCamera = this.cameras[index];
  237. return;
  238. }
  239. }
  240. };
  241. BABYLON.Scene.prototype.getMaterialByID = function (id) {
  242. for (var index = 0; index < this.materials.length; index++) {
  243. if (this.materials[index].id === id) {
  244. return this.materials[index];
  245. }
  246. }
  247. return null;
  248. };
  249. BABYLON.Scene.prototype.getLightByID = function (id) {
  250. for (var index = 0; index < this.lights.length; index++) {
  251. if (this.lights[index].id === id) {
  252. return this.lights[index];
  253. }
  254. }
  255. return null;
  256. };
  257. BABYLON.Scene.prototype.getMeshByID = function (id) {
  258. for (var index = 0; index < this.meshes.length; index++) {
  259. if (this.meshes[index].id === id) {
  260. return this.meshes[index];
  261. }
  262. }
  263. return null;
  264. };
  265. BABYLON.Scene.prototype.getLastMeshByID = function (id) {
  266. for (var index = this.meshes.length - 1; index >= 0 ; index--) {
  267. if (this.meshes[index].id === id) {
  268. return this.meshes[index];
  269. }
  270. }
  271. return null;
  272. };
  273. BABYLON.Scene.prototype.getMeshByName = function (name) {
  274. for (var index = 0; index < this.meshes.length; index++) {
  275. if (this.meshes[index].name === name) {
  276. return this.meshes[index];
  277. }
  278. }
  279. return null;
  280. };
  281. BABYLON.Scene.prototype.getLastSkeletonByID = function (id) {
  282. for (var index = this.skeletons.length - 1; index >= 0 ; index--) {
  283. if (this.skeletons[index].id === id) {
  284. return this.skeletons[index];
  285. }
  286. }
  287. return null;
  288. };
  289. BABYLON.Scene.prototype.getSkeletonById = function (id) {
  290. for (var index = 0; index < this.skeletons.length; index++) {
  291. if (this.skeletons[index].id === id) {
  292. return this.skeletons[index];
  293. }
  294. }
  295. return null;
  296. };
  297. BABYLON.Scene.prototype.getSkeletonByName = function (name) {
  298. for (var index = 0; index < this.skeleton.length; index++) {
  299. if (this.skeletons[index].name === name) {
  300. return this.skeletons[index];
  301. }
  302. }
  303. return null;
  304. };
  305. BABYLON.Scene.prototype.isActiveMesh = function (mesh) {
  306. return (this._activeMeshes.indexOf(mesh) !== -1);
  307. };
  308. BABYLON.Scene.prototype._evaluateSubMesh = function (subMesh, mesh) {
  309. if (mesh.subMeshes.length == 1 || subMesh.isInFrustrum(this._frustumPlanes)) {
  310. var material = subMesh.getMaterial();
  311. if (material) {
  312. // Render targets
  313. if (material.getRenderTargetTextures) {
  314. if (this._processedMaterials.indexOf(material) === -1) {
  315. this._processedMaterials.push(material);
  316. this._renderTargets.concat(material.getRenderTargetTextures());
  317. }
  318. }
  319. // Dispatch
  320. this._activeVertices += subMesh.verticesCount;
  321. this._renderingManager.dispatch(subMesh);
  322. }
  323. }
  324. };
  325. BABYLON.Scene.prototype._evaluateActiveMeshes = function () {
  326. this._activeMeshes.reset();
  327. this._renderingManager.reset();
  328. this._processedMaterials.reset();
  329. this._renderTargets.reset();
  330. this._activeParticleSystems.reset();
  331. this._activeSkeletons.reset();
  332. if (!this._frustumPlanes) {
  333. this._frustumPlanes = BABYLON.Frustum.GetPlanes(this._transformMatrix);
  334. } else {
  335. BABYLON.Frustum.GetPlanesToRef(this._transformMatrix, this._frustumPlanes);
  336. }
  337. this._totalVertices = 0;
  338. this._activeVertices = 0;
  339. // Meshes
  340. if (this._selectionOctree) { // Octree
  341. var selection = this._selectionOctree.select(this._frustumPlanes);
  342. for (var blockIndex = 0; blockIndex < selection.length; blockIndex++) {
  343. var block = selection.data[blockIndex];
  344. for (var meshIndex = 0; meshIndex < block.meshes.length; meshIndex++) {
  345. var mesh = block.meshes[meshIndex];
  346. if (Math.abs(mesh._renderId) !== this._renderId) {
  347. this._totalVertices += mesh.getTotalVertices();
  348. if (!mesh.isReady()) {
  349. continue;
  350. }
  351. mesh.computeWorldMatrix();
  352. mesh._renderId = 0;
  353. }
  354. if (mesh._renderId === this._renderId || (mesh._renderId === 0 && mesh.isEnabled() && mesh.isVisible && mesh.visibility > 0 && mesh.isInFrustrum(this._frustumPlanes))) {
  355. if (mesh._renderId === 0) {
  356. this._activeMeshes.push(mesh);
  357. }
  358. mesh._renderId = this._renderId;
  359. if (mesh.skeleton) {
  360. this._activeSkeletons.pushNoDuplicate(mesh.skeleton);
  361. }
  362. var subMeshes = block.subMeshes[meshIndex];
  363. for (var subIndex = 0; subIndex < subMeshes.length; subIndex++) {
  364. var subMesh = subMeshes[subIndex];
  365. if (subMesh._renderId === this._renderId) {
  366. continue;
  367. }
  368. subMesh._renderId = this._renderId;
  369. this._evaluateSubMesh(subMesh, mesh);
  370. }
  371. } else {
  372. mesh._renderId = -this._renderId;
  373. }
  374. }
  375. }
  376. } else { // Full scene traversal
  377. for (var meshIndex = 0; meshIndex < this.meshes.length; meshIndex++) {
  378. var mesh = this.meshes[meshIndex];
  379. this._totalVertices += mesh.getTotalVertices();
  380. if (!mesh.isReady()) {
  381. continue;
  382. }
  383. mesh.computeWorldMatrix();
  384. if (mesh.isEnabled() && mesh.isVisible && mesh.visibility > 0 && mesh.isInFrustrum(this._frustumPlanes)) {
  385. this._activeMeshes.push(mesh);
  386. if (mesh.skeleton) {
  387. this._activeSkeletons.pushNoDuplicate(mesh.skeleton);
  388. }
  389. for (var subIndex = 0; subIndex < mesh.subMeshes.length; subIndex++) {
  390. var subMesh = mesh.subMeshes[subIndex];
  391. this._evaluateSubMesh(subMesh, mesh);
  392. }
  393. }
  394. }
  395. }
  396. // Particle systems
  397. var beforeParticlesDate = new Date();
  398. if (this.particlesEnabled) {
  399. for (var particleIndex = 0; particleIndex < this.particleSystems.length; particleIndex++) {
  400. var particleSystem = this.particleSystems[particleIndex];
  401. if (!particleSystem.emitter.position || (particleSystem.emitter && particleSystem.emitter.isEnabled())) {
  402. this._activeParticleSystems.push(particleSystem);
  403. particleSystem.animate();
  404. }
  405. }
  406. }
  407. this._particlesDuration += new Date() - beforeParticlesDate;
  408. };
  409. BABYLON.Scene.prototype.render = function () {
  410. var startDate = new Date();
  411. this._particlesDuration = 0;
  412. this._activeParticles = 0;
  413. var engine = this._engine;
  414. // Before render
  415. if (this.beforeRender) {
  416. this.beforeRender();
  417. }
  418. for (var callbackIndex = 0; callbackIndex < this._onBeforeRenderCallbacks.length; callbackIndex++) {
  419. this._onBeforeRenderCallbacks[callbackIndex]();
  420. }
  421. // Camera
  422. if (!this.activeCamera)
  423. throw new Error("Active camera not set");
  424. this.setTransformMatrix(this.activeCamera.getViewMatrix(), this.activeCamera.getProjectionMatrix());
  425. // Animations
  426. this._animationRatio = BABYLON.Tools.GetDeltaTime() * (60.0 / 1000.0);
  427. this._animate();
  428. // Meshes
  429. this._renderId++;
  430. var beforeEvaluateActiveMeshesDate = new Date();
  431. this._evaluateActiveMeshes();
  432. this._evaluateActiveMeshesDuration = new Date() - beforeEvaluateActiveMeshesDate;
  433. // Skeletons
  434. for (var skeletonIndex = 0; skeletonIndex < this._activeSkeletons.length; skeletonIndex++) {
  435. var skeleton = this._activeSkeletons.data[skeletonIndex];
  436. skeleton.prepare();
  437. }
  438. // Shadows
  439. for (var lightIndex = 0; lightIndex < this.lights.length; lightIndex++) {
  440. var light = this.lights[lightIndex];
  441. var shadowGenerator = light.getShadowGenerator();
  442. if (light.isEnabled && shadowGenerator) {
  443. this._renderTargets.push(shadowGenerator.getShadowMap());
  444. }
  445. }
  446. // Render targets
  447. var beforeRenderTargetDate = new Date();
  448. for (var renderIndex = 0; renderIndex < this._renderTargets.length; renderIndex++) {
  449. var renderTarget = this._renderTargets.data[renderIndex];
  450. this._renderId++;
  451. renderTarget.render();
  452. }
  453. if (this._renderTargets.length > 0) { // Restore back buffer
  454. engine.restoreDefaultFramebuffer();
  455. }
  456. this._renderTargetsDuration = new Date() - beforeRenderTargetDate;
  457. // Prepare Frame
  458. this.postProcessManager._prepareFrame();
  459. // Clear
  460. var beforeRenderDate = new Date();
  461. engine.clear(this.clearColor, this.autoClear || this.forceWireframe, true);
  462. // Backgrounds
  463. if (this.layers.length) {
  464. engine.setDepthBuffer(false);
  465. var layerIndex;
  466. var layer;
  467. for (layerIndex = 0; layerIndex < this.layers.length; layerIndex++) {
  468. layer = this.layers[layerIndex];
  469. if (layer.isBackground) {
  470. layer.render();
  471. }
  472. }
  473. engine.setDepthBuffer(true);
  474. }
  475. // Render
  476. this._renderingManager.render(null, null, true, true);
  477. // Foregrounds
  478. if (this.layers.length) {
  479. engine.setDepthBuffer(false);
  480. for (layerIndex = 0; layerIndex < this.layers.length; layerIndex++) {
  481. layer = this.layers[layerIndex];
  482. if (!layer.isBackground) {
  483. layer.render();
  484. }
  485. }
  486. engine.setDepthBuffer(true);
  487. }
  488. this._renderDuration = new Date() - beforeRenderDate;
  489. // Finalize frame
  490. this.postProcessManager._finalizeFrame();
  491. // Update camera
  492. this.activeCamera._update();
  493. // After render
  494. if (this.afterRender) {
  495. this.afterRender();
  496. }
  497. // Cleaning
  498. for (var index = 0; index < this._toBeDisposed.length; index++) {
  499. this._toBeDisposed.data[index].dispose();
  500. this._toBeDisposed[index] = null;
  501. }
  502. this._toBeDisposed.reset();
  503. this._lastFrameDuration = new Date() - startDate;
  504. };
  505. BABYLON.Scene.prototype.dispose = function () {
  506. this.beforeRender = null;
  507. this.afterRender = null;
  508. this.skeletons = [];
  509. // Detach cameras
  510. var canvas = this._engine.getRenderingCanvas();
  511. var index;
  512. for (index = 0; index < this.cameras.length; index++) {
  513. this.cameras[index].detachControl(canvas);
  514. }
  515. // Release lights
  516. while (this.lights.length) {
  517. this.lights[0].dispose(true);
  518. }
  519. // Release meshes
  520. while (this.meshes.length) {
  521. this.meshes[0].dispose(true);
  522. }
  523. // Release materials
  524. while (this.materials.length) {
  525. this.materials[0].dispose();
  526. }
  527. // Release particles
  528. while (this.particleSystems.length) {
  529. this.particleSystems[0].dispose();
  530. }
  531. // Release sprites
  532. while (this.spriteManagers.length) {
  533. this.spriteManagers[0].dispose();
  534. }
  535. // Release layers
  536. while (this.layers.length) {
  537. this.layers[0].dispose();
  538. }
  539. // Release textures
  540. while (this.textures.length) {
  541. this.textures[0].dispose();
  542. }
  543. // Remove from engine
  544. index = this._engine.scenes.indexOf(this);
  545. this._engine.scenes.splice(index, 1);
  546. this._engine.wipeCaches();
  547. };
  548. // Collisions
  549. BABYLON.Scene.prototype._getNewPosition = function (position, velocity, collider, maximumRetry, finalPosition) {
  550. position.divideToRef(collider.radius, this._scaledPosition);
  551. velocity.divideToRef(collider.radius, this._scaledVelocity);
  552. collider.retry = 0;
  553. collider.initialVelocity = this._scaledVelocity;
  554. collider.initialPosition = this._scaledPosition;
  555. this._collideWithWorld(this._scaledPosition, this._scaledVelocity, collider, maximumRetry, finalPosition);
  556. finalPosition.multiplyInPlace(collider.radius);
  557. };
  558. BABYLON.Scene.prototype._collideWithWorld = function (position, velocity, collider, maximumRetry, finalPosition) {
  559. var closeDistance = BABYLON.Engine.collisionsEpsilon * 10.0;
  560. if (collider.retry >= maximumRetry) {
  561. finalPosition.copyFrom(position);
  562. return;
  563. }
  564. collider._initialize(position, velocity, closeDistance);
  565. // Check all meshes
  566. for (var index = 0; index < this.meshes.length; index++) {
  567. var mesh = this.meshes[index];
  568. if (mesh.isEnabled() && mesh.checkCollisions) {
  569. mesh._checkCollision(collider);
  570. }
  571. }
  572. if (!collider.collisionFound) {
  573. position.addToRef(velocity, finalPosition);
  574. return;
  575. }
  576. if (velocity.x != 0 || velocity.y != 0 || velocity.z != 0) {
  577. collider._getResponse(position, velocity);
  578. }
  579. if (velocity.length() <= closeDistance) {
  580. finalPosition.copyFrom(position);
  581. return;
  582. }
  583. collider.retry++;
  584. this._collideWithWorld(position, velocity, collider, maximumRetry, finalPosition);
  585. };
  586. // Octrees
  587. BABYLON.Scene.prototype.createOrUpdateSelectionOctree = function () {
  588. if (!this._selectionOctree) {
  589. this._selectionOctree = new BABYLON.Octree();
  590. }
  591. // World limits
  592. var checkExtends = function (v, min, max) {
  593. if (v.x < min.x)
  594. min.x = v.x;
  595. if (v.y < min.y)
  596. min.y = v.y;
  597. if (v.z < min.z)
  598. min.z = v.z;
  599. if (v.x > max.x)
  600. max.x = v.x;
  601. if (v.y > max.y)
  602. max.y = v.y;
  603. if (v.z > max.z)
  604. max.z = v.z;
  605. };
  606. var min = new BABYLON.Vector3(Number.MAX_VALUE, Number.MAX_VALUE, Number.MAX_VALUE);
  607. var max = new BABYLON.Vector3(-Number.MAX_VALUE, -Number.MAX_VALUE, -Number.MAX_VALUE);
  608. for (var index = 0; index < this.meshes.length; index++) {
  609. var mesh = this.meshes[index];
  610. mesh.computeWorldMatrix();
  611. var minBox = mesh.getBoundingInfo().boundingBox.minimumWorld;
  612. var maxBox = mesh.getBoundingInfo().boundingBox.maximumWorld;
  613. checkExtends(minBox, min, max);
  614. checkExtends(maxBox, min, max);
  615. }
  616. // Update octree
  617. this._selectionOctree.update(min, max, this.meshes);
  618. };
  619. // Picking
  620. BABYLON.Scene.prototype.createPickingRay = function (x, y, world) {
  621. var engine = this._engine;
  622. if (!this._viewMatrix) {
  623. if (!this.activeCamera)
  624. throw new Error("Active camera not set");
  625. this.setTransformMatrix(this.activeCamera.getViewMatrix(), this.activeCamera.getProjectionMatrix());
  626. }
  627. return BABYLON.Ray.CreateNew(x, y, engine.getRenderWidth() * engine.getHardwareScalingLevel(), engine.getRenderHeight() * engine.getHardwareScalingLevel(), world ? world : BABYLON.Matrix.Identity(), this._viewMatrix, this._projectionMatrix);
  628. };
  629. BABYLON.Scene.prototype.pick = function (x, y, predicate) {
  630. var distance = Number.MAX_VALUE;
  631. var pickedPoint = null;
  632. var pickedMesh = null;
  633. for (var meshIndex = 0; meshIndex < this.meshes.length; meshIndex++) {
  634. var mesh = this.meshes[meshIndex];
  635. if (predicate) {
  636. if (!predicate(mesh)) {
  637. continue;
  638. }
  639. }
  640. else if (!mesh.isEnabled() || !mesh.isVisible || !mesh.isPickable) {
  641. continue;
  642. }
  643. var world = mesh.getWorldMatrix();
  644. var ray = this.createPickingRay(x, y, world);
  645. var result = mesh.intersects(ray);
  646. if (!result.hit)
  647. continue;
  648. if (result.distance >= distance)
  649. continue;
  650. distance = result.distance;
  651. pickedMesh = mesh;
  652. pickedPoint = result.pickedPoint;
  653. }
  654. return { hit: distance != Number.MAX_VALUE, distance: distance, pickedMesh: pickedMesh, pickedPoint: pickedPoint };
  655. };
  656. // Statics
  657. BABYLON.Scene.FOGMODE_NONE = 0;
  658. BABYLON.Scene.FOGMODE_EXP = 1;
  659. BABYLON.Scene.FOGMODE_EXP2 = 2;
  660. BABYLON.Scene.FOGMODE_LINEAR = 3;
  661. })();