babylon.abstractMesh.js 29 KB

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