LinePoints.js 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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 LinePoints extends Line2 {
  19. constructor(points, dir, matLine) {
  20. let g = new THREE.PlaneGeometry(0.1, 0.1);
  21. g.rotateX(-Math.PI / 2);
  22. let cross = new THREE.Mesh(g, m);
  23. let a = new THREE.Vector3(points[0], points[1], points[2]);
  24. let b = new THREE.Vector3(points[3], points[4], points[5]);
  25. let c = new THREE.Vector3(points[6], points[7], points[8]);
  26. let d = new THREE.Vector3(points[9], points[10], points[11]);
  27. let diff;
  28. switch (dir) {
  29. case 0:
  30. //top
  31. cross.rotation.y = -Math.PI / 2;
  32. cross.position.copy(d);
  33. break;
  34. case 1:
  35. //left
  36. diff = c.x < d.x;
  37. cross.rotation.y = diff ? 0 : -Math.PI;
  38. diff ? (cross.position.x -= 0.02) : (cross.position.x += 0.02);
  39. break;
  40. case 2:
  41. //bottom
  42. cross.position.copy(d);
  43. cross.rotation.y = Math.PI / 2;
  44. cross.position.z += 0.02;
  45. break;
  46. case 3:
  47. //right
  48. diff = c.x < d.x;
  49. cross.position.copy(d);
  50. cross.rotation.y = diff ? 0 : Math.PI;
  51. diff ? (cross.position.x -= 0.02) : (cross.position.x += 0.02);
  52. break;
  53. }
  54. const geometry = new LineGeometry();
  55. cross.visible = false;
  56. // console.log("points", points);
  57. geometry.setPositions(points);
  58. super(geometry, matLine);
  59. this.name = "line_point_" + this.uuid;
  60. this.scale.set(1, 1, 1);
  61. this.position.y += 0.5;
  62. this.add(cross);
  63. }
  64. }