如何在忽略其他纹理的情况下在 GLSL 中缩放纹理图集上的特定纹理?例如,假设我有一个包含 16 个纹理的纹理 Atlas。
我用这个公式找到了我需要的质地:
float scale = 1.0/0.25;
vec2 idx = vec2(1.0,2.0);
vec2 newUvForTexture = (vUv+idx)*scale;
vec4 tex = texture2D(texArray[0],newUvForTexture);
(1.0,2.0)处的纹理:
一旦我有了合适的 UV (newUvForTexture),我需要缩小它而不显示其他纹理。有人可以提供一个 GLSL 代码示例来说明如何完成此操作吗?
clamp()
glsl 函数 来阻止 UV 超出所需的最小/最大值:
float scale = 1.0/0.25;
vec2 idx = vec2(1.0,2.0);
vec2 newUvForTexture = (vUv+idx) * scale;
// Get minimum & maximum limits of UVs
vec2 min = vec2(idx.x + 0.0, idx.y + 0.0) * scale;
vec2 max = vec2(idx.x + 1.0, idx.y + 1.0) * scale;
// Limit the range of UVs with clamp()
vec2 clampedUVs = clamp(newUvForTexture, min, max);
// Sample texture with clamped UVs.
vec4 tex = texture2D(texArray[0], clampedUVs);
您可能可以通过仅执行一次
* scale
乘法来进一步优化它,但为了清晰起见,这样它最接近您的原始代码。