babylon.abstractMesh.js 39 KB

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