ImgLabelBox.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. import * as THREE from "three";
  2. import { TriangleBlurShader } from "three/addons/shaders/TriangleBlurShader.js";
  3. import TouchEdge from "./TouchEdge";
  4. function makeTriangleBlurShader(iterations = 10) {
  5. // Remove texture, because texture is a reserved word in WebGL 2
  6. const { texture, ...uniforms } = TriangleBlurShader.uniforms;
  7. const TriangleBlurShader2 = {
  8. ...TriangleBlurShader,
  9. name: "TriangleBlurShader2",
  10. uniforms: {
  11. ...uniforms,
  12. // Replace texture with blurTexture for WebGL 2
  13. blurTexture: { value: null },
  14. },
  15. };
  16. // Replace texture with blurTexture for WebGL 2
  17. TriangleBlurShader2.fragmentShader =
  18. TriangleBlurShader2.fragmentShader.replace(
  19. "uniform sampler2D texture;",
  20. "uniform sampler2D blurTexture;"
  21. );
  22. TriangleBlurShader2.fragmentShader =
  23. TriangleBlurShader2.fragmentShader.replace(
  24. "texture2D( texture",
  25. "texture2D( blurTexture"
  26. );
  27. // Make iterations configurable.
  28. TriangleBlurShader2.fragmentShader =
  29. TriangleBlurShader2.fragmentShader.replace(
  30. "#define ITERATIONS 10.0",
  31. "#define ITERATIONS " + iterations + ".0"
  32. );
  33. console.log("shader:", TriangleBlurShader2.fragmentShader);
  34. return TriangleBlurShader2;
  35. }
  36. export default class ImgLabel extends THREE.Mesh {
  37. constructor(texture, matLine, isHorizontal = true) {
  38. let width, height, p;
  39. if (isHorizontal) {
  40. width = 1.5;
  41. height = 0.85;
  42. p = [
  43. [-0.75, 0, -0.425, 0.75, 0, -0.425],
  44. [-0.75, 0, -0.425, -0.75, 0, 0.425],
  45. [-0.75, 0, 0.425, 0.75, 0, 0.425],
  46. [0.75, 0, 0.425, 0.75, 0, -0.425],
  47. ];
  48. } else {
  49. width = 1.5;
  50. height = 2;
  51. p = [
  52. [-0.75, 0, -1, 0.75, 0, -1],
  53. [-0.75, 0, -1, -0.75, 0, 1],
  54. [-0.75, 0, 1, 0.75, 0, 1],
  55. [0.75, 0, 1, 0.75, 0, -1],
  56. ];
  57. }
  58. const g = new THREE.PlaneGeometry(width, height);
  59. g.rotateX(-Math.PI / 2);
  60. // const m = new THREE.MeshBasicMaterial({
  61. // map: texture,
  62. // transparent: true,
  63. // });
  64. // const shader = makeTriangleBlurShader(12);
  65. // const blurMaterial = new THREE.ShaderMaterial({
  66. // vertexShader: shader.vertexShader,
  67. // fragmentShader: shader.fragmentShader,
  68. // uniforms: THREE.UniformsUtils.clone(shader.uniforms),
  69. // });
  70. // // console.log("blurMaterial", blurMaterial.uniforms);
  71. // blurMaterial.uniforms.blurTexture.value = texture;
  72. // blurMaterial.uniforms.delta.value = new THREE.Vector2(0.5, 0.9);
  73. const bg = new THREE.MeshBasicMaterial({
  74. color: 0xf2f2f2,
  75. transparent: false,
  76. });
  77. super(g, bg);
  78. let imgRatio = texture.image.width / texture.image.height;
  79. const imgHeight = width / imgRatio >= 2 ? 2 : width / imgRatio;
  80. const imageG = new THREE.PlaneGeometry(width, imgHeight);
  81. imageG.rotateX(-Math.PI / 2);
  82. const im = new THREE.MeshBasicMaterial({
  83. map: texture,
  84. transparent: false,
  85. });
  86. const imageMesh = new THREE.Mesh(imageG, im);
  87. imageMesh.renderOrder = 10;
  88. this.add(imageMesh);
  89. this.width = width;
  90. this.height = height;
  91. this.touchLines = new TouchEdge(p, matLine);
  92. this.touchLines.position.y += 0.5;
  93. this.add(this.touchLines);
  94. // this.touchLines.children.forEach((child) => (child.visible = true));
  95. this.name = "imglabel";
  96. }
  97. }