babylon.pickingInfo.js 3.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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. this.pickedSprite = null;
  25. }
  26. // Methods
  27. PickingInfo.prototype.getNormal = function (useWorldCoordinates, useVerticesNormals) {
  28. if (useWorldCoordinates === void 0) { useWorldCoordinates = false; }
  29. if (useVerticesNormals === void 0) { useVerticesNormals = true; }
  30. if (!this.pickedMesh || !this.pickedMesh.isVerticesDataPresent(BABYLON.VertexBuffer.NormalKind)) {
  31. return null;
  32. }
  33. var indices = this.pickedMesh.getIndices();
  34. var result;
  35. if (useVerticesNormals) {
  36. var normals = this.pickedMesh.getVerticesData(BABYLON.VertexBuffer.NormalKind);
  37. var normal0 = BABYLON.Vector3.FromArray(normals, indices[this.faceId * 3] * 3);
  38. var normal1 = BABYLON.Vector3.FromArray(normals, indices[this.faceId * 3 + 1] * 3);
  39. var normal2 = BABYLON.Vector3.FromArray(normals, indices[this.faceId * 3 + 2] * 3);
  40. normal0 = normal0.scale(this.bu);
  41. normal1 = normal1.scale(this.bv);
  42. normal2 = normal2.scale(1.0 - this.bu - this.bv);
  43. result = new BABYLON.Vector3(normal0.x + normal1.x + normal2.x, normal0.y + normal1.y + normal2.y, normal0.z + normal1.z + normal2.z);
  44. }
  45. else {
  46. var positions = this.pickedMesh.getVerticesData(BABYLON.VertexBuffer.PositionKind);
  47. var vertex1 = BABYLON.Vector3.FromArray(positions, indices[this.faceId * 3] * 3);
  48. var vertex2 = BABYLON.Vector3.FromArray(positions, indices[this.faceId * 3 + 1] * 3);
  49. var vertex3 = BABYLON.Vector3.FromArray(positions, indices[this.faceId * 3 + 2] * 3);
  50. var p1p2 = vertex1.subtract(vertex2);
  51. var p3p2 = vertex3.subtract(vertex2);
  52. result = BABYLON.Vector3.Cross(p1p2, p3p2);
  53. }
  54. if (useWorldCoordinates) {
  55. result = BABYLON.Vector3.TransformNormal(result, this.pickedMesh.getWorldMatrix());
  56. }
  57. return BABYLON.Vector3.Normalize(result);
  58. };
  59. PickingInfo.prototype.getTextureCoordinates = function () {
  60. if (!this.pickedMesh || !this.pickedMesh.isVerticesDataPresent(BABYLON.VertexBuffer.UVKind)) {
  61. return null;
  62. }
  63. var indices = this.pickedMesh.getIndices();
  64. var uvs = this.pickedMesh.getVerticesData(BABYLON.VertexBuffer.UVKind);
  65. var uv0 = BABYLON.Vector2.FromArray(uvs, indices[this.faceId * 3] * 2);
  66. var uv1 = BABYLON.Vector2.FromArray(uvs, indices[this.faceId * 3 + 1] * 2);
  67. var uv2 = BABYLON.Vector2.FromArray(uvs, indices[this.faceId * 3 + 2] * 2);
  68. uv0 = uv0.scale(1.0 - this.bu - this.bv);
  69. uv1 = uv1.scale(this.bu);
  70. uv2 = uv2.scale(this.bv);
  71. return new BABYLON.Vector2(uv0.x + uv1.x + uv2.x, uv0.y + uv1.y + uv2.y);
  72. };
  73. return PickingInfo;
  74. })();
  75. BABYLON.PickingInfo = PickingInfo;
  76. })(BABYLON || (BABYLON = {}));
  77. //# sourceMappingURL=babylon.pickingInfo.js.map