babylon.abstractMesh.js 39 KB

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