Line.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. import * as THREE from "three";
  2. import { Line2 } from "three/examples/jsm/lines/Line2.js";
  3. import { LineGeometry } from "three/examples/jsm/lines/LineGeometry.js";
  4. import gotoPic from "@/assets/image/goto.png";
  5. const offset = 0.25;
  6. function pointsToArray(arr) {
  7. let res = [];
  8. arr.forEach((i) => {
  9. res = res.concat(i.toArray());
  10. });
  11. return res;
  12. }
  13. let m = new THREE.MeshBasicMaterial({
  14. map: new THREE.TextureLoader().load(gotoPic),
  15. color: 0x26559b,
  16. transparent: true,
  17. });
  18. export default class Line extends Line2 {
  19. constructor(startPoint, endPoint, endEdge, matLine) {
  20. let points;
  21. let g = new THREE.PlaneGeometry(0.1, 0.1);
  22. g.rotateX(-Math.PI / 2);
  23. let cross = new THREE.Mesh(g, m);
  24. if (endEdge.name === 0) {
  25. // top
  26. let a = startPoint.clone();
  27. let b = new THREE.Vector3(
  28. startPoint.x,
  29. startPoint.y,
  30. endEdge.y + endEdge.parent.parent.position.z - offset
  31. );
  32. let c = new THREE.Vector3(
  33. endPoint.x,
  34. endPoint.y,
  35. endEdge.y + endEdge.parent.parent.position.z - offset
  36. );
  37. let d = new THREE.Vector3(
  38. endPoint.x,
  39. endPoint.y,
  40. endEdge.y + endEdge.parent.parent.position.z
  41. );
  42. cross.position.copy(d);
  43. cross.rotation.y = -Math.PI / 2;
  44. cross.position.z -= 0.02;
  45. points = pointsToArray([a, b, c, d]);
  46. } else if (endEdge.name === 1) {
  47. //left
  48. let a = startPoint.clone();
  49. let b = new THREE.Vector3(
  50. (startPoint.x + endPoint.x) / 2,
  51. startPoint.y,
  52. startPoint.z
  53. );
  54. let c = new THREE.Vector3(
  55. (startPoint.x + endPoint.x) / 2,
  56. startPoint.y,
  57. endPoint.z
  58. );
  59. let d = new THREE.Vector3(
  60. endEdge.x + endEdge.parent.parent.parent.position.x,
  61. startPoint.y,
  62. endPoint.z
  63. );
  64. cross.position.copy(d);
  65. const diff = c.x < d.x;
  66. cross.rotation.y = diff ? 0 : -Math.PI;
  67. diff ? (cross.position.x -= 0.02) : (cross.position.x += 0.02);
  68. points = pointsToArray([a, b, c, d]);
  69. } else if (endEdge.name === 2) {
  70. //bottom
  71. let a = startPoint.clone();
  72. let b = new THREE.Vector3(
  73. startPoint.x,
  74. startPoint.y,
  75. endEdge.y + endEdge.parent.parent.position.z + offset
  76. );
  77. let c = new THREE.Vector3(
  78. endPoint.x,
  79. endPoint.y,
  80. endEdge.y + endEdge.parent.parent.position.z + offset
  81. );
  82. let d = new THREE.Vector3(
  83. endPoint.x,
  84. endPoint.y,
  85. endEdge.y + endEdge.parent.parent.position.z
  86. );
  87. cross.rotation.y = Math.PI / 2;
  88. cross.position.copy(d);
  89. cross.position.z += 0.02;
  90. points = pointsToArray([a, b, c, d]);
  91. } else {
  92. //right
  93. let a = startPoint.clone();
  94. let b = new THREE.Vector3(
  95. (startPoint.x + endPoint.x) / 2,
  96. startPoint.y,
  97. startPoint.z
  98. );
  99. let c = new THREE.Vector3(
  100. (startPoint.x + endPoint.x) / 2,
  101. startPoint.y,
  102. endPoint.z
  103. );
  104. let d = new THREE.Vector3(
  105. endEdge.x + endEdge.parent.parent.parent.position.x,
  106. startPoint.y,
  107. endPoint.z
  108. );
  109. const diff = c.x < d.x;
  110. cross.position.copy(d);
  111. cross.rotation.y = diff ? 0 : Math.PI;
  112. diff ? (cross.position.x -= 0.02) : (cross.position.x += 0.02);
  113. points = pointsToArray([a, b, c, d]);
  114. }
  115. const geometry = new LineGeometry();
  116. console.log("points", points);
  117. // console.log("endEdge", endEdge.name);
  118. geometry.setPositions(points);
  119. super(geometry, matLine);
  120. this.scale.set(1, 1, 1);
  121. this.position.y += 0.5;
  122. this.add(cross);
  123. }
  124. }