babylon.abstractMesh.ts 29 KB

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