babylon.pickingInfo.ts 2.3 KB

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