MirrorShader.js 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. /**
  2. * @module MirrorShader
  3. * @three_import import { MirrorShader } from 'three/addons/shaders/MirrorShader.js';
  4. */
  5. /**
  6. * Copies half the input to the other half.
  7. *
  8. * side: side of input to mirror (0 = left, 1 = right, 2 = top, 3 = bottom).
  9. *
  10. * @constant
  11. * @type {ShaderMaterial~Shader}
  12. */
  13. const MirrorShader = {
  14. name: 'MirrorShader',
  15. uniforms: {
  16. 'tDiffuse': { value: null },
  17. 'side': { value: 1 }
  18. },
  19. vertexShader: /* glsl */`
  20. varying vec2 vUv;
  21. void main() {
  22. vUv = uv;
  23. gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );
  24. }`,
  25. fragmentShader: /* glsl */`
  26. uniform sampler2D tDiffuse;
  27. uniform int side;
  28. varying vec2 vUv;
  29. void main() {
  30. vec2 p = vUv;
  31. if (side == 0){
  32. if (p.x > 0.5) p.x = 1.0 - p.x;
  33. }else if (side == 1){
  34. if (p.x < 0.5) p.x = 1.0 - p.x;
  35. }else if (side == 2){
  36. if (p.y < 0.5) p.y = 1.0 - p.y;
  37. }else if (side == 3){
  38. if (p.y > 0.5) p.y = 1.0 - p.y;
  39. }
  40. vec4 color = texture2D(tDiffuse, p);
  41. gl_FragColor = color;
  42. }`
  43. };
  44. export { MirrorShader };