HorizontalBox.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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 - 1.8;
  39. const matLine = new LineMaterial({
  40. color: 0xe44d54,
  41. linewidth: 4, // 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. texture.matrixAutoUpdate = false;
  56. let planeRatio = 1.5 / 0.85;
  57. // let ratio = planeRatio / imgRatio;
  58. texture.matrixAutoUpdate = false;
  59. if (planeRatio < imgRatio) {
  60. texture.matrix.setUvTransform(
  61. 0,
  62. 0,
  63. planeRatio / imgRatio,
  64. 1,
  65. 0,
  66. 0.5,
  67. 0.5
  68. );
  69. } else {
  70. texture.matrix.setUvTransform(
  71. 0,
  72. 0,
  73. 1,
  74. imgRatio / planeRatio,
  75. 0,
  76. 0.5,
  77. 0.5
  78. );
  79. }
  80. // texture.wrapS = THREE.RepeatWrapping;
  81. // texture.wrapS = THREE.RepeatWrapping;
  82. // texture.wrapT = THREE.ClampToEdgeWrapping;
  83. // texture.repeat.x = ratio;
  84. // texture.offset.x = 0.5 * (1 - ratio);
  85. // console.log("texture", texture);
  86. texture.colorSpace = THREE.SRGBColorSpace;
  87. img = new ImgLabel(texture, matLine);
  88. img.userData = i.id;
  89. img.position.y += 1;
  90. if (j === 0) {
  91. img.position.z -= 0.8;
  92. } else {
  93. img.position.z += 0.43;
  94. }
  95. this.add(img);
  96. this.manager.imgList.push(img);
  97. const textlabel = new TextLabel(i.imgInfo, true);
  98. this.add(textlabel);
  99. textlabel.position.copy(img.position);
  100. textlabel.position.z += textlabel.scale.z * 0.5 + 0.1;
  101. });
  102. });
  103. //页脚
  104. const f_txt_label = ` 第 ${index + 1} 页 共 ${this.total} 页`;
  105. const footlabel = new SimpleLabel(f_txt_label, true);
  106. footlabel.renderOrder = 100;
  107. footlabel.position.z += 1.26;
  108. this.add(footlabel);
  109. }
  110. }