babylon.abstractMesh.ts 29 KB

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