babylon.abstractMesh.js 29 KB

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