babylon.abstractMesh.js 38 KB

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