babylon.pickingInfo.ts 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. module BABYLON {
  2. export class IntersectionInfo {
  3. public faceId = 0;
  4. constructor(public bu: number, public bv: number, public distance: number) {
  5. }
  6. }
  7. export class PickingInfo {
  8. public hit = false;
  9. public distance = 0;
  10. public pickedPoint: Vector3 = null;
  11. public pickedMesh: AbstractMesh = null;
  12. public bu = 0;
  13. public bv = 0;
  14. public faceId = -1;
  15. // Methods
  16. public getNormal(): Vector3 {
  17. if (!this.pickedMesh || !this.pickedMesh.isVerticesDataPresent(BABYLON.VertexBuffer.NormalKind)) {
  18. return null;
  19. }
  20. var indices = this.pickedMesh.getIndices();
  21. var normals = this.pickedMesh.getVerticesData(BABYLON.VertexBuffer.NormalKind);
  22. var normal0 = BABYLON.Vector3.FromArray(normals, indices[this.faceId * 3] * 3);
  23. var normal1 = BABYLON.Vector3.FromArray(normals, indices[this.faceId * 3 + 1] * 3);
  24. var normal2 = BABYLON.Vector3.FromArray(normals, indices[this.faceId * 3 + 2] * 3);
  25. normal0 = normal0.scale(this.bu);
  26. normal1 = normal1.scale(this.bv);
  27. normal2 = normal2.scale(1.0 - this.bu - this.bv);
  28. return new BABYLON.Vector3(normal0.x + normal1.x + normal2.x, normal0.y + normal1.y + normal2.y, normal0.z + normal1.z + normal2.z);
  29. }
  30. public getTextureCoordinates(): Vector2 {
  31. if (!this.pickedMesh || !this.pickedMesh.isVerticesDataPresent(BABYLON.VertexBuffer.UVKind)) {
  32. return null;
  33. }
  34. var indices = this.pickedMesh.getIndices();
  35. var uvs = this.pickedMesh.getVerticesData(BABYLON.VertexBuffer.UVKind);
  36. var uv0 = BABYLON.Vector2.FromArray(uvs, indices[this.faceId * 3] * 2);
  37. var uv1 = BABYLON.Vector2.FromArray(uvs, indices[this.faceId * 3 + 1] * 2);
  38. var uv2 = BABYLON.Vector2.FromArray(uvs, indices[this.faceId * 3 + 2] * 2);
  39. uv0 = uv0.scale(this.bu);
  40. uv1 = uv1.scale(this.bv);
  41. uv2 = uv2.scale(1.0 - this.bu - this.bv);
  42. return new BABYLON.Vector2(uv0.x + uv1.x + uv2.x, uv0.y + uv1.y + uv2.y);
  43. }
  44. }
  45. }