babylon.abstractMesh.js 37 KB

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