OpenGL 中的贴纸/贴花:绘制没有从 CLAMP_TO_EDGE 拉伸的纹理?
基本上是想在飞机上贴一张贴纸。纹理图像显示在平面上(可以四处移动),但我不想将图像拉伸到边缘或重复它。如何才能像贴纸一样将图像贴在墙上?我正在使用着色器,不知道这是否是我解决问题的地方。想象一下棋盘是我的图像,这就是发生的情况:
Basically trying to make a sticker on a plane. The texture image shows up on the plane(can move around) but I don't want to stretch the image to edge or repeat it. How can I just have the image on the wall like a sticker? I'm using shaders, don't know if that's where I tackle the problem. Imagine a checkerboard is my image, this is what happens:
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以将范围检查合并到着色器中。设置clamp_to_edge仅影响从采样返回的像素,因此当您位于不想输出任何像素的区域时,进入着色器的纹理坐标仍将超出从0到1的范围。在当今 GL ES 常用的硬件上,传递 alpha 为 0 的颜色比丢弃颜色更快,因此如果您的混合模式允许,请这样做。
例如,假设您当前只有最简单的纹理片段着色器:
您可以改为:
但是,显式范围检查将增加处理成本。显然,只有当它成为问题时才担心,但这种情况下的解决方案只是在纹理内添加边框。因此,如果您的纹理是 256x256,那么您将使用中央 254x254 像素,并在外部放置完全透明的边框。
编辑:更清楚地思考,如果 alpha 而不是丢弃是可以接受的,那么您可以使用步骤函数来消除条件。重复四次 - 每个轴上的每个限制一次,反转您想要大于测试而不是小于测试的结果,然后将输出 alpha 乘以所有四个结果。如果任何一个为零,则输出 alpha 将为零。
You could incorporate a range check into your shader. Setting clamp_to_edge affects what pixels you get back from sampling only, so the texture coordinates coming into your shader will still be outside of the range from zero to one when you're in the region where you don't want to output any pixels. On the hardware commonly used for GL ES today, it's faster to pass on a colour with an alpha of 0 than to discard, so do that if your blend mode allows it.
E.g. supposing you currently have just the most trivial textured fragment shader:
You could instead go with:
However, an explicit range check is going to add processing cost. Obviously worry about that only if it becomes an issue, but the solution in that case is just to add a border inside your texture. So if your texture is 256x256 then you'd use the central 254x254 pixels and put a completely transparent border around the outside.
EDIT: thinking more clearly, if alpha rather than discard is acceptable then you could use the step function to eliminate the conditional. Do it four times over - once for each limit on each axis, invert the results where you want a greater than test rather than a less than, then multiply the output alpha by all four results. If any one is zero, the output alpha will be zero.
请改用 CLAMP_TO_BORDER 并且不要忘记设置边框颜色。
Use CLAMP_TO_BORDER instead and don't forget to set the border color.