babylon.abstractMesh.js 31 KB

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