问题记录:Shaderlab 在获取uv坐标时发现不是从0~1,最后发现是图集问题!!!
由于图片位于大图中,坐标变成了一个较小的范围
解决方式:
转化图片在图集中的uv坐标(谷歌大法好)
原文 forum.unity.com/threads/acc…
1,shader转化坐标计算
float4 atlasOffsetScale = _AtlasPosition * _MainTex_TexelSize.xyxy;
fixed2 fixUV = (IN.texcoord - atlasOffsetScale.xy) / atlasOffsetScale.zw;
2,代码中传入图片在图集中的位置和大小
local result =
Vector4(
self.m_spProgress.sprite.textureRect.position.x,
self.m_spProgress.sprite.textureRect.position.y,
self.m_spProgress.sprite.textureRect.size.x,
self.m_spProgress.sprite.textureRect.size.y
)
self.m_spProgress.material:SetVector("_AtlasPosition", result)
其中_AtlasPosition为图片在图集中的位置和大小
- 1 传入到shader中与纹素相乘得到 在大图中的标准坐标 (和尺寸)
- 2 用传入的在本像素的uv坐标减去xy得到在小图中剩余的尺寸
- 3 除以尺寸得到正确的纹理坐标