Label.js 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. import * as THREE from "three";
  2. import convertTool from "@/utils";
  3. let labels = []
  4. class Label2D {
  5. constructor(o = {}) {
  6. this.position = o.position
  7. this.elem = window.$(o.innerHTML || '<div ><a></a></div>');
  8. window.$(o.domElement).append(this.elem)
  9. this.pos2d = new THREE.Vector3
  10. this.elem.css({ position: 'absolute', 'z-index': 999 })
  11. this.clickFun = o.clickFun;
  12. this.clickFun && this.elem.on('click', this.clickFun.bind(this))
  13. this.visible = true
  14. this.camera = o.camera
  15. this.domElement = o.domElement
  16. this.modelobj = o.modelobj
  17. this.shelterByModel = o.shelterByModel
  18. labels.push(this)
  19. this.init()
  20. }
  21. init() {
  22. this.update()
  23. }
  24. update() {
  25. if (!this.position || !this.visible) return
  26. var p = convertTool.getPos2d(this.position, this.camera, this.domElement);
  27. if (!p || !p.trueSide) {
  28. this.elem.css('display', 'none'); return;
  29. }
  30. //判断label是否被模型遮挡,遮挡则消失(如果是漫游模式最好提前计算visiblePanos)
  31. if (this.shelterByModel && convertTool.ifShelter(this.position, p.vector, this.camera, this.modelobj,this.domElement)) {
  32. this.elem.css('display', 'none'); return;
  33. }
  34. this.elem.css({
  35. left: p.pos.x + 'px',
  36. top: p.pos.y + 'px'
  37. })
  38. this.elem.css('display', 'block');
  39. this.pos2d = p.vector;
  40. }
  41. setVisible(visi, reason, level = 0, type) {
  42. convertTool.updateVisible(this, reason, visi, level, type)
  43. if (!this.visible) {
  44. this.elem.css('display', 'none');
  45. } else {
  46. this.update()
  47. }
  48. }
  49. setPos(pos) {
  50. this.position = pos;
  51. this.update()
  52. }
  53. dispose() {
  54. this.elem.remove();
  55. this._listeners = {}
  56. this.dispatchEvent({ type: 'dispose' })
  57. let index = labels.indexOf(this)
  58. index > -1 && labels.splice(index, 1)
  59. }
  60. }
  61. export default Label2D