babylon.abstractMesh.js 38 KB

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