babylon.pickingInfo.ts 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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(): 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. return new Vector3(normal0.x + normal1.x + normal2.x, normal0.y + normal1.y + normal2.y, normal0.z + normal1.z + normal2.z);
  31. }
  32. public getTextureCoordinates(): Vector2 {
  33. if (!this.pickedMesh || !this.pickedMesh.isVerticesDataPresent(VertexBuffer.UVKind)) {
  34. return null;
  35. }
  36. var indices = this.pickedMesh.getIndices();
  37. var uvs = this.pickedMesh.getVerticesData(VertexBuffer.UVKind);
  38. var uv0 = Vector2.FromArray(uvs, indices[this.faceId * 3] * 2);
  39. var uv1 = Vector2.FromArray(uvs, indices[this.faceId * 3 + 1] * 2);
  40. var uv2 = Vector2.FromArray(uvs, indices[this.faceId * 3 + 2] * 2);
  41. uv0 = uv0.scale(this.bu);
  42. uv1 = uv1.scale(this.bv);
  43. uv2 = uv2.scale(1.0 - this.bu - this.bv);
  44. return new Vector2(uv0.x + uv1.x + uv2.x, uv0.y + uv1.y + uv2.y);
  45. }
  46. }
  47. }