babylon.abstractMesh.js 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843
  1. var __extends = this.__extends || function (d, b) {
  2. for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
  3. function __() { this.constructor = d; }
  4. __.prototype = b.prototype;
  5. d.prototype = new __();
  6. };
  7. var BABYLON;
  8. (function (BABYLON) {
  9. var AbstractMesh = (function (_super) {
  10. __extends(AbstractMesh, _super);
  11. function AbstractMesh(name, scene) {
  12. _super.call(this, name, scene);
  13. // Properties
  14. this.position = new BABYLON.Vector3(0, 0, 0);
  15. this.rotation = new BABYLON.Vector3(0, 0, 0);
  16. this.scaling = new BABYLON.Vector3(1, 1, 1);
  17. this.billboardMode = BABYLON.AbstractMesh.BILLBOARDMODE_NONE;
  18. this.visibility = 1.0;
  19. this.infiniteDistance = false;
  20. this.isVisible = true;
  21. this.isPickable = true;
  22. this.showBoundingBox = false;
  23. this.showSubMeshesBoundingBox = false;
  24. this.onDispose = null;
  25. this.checkCollisions = false;
  26. this.isBlocker = false;
  27. this.renderingGroupId = 0;
  28. this.receiveShadows = false;
  29. this.renderOutline = false;
  30. this.outlineColor = BABYLON.Color3.Red();
  31. this.outlineWidth = 0.02;
  32. this.useOctreeForRenderingSelection = true;
  33. this.useOctreeForPicking = true;
  34. this.useOctreeForCollisions = true;
  35. this.layerMask = 0xFFFFFFFF;
  36. // Physics
  37. this._physicImpostor = BABYLON.PhysicsEngine.NoImpostor;
  38. // Collisions
  39. this.ellipsoid = new BABYLON.Vector3(0.5, 1, 0.5);
  40. this.ellipsoidOffset = new BABYLON.Vector3(0, 0, 0);
  41. this._collider = new BABYLON.Collider();
  42. this._oldPositionForCollisions = new BABYLON.Vector3(0, 0, 0);
  43. this._diffPositionForCollisions = new BABYLON.Vector3(0, 0, 0);
  44. this._newPositionForCollisions = new BABYLON.Vector3(0, 0, 0);
  45. // Cache
  46. this._localScaling = BABYLON.Matrix.Zero();
  47. this._localRotation = BABYLON.Matrix.Zero();
  48. this._localTranslation = BABYLON.Matrix.Zero();
  49. this._localBillboard = BABYLON.Matrix.Zero();
  50. this._localPivotScaling = BABYLON.Matrix.Zero();
  51. this._localPivotScalingRotation = BABYLON.Matrix.Zero();
  52. this._localWorld = BABYLON.Matrix.Zero();
  53. this._worldMatrix = BABYLON.Matrix.Zero();
  54. this._rotateYByPI = BABYLON.Matrix.RotationY(Math.PI);
  55. this._absolutePosition = BABYLON.Vector3.Zero();
  56. this._collisionsTransformMatrix = BABYLON.Matrix.Zero();
  57. this._collisionsScalingMatrix = BABYLON.Matrix.Zero();
  58. this._isDirty = false;
  59. this._pivotMatrix = BABYLON.Matrix.Identity();
  60. this._isDisposed = false;
  61. this._renderId = 0;
  62. this._intersectionsInProgress = new Array();
  63. scene.meshes.push(this);
  64. }
  65. Object.defineProperty(AbstractMesh, "BILLBOARDMODE_NONE", {
  66. get: function () {
  67. return AbstractMesh._BILLBOARDMODE_NONE;
  68. },
  69. enumerable: true,
  70. configurable: true
  71. });
  72. Object.defineProperty(AbstractMesh, "BILLBOARDMODE_X", {
  73. get: function () {
  74. return AbstractMesh._BILLBOARDMODE_X;
  75. },
  76. enumerable: true,
  77. configurable: true
  78. });
  79. Object.defineProperty(AbstractMesh, "BILLBOARDMODE_Y", {
  80. get: function () {
  81. return AbstractMesh._BILLBOARDMODE_Y;
  82. },
  83. enumerable: true,
  84. configurable: true
  85. });
  86. Object.defineProperty(AbstractMesh, "BILLBOARDMODE_Z", {
  87. get: function () {
  88. return AbstractMesh._BILLBOARDMODE_Z;
  89. },
  90. enumerable: true,
  91. configurable: true
  92. });
  93. Object.defineProperty(AbstractMesh, "BILLBOARDMODE_ALL", {
  94. get: function () {
  95. return AbstractMesh._BILLBOARDMODE_ALL;
  96. },
  97. enumerable: true,
  98. configurable: true
  99. });
  100. Object.defineProperty(AbstractMesh.prototype, "isBlocked", {
  101. // Methods
  102. get: function () {
  103. return false;
  104. },
  105. enumerable: true,
  106. configurable: true
  107. });
  108. AbstractMesh.prototype.getLOD = function (camera) {
  109. return this;
  110. };
  111. AbstractMesh.prototype.getTotalVertices = function () {
  112. return 0;
  113. };
  114. AbstractMesh.prototype.getIndices = function () {
  115. return null;
  116. };
  117. AbstractMesh.prototype.getVerticesData = function (kind) {
  118. return null;
  119. };
  120. AbstractMesh.prototype.isVerticesDataPresent = function (kind) {
  121. return false;
  122. };
  123. AbstractMesh.prototype.getBoundingInfo = function () {
  124. if (!this._boundingInfo) {
  125. this._updateBoundingInfo();
  126. }
  127. return this._boundingInfo;
  128. };
  129. AbstractMesh.prototype._preActivate = function () {
  130. };
  131. AbstractMesh.prototype._activate = function (renderId) {
  132. this._renderId = renderId;
  133. };
  134. AbstractMesh.prototype.getWorldMatrix = function () {
  135. if (this._currentRenderId !== this.getScene().getRenderId()) {
  136. this.computeWorldMatrix();
  137. }
  138. return this._worldMatrix;
  139. };
  140. Object.defineProperty(AbstractMesh.prototype, "worldMatrixFromCache", {
  141. get: function () {
  142. return this._worldMatrix;
  143. },
  144. enumerable: true,
  145. configurable: true
  146. });
  147. Object.defineProperty(AbstractMesh.prototype, "absolutePosition", {
  148. get: function () {
  149. return this._absolutePosition;
  150. },
  151. enumerable: true,
  152. configurable: true
  153. });
  154. AbstractMesh.prototype.rotate = function (axis, amount, space) {
  155. if (!this.rotationQuaternion) {
  156. this.rotationQuaternion = BABYLON.Quaternion.RotationYawPitchRoll(this.rotation.y, this.rotation.x, this.rotation.z);
  157. this.rotation = BABYLON.Vector3.Zero();
  158. }
  159. if (!space || space == 0 /* LOCAL */) {
  160. var rotationQuaternion = BABYLON.Quaternion.RotationAxis(axis, amount);
  161. this.rotationQuaternion = this.rotationQuaternion.multiply(rotationQuaternion);
  162. } else {
  163. if (this.parent) {
  164. var invertParentWorldMatrix = this.parent.getWorldMatrix().clone();
  165. invertParentWorldMatrix.invert();
  166. axis = BABYLON.Vector3.TransformNormal(axis, invertParentWorldMatrix);
  167. }
  168. rotationQuaternion = BABYLON.Quaternion.RotationAxis(axis, amount);
  169. this.rotationQuaternion = rotationQuaternion.multiply(this.rotationQuaternion);
  170. }
  171. };
  172. AbstractMesh.prototype.translate = function (axis, distance, space) {
  173. var displacementVector = axis.scale(distance);
  174. if (!space || space == 0 /* LOCAL */) {
  175. var tempV3 = this.getPositionExpressedInLocalSpace().add(displacementVector);
  176. this.setPositionWithLocalVector(tempV3);
  177. } else {
  178. this.setAbsolutePosition(this.getAbsolutePosition().add(displacementVector));
  179. }
  180. };
  181. AbstractMesh.prototype.getAbsolutePosition = function () {
  182. this.computeWorldMatrix();
  183. return this._absolutePosition;
  184. };
  185. AbstractMesh.prototype.setAbsolutePosition = function (absolutePosition) {
  186. if (!absolutePosition) {
  187. return;
  188. }
  189. var absolutePositionX;
  190. var absolutePositionY;
  191. var absolutePositionZ;
  192. if (absolutePosition.x === undefined) {
  193. if (arguments.length < 3) {
  194. return;
  195. }
  196. absolutePositionX = arguments[0];
  197. absolutePositionY = arguments[1];
  198. absolutePositionZ = arguments[2];
  199. } else {
  200. absolutePositionX = absolutePosition.x;
  201. absolutePositionY = absolutePosition.y;
  202. absolutePositionZ = absolutePosition.z;
  203. }
  204. if (this.parent) {
  205. var invertParentWorldMatrix = this.parent.getWorldMatrix().clone();
  206. invertParentWorldMatrix.invert();
  207. var worldPosition = new BABYLON.Vector3(absolutePositionX, absolutePositionY, absolutePositionZ);
  208. this.position = BABYLON.Vector3.TransformCoordinates(worldPosition, invertParentWorldMatrix);
  209. } else {
  210. this.position.x = absolutePositionX;
  211. this.position.y = absolutePositionY;
  212. this.position.z = absolutePositionZ;
  213. }
  214. };
  215. AbstractMesh.prototype.setPivotMatrix = function (matrix) {
  216. this._pivotMatrix = matrix;
  217. this._cache.pivotMatrixUpdated = true;
  218. };
  219. AbstractMesh.prototype.getPivotMatrix = function () {
  220. return this._pivotMatrix;
  221. };
  222. AbstractMesh.prototype._isSynchronized = function () {
  223. if (this._isDirty) {
  224. return false;
  225. }
  226. if (this.billboardMode !== AbstractMesh.BILLBOARDMODE_NONE)
  227. return false;
  228. if (this._cache.pivotMatrixUpdated) {
  229. return false;
  230. }
  231. if (this.infiniteDistance) {
  232. return false;
  233. }
  234. if (!this._cache.position.equals(this.position))
  235. return false;
  236. if (this.rotationQuaternion) {
  237. if (!this._cache.rotationQuaternion.equals(this.rotationQuaternion))
  238. return false;
  239. } else {
  240. if (!this._cache.rotation.equals(this.rotation))
  241. return false;
  242. }
  243. if (!this._cache.scaling.equals(this.scaling))
  244. return false;
  245. return true;
  246. };
  247. AbstractMesh.prototype._initCache = function () {
  248. _super.prototype._initCache.call(this);
  249. this._cache.localMatrixUpdated = false;
  250. this._cache.position = BABYLON.Vector3.Zero();
  251. this._cache.scaling = BABYLON.Vector3.Zero();
  252. this._cache.rotation = BABYLON.Vector3.Zero();
  253. this._cache.rotationQuaternion = new BABYLON.Quaternion(0, 0, 0, 0);
  254. };
  255. AbstractMesh.prototype.markAsDirty = function (property) {
  256. if (property === "rotation") {
  257. this.rotationQuaternion = null;
  258. }
  259. this._currentRenderId = Number.MAX_VALUE;
  260. this._isDirty = true;
  261. };
  262. AbstractMesh.prototype._updateBoundingInfo = function () {
  263. this._boundingInfo = this._boundingInfo || new BABYLON.BoundingInfo(this.absolutePosition, this.absolutePosition);
  264. this._boundingInfo._update(this.worldMatrixFromCache);
  265. if (!this.subMeshes) {
  266. return;
  267. }
  268. for (var subIndex = 0; subIndex < this.subMeshes.length; subIndex++) {
  269. var subMesh = this.subMeshes[subIndex];
  270. subMesh.updateBoundingInfo(this.worldMatrixFromCache);
  271. }
  272. };
  273. AbstractMesh.prototype.computeWorldMatrix = function (force) {
  274. if (!force && (this._currentRenderId == this.getScene().getRenderId() || this.isSynchronized(true))) {
  275. return this._worldMatrix;
  276. }
  277. this._cache.position.copyFrom(this.position);
  278. this._cache.scaling.copyFrom(this.scaling);
  279. this._cache.pivotMatrixUpdated = false;
  280. this._currentRenderId = this.getScene().getRenderId();
  281. this._isDirty = false;
  282. // Scaling
  283. BABYLON.Matrix.ScalingToRef(this.scaling.x, this.scaling.y, this.scaling.z, this._localScaling);
  284. // Rotation
  285. if (this.rotationQuaternion) {
  286. this.rotationQuaternion.toRotationMatrix(this._localRotation);
  287. this._cache.rotationQuaternion.copyFrom(this.rotationQuaternion);
  288. } else {
  289. BABYLON.Matrix.RotationYawPitchRollToRef(this.rotation.y, this.rotation.x, this.rotation.z, this._localRotation);
  290. this._cache.rotation.copyFrom(this.rotation);
  291. }
  292. // Translation
  293. if (this.infiniteDistance && !this.parent) {
  294. var camera = this.getScene().activeCamera;
  295. var cameraWorldMatrix = camera.getWorldMatrix();
  296. var cameraGlobalPosition = new BABYLON.Vector3(cameraWorldMatrix.m[12], cameraWorldMatrix.m[13], cameraWorldMatrix.m[14]);
  297. BABYLON.Matrix.TranslationToRef(this.position.x + cameraGlobalPosition.x, this.position.y + cameraGlobalPosition.y, this.position.z + cameraGlobalPosition.z, this._localTranslation);
  298. } else {
  299. BABYLON.Matrix.TranslationToRef(this.position.x, this.position.y, this.position.z, this._localTranslation);
  300. }
  301. // Composing transformations
  302. this._pivotMatrix.multiplyToRef(this._localScaling, this._localPivotScaling);
  303. this._localPivotScaling.multiplyToRef(this._localRotation, this._localPivotScalingRotation);
  304. // Billboarding
  305. if (this.billboardMode !== AbstractMesh.BILLBOARDMODE_NONE && this.getScene().activeCamera) {
  306. var localPosition = this.position.clone();
  307. var zero = this.getScene().activeCamera.position.clone();
  308. if (this.parent && this.parent.position) {
  309. localPosition.addInPlace(this.parent.position);
  310. BABYLON.Matrix.TranslationToRef(localPosition.x, localPosition.y, localPosition.z, this._localTranslation);
  311. }
  312. if ((this.billboardMode & AbstractMesh.BILLBOARDMODE_ALL) === AbstractMesh.BILLBOARDMODE_ALL) {
  313. zero = this.getScene().activeCamera.position;
  314. } else {
  315. if (this.billboardMode & BABYLON.AbstractMesh.BILLBOARDMODE_X)
  316. zero.x = localPosition.x + BABYLON.Engine.Epsilon;
  317. if (this.billboardMode & BABYLON.AbstractMesh.BILLBOARDMODE_Y)
  318. zero.y = localPosition.y + 0.001;
  319. if (this.billboardMode & BABYLON.AbstractMesh.BILLBOARDMODE_Z)
  320. zero.z = localPosition.z + 0.001;
  321. }
  322. BABYLON.Matrix.LookAtLHToRef(localPosition, zero, BABYLON.Vector3.Up(), this._localBillboard);
  323. this._localBillboard.m[12] = this._localBillboard.m[13] = this._localBillboard.m[14] = 0;
  324. this._localBillboard.invert();
  325. this._localPivotScalingRotation.multiplyToRef(this._localBillboard, this._localWorld);
  326. this._rotateYByPI.multiplyToRef(this._localWorld, this._localPivotScalingRotation);
  327. }
  328. // Local world
  329. this._localPivotScalingRotation.multiplyToRef(this._localTranslation, this._localWorld);
  330. // Parent
  331. if (this.parent && this.parent.getWorldMatrix && this.billboardMode === BABYLON.AbstractMesh.BILLBOARDMODE_NONE) {
  332. this._localWorld.multiplyToRef(this.parent.getWorldMatrix(), this._worldMatrix);
  333. } else {
  334. this._worldMatrix.copyFrom(this._localWorld);
  335. }
  336. // Bounding info
  337. this._updateBoundingInfo();
  338. // Absolute position
  339. this._absolutePosition.copyFromFloats(this._worldMatrix.m[12], this._worldMatrix.m[13], this._worldMatrix.m[14]);
  340. return this._worldMatrix;
  341. };
  342. AbstractMesh.prototype.setPositionWithLocalVector = function (vector3) {
  343. this.computeWorldMatrix();
  344. this.position = BABYLON.Vector3.TransformNormal(vector3, this._localWorld);
  345. };
  346. AbstractMesh.prototype.getPositionExpressedInLocalSpace = function () {
  347. this.computeWorldMatrix();
  348. var invLocalWorldMatrix = this._localWorld.clone();
  349. invLocalWorldMatrix.invert();
  350. return BABYLON.Vector3.TransformNormal(this.position, invLocalWorldMatrix);
  351. };
  352. AbstractMesh.prototype.locallyTranslate = function (vector3) {
  353. this.computeWorldMatrix();
  354. this.position = BABYLON.Vector3.TransformCoordinates(vector3, this._localWorld);
  355. };
  356. AbstractMesh.prototype.lookAt = function (targetPoint, yawCor, pitchCor, rollCor) {
  357. /// <summary>Orients a mesh towards a target point. Mesh must be drawn facing user.</summary>
  358. /// <param name="targetPoint" type="BABYLON.Vector3">The position (must be in same space as current mesh) to look at</param>
  359. /// <param name="yawCor" type="Number">optional yaw (y-axis) correction in radians</param>
  360. /// <param name="pitchCor" type="Number">optional pitch (x-axis) correction in radians</param>
  361. /// <param name="rollCor" type="Number">optional roll (z-axis) correction in radians</param>
  362. /// <returns>Mesh oriented towards targetMesh</returns>
  363. yawCor = yawCor || 0; // default to zero if undefined
  364. pitchCor = pitchCor || 0;
  365. rollCor = rollCor || 0;
  366. var dv = targetPoint.subtract(this.position);
  367. var yaw = -Math.atan2(dv.z, dv.x) - Math.PI / 2;
  368. var len = Math.sqrt(dv.x * dv.x + dv.z * dv.z);
  369. var pitch = Math.atan2(dv.y, len);
  370. this.rotationQuaternion = BABYLON.Quaternion.RotationYawPitchRoll(yaw + yawCor, pitch + pitchCor, rollCor);
  371. };
  372. AbstractMesh.prototype.isInFrustum = function (frustumPlanes) {
  373. if (!this._boundingInfo.isInFrustum(frustumPlanes)) {
  374. return false;
  375. }
  376. return true;
  377. };
  378. AbstractMesh.prototype.isCompletelyInFrustum = function (camera) {
  379. if (!camera) {
  380. camera = this.getScene().activeCamera;
  381. }
  382. var transformMatrix = camera.getViewMatrix().multiply(camera.getProjectionMatrix());
  383. if (!this._boundingInfo.isCompletelyInFrustum(BABYLON.Frustum.GetPlanes(transformMatrix))) {
  384. return false;
  385. }
  386. return true;
  387. };
  388. AbstractMesh.prototype.intersectsMesh = function (mesh, precise) {
  389. if (!this._boundingInfo || !mesh._boundingInfo) {
  390. return false;
  391. }
  392. return this._boundingInfo.intersects(mesh._boundingInfo, precise);
  393. };
  394. AbstractMesh.prototype.intersectsPoint = function (point) {
  395. if (!this._boundingInfo) {
  396. return false;
  397. }
  398. return this._boundingInfo.intersectsPoint(point);
  399. };
  400. // Physics
  401. AbstractMesh.prototype.setPhysicsState = function (impostor, options) {
  402. var physicsEngine = this.getScene().getPhysicsEngine();
  403. if (!physicsEngine) {
  404. return;
  405. }
  406. if (impostor.impostor) {
  407. // Old API
  408. options = impostor;
  409. impostor = impostor.impostor;
  410. }
  411. impostor = impostor || BABYLON.PhysicsEngine.NoImpostor;
  412. if (impostor === BABYLON.PhysicsEngine.NoImpostor) {
  413. physicsEngine._unregisterMesh(this);
  414. return;
  415. }
  416. options.mass = options.mass || 0;
  417. options.friction = options.friction || 0.2;
  418. options.restitution = options.restitution || 0.2;
  419. this._physicImpostor = impostor;
  420. this._physicsMass = options.mass;
  421. this._physicsFriction = options.friction;
  422. this._physicRestitution = options.restitution;
  423. physicsEngine._registerMesh(this, impostor, options);
  424. };
  425. AbstractMesh.prototype.getPhysicsImpostor = function () {
  426. if (!this._physicImpostor) {
  427. return BABYLON.PhysicsEngine.NoImpostor;
  428. }
  429. return this._physicImpostor;
  430. };
  431. AbstractMesh.prototype.getPhysicsMass = function () {
  432. if (!this._physicsMass) {
  433. return 0;
  434. }
  435. return this._physicsMass;
  436. };
  437. AbstractMesh.prototype.getPhysicsFriction = function () {
  438. if (!this._physicsFriction) {
  439. return 0;
  440. }
  441. return this._physicsFriction;
  442. };
  443. AbstractMesh.prototype.getPhysicsRestitution = function () {
  444. if (!this._physicRestitution) {
  445. return 0;
  446. }
  447. return this._physicRestitution;
  448. };
  449. AbstractMesh.prototype.getPositionInCameraSpace = function (camera) {
  450. if (!camera) {
  451. camera = this.getScene().activeCamera;
  452. }
  453. return BABYLON.Vector3.TransformCoordinates(this.absolutePosition, camera.getViewMatrix());
  454. };
  455. AbstractMesh.prototype.getDistanceToCamera = function (camera) {
  456. if (!camera) {
  457. camera = this.getScene().activeCamera;
  458. }
  459. return this.absolutePosition.subtract(camera.position);
  460. };
  461. AbstractMesh.prototype.applyImpulse = function (force, contactPoint) {
  462. if (!this._physicImpostor) {
  463. return;
  464. }
  465. this.getScene().getPhysicsEngine()._applyImpulse(this, force, contactPoint);
  466. };
  467. AbstractMesh.prototype.setPhysicsLinkWith = function (otherMesh, pivot1, pivot2, options) {
  468. if (!this._physicImpostor) {
  469. return;
  470. }
  471. this.getScene().getPhysicsEngine()._createLink(this, otherMesh, pivot1, pivot2, options);
  472. };
  473. AbstractMesh.prototype.updatePhysicsBodyPosition = function () {
  474. if (!this._physicImpostor) {
  475. return;
  476. }
  477. this.getScene().getPhysicsEngine()._updateBodyPosition(this);
  478. };
  479. // Collisions
  480. AbstractMesh.prototype.moveWithCollisions = function (velocity) {
  481. var globalPosition = this.getAbsolutePosition();
  482. globalPosition.subtractFromFloatsToRef(0, this.ellipsoid.y, 0, this._oldPositionForCollisions);
  483. this._oldPositionForCollisions.addInPlace(this.ellipsoidOffset);
  484. this._collider.radius = this.ellipsoid;
  485. this.getScene()._getNewPosition(this._oldPositionForCollisions, velocity, this._collider, 3, this._newPositionForCollisions, this);
  486. this._newPositionForCollisions.subtractToRef(this._oldPositionForCollisions, this._diffPositionForCollisions);
  487. if (this._diffPositionForCollisions.length() > BABYLON.Engine.CollisionsEpsilon) {
  488. this.position.addInPlace(this._diffPositionForCollisions);
  489. }
  490. };
  491. // Submeshes octree
  492. /**
  493. * This function will create an octree to help select the right submeshes for rendering, picking and collisions
  494. * Please note that you must have a decent number of submeshes to get performance improvements when using octree
  495. */
  496. AbstractMesh.prototype.createOrUpdateSubmeshesOctree = function (maxCapacity, maxDepth) {
  497. if (typeof maxCapacity === "undefined") { maxCapacity = 64; }
  498. if (typeof maxDepth === "undefined") { maxDepth = 2; }
  499. if (!this._submeshesOctree) {
  500. this._submeshesOctree = new BABYLON.Octree(BABYLON.Octree.CreationFuncForSubMeshes, maxCapacity, maxDepth);
  501. }
  502. this.computeWorldMatrix(true);
  503. // Update octree
  504. var bbox = this.getBoundingInfo().boundingBox;
  505. this._submeshesOctree.update(bbox.minimumWorld, bbox.maximumWorld, this.subMeshes);
  506. return this._submeshesOctree;
  507. };
  508. // Collisions
  509. AbstractMesh.prototype._collideForSubMesh = function (subMesh, transformMatrix, collider) {
  510. this._generatePointsArray();
  511. // Transformation
  512. if (!subMesh._lastColliderWorldVertices || !subMesh._lastColliderTransformMatrix.equals(transformMatrix)) {
  513. subMesh._lastColliderTransformMatrix = transformMatrix.clone();
  514. subMesh._lastColliderWorldVertices = [];
  515. subMesh._trianglePlanes = [];
  516. var start = subMesh.verticesStart;
  517. var end = (subMesh.verticesStart + subMesh.verticesCount);
  518. for (var i = start; i < end; i++) {
  519. subMesh._lastColliderWorldVertices.push(BABYLON.Vector3.TransformCoordinates(this._positions[i], transformMatrix));
  520. }
  521. }
  522. // Collide
  523. collider._collide(subMesh, subMesh._lastColliderWorldVertices, this.getIndices(), subMesh.indexStart, subMesh.indexStart + subMesh.indexCount, subMesh.verticesStart);
  524. };
  525. AbstractMesh.prototype._processCollisionsForSubMeshes = function (collider, transformMatrix) {
  526. var subMeshes;
  527. var len;
  528. // Octrees
  529. if (this._submeshesOctree && this.useOctreeForCollisions) {
  530. var radius = collider.velocityWorldLength + Math.max(collider.radius.x, collider.radius.y, collider.radius.z);
  531. var intersections = this._submeshesOctree.intersects(collider.basePointWorld, radius);
  532. len = intersections.length;
  533. subMeshes = intersections.data;
  534. } else {
  535. subMeshes = this.subMeshes;
  536. len = subMeshes.length;
  537. }
  538. for (var index = 0; index < len; index++) {
  539. var subMesh = subMeshes[index];
  540. // Bounding test
  541. if (len > 1 && !subMesh._checkCollision(collider))
  542. continue;
  543. this._collideForSubMesh(subMesh, transformMatrix, collider);
  544. }
  545. };
  546. AbstractMesh.prototype._checkCollision = function (collider) {
  547. // Bounding box test
  548. if (!this._boundingInfo._checkCollision(collider))
  549. return;
  550. // Transformation matrix
  551. BABYLON.Matrix.ScalingToRef(1.0 / collider.radius.x, 1.0 / collider.radius.y, 1.0 / collider.radius.z, this._collisionsScalingMatrix);
  552. this.worldMatrixFromCache.multiplyToRef(this._collisionsScalingMatrix, this._collisionsTransformMatrix);
  553. this._processCollisionsForSubMeshes(collider, this._collisionsTransformMatrix);
  554. };
  555. // Picking
  556. AbstractMesh.prototype._generatePointsArray = function () {
  557. return false;
  558. };
  559. AbstractMesh.prototype.intersects = function (ray, fastCheck) {
  560. var pickingInfo = new BABYLON.PickingInfo();
  561. if (!this.subMeshes || !this._boundingInfo || !ray.intersectsSphere(this._boundingInfo.boundingSphere) || !ray.intersectsBox(this._boundingInfo.boundingBox)) {
  562. return pickingInfo;
  563. }
  564. if (!this._generatePointsArray()) {
  565. return pickingInfo;
  566. }
  567. var intersectInfo = null;
  568. // Octrees
  569. var subMeshes;
  570. var len;
  571. if (this._submeshesOctree && this.useOctreeForPicking) {
  572. var worldRay = BABYLON.Ray.Transform(ray, this.getWorldMatrix());
  573. var intersections = this._submeshesOctree.intersectsRay(worldRay);
  574. len = intersections.length;
  575. subMeshes = intersections.data;
  576. } else {
  577. subMeshes = this.subMeshes;
  578. len = subMeshes.length;
  579. }
  580. for (var index = 0; index < len; index++) {
  581. var subMesh = subMeshes[index];
  582. // Bounding test
  583. if (len > 1 && !subMesh.canIntersects(ray))
  584. continue;
  585. var currentIntersectInfo = subMesh.intersects(ray, this._positions, this.getIndices(), fastCheck);
  586. if (currentIntersectInfo) {
  587. if (fastCheck || !intersectInfo || currentIntersectInfo.distance < intersectInfo.distance) {
  588. intersectInfo = currentIntersectInfo;
  589. if (fastCheck) {
  590. break;
  591. }
  592. }
  593. }
  594. }
  595. if (intersectInfo) {
  596. // Get picked point
  597. var world = this.getWorldMatrix();
  598. var worldOrigin = BABYLON.Vector3.TransformCoordinates(ray.origin, world);
  599. var direction = ray.direction.clone();
  600. direction.normalize();
  601. direction = direction.scale(intersectInfo.distance);
  602. var worldDirection = BABYLON.Vector3.TransformNormal(direction, world);
  603. var pickedPoint = worldOrigin.add(worldDirection);
  604. // Return result
  605. pickingInfo.hit = true;
  606. pickingInfo.distance = BABYLON.Vector3.Distance(worldOrigin, pickedPoint);
  607. pickingInfo.pickedPoint = pickedPoint;
  608. pickingInfo.pickedMesh = this;
  609. pickingInfo.bu = intersectInfo.bu;
  610. pickingInfo.bv = intersectInfo.bv;
  611. pickingInfo.faceId = intersectInfo.faceId;
  612. return pickingInfo;
  613. }
  614. return pickingInfo;
  615. };
  616. AbstractMesh.prototype.clone = function (name, newParent, doNotCloneChildren) {
  617. return null;
  618. };
  619. AbstractMesh.prototype.releaseSubMeshes = function () {
  620. if (this.subMeshes) {
  621. while (this.subMeshes.length) {
  622. this.subMeshes[0].dispose();
  623. }
  624. } else {
  625. this.subMeshes = new Array();
  626. }
  627. };
  628. AbstractMesh.prototype.dispose = function (doNotRecurse) {
  629. // Physics
  630. if (this.getPhysicsImpostor() != BABYLON.PhysicsEngine.NoImpostor) {
  631. this.setPhysicsState(BABYLON.PhysicsEngine.NoImpostor);
  632. }
  633. for (index = 0; index < this._intersectionsInProgress.length; index++) {
  634. var other = this._intersectionsInProgress[index];
  635. var pos = other._intersectionsInProgress.indexOf(this);
  636. other._intersectionsInProgress.splice(pos, 1);
  637. }
  638. this._intersectionsInProgress = [];
  639. // SubMeshes
  640. this.releaseSubMeshes();
  641. // Remove from scene
  642. var index = this.getScene().meshes.indexOf(this);
  643. if (index != -1) {
  644. // Remove from the scene if mesh found
  645. this.getScene().meshes.splice(index, 1);
  646. }
  647. if (!doNotRecurse) {
  648. for (index = 0; index < this.getScene().particleSystems.length; index++) {
  649. if (this.getScene().particleSystems[index].emitter == this) {
  650. this.getScene().particleSystems[index].dispose();
  651. index--;
  652. }
  653. }
  654. // Children
  655. var objects = this.getScene().meshes.slice(0);
  656. for (index = 0; index < objects.length; index++) {
  657. if (objects[index].parent == this) {
  658. objects[index].dispose();
  659. }
  660. }
  661. } else {
  662. for (index = 0; index < this.getScene().meshes.length; index++) {
  663. var obj = this.getScene().meshes[index];
  664. if (obj.parent === this) {
  665. obj.parent = null;
  666. obj.computeWorldMatrix(true);
  667. }
  668. }
  669. }
  670. this._isDisposed = true;
  671. // Callback
  672. if (this.onDispose) {
  673. this.onDispose();
  674. }
  675. };
  676. AbstractMesh._BILLBOARDMODE_NONE = 0;
  677. AbstractMesh._BILLBOARDMODE_X = 1;
  678. AbstractMesh._BILLBOARDMODE_Y = 2;
  679. AbstractMesh._BILLBOARDMODE_Z = 4;
  680. AbstractMesh._BILLBOARDMODE_ALL = 7;
  681. return AbstractMesh;
  682. })(BABYLON.Node);
  683. BABYLON.AbstractMesh = AbstractMesh;
  684. })(BABYLON || (BABYLON = {}));
  685. //# sourceMappingURL=babylon.abstractMesh.js.map