pickingInfo.ts 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. import { Nullable, FloatArray } from "types";
  2. import { Vector3, Vector2, Tmp } from "Math";
  3. import { AbstractMesh, VertexBuffer } from "Mesh";
  4. import { Ray } from "Culling";
  5. /**
  6. * @hidden
  7. */
  8. export class IntersectionInfo {
  9. public faceId = 0;
  10. public subMeshId = 0;
  11. constructor(
  12. public bu: Nullable<number>,
  13. public bv: Nullable<number>,
  14. public distance: number) {
  15. }
  16. }
  17. /**
  18. * Information about the result of picking within a scene
  19. * @see https://doc.babylonjs.com/babylon101/picking_collisions
  20. */
  21. export class PickingInfo {
  22. /**
  23. * If the pick collided with an object
  24. */
  25. public hit = false;
  26. /**
  27. * Distance away where the pick collided
  28. */
  29. public distance = 0;
  30. /**
  31. * The location of pick collision
  32. */
  33. public pickedPoint: Nullable<Vector3> = null;
  34. /**
  35. * The mesh corresponding the the pick collision
  36. */
  37. public pickedMesh: Nullable<AbstractMesh> = null;
  38. /** (See getTextureCoordinates) The barycentric U coordinate that is used when calulating the texture coordinates of the collision.*/
  39. public bu = 0;
  40. /** (See getTextureCoordinates) The barycentric V coordinate that is used when calulating the texture coordinates of the collision.*/
  41. public bv = 0;
  42. /** The id of the face on the mesh that was picked */
  43. public faceId = -1;
  44. /** Id of the the submesh that was picked */
  45. public subMeshId = 0;
  46. /** If a sprite was picked, this will be the sprite the pick collided with */
  47. public pickedSprite: Nullable<Sprite> = null;
  48. /**
  49. * If a mesh was used to do the picking (eg. 6dof controller) this will be populated.
  50. */
  51. public originMesh: Nullable<AbstractMesh> = null;
  52. /**
  53. * The ray that was used to perform the picking.
  54. */
  55. public ray: Nullable<Ray> = null;
  56. /**
  57. * Gets the normal correspodning to the face the pick collided with
  58. * @param useWorldCoordinates If the resulting normal should be relative to the world (default: false)
  59. * @param useVerticesNormals If the vertices normals should be used to calculate the normal instead of the normal map
  60. * @returns The normal correspodning to the face the pick collided with
  61. */
  62. public getNormal(useWorldCoordinates = false, useVerticesNormals = true): Nullable<Vector3> {
  63. if (!this.pickedMesh || !this.pickedMesh.isVerticesDataPresent(VertexBuffer.NormalKind)) {
  64. return null;
  65. }
  66. var indices = this.pickedMesh.getIndices();
  67. if (!indices) {
  68. return null;
  69. }
  70. var result: Vector3;
  71. if (useVerticesNormals) {
  72. var normals = (<FloatArray>this.pickedMesh.getVerticesData(VertexBuffer.NormalKind));
  73. var normal0 = Vector3.FromArray(normals, indices[this.faceId * 3] * 3);
  74. var normal1 = Vector3.FromArray(normals, indices[this.faceId * 3 + 1] * 3);
  75. var normal2 = Vector3.FromArray(normals, indices[this.faceId * 3 + 2] * 3);
  76. normal0 = normal0.scale(this.bu);
  77. normal1 = normal1.scale(this.bv);
  78. normal2 = normal2.scale(1.0 - this.bu - this.bv);
  79. result = new Vector3(normal0.x + normal1.x + normal2.x, normal0.y + normal1.y + normal2.y, normal0.z + normal1.z + normal2.z);
  80. } else {
  81. var positions = (<FloatArray>this.pickedMesh.getVerticesData(VertexBuffer.PositionKind));
  82. var vertex1 = Vector3.FromArray(positions, indices[this.faceId * 3] * 3);
  83. var vertex2 = Vector3.FromArray(positions, indices[this.faceId * 3 + 1] * 3);
  84. var vertex3 = Vector3.FromArray(positions, indices[this.faceId * 3 + 2] * 3);
  85. var p1p2 = vertex1.subtract(vertex2);
  86. var p3p2 = vertex3.subtract(vertex2);
  87. result = Vector3.Cross(p1p2, p3p2);
  88. }
  89. if (useWorldCoordinates) {
  90. let wm = this.pickedMesh.getWorldMatrix();
  91. if (this.pickedMesh.nonUniformScaling) {
  92. Tmp.Matrix[0].copyFrom(wm);
  93. wm = Tmp.Matrix[0];
  94. wm.setTranslationFromFloats(0, 0, 0);
  95. wm.invert();
  96. wm.transposeToRef(Tmp.Matrix[1]);
  97. wm = Tmp.Matrix[1];
  98. }
  99. result = Vector3.TransformNormal(result, wm);
  100. }
  101. result.normalize();
  102. return result;
  103. }
  104. /**
  105. * Gets the texture coordinates of where the pick occured
  106. * @returns the vector containing the coordnates of the texture
  107. */
  108. public getTextureCoordinates(): Nullable<Vector2> {
  109. if (!this.pickedMesh || !this.pickedMesh.isVerticesDataPresent(VertexBuffer.UVKind)) {
  110. return null;
  111. }
  112. var indices = this.pickedMesh.getIndices();
  113. if (!indices) {
  114. return null;
  115. }
  116. var uvs = this.pickedMesh.getVerticesData(VertexBuffer.UVKind);
  117. if (!uvs) {
  118. return null;
  119. }
  120. var uv0 = Vector2.FromArray(uvs, indices[this.faceId * 3] * 2);
  121. var uv1 = Vector2.FromArray(uvs, indices[this.faceId * 3 + 1] * 2);
  122. var uv2 = Vector2.FromArray(uvs, indices[this.faceId * 3 + 2] * 2);
  123. uv0 = uv0.scale(1.0 - this.bu - this.bv);
  124. uv1 = uv1.scale(this.bu);
  125. uv2 = uv2.scale(this.bv);
  126. return new Vector2(uv0.x + uv1.x + uv2.x, uv0.y + uv1.y + uv2.y);
  127. }
  128. }