babylon.abstractMesh.js 33 KB

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