babylon.abstractMesh.ts 27 KB

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