babylon.abstractMesh.js 37 KB

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