subMesh.project.js 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. import { TmpVectors, Vector3 } from "../Maths/math.vector.js";
  2. import { SubMesh } from "./subMesh.js";
  3. /**
  4. * @internal
  5. */
  6. SubMesh.prototype._projectOnTrianglesToRef = function (vector, positions, indices, step, checkStopper, ref) {
  7. // Triangles test
  8. const proj = TmpVectors.Vector3[0];
  9. const tmp = TmpVectors.Vector3[1];
  10. let distance = +Infinity;
  11. for (let index = this.indexStart; index < this.indexStart + this.indexCount - (3 - step); index += step) {
  12. const indexA = indices[index];
  13. const indexB = indices[index + 1];
  14. const indexC = indices[index + 2];
  15. if (checkStopper && indexC === 0xffffffff) {
  16. index += 2;
  17. continue;
  18. }
  19. const p0 = positions[indexA];
  20. const p1 = positions[indexB];
  21. const p2 = positions[indexC];
  22. // stay defensive and don't check against undefined positions.
  23. if (!p0 || !p1 || !p2) {
  24. continue;
  25. }
  26. const tmpDist = Vector3.ProjectOnTriangleToRef(vector, p0, p1, p2, tmp);
  27. if (tmpDist < distance) {
  28. proj.copyFrom(tmp);
  29. distance = tmpDist;
  30. }
  31. }
  32. ref.copyFrom(proj);
  33. return distance;
  34. };
  35. /**
  36. * @internal
  37. */
  38. SubMesh.prototype._projectOnUnIndexedTrianglesToRef = function (vector, positions, indices, ref) {
  39. // Triangles test
  40. const proj = TmpVectors.Vector3[0];
  41. const tmp = TmpVectors.Vector3[1];
  42. let distance = +Infinity;
  43. for (let index = this.verticesStart; index < this.verticesStart + this.verticesCount; index += 3) {
  44. const p0 = positions[index];
  45. const p1 = positions[index + 1];
  46. const p2 = positions[index + 2];
  47. const tmpDist = Vector3.ProjectOnTriangleToRef(vector, p0, p1, p2, tmp);
  48. if (tmpDist < distance) {
  49. proj.copyFrom(tmp);
  50. distance = tmpDist;
  51. }
  52. }
  53. ref.copyFrom(proj);
  54. return distance;
  55. };
  56. SubMesh.prototype.projectToRef = function (vector, positions, indices, ref) {
  57. const material = this.getMaterial();
  58. if (!material) {
  59. return -1;
  60. }
  61. let step = 3;
  62. let checkStopper = false;
  63. switch (material.fillMode) {
  64. case 3:
  65. case 5:
  66. case 6:
  67. case 8:
  68. return -1;
  69. case 7:
  70. step = 1;
  71. checkStopper = true;
  72. break;
  73. default:
  74. break;
  75. }
  76. // LineMesh first as it's also a Mesh...
  77. if (material.fillMode === 4) {
  78. return -1;
  79. }
  80. else {
  81. // Check if mesh is unindexed
  82. if (!indices.length && this._mesh._unIndexed) {
  83. return this._projectOnUnIndexedTrianglesToRef(vector, positions, indices, ref);
  84. }
  85. return this._projectOnTrianglesToRef(vector, positions, indices, step, checkStopper, ref);
  86. }
  87. };
  88. //# sourceMappingURL=subMesh.project.js.map