babylon.abstractMesh.js 37 KB

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