babylon.abstractMesh.js 30 KB

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