如何将纹理放置在特定位置?

发布于 2025-01-30 13:42:32 字数 1587 浏览 3 评论 0 原文

在我的代码中,我要混合两个纹理。我想在平面上的任何位置都放置纹理,但是当我在纹理上添加偏移时,uv xy协调图像就会伸展。

offsetText1 = vec2(0.1,0.1);       
vec4 displacement  = texture2D(utexture1,vUv+offsetText1);

如何在不拉伸的情况下将质地移至任何位置?

顶点着色器:

  
  varying vec2 vUv;
  uniform sampler2D utexture1;
  uniform sampler2D utexture2;
  varying vec2 offsetText1;

  void main() {
      offsetText1 = vec2(0.1,0.1);
      vUv = uv;
      vec4 modelPosition = modelMatrix * vec4(position, 1.0);
      vec4 displacement  = texture2D(utexture1,vUv+offsetText1);
      vec4 displacement2 = texture2D(utexture2,vUv);
      modelPosition.z   += displacement.r*1.0;
      modelPosition.z   += displacement2.r*40.0;
      gl_Position       = projectionMatrix * viewMatrix * modelPosition;
  }
  

片段着色器:

  #ifdef GL_ES
  precision highp float;
  #endif
  uniform sampler2D utexture1;
  uniform sampler2D utexture2;
  varying vec2 vUv;
  varying vec2 offsetText1;

  void main() {
      vec3 c;
      vec4 Ca = texture2D(utexture1,vUv+offsetText1 );
      vec4 Cb = texture2D(utexture2,vUv);
      c       = Ca.rgb * Ca.a + Cb.rgb * Cb.a * (2.0 - Ca.a);
      gl_FragColor = vec4(c, 1.0);
  }
  

带有 offsetText1 = vec2(0.0,0.0)的图像; > no straptioning

带有 offsetText1 = vec2(0.1,0.1)的图像图像从右上角伸展。 拉伸

In my code, I'm mixing two textures. I want to position a texture at any place on the plane but when I add an offset to the texture UV XY coordinate the image just gets stretched.

offsetText1 = vec2(0.1,0.1);       
vec4 displacement  = texture2D(utexture1,vUv+offsetText1);

How do I move the texture to any position without stretching it?

VERTEX SHADER:

  
  varying vec2 vUv;
  uniform sampler2D utexture1;
  uniform sampler2D utexture2;
  varying vec2 offsetText1;

  void main() {
      offsetText1 = vec2(0.1,0.1);
      vUv = uv;
      vec4 modelPosition = modelMatrix * vec4(position, 1.0);
      vec4 displacement  = texture2D(utexture1,vUv+offsetText1);
      vec4 displacement2 = texture2D(utexture2,vUv);
      modelPosition.z   += displacement.r*1.0;
      modelPosition.z   += displacement2.r*40.0;
      gl_Position       = projectionMatrix * viewMatrix * modelPosition;
  }
  

FRAGMENT SHADER:

  #ifdef GL_ES
  precision highp float;
  #endif
  uniform sampler2D utexture1;
  uniform sampler2D utexture2;
  varying vec2 vUv;
  varying vec2 offsetText1;

  void main() {
      vec3 c;
      vec4 Ca = texture2D(utexture1,vUv+offsetText1 );
      vec4 Cb = texture2D(utexture2,vUv);
      c       = Ca.rgb * Ca.a + Cb.rgb * Cb.a * (2.0 - Ca.a);
      gl_FragColor = vec4(c, 1.0);
  }
  

image with offsetText1 = vec2(0.0,0.0);
no stretching

image with offsetText1 = vec2(0.1,0.1); image is being stretched from the top right corner.
stretching

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

心如狂蝶 2025-02-06 13:42:32

那就是纹理的行为。它们扩展到 [0,1] 的范围,因此,当您超过1或以下时,它们将“包装”。您需要告诉它在包装时该怎么办。您要它重复,伸展还是镜子?

您可以使用 texture.traps .wratp

THREE.RepeatWrapping
THREE.ClampToEdgeWrapping
THREE.MirroredRepeatWrapping

/code>和 在您的着色器代码中。这是一些伪代码:

if (uv < 0 || > 1)
   color = white

That's the behavior of textures. They extend in the range from [0, 1], so when you go beyond 1 or below 0, they'll "wrap". You need to tell it what to do when wrapping. Do you want it to repeat, stretch, or mirror?

You could establish this with the texture.wrapS and .wrapT properties, which accepts one of 3 values:

THREE.RepeatWrapping
THREE.ClampToEdgeWrapping
THREE.MirroredRepeatWrapping

If you want to just show white where the texture extends out of bounds, then you'd have to do that programmatically in your shader code. Here's some pseudocode:

if (uv < 0 || > 1)
   color = white
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文