babylon.abstractMesh.js 31 KB

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