babylon.abstractMesh.js 37 KB

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