HorizontalBox.js 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. cover(texture, aspect) {
  21. var imageAspect = texture.image.width / texture.image.height;
  22. if (aspect < imageAspect) {
  23. texture.matrix.setUvTransform(0, 0, aspect / imageAspect, 1, 0, 0.5, 0.5);
  24. } else {
  25. texture.matrix.setUvTransform(0, 0, 1, imageAspect / aspect, 0, 0.5, 0.5);
  26. }
  27. }
  28. load(data, index) {
  29. //box
  30. const geometry = new THREE.PlaneGeometry(1, 1);
  31. geometry.rotateX(-Math.PI / 2);
  32. const bm = new THREE.MeshBasicMaterial({
  33. color: this.color,
  34. });
  35. const box = new THREE.Mesh(geometry, bm);
  36. box.scale.set(this.width, 1, this.height);
  37. this.add(box);
  38. this.position.x = (this.width + 0.125) * index - 2.2;
  39. const matLine = new LineMaterial({
  40. color: 0xe44d54,
  41. linewidth: 3, // in world units with size attenuation, pixels otherwise
  42. dashed: false,
  43. alphaToCoverage: true,
  44. });
  45. matLine.resolution = new THREE.Vector2(
  46. this.manager.scene.width,
  47. this.manager.scene.height
  48. );
  49. //content
  50. data.forEach((i, j) => {
  51. //img
  52. let img;
  53. this.manager.loader.load(i.imgUrl, (texture) => {
  54. let imgRatio = texture.image.width / texture.image.height;
  55. let planeRatio = 1.5 / 0.85;
  56. let ratio = planeRatio / imgRatio;
  57. texture.repeat.x = ratio;
  58. texture.offset.x = 0.5 * (1 - ratio);
  59. // console.log("texture", texture);
  60. texture.colorSpace = THREE.SRGBColorSpace;
  61. img = new ImgLabel(texture, matLine);
  62. img.userData = i.id;
  63. img.position.y += 1;
  64. if (j === 0) {
  65. img.position.z -= 0.8;
  66. } else {
  67. img.position.z += 0.43;
  68. }
  69. this.add(img);
  70. this.manager.imgList.push(img);
  71. const textlabel = new TextLabel(i.imgInfo, true);
  72. this.add(textlabel);
  73. textlabel.position.copy(img.position);
  74. textlabel.position.z += textlabel.scale.z * 0.5 + 0.1;
  75. });
  76. });
  77. //页脚
  78. const f_txt_label = ` 第 ${index + 1} 页 共 ${this.total} 页`;
  79. const footlabel = new SimpleLabel(f_txt_label, true);
  80. footlabel.renderOrder = 100;
  81. footlabel.position.z += 1.26;
  82. this.add(footlabel);
  83. }
  84. }