babylon.pickingInfo.js 3.7 KB

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