babylon.abstractMesh.js 32 KB

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