babylon.abstractMesh.js 28 KB

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