babylon.abstractMesh.js 31 KB

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