babylon.abstractMesh.js 37 KB

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