babylon.abstractMesh.js 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818
  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. this.alwaysSelectAsActiveMesh = false;
  45. // Physics
  46. this._physicImpostor = BABYLON.PhysicsEngine.NoImpostor;
  47. // Collisions
  48. this.ellipsoid = new BABYLON.Vector3(0.5, 1, 0.5);
  49. this.ellipsoidOffset = new BABYLON.Vector3(0, 0, 0);
  50. this._collider = new BABYLON.Collider();
  51. this._oldPositionForCollisions = new BABYLON.Vector3(0, 0, 0);
  52. this._diffPositionForCollisions = new BABYLON.Vector3(0, 0, 0);
  53. this._newPositionForCollisions = new BABYLON.Vector3(0, 0, 0);
  54. // Cache
  55. this._localScaling = BABYLON.Matrix.Zero();
  56. this._localRotation = BABYLON.Matrix.Zero();
  57. this._localTranslation = BABYLON.Matrix.Zero();
  58. this._localBillboard = BABYLON.Matrix.Zero();
  59. this._localPivotScaling = BABYLON.Matrix.Zero();
  60. this._localPivotScalingRotation = BABYLON.Matrix.Zero();
  61. this._localWorld = BABYLON.Matrix.Zero();
  62. this._worldMatrix = BABYLON.Matrix.Zero();
  63. this._rotateYByPI = BABYLON.Matrix.RotationY(Math.PI);
  64. this._absolutePosition = BABYLON.Vector3.Zero();
  65. this._collisionsTransformMatrix = BABYLON.Matrix.Zero();
  66. this._collisionsScalingMatrix = BABYLON.Matrix.Zero();
  67. this._isDirty = false;
  68. this._pivotMatrix = BABYLON.Matrix.Identity();
  69. this._isDisposed = false;
  70. this._renderId = 0;
  71. this._intersectionsInProgress = new Array();
  72. this._onAfterWorldMatrixUpdate = new Array();
  73. this._isWorldMatrixFrozen = false;
  74. scene.addMesh(this);
  75. }
  76. Object.defineProperty(AbstractMesh, "BILLBOARDMODE_NONE", {
  77. get: function () {
  78. return AbstractMesh._BILLBOARDMODE_NONE;
  79. },
  80. enumerable: true,
  81. configurable: true
  82. });
  83. Object.defineProperty(AbstractMesh, "BILLBOARDMODE_X", {
  84. get: function () {
  85. return AbstractMesh._BILLBOARDMODE_X;
  86. },
  87. enumerable: true,
  88. configurable: true
  89. });
  90. Object.defineProperty(AbstractMesh, "BILLBOARDMODE_Y", {
  91. get: function () {
  92. return AbstractMesh._BILLBOARDMODE_Y;
  93. },
  94. enumerable: true,
  95. configurable: true
  96. });
  97. Object.defineProperty(AbstractMesh, "BILLBOARDMODE_Z", {
  98. get: function () {
  99. return AbstractMesh._BILLBOARDMODE_Z;
  100. },
  101. enumerable: true,
  102. configurable: true
  103. });
  104. Object.defineProperty(AbstractMesh, "BILLBOARDMODE_ALL", {
  105. get: function () {
  106. return AbstractMesh._BILLBOARDMODE_ALL;
  107. },
  108. enumerable: true,
  109. configurable: true
  110. });
  111. Object.defineProperty(AbstractMesh.prototype, "isBlocked", {
  112. // Methods
  113. get: function () {
  114. return false;
  115. },
  116. enumerable: true,
  117. configurable: true
  118. });
  119. AbstractMesh.prototype.getLOD = function (camera) {
  120. return this;
  121. };
  122. AbstractMesh.prototype.getTotalVertices = function () {
  123. return 0;
  124. };
  125. AbstractMesh.prototype.getIndices = function () {
  126. return null;
  127. };
  128. AbstractMesh.prototype.getVerticesData = function (kind) {
  129. return null;
  130. };
  131. AbstractMesh.prototype.isVerticesDataPresent = function (kind) {
  132. return false;
  133. };
  134. AbstractMesh.prototype.getBoundingInfo = function () {
  135. if (this._masterMesh) {
  136. return this._masterMesh.getBoundingInfo();
  137. }
  138. if (!this._boundingInfo) {
  139. this._updateBoundingInfo();
  140. }
  141. return this._boundingInfo;
  142. };
  143. Object.defineProperty(AbstractMesh.prototype, "useBones", {
  144. get: function () {
  145. return this.skeleton && this.getScene().skeletonsEnabled && this.isVerticesDataPresent(BABYLON.VertexBuffer.MatricesIndicesKind) && this.isVerticesDataPresent(BABYLON.VertexBuffer.MatricesWeightsKind);
  146. },
  147. enumerable: true,
  148. configurable: true
  149. });
  150. AbstractMesh.prototype._preActivate = function () {
  151. };
  152. AbstractMesh.prototype._activate = function (renderId) {
  153. this._renderId = renderId;
  154. };
  155. AbstractMesh.prototype.getWorldMatrix = function () {
  156. if (this._masterMesh) {
  157. return this._masterMesh.getWorldMatrix();
  158. }
  159. if (this._currentRenderId !== this.getScene().getRenderId()) {
  160. this.computeWorldMatrix();
  161. }
  162. return this._worldMatrix;
  163. };
  164. Object.defineProperty(AbstractMesh.prototype, "worldMatrixFromCache", {
  165. get: function () {
  166. return this._worldMatrix;
  167. },
  168. enumerable: true,
  169. configurable: true
  170. });
  171. Object.defineProperty(AbstractMesh.prototype, "absolutePosition", {
  172. get: function () {
  173. return this._absolutePosition;
  174. },
  175. enumerable: true,
  176. configurable: true
  177. });
  178. AbstractMesh.prototype.freezeWorldMatrix = function () {
  179. this._isWorldMatrixFrozen = false; // no guarantee world is not already frozen, switch off temporarily
  180. this.computeWorldMatrix(true);
  181. this._isWorldMatrixFrozen = true;
  182. };
  183. AbstractMesh.prototype.unfreezeWorldMatrix = function () {
  184. this._isWorldMatrixFrozen = false;
  185. this.computeWorldMatrix(true);
  186. };
  187. Object.defineProperty(AbstractMesh.prototype, "isWorldMatrixFrozen", {
  188. get: function () {
  189. return this._isWorldMatrixFrozen;
  190. },
  191. enumerable: true,
  192. configurable: true
  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 === 0 /* 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 === 0 /* 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()._getNewPosition(this._oldPositionForCollisions, velocity, this._collider, 3, this._newPositionForCollisions, this);
  614. this._newPositionForCollisions.subtractToRef(this._oldPositionForCollisions, this._diffPositionForCollisions);
  615. if (this._diffPositionForCollisions.length() > BABYLON.Engine.CollisionsEpsilon) {
  616. this.position.addInPlace(this._diffPositionForCollisions);
  617. }
  618. };
  619. // Submeshes octree
  620. /**
  621. * This function will create an octree to help select the right submeshes for rendering, picking and collisions
  622. * Please note that you must have a decent number of submeshes to get performance improvements when using octree
  623. */
  624. AbstractMesh.prototype.createOrUpdateSubmeshesOctree = function (maxCapacity, maxDepth) {
  625. if (maxCapacity === void 0) { maxCapacity = 64; }
  626. if (maxDepth === void 0) { maxDepth = 2; }
  627. if (!this._submeshesOctree) {
  628. this._submeshesOctree = new BABYLON.Octree(BABYLON.Octree.CreationFuncForSubMeshes, maxCapacity, maxDepth);
  629. }
  630. this.computeWorldMatrix(true);
  631. // Update octree
  632. var bbox = this.getBoundingInfo().boundingBox;
  633. this._submeshesOctree.update(bbox.minimumWorld, bbox.maximumWorld, this.subMeshes);
  634. return this._submeshesOctree;
  635. };
  636. // Collisions
  637. AbstractMesh.prototype._collideForSubMesh = function (subMesh, transformMatrix, collider) {
  638. this._generatePointsArray();
  639. // Transformation
  640. if (!subMesh._lastColliderWorldVertices || !subMesh._lastColliderTransformMatrix.equals(transformMatrix)) {
  641. subMesh._lastColliderTransformMatrix = transformMatrix.clone();
  642. subMesh._lastColliderWorldVertices = [];
  643. subMesh._trianglePlanes = [];
  644. var start = subMesh.verticesStart;
  645. var end = (subMesh.verticesStart + subMesh.verticesCount);
  646. for (var i = start; i < end; i++) {
  647. subMesh._lastColliderWorldVertices.push(BABYLON.Vector3.TransformCoordinates(this._positions[i], transformMatrix));
  648. }
  649. }
  650. // Collide
  651. collider._collide(subMesh, subMesh._lastColliderWorldVertices, this.getIndices(), subMesh.indexStart, subMesh.indexStart + subMesh.indexCount, subMesh.verticesStart);
  652. };
  653. AbstractMesh.prototype._processCollisionsForSubMeshes = function (collider, transformMatrix) {
  654. var subMeshes;
  655. var len;
  656. // Octrees
  657. if (this._submeshesOctree && this.useOctreeForCollisions) {
  658. var radius = collider.velocityWorldLength + Math.max(collider.radius.x, collider.radius.y, collider.radius.z);
  659. var intersections = this._submeshesOctree.intersects(collider.basePointWorld, radius);
  660. len = intersections.length;
  661. subMeshes = intersections.data;
  662. }
  663. else {
  664. subMeshes = this.subMeshes;
  665. len = subMeshes.length;
  666. }
  667. for (var index = 0; index < len; index++) {
  668. var subMesh = subMeshes[index];
  669. // Bounding test
  670. if (len > 1 && !subMesh._checkCollision(collider))
  671. continue;
  672. this._collideForSubMesh(subMesh, transformMatrix, collider);
  673. }
  674. };
  675. AbstractMesh.prototype._checkCollision = function (collider) {
  676. // Bounding box test
  677. if (!this._boundingInfo._checkCollision(collider))
  678. return;
  679. // Transformation matrix
  680. BABYLON.Matrix.ScalingToRef(1.0 / collider.radius.x, 1.0 / collider.radius.y, 1.0 / collider.radius.z, this._collisionsScalingMatrix);
  681. this.worldMatrixFromCache.multiplyToRef(this._collisionsScalingMatrix, this._collisionsTransformMatrix);
  682. this._processCollisionsForSubMeshes(collider, this._collisionsTransformMatrix);
  683. };
  684. // Picking
  685. AbstractMesh.prototype._generatePointsArray = function () {
  686. return false;
  687. };
  688. AbstractMesh.prototype.intersects = function (ray, fastCheck) {
  689. var pickingInfo = new BABYLON.PickingInfo();
  690. if (!this.subMeshes || !this._boundingInfo || !ray.intersectsSphere(this._boundingInfo.boundingSphere) || !ray.intersectsBox(this._boundingInfo.boundingBox)) {
  691. return pickingInfo;
  692. }
  693. if (!this._generatePointsArray()) {
  694. return pickingInfo;
  695. }
  696. var intersectInfo = null;
  697. // Octrees
  698. var subMeshes;
  699. var len;
  700. if (this._submeshesOctree && this.useOctreeForPicking) {
  701. var worldRay = BABYLON.Ray.Transform(ray, this.getWorldMatrix());
  702. var intersections = this._submeshesOctree.intersectsRay(worldRay);
  703. len = intersections.length;
  704. subMeshes = intersections.data;
  705. }
  706. else {
  707. subMeshes = this.subMeshes;
  708. len = subMeshes.length;
  709. }
  710. for (var index = 0; index < len; index++) {
  711. var subMesh = subMeshes[index];
  712. // Bounding test
  713. if (len > 1 && !subMesh.canIntersects(ray))
  714. continue;
  715. var currentIntersectInfo = subMesh.intersects(ray, this._positions, this.getIndices(), fastCheck);
  716. if (currentIntersectInfo) {
  717. if (fastCheck || !intersectInfo || currentIntersectInfo.distance < intersectInfo.distance) {
  718. intersectInfo = currentIntersectInfo;
  719. intersectInfo.subMeshId = index;
  720. if (fastCheck) {
  721. break;
  722. }
  723. }
  724. }
  725. }
  726. if (intersectInfo) {
  727. // Get picked point
  728. var world = this.getWorldMatrix();
  729. var worldOrigin = BABYLON.Vector3.TransformCoordinates(ray.origin, world);
  730. var direction = ray.direction.clone();
  731. direction = direction.scale(intersectInfo.distance);
  732. var worldDirection = BABYLON.Vector3.TransformNormal(direction, world);
  733. var pickedPoint = worldOrigin.add(worldDirection);
  734. // Return result
  735. pickingInfo.hit = true;
  736. pickingInfo.distance = BABYLON.Vector3.Distance(worldOrigin, pickedPoint);
  737. pickingInfo.pickedPoint = pickedPoint;
  738. pickingInfo.pickedMesh = this;
  739. pickingInfo.bu = intersectInfo.bu;
  740. pickingInfo.bv = intersectInfo.bv;
  741. pickingInfo.faceId = intersectInfo.faceId;
  742. pickingInfo.subMeshId = intersectInfo.subMeshId;
  743. return pickingInfo;
  744. }
  745. return pickingInfo;
  746. };
  747. AbstractMesh.prototype.clone = function (name, newParent, doNotCloneChildren) {
  748. return null;
  749. };
  750. AbstractMesh.prototype.releaseSubMeshes = function () {
  751. if (this.subMeshes) {
  752. while (this.subMeshes.length) {
  753. this.subMeshes[0].dispose();
  754. }
  755. }
  756. else {
  757. this.subMeshes = new Array();
  758. }
  759. };
  760. AbstractMesh.prototype.dispose = function (doNotRecurse) {
  761. var index;
  762. // Physics
  763. if (this.getPhysicsImpostor() !== BABYLON.PhysicsEngine.NoImpostor) {
  764. this.setPhysicsState(BABYLON.PhysicsEngine.NoImpostor);
  765. }
  766. for (index = 0; index < this._intersectionsInProgress.length; index++) {
  767. var other = this._intersectionsInProgress[index];
  768. var pos = other._intersectionsInProgress.indexOf(this);
  769. other._intersectionsInProgress.splice(pos, 1);
  770. }
  771. this._intersectionsInProgress = [];
  772. // SubMeshes
  773. this.releaseSubMeshes();
  774. // Remove from scene
  775. this.getScene().removeMesh(this);
  776. if (!doNotRecurse) {
  777. for (index = 0; index < this.getScene().particleSystems.length; index++) {
  778. if (this.getScene().particleSystems[index].emitter === this) {
  779. this.getScene().particleSystems[index].dispose();
  780. index--;
  781. }
  782. }
  783. // Children
  784. var objects = this.getScene().meshes.slice(0);
  785. for (index = 0; index < objects.length; index++) {
  786. if (objects[index].parent === this) {
  787. objects[index].dispose();
  788. }
  789. }
  790. }
  791. else {
  792. for (index = 0; index < this.getScene().meshes.length; index++) {
  793. var obj = this.getScene().meshes[index];
  794. if (obj.parent === this) {
  795. obj.parent = null;
  796. obj.computeWorldMatrix(true);
  797. }
  798. }
  799. }
  800. this._onAfterWorldMatrixUpdate = [];
  801. this._isDisposed = true;
  802. // Callback
  803. if (this.onDispose) {
  804. this.onDispose();
  805. }
  806. };
  807. // Statics
  808. AbstractMesh._BILLBOARDMODE_NONE = 0;
  809. AbstractMesh._BILLBOARDMODE_X = 1;
  810. AbstractMesh._BILLBOARDMODE_Y = 2;
  811. AbstractMesh._BILLBOARDMODE_Z = 4;
  812. AbstractMesh._BILLBOARDMODE_ALL = 7;
  813. return AbstractMesh;
  814. })(BABYLON.Node);
  815. BABYLON.AbstractMesh = AbstractMesh;
  816. })(BABYLON || (BABYLON = {}));
  817. //# sourceMappingURL=../Mesh/babylon.abstractMesh.js.map