babylon.abstractMesh.js 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798
  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.position.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. zero = this.getScene().activeCamera.position;
  397. }
  398. else {
  399. if (this.billboardMode & AbstractMesh.BILLBOARDMODE_X)
  400. zero.x = localPosition.x + BABYLON.Engine.Epsilon;
  401. if (this.billboardMode & AbstractMesh.BILLBOARDMODE_Y)
  402. zero.y = localPosition.y + 0.001;
  403. if (this.billboardMode & AbstractMesh.BILLBOARDMODE_Z)
  404. zero.z = localPosition.z + 0.001;
  405. }
  406. BABYLON.Matrix.LookAtLHToRef(localPosition, zero, BABYLON.Vector3.Up(), this._localBillboard);
  407. this._localBillboard.m[12] = this._localBillboard.m[13] = this._localBillboard.m[14] = 0;
  408. this._localBillboard.invert();
  409. this._localPivotScalingRotation.multiplyToRef(this._localBillboard, this._localWorld);
  410. this._rotateYByPI.multiplyToRef(this._localWorld, this._localPivotScalingRotation);
  411. }
  412. // Local world
  413. this._localPivotScalingRotation.multiplyToRef(this._localTranslation, this._localWorld);
  414. // Parent
  415. if (this.parent && this.parent.getWorldMatrix && this.billboardMode === AbstractMesh.BILLBOARDMODE_NONE) {
  416. this._localWorld.multiplyToRef(this.parent.getWorldMatrix(), this._worldMatrix);
  417. }
  418. else {
  419. this._worldMatrix.copyFrom(this._localWorld);
  420. }
  421. // Bounding info
  422. this._updateBoundingInfo();
  423. // Absolute position
  424. this._absolutePosition.copyFromFloats(this._worldMatrix.m[12], this._worldMatrix.m[13], this._worldMatrix.m[14]);
  425. for (var callbackIndex = 0; callbackIndex < this._onAfterWorldMatrixUpdate.length; callbackIndex++) {
  426. this._onAfterWorldMatrixUpdate[callbackIndex](this);
  427. }
  428. return this._worldMatrix;
  429. };
  430. /**
  431. * If you'd like to be callbacked after the mesh position, rotation or scaling has been updated
  432. * @param func: callback function to add
  433. */
  434. AbstractMesh.prototype.registerAfterWorldMatrixUpdate = function (func) {
  435. this._onAfterWorldMatrixUpdate.push(func);
  436. };
  437. AbstractMesh.prototype.unregisterAfterWorldMatrixUpdate = function (func) {
  438. var index = this._onAfterWorldMatrixUpdate.indexOf(func);
  439. if (index > -1) {
  440. this._onAfterWorldMatrixUpdate.splice(index, 1);
  441. }
  442. };
  443. AbstractMesh.prototype.setPositionWithLocalVector = function (vector3) {
  444. this.computeWorldMatrix();
  445. this.position = BABYLON.Vector3.TransformNormal(vector3, this._localWorld);
  446. };
  447. AbstractMesh.prototype.getPositionExpressedInLocalSpace = function () {
  448. this.computeWorldMatrix();
  449. var invLocalWorldMatrix = this._localWorld.clone();
  450. invLocalWorldMatrix.invert();
  451. return BABYLON.Vector3.TransformNormal(this.position, invLocalWorldMatrix);
  452. };
  453. AbstractMesh.prototype.locallyTranslate = function (vector3) {
  454. this.computeWorldMatrix();
  455. this.position = BABYLON.Vector3.TransformCoordinates(vector3, this._localWorld);
  456. };
  457. AbstractMesh.prototype.lookAt = function (targetPoint, yawCor, pitchCor, rollCor) {
  458. /// <summary>Orients a mesh towards a target point. Mesh must be drawn facing user.</summary>
  459. /// <param name="targetPoint" type="Vector3">The position (must be in same space as current mesh) to look at</param>
  460. /// <param name="yawCor" type="Number">optional yaw (y-axis) correction in radians</param>
  461. /// <param name="pitchCor" type="Number">optional pitch (x-axis) correction in radians</param>
  462. /// <param name="rollCor" type="Number">optional roll (z-axis) correction in radians</param>
  463. /// <returns>Mesh oriented towards targetMesh</returns>
  464. yawCor = yawCor || 0; // default to zero if undefined
  465. pitchCor = pitchCor || 0;
  466. rollCor = rollCor || 0;
  467. var dv = targetPoint.subtract(this.position);
  468. var yaw = -Math.atan2(dv.z, dv.x) - Math.PI / 2;
  469. var len = Math.sqrt(dv.x * dv.x + dv.z * dv.z);
  470. var pitch = Math.atan2(dv.y, len);
  471. this.rotationQuaternion = BABYLON.Quaternion.RotationYawPitchRoll(yaw + yawCor, pitch + pitchCor, rollCor);
  472. };
  473. AbstractMesh.prototype.isInFrustum = function (frustumPlanes) {
  474. if (!this._boundingInfo.isInFrustum(frustumPlanes)) {
  475. return false;
  476. }
  477. return true;
  478. };
  479. AbstractMesh.prototype.isCompletelyInFrustum = function (camera) {
  480. if (!camera) {
  481. camera = this.getScene().activeCamera;
  482. }
  483. var transformMatrix = camera.getViewMatrix().multiply(camera.getProjectionMatrix());
  484. if (!this._boundingInfo.isCompletelyInFrustum(BABYLON.Frustum.GetPlanes(transformMatrix))) {
  485. return false;
  486. }
  487. return true;
  488. };
  489. AbstractMesh.prototype.intersectsMesh = function (mesh, precise) {
  490. if (!this._boundingInfo || !mesh._boundingInfo) {
  491. return false;
  492. }
  493. return this._boundingInfo.intersects(mesh._boundingInfo, precise);
  494. };
  495. AbstractMesh.prototype.intersectsPoint = function (point) {
  496. if (!this._boundingInfo) {
  497. return false;
  498. }
  499. return this._boundingInfo.intersectsPoint(point);
  500. };
  501. // Physics
  502. AbstractMesh.prototype.setPhysicsState = function (impostor, options) {
  503. var physicsEngine = this.getScene().getPhysicsEngine();
  504. if (!physicsEngine) {
  505. return;
  506. }
  507. impostor = impostor || BABYLON.PhysicsEngine.NoImpostor;
  508. if (impostor.impostor) {
  509. // Old API
  510. options = impostor;
  511. impostor = impostor.impostor;
  512. }
  513. if (impostor === BABYLON.PhysicsEngine.NoImpostor) {
  514. physicsEngine._unregisterMesh(this);
  515. return;
  516. }
  517. if (!options) {
  518. options = { mass: 0, friction: 0.2, restitution: 0.2 };
  519. }
  520. else {
  521. if (!options.mass && options.mass !== 0)
  522. options.mass = 0;
  523. if (!options.friction && options.friction !== 0)
  524. options.friction = 0.2;
  525. if (!options.restitution && options.restitution !== 0)
  526. options.restitution = 0.2;
  527. }
  528. this._physicImpostor = impostor;
  529. this._physicsMass = options.mass;
  530. this._physicsFriction = options.friction;
  531. this._physicRestitution = options.restitution;
  532. return physicsEngine._registerMesh(this, impostor, options);
  533. };
  534. AbstractMesh.prototype.getPhysicsImpostor = function () {
  535. if (!this._physicImpostor) {
  536. return BABYLON.PhysicsEngine.NoImpostor;
  537. }
  538. return this._physicImpostor;
  539. };
  540. AbstractMesh.prototype.getPhysicsMass = function () {
  541. if (!this._physicsMass) {
  542. return 0;
  543. }
  544. return this._physicsMass;
  545. };
  546. AbstractMesh.prototype.getPhysicsFriction = function () {
  547. if (!this._physicsFriction) {
  548. return 0;
  549. }
  550. return this._physicsFriction;
  551. };
  552. AbstractMesh.prototype.getPhysicsRestitution = function () {
  553. if (!this._physicRestitution) {
  554. return 0;
  555. }
  556. return this._physicRestitution;
  557. };
  558. AbstractMesh.prototype.getPositionInCameraSpace = function (camera) {
  559. if (!camera) {
  560. camera = this.getScene().activeCamera;
  561. }
  562. return BABYLON.Vector3.TransformCoordinates(this.absolutePosition, camera.getViewMatrix());
  563. };
  564. AbstractMesh.prototype.getDistanceToCamera = function (camera) {
  565. if (!camera) {
  566. camera = this.getScene().activeCamera;
  567. }
  568. return this.absolutePosition.subtract(camera.position).length();
  569. };
  570. AbstractMesh.prototype.applyImpulse = function (force, contactPoint) {
  571. if (!this._physicImpostor) {
  572. return;
  573. }
  574. this.getScene().getPhysicsEngine()._applyImpulse(this, force, contactPoint);
  575. };
  576. AbstractMesh.prototype.setPhysicsLinkWith = function (otherMesh, pivot1, pivot2, options) {
  577. if (!this._physicImpostor) {
  578. return;
  579. }
  580. this.getScene().getPhysicsEngine()._createLink(this, otherMesh, pivot1, pivot2, options);
  581. };
  582. AbstractMesh.prototype.updatePhysicsBodyPosition = function () {
  583. if (!this._physicImpostor) {
  584. return;
  585. }
  586. this.getScene().getPhysicsEngine()._updateBodyPosition(this);
  587. };
  588. // Collisions
  589. AbstractMesh.prototype.moveWithCollisions = function (velocity) {
  590. var globalPosition = this.getAbsolutePosition();
  591. globalPosition.subtractFromFloatsToRef(0, this.ellipsoid.y, 0, this._oldPositionForCollisions);
  592. this._oldPositionForCollisions.addInPlace(this.ellipsoidOffset);
  593. this._collider.radius = this.ellipsoid;
  594. this.getScene()._getNewPosition(this._oldPositionForCollisions, velocity, this._collider, 3, this._newPositionForCollisions, this);
  595. this._newPositionForCollisions.subtractToRef(this._oldPositionForCollisions, this._diffPositionForCollisions);
  596. if (this._diffPositionForCollisions.length() > BABYLON.Engine.CollisionsEpsilon) {
  597. this.position.addInPlace(this._diffPositionForCollisions);
  598. }
  599. };
  600. // Submeshes octree
  601. /**
  602. * This function will create an octree to help select the right submeshes for rendering, picking and collisions
  603. * Please note that you must have a decent number of submeshes to get performance improvements when using octree
  604. */
  605. AbstractMesh.prototype.createOrUpdateSubmeshesOctree = function (maxCapacity, maxDepth) {
  606. if (maxCapacity === void 0) { maxCapacity = 64; }
  607. if (maxDepth === void 0) { maxDepth = 2; }
  608. if (!this._submeshesOctree) {
  609. this._submeshesOctree = new BABYLON.Octree(BABYLON.Octree.CreationFuncForSubMeshes, maxCapacity, maxDepth);
  610. }
  611. this.computeWorldMatrix(true);
  612. // Update octree
  613. var bbox = this.getBoundingInfo().boundingBox;
  614. this._submeshesOctree.update(bbox.minimumWorld, bbox.maximumWorld, this.subMeshes);
  615. return this._submeshesOctree;
  616. };
  617. // Collisions
  618. AbstractMesh.prototype._collideForSubMesh = function (subMesh, transformMatrix, collider) {
  619. this._generatePointsArray();
  620. // Transformation
  621. if (!subMesh._lastColliderWorldVertices || !subMesh._lastColliderTransformMatrix.equals(transformMatrix)) {
  622. subMesh._lastColliderTransformMatrix = transformMatrix.clone();
  623. subMesh._lastColliderWorldVertices = [];
  624. subMesh._trianglePlanes = [];
  625. var start = subMesh.verticesStart;
  626. var end = (subMesh.verticesStart + subMesh.verticesCount);
  627. for (var i = start; i < end; i++) {
  628. subMesh._lastColliderWorldVertices.push(BABYLON.Vector3.TransformCoordinates(this._positions[i], transformMatrix));
  629. }
  630. }
  631. // Collide
  632. collider._collide(subMesh, subMesh._lastColliderWorldVertices, this.getIndices(), subMesh.indexStart, subMesh.indexStart + subMesh.indexCount, subMesh.verticesStart);
  633. };
  634. AbstractMesh.prototype._processCollisionsForSubMeshes = function (collider, transformMatrix) {
  635. var subMeshes;
  636. var len;
  637. // Octrees
  638. if (this._submeshesOctree && this.useOctreeForCollisions) {
  639. var radius = collider.velocityWorldLength + Math.max(collider.radius.x, collider.radius.y, collider.radius.z);
  640. var intersections = this._submeshesOctree.intersects(collider.basePointWorld, radius);
  641. len = intersections.length;
  642. subMeshes = intersections.data;
  643. }
  644. else {
  645. subMeshes = this.subMeshes;
  646. len = subMeshes.length;
  647. }
  648. for (var index = 0; index < len; index++) {
  649. var subMesh = subMeshes[index];
  650. // Bounding test
  651. if (len > 1 && !subMesh._checkCollision(collider))
  652. continue;
  653. this._collideForSubMesh(subMesh, transformMatrix, collider);
  654. }
  655. };
  656. AbstractMesh.prototype._checkCollision = function (collider) {
  657. // Bounding box test
  658. if (!this._boundingInfo._checkCollision(collider))
  659. return;
  660. // Transformation matrix
  661. BABYLON.Matrix.ScalingToRef(1.0 / collider.radius.x, 1.0 / collider.radius.y, 1.0 / collider.radius.z, this._collisionsScalingMatrix);
  662. this.worldMatrixFromCache.multiplyToRef(this._collisionsScalingMatrix, this._collisionsTransformMatrix);
  663. this._processCollisionsForSubMeshes(collider, this._collisionsTransformMatrix);
  664. };
  665. // Picking
  666. AbstractMesh.prototype._generatePointsArray = function () {
  667. return false;
  668. };
  669. AbstractMesh.prototype.intersects = function (ray, fastCheck) {
  670. var pickingInfo = new BABYLON.PickingInfo();
  671. if (!this.subMeshes || !this._boundingInfo || !ray.intersectsSphere(this._boundingInfo.boundingSphere) || !ray.intersectsBox(this._boundingInfo.boundingBox)) {
  672. return pickingInfo;
  673. }
  674. if (!this._generatePointsArray()) {
  675. return pickingInfo;
  676. }
  677. var intersectInfo = null;
  678. // Octrees
  679. var subMeshes;
  680. var len;
  681. if (this._submeshesOctree && this.useOctreeForPicking) {
  682. var worldRay = BABYLON.Ray.Transform(ray, this.getWorldMatrix());
  683. var intersections = this._submeshesOctree.intersectsRay(worldRay);
  684. len = intersections.length;
  685. subMeshes = intersections.data;
  686. }
  687. else {
  688. subMeshes = this.subMeshes;
  689. len = subMeshes.length;
  690. }
  691. for (var index = 0; index < len; index++) {
  692. var subMesh = subMeshes[index];
  693. // Bounding test
  694. if (len > 1 && !subMesh.canIntersects(ray))
  695. continue;
  696. var currentIntersectInfo = subMesh.intersects(ray, this._positions, this.getIndices(), fastCheck);
  697. if (currentIntersectInfo) {
  698. if (fastCheck || !intersectInfo || currentIntersectInfo.distance < intersectInfo.distance) {
  699. intersectInfo = currentIntersectInfo;
  700. intersectInfo.subMeshId = index;
  701. if (fastCheck) {
  702. break;
  703. }
  704. }
  705. }
  706. }
  707. if (intersectInfo) {
  708. // Get picked point
  709. var world = this.getWorldMatrix();
  710. var worldOrigin = BABYLON.Vector3.TransformCoordinates(ray.origin, world);
  711. var direction = ray.direction.clone();
  712. direction = direction.scale(intersectInfo.distance);
  713. var worldDirection = BABYLON.Vector3.TransformNormal(direction, world);
  714. var pickedPoint = worldOrigin.add(worldDirection);
  715. // Return result
  716. pickingInfo.hit = true;
  717. pickingInfo.distance = BABYLON.Vector3.Distance(worldOrigin, pickedPoint);
  718. pickingInfo.pickedPoint = pickedPoint;
  719. pickingInfo.pickedMesh = this;
  720. pickingInfo.bu = intersectInfo.bu;
  721. pickingInfo.bv = intersectInfo.bv;
  722. pickingInfo.faceId = intersectInfo.faceId;
  723. pickingInfo.subMeshId = intersectInfo.subMeshId;
  724. return pickingInfo;
  725. }
  726. return pickingInfo;
  727. };
  728. AbstractMesh.prototype.clone = function (name, newParent, doNotCloneChildren) {
  729. return null;
  730. };
  731. AbstractMesh.prototype.releaseSubMeshes = function () {
  732. if (this.subMeshes) {
  733. while (this.subMeshes.length) {
  734. this.subMeshes[0].dispose();
  735. }
  736. }
  737. else {
  738. this.subMeshes = new Array();
  739. }
  740. };
  741. AbstractMesh.prototype.dispose = function (doNotRecurse) {
  742. var index;
  743. // Physics
  744. if (this.getPhysicsImpostor() !== BABYLON.PhysicsEngine.NoImpostor) {
  745. this.setPhysicsState(BABYLON.PhysicsEngine.NoImpostor);
  746. }
  747. for (index = 0; index < this._intersectionsInProgress.length; index++) {
  748. var other = this._intersectionsInProgress[index];
  749. var pos = other._intersectionsInProgress.indexOf(this);
  750. other._intersectionsInProgress.splice(pos, 1);
  751. }
  752. this._intersectionsInProgress = [];
  753. // SubMeshes
  754. this.releaseSubMeshes();
  755. // Remove from scene
  756. this.getScene().removeMesh(this);
  757. if (!doNotRecurse) {
  758. for (index = 0; index < this.getScene().particleSystems.length; index++) {
  759. if (this.getScene().particleSystems[index].emitter === this) {
  760. this.getScene().particleSystems[index].dispose();
  761. index--;
  762. }
  763. }
  764. // Children
  765. var objects = this.getScene().meshes.slice(0);
  766. for (index = 0; index < objects.length; index++) {
  767. if (objects[index].parent === this) {
  768. objects[index].dispose();
  769. }
  770. }
  771. }
  772. else {
  773. for (index = 0; index < this.getScene().meshes.length; index++) {
  774. var obj = this.getScene().meshes[index];
  775. if (obj.parent === this) {
  776. obj.parent = null;
  777. obj.computeWorldMatrix(true);
  778. }
  779. }
  780. }
  781. this._onAfterWorldMatrixUpdate = [];
  782. this._isDisposed = true;
  783. // Callback
  784. if (this.onDispose) {
  785. this.onDispose();
  786. }
  787. };
  788. // Statics
  789. AbstractMesh._BILLBOARDMODE_NONE = 0;
  790. AbstractMesh._BILLBOARDMODE_X = 1;
  791. AbstractMesh._BILLBOARDMODE_Y = 2;
  792. AbstractMesh._BILLBOARDMODE_Z = 4;
  793. AbstractMesh._BILLBOARDMODE_ALL = 7;
  794. return AbstractMesh;
  795. })(BABYLON.Node);
  796. BABYLON.AbstractMesh = AbstractMesh;
  797. })(BABYLON || (BABYLON = {}));
  798. //# sourceMappingURL=babylon.abstractMesh.js.map