babylon.abstractMesh.ts 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804
  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,
  272. this.position.z + cameraGlobalPosition.z, this._localTranslation);
  273. } else {
  274. BABYLON.Matrix.TranslationToRef(this.position.x, this.position.y, this.position.z, this._localTranslation);
  275. }
  276. // Composing transformations
  277. this._pivotMatrix.multiplyToRef(this._localScaling, this._localPivotScaling);
  278. this._localPivotScaling.multiplyToRef(this._localRotation, this._localPivotScalingRotation);
  279. // Billboarding
  280. if (this.billboardMode !== AbstractMesh.BILLBOARDMODE_NONE && this.getScene().activeCamera) {
  281. var localPosition = this.position.clone();
  282. var zero = this.getScene().activeCamera.position.clone();
  283. if (this.parent && (<any>this.parent).position) {
  284. localPosition.addInPlace((<any>this.parent).position);
  285. BABYLON.Matrix.TranslationToRef(localPosition.x, localPosition.y, localPosition.z, this._localTranslation);
  286. }
  287. if ((this.billboardMode & AbstractMesh.BILLBOARDMODE_ALL) === AbstractMesh.BILLBOARDMODE_ALL) {
  288. zero = this.getScene().activeCamera.position;
  289. } else {
  290. if (this.billboardMode & BABYLON.AbstractMesh.BILLBOARDMODE_X)
  291. zero.x = localPosition.x + BABYLON.Engine.Epsilon;
  292. if (this.billboardMode & BABYLON.AbstractMesh.BILLBOARDMODE_Y)
  293. zero.y = localPosition.y + 0.001;
  294. if (this.billboardMode & BABYLON.AbstractMesh.BILLBOARDMODE_Z)
  295. zero.z = localPosition.z + 0.001;
  296. }
  297. BABYLON.Matrix.LookAtLHToRef(localPosition, zero, BABYLON.Vector3.Up(), this._localBillboard);
  298. this._localBillboard.m[12] = this._localBillboard.m[13] = this._localBillboard.m[14] = 0;
  299. this._localBillboard.invert();
  300. this._localPivotScalingRotation.multiplyToRef(this._localBillboard, this._localWorld);
  301. this._rotateYByPI.multiplyToRef(this._localWorld, this._localPivotScalingRotation);
  302. }
  303. // Local world
  304. this._localPivotScalingRotation.multiplyToRef(this._localTranslation, this._localWorld);
  305. // Parent
  306. if (this.parent && this.parent.getWorldMatrix && this.billboardMode === BABYLON.AbstractMesh.BILLBOARDMODE_NONE) {
  307. this._localWorld.multiplyToRef(this.parent.getWorldMatrix(), this._worldMatrix);
  308. } else {
  309. this._worldMatrix.copyFrom(this._localWorld);
  310. }
  311. // Bounding info
  312. this._updateBoundingInfo();
  313. // Absolute position
  314. this._absolutePosition.copyFromFloats(this._worldMatrix.m[12], this._worldMatrix.m[13], this._worldMatrix.m[14]);
  315. return this._worldMatrix;
  316. }
  317. public setPositionWithLocalVector(vector3: Vector3): void {
  318. this.computeWorldMatrix();
  319. this.position = BABYLON.Vector3.TransformNormal(vector3, this._localWorld);
  320. }
  321. public getPositionExpressedInLocalSpace(): Vector3 {
  322. this.computeWorldMatrix();
  323. var invLocalWorldMatrix = this._localWorld.clone();
  324. invLocalWorldMatrix.invert();
  325. return BABYLON.Vector3.TransformNormal(this.position, invLocalWorldMatrix);
  326. }
  327. public locallyTranslate(vector3: Vector3): void {
  328. this.computeWorldMatrix();
  329. this.position = BABYLON.Vector3.TransformCoordinates(vector3, this._localWorld);
  330. }
  331. public lookAt(targetPoint: Vector3, yawCor: number, pitchCor: number, rollCor: number): void {
  332. /// <summary>Orients a mesh towards a target point. Mesh must be drawn facing user.</summary>
  333. /// <param name="targetPoint" type="BABYLON.Vector3">The position (must be in same space as current mesh) to look at</param>
  334. /// <param name="yawCor" type="Number">optional yaw (y-axis) correction in radians</param>
  335. /// <param name="pitchCor" type="Number">optional pitch (x-axis) correction in radians</param>
  336. /// <param name="rollCor" type="Number">optional roll (z-axis) correction in radians</param>
  337. /// <returns>Mesh oriented towards targetMesh</returns>
  338. yawCor = yawCor || 0; // default to zero if undefined
  339. pitchCor = pitchCor || 0;
  340. rollCor = rollCor || 0;
  341. var dv = targetPoint.subtract(this.position);
  342. var yaw = -Math.atan2(dv.z, dv.x) - Math.PI / 2;
  343. var len = Math.sqrt(dv.x * dv.x + dv.z * dv.z);
  344. var pitch = Math.atan2(dv.y, len);
  345. this.rotationQuaternion = BABYLON.Quaternion.RotationYawPitchRoll(yaw + yawCor, pitch + pitchCor, rollCor);
  346. }
  347. public isInFrustum(frustumPlanes: Plane[]): boolean {
  348. if (!this._boundingInfo.isInFrustum(frustumPlanes)) {
  349. return false;
  350. }
  351. return true;
  352. }
  353. public isCompletelyInFrustum(camera?: Camera): boolean {
  354. if (!camera) {
  355. camera = this.getScene().activeCamera;
  356. }
  357. var transformMatrix = camera.getViewMatrix().multiply(camera.getProjectionMatrix());
  358. if (!this._boundingInfo.isCompletelyInFrustum(BABYLON.Frustum.GetPlanes(transformMatrix))) {
  359. return false;
  360. }
  361. return true;
  362. }
  363. public intersectsMesh(mesh: AbstractMesh, precise?: boolean): boolean {
  364. if (!this._boundingInfo || !mesh._boundingInfo) {
  365. return false;
  366. }
  367. return this._boundingInfo.intersects(mesh._boundingInfo, precise);
  368. }
  369. public intersectsPoint(point: Vector3): boolean {
  370. if (!this._boundingInfo) {
  371. return false;
  372. }
  373. return this._boundingInfo.intersectsPoint(point);
  374. }
  375. // Physics
  376. public setPhysicsState(impostor?: any, options?: PhysicsBodyCreationOptions): void {
  377. var physicsEngine = this.getScene().getPhysicsEngine();
  378. if (!physicsEngine) {
  379. return;
  380. }
  381. if (impostor.impostor) {
  382. // Old API
  383. options = impostor;
  384. impostor = impostor.impostor;
  385. }
  386. impostor = impostor || PhysicsEngine.NoImpostor;
  387. if (impostor === BABYLON.PhysicsEngine.NoImpostor) {
  388. physicsEngine._unregisterMesh(this);
  389. return;
  390. }
  391. options.mass = options.mass || 0;
  392. options.friction = options.friction || 0.2;
  393. options.restitution = options.restitution || 0.2;
  394. this._physicImpostor = impostor;
  395. this._physicsMass = options.mass;
  396. this._physicsFriction = options.friction;
  397. this._physicRestitution = options.restitution;
  398. physicsEngine._registerMesh(this, impostor, options);
  399. }
  400. public getPhysicsImpostor(): number {
  401. if (!this._physicImpostor) {
  402. return BABYLON.PhysicsEngine.NoImpostor;
  403. }
  404. return this._physicImpostor;
  405. }
  406. public getPhysicsMass(): number {
  407. if (!this._physicsMass) {
  408. return 0;
  409. }
  410. return this._physicsMass;
  411. }
  412. public getPhysicsFriction(): number {
  413. if (!this._physicsFriction) {
  414. return 0;
  415. }
  416. return this._physicsFriction;
  417. }
  418. public getPhysicsRestitution(): number {
  419. if (!this._physicRestitution) {
  420. return 0;
  421. }
  422. return this._physicRestitution;
  423. }
  424. public applyImpulse(force: Vector3, contactPoint: Vector3): void {
  425. if (!this._physicImpostor) {
  426. return;
  427. }
  428. this.getScene().getPhysicsEngine()._applyImpulse(this, force, contactPoint);
  429. }
  430. public setPhysicsLinkWith(otherMesh: Mesh, pivot1: Vector3, pivot2: Vector3, options?: any): void {
  431. if (!this._physicImpostor) {
  432. return;
  433. }
  434. this.getScene().getPhysicsEngine()._createLink(this, otherMesh, pivot1, pivot2, options);
  435. }
  436. public updatePhysicsBodyPosition(): void {
  437. if (!this._physicImpostor) {
  438. return;
  439. }
  440. this.getScene().getPhysicsEngine()._updateBodyPosition(this);
  441. }
  442. // Collisions
  443. public moveWithCollisions(velocity: Vector3): void {
  444. var globalPosition = this.getAbsolutePosition();
  445. globalPosition.subtractFromFloatsToRef(0, this.ellipsoid.y, 0, this._oldPositionForCollisions);
  446. this._oldPositionForCollisions.addInPlace(this.ellipsoidOffset);
  447. this._collider.radius = this.ellipsoid;
  448. this.getScene()._getNewPosition(this._oldPositionForCollisions, velocity, this._collider, 3, this._newPositionForCollisions, this);
  449. this._newPositionForCollisions.subtractToRef(this._oldPositionForCollisions, this._diffPositionForCollisions);
  450. if (this._diffPositionForCollisions.length() > Engine.CollisionsEpsilon) {
  451. this.position.addInPlace(this._diffPositionForCollisions);
  452. }
  453. }
  454. // Submeshes octree
  455. /**
  456. * This function will create an octree to help select the right submeshes for rendering, picking and collisions
  457. * Please note that you must have a decent number of submeshes to get performance improvements when using octree
  458. */
  459. public createOrUpdateSubmeshesOctree(maxCapacity = 64, maxDepth = 2): Octree<SubMesh> {
  460. if (!this._submeshesOctree) {
  461. this._submeshesOctree = new BABYLON.Octree<SubMesh>(Octree.CreationFuncForSubMeshes, maxCapacity, maxDepth);
  462. }
  463. this.computeWorldMatrix(true);
  464. // Update octree
  465. var bbox = this.getBoundingInfo().boundingBox;
  466. this._submeshesOctree.update(bbox.minimumWorld, bbox.maximumWorld, this.subMeshes);
  467. return this._submeshesOctree;
  468. }
  469. // Collisions
  470. public _collideForSubMesh(subMesh: SubMesh, transformMatrix: Matrix, collider: Collider): void {
  471. this._generatePointsArray();
  472. // Transformation
  473. if (!subMesh._lastColliderWorldVertices || !subMesh._lastColliderTransformMatrix.equals(transformMatrix)) {
  474. subMesh._lastColliderTransformMatrix = transformMatrix.clone();
  475. subMesh._lastColliderWorldVertices = [];
  476. subMesh._trianglePlanes = [];
  477. var start = subMesh.verticesStart;
  478. var end = (subMesh.verticesStart + subMesh.verticesCount);
  479. for (var i = start; i < end; i++) {
  480. subMesh._lastColliderWorldVertices.push(BABYLON.Vector3.TransformCoordinates(this._positions[i], transformMatrix));
  481. }
  482. }
  483. // Collide
  484. collider._collide(subMesh, subMesh._lastColliderWorldVertices, this.getIndices(), subMesh.indexStart, subMesh.indexStart + subMesh.indexCount, subMesh.verticesStart);
  485. }
  486. public _processCollisionsForSubMeshes(collider: Collider, transformMatrix: Matrix): void {
  487. var subMeshes: SubMesh[];
  488. var len: number;
  489. // Octrees
  490. if (this._submeshesOctree && this.useOctreeForCollisions) {
  491. var radius = collider.velocityWorldLength + Math.max(collider.radius.x, collider.radius.y, collider.radius.z);
  492. var intersections = this._submeshesOctree.intersects(collider.basePointWorld, radius);
  493. len = intersections.length;
  494. subMeshes = intersections.data;
  495. } else {
  496. subMeshes = this.subMeshes;
  497. len = subMeshes.length;
  498. }
  499. for (var index = 0; index < len; index++) {
  500. var subMesh = subMeshes[index];
  501. // Bounding test
  502. if (len > 1 && !subMesh._checkCollision(collider))
  503. continue;
  504. this._collideForSubMesh(subMesh, transformMatrix, collider);
  505. }
  506. }
  507. public _checkCollision(collider: Collider): void {
  508. // Bounding box test
  509. if (!this._boundingInfo._checkCollision(collider))
  510. return;
  511. // Transformation matrix
  512. BABYLON.Matrix.ScalingToRef(1.0 / collider.radius.x, 1.0 / collider.radius.y, 1.0 / collider.radius.z, this._collisionsScalingMatrix);
  513. this.worldMatrixFromCache.multiplyToRef(this._collisionsScalingMatrix, this._collisionsTransformMatrix);
  514. this._processCollisionsForSubMeshes(collider, this._collisionsTransformMatrix);
  515. }
  516. // Picking
  517. public _generatePointsArray(): boolean {
  518. return false;
  519. }
  520. public intersects(ray: Ray, fastCheck?: boolean): PickingInfo {
  521. var pickingInfo = new BABYLON.PickingInfo();
  522. if (!this.subMeshes || !this._boundingInfo || !ray.intersectsSphere(this._boundingInfo.boundingSphere) || !ray.intersectsBox(this._boundingInfo.boundingBox)) {
  523. return pickingInfo;
  524. }
  525. if (!this._generatePointsArray()) {
  526. return pickingInfo;
  527. }
  528. var intersectInfo: IntersectionInfo = null;
  529. // Octrees
  530. var subMeshes: SubMesh[];
  531. var len: number;
  532. if (this._submeshesOctree && this.useOctreeForPicking) {
  533. var worldRay = Ray.Transform(ray, this.getWorldMatrix());
  534. var intersections = this._submeshesOctree.intersectsRay(worldRay);
  535. len = intersections.length;
  536. subMeshes = intersections.data;
  537. } else {
  538. subMeshes = this.subMeshes;
  539. len = subMeshes.length;
  540. }
  541. for (var index = 0; index < len; index++) {
  542. var subMesh = subMeshes[index];
  543. // Bounding test
  544. if (len > 1 && !subMesh.canIntersects(ray))
  545. continue;
  546. var currentIntersectInfo = subMesh.intersects(ray, this._positions, this.getIndices(), fastCheck);
  547. if (currentIntersectInfo) {
  548. if (fastCheck || !intersectInfo || currentIntersectInfo.distance < intersectInfo.distance) {
  549. intersectInfo = currentIntersectInfo;
  550. if (fastCheck) {
  551. break;
  552. }
  553. }
  554. }
  555. }
  556. if (intersectInfo) {
  557. // Get picked point
  558. var world = this.getWorldMatrix();
  559. var worldOrigin = BABYLON.Vector3.TransformCoordinates(ray.origin, world);
  560. var direction = ray.direction.clone();
  561. direction.normalize();
  562. direction = direction.scale(intersectInfo.distance);
  563. var worldDirection = BABYLON.Vector3.TransformNormal(direction, world);
  564. var pickedPoint = worldOrigin.add(worldDirection);
  565. // Return result
  566. pickingInfo.hit = true;
  567. pickingInfo.distance = BABYLON.Vector3.Distance(worldOrigin, pickedPoint);
  568. pickingInfo.pickedPoint = pickedPoint;
  569. pickingInfo.pickedMesh = this;
  570. pickingInfo.bu = intersectInfo.bu;
  571. pickingInfo.bv = intersectInfo.bv;
  572. pickingInfo.faceId = intersectInfo.faceId;
  573. return pickingInfo;
  574. }
  575. return pickingInfo;
  576. }
  577. public clone(name: string, newParent: Node, doNotCloneChildren?: boolean): AbstractMesh {
  578. return null;
  579. }
  580. public releaseSubMeshes(): void {
  581. if (this.subMeshes) {
  582. while (this.subMeshes.length) {
  583. this.subMeshes[0].dispose();
  584. }
  585. } else {
  586. this.subMeshes = new Array<SubMesh>();
  587. }
  588. }
  589. public dispose(doNotRecurse?: boolean): void {
  590. // Physics
  591. if (this.getPhysicsImpostor() != PhysicsEngine.NoImpostor) {
  592. this.setPhysicsState(PhysicsEngine.NoImpostor);
  593. }
  594. // Intersections in progress
  595. for (index = 0; index < this._intersectionsInProgress.length; index++) {
  596. var other = this._intersectionsInProgress[index];
  597. var pos = other._intersectionsInProgress.indexOf(this);
  598. other._intersectionsInProgress.splice(pos, 1);
  599. }
  600. this._intersectionsInProgress = [];
  601. // SubMeshes
  602. this.releaseSubMeshes();
  603. // Remove from scene
  604. var index = this.getScene().meshes.indexOf(this);
  605. if (index != -1) {
  606. // Remove from the scene if mesh found
  607. this.getScene().meshes.splice(index, 1);
  608. }
  609. if (!doNotRecurse) {
  610. // Particles
  611. for (index = 0; index < this.getScene().particleSystems.length; index++) {
  612. if (this.getScene().particleSystems[index].emitter == this) {
  613. this.getScene().particleSystems[index].dispose();
  614. index--;
  615. }
  616. }
  617. // Children
  618. var objects = this.getScene().meshes.slice(0);
  619. for (index = 0; index < objects.length; index++) {
  620. if (objects[index].parent == this) {
  621. objects[index].dispose();
  622. }
  623. }
  624. } else {
  625. for (index = 0; index < this.getScene().meshes.length; index++) {
  626. var obj = this.getScene().meshes[index];
  627. if (obj.parent === this) {
  628. obj.parent = null;
  629. obj.computeWorldMatrix(true);
  630. }
  631. }
  632. }
  633. this._isDisposed = true;
  634. // Callback
  635. if (this.onDispose) {
  636. this.onDispose();
  637. }
  638. }
  639. }
  640. }