babylon.abstractMesh.ts 29 KB

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