babylon.pickingInfo.ts 5.8 KB

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