babylon.pickingInfo.ts 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. module BABYLON {
  2. export class IntersectionInfo {
  3. public faceId = 0;
  4. public subMeshId = 0;
  5. constructor(public bu: Nullable<number>, public bv: Nullable<number>, public distance: number) {
  6. }
  7. }
  8. export class PickingInfo {
  9. public hit = false;
  10. public distance = 0;
  11. public pickedPoint: Nullable<Vector3> = null;
  12. public pickedMesh: Nullable<AbstractMesh> = null;
  13. public bu = 0;
  14. public bv = 0;
  15. public faceId = -1;
  16. public subMeshId = 0;
  17. public pickedSprite: Nullable<Sprite> = null;
  18. // Methods
  19. public getNormal(useWorldCoordinates = false, useVerticesNormals = true): Nullable<Vector3> {
  20. if (!this.pickedMesh || !this.pickedMesh.isVerticesDataPresent(VertexBuffer.NormalKind)) {
  21. return null;
  22. }
  23. var indices = this.pickedMesh.getIndices();
  24. if (!indices) {
  25. return null;
  26. }
  27. var result: Vector3;
  28. if (useVerticesNormals) {
  29. var normals = (<FloatArray>this.pickedMesh.getVerticesData(VertexBuffer.NormalKind));
  30. var normal0 = Vector3.FromArray(normals, indices[this.faceId * 3] * 3);
  31. var normal1 = Vector3.FromArray(normals, indices[this.faceId * 3 + 1] * 3);
  32. var normal2 = Vector3.FromArray(normals, indices[this.faceId * 3 + 2] * 3);
  33. normal0 = normal0.scale(this.bu);
  34. normal1 = normal1.scale(this.bv);
  35. normal2 = normal2.scale(1.0 - this.bu - this.bv);
  36. result = new Vector3(normal0.x + normal1.x + normal2.x, normal0.y + normal1.y + normal2.y, normal0.z + normal1.z + normal2.z);
  37. } else {
  38. var positions = (<FloatArray>this.pickedMesh.getVerticesData(VertexBuffer.PositionKind));
  39. var vertex1 = Vector3.FromArray(positions, indices[this.faceId * 3] * 3);
  40. var vertex2 = Vector3.FromArray(positions, indices[this.faceId * 3 + 1] * 3);
  41. var vertex3 = Vector3.FromArray(positions, indices[this.faceId * 3 + 2] * 3);
  42. var p1p2 = vertex1.subtract(vertex2);
  43. var p3p2 = vertex3.subtract(vertex2);
  44. result = BABYLON.Vector3.Cross(p1p2, p3p2);
  45. }
  46. if (useWorldCoordinates) {
  47. result = Vector3.TransformNormal(result, this.pickedMesh.getWorldMatrix());
  48. }
  49. return BABYLON.Vector3.Normalize(result);
  50. }
  51. public getTextureCoordinates(): Nullable<Vector2> {
  52. if (!this.pickedMesh || !this.pickedMesh.isVerticesDataPresent(VertexBuffer.UVKind)) {
  53. return null;
  54. }
  55. var indices = this.pickedMesh.getIndices();
  56. if (!indices) {
  57. return null;
  58. }
  59. var uvs = this.pickedMesh.getVerticesData(VertexBuffer.UVKind);
  60. if (!uvs) {
  61. return null;
  62. }
  63. var uv0 = Vector2.FromArray(uvs, indices[this.faceId * 3] * 2);
  64. var uv1 = Vector2.FromArray(uvs, indices[this.faceId * 3 + 1] * 2);
  65. var uv2 = Vector2.FromArray(uvs, indices[this.faceId * 3 + 2] * 2);
  66. uv0 = uv0.scale(1.0 - this.bu - this.bv);
  67. uv1 = uv1.scale(this.bu);
  68. uv2 = uv2.scale(this.bv);
  69. return new Vector2(uv0.x + uv1.x + uv2.x, uv0.y + uv1.y + uv2.y);
  70. }
  71. }
  72. }