babylon.abstractMesh.js 34 KB

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