HorizontalBox.js 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. import * as THREE from "three";
  2. import TextLabel from "./object/TextLabel";
  3. import SimpleLabel from "./object/SimpleLabel";
  4. import ImgLabel from "./object/ImgLabel";
  5. import { LineMaterial } from "three/examples/jsm/lines/LineMaterial.js";
  6. export default class HorizontalBox extends THREE.Group {
  7. constructor(manager, data, index, total) {
  8. super();
  9. this.manager = manager;
  10. this.name = "horizontal_box";
  11. this.total = total;
  12. this.getStyle();
  13. this.load(data, index);
  14. }
  15. getStyle() {
  16. this.width = 2;
  17. this.height = (2 * 710) / 500;
  18. this.color = 0xffffff;
  19. }
  20. load(data, index) {
  21. //box
  22. const geometry = new THREE.PlaneGeometry(1, 1);
  23. geometry.rotateX(-Math.PI / 2);
  24. const bm = new THREE.MeshBasicMaterial({
  25. color: this.color,
  26. });
  27. const box = new THREE.Mesh(geometry, bm);
  28. box.scale.set(this.width, 1, this.height);
  29. this.add(box);
  30. this.position.x = (this.width + 0.125) * index;
  31. const matLine = new LineMaterial({
  32. color: 0xe44d54,
  33. linewidth: 3, // in world units with size attenuation, pixels otherwise
  34. dashed: false,
  35. alphaToCoverage: true,
  36. });
  37. matLine.resolution = new THREE.Vector2(
  38. this.manager.scene.width,
  39. this.manager.scene.height
  40. );
  41. //content
  42. data.forEach((i, j) => {
  43. //img
  44. let img;
  45. this.manager.loader.load(i.imgUrl, (texture) => {
  46. texture.colorSpace = THREE.SRGBColorSpace;
  47. img = new ImgLabel(texture, matLine);
  48. img.userData = i.id;
  49. img.position.y += 1;
  50. if (j === 0) {
  51. img.position.z -= 0.8;
  52. } else {
  53. img.position.z += 0.43;
  54. }
  55. this.add(img);
  56. this.manager.imgList.push(img);
  57. const textlabel = new TextLabel(i.imgInfo, true);
  58. this.add(textlabel);
  59. textlabel.position.copy(img.position);
  60. textlabel.position.z += textlabel.scale.z * 0.5 + 0.1;
  61. });
  62. });
  63. //页脚
  64. const f_txt_label = ` 第 ${index + 1} 页 共 ${this.total} 页`;
  65. const footlabel = new SimpleLabel(f_txt_label, true);
  66. footlabel.renderOrder = 100;
  67. footlabel.position.z += 1.26;
  68. this.add(footlabel);
  69. }
  70. }