现在的位置: 首页 > 综合 > 正文

纹理拼接后的Wrap寻址

2013年06月01日 ⁄ 综合 ⁄ 共 3495字 ⁄ 字号 评论关闭

http://blog.csdn.net/xoyojank/article/details/3212425

 

 

拼接后的纹理:

正常的草地(不进行WRAP寻址):

WRAP = 5时的情况:

MinFilter = Linear时的情况:

shader实现:

  1. sampler2D atlasTexture;  
  2. float4 texRect;   //(left, top, width, height) of uv
      
  3. float2 invSize;   //(1/width, 1/height)
      
  4. float4 uvRange;   //(left, top, right, bottom) of uv
      
  5. float4 ps_main( float2 tex : TEXCOORD0 ) : COLOR  
  6. {  
  7.    // convert original uv to atlas uv's unit
      
  8.    tex *= texRect.zw;  
  9.      
  10.    float2 uv = tex - texRect.xy;  
  11.    // bring coordinates into normalized local texture coord [0..1]
      
  12.    uv *= invSize;  
  13.    // if texture repeats then coords are > 1, use frc to bring 
      
  14.    // these coords back into [0, 1) interval.
      
  15.    uv = frac(uv);  
  16.    // transform coords back to texture atlas coords
      
  17.    uv = uv * texRect.zw + texRect.xy;  
  18.    // clamp to inside texture (to avoid bi-linear filter pulling in foreign texels)
      
  19.    uv = clamp(uv, uvRange.xy, uvRange.zw);  
  20.    // use the original coords for mip-map calculation
      
  21.    return tex2Dgrad(atlasTexture, uv, ddx(tex), ddy(tex));  
  22. }  

 

Reference:

ShaderX3 : Improved Batching via Texture Atlases

拼接后的纹理:

正常的草地(不进行WRAP寻址):

WRAP = 5时的情况:

MinFilter = Linear时的情况:

shader实现:

  1. sampler2D atlasTexture;  
  2. float4 texRect;   //(left, top, width, height) of uv
      
  3. float2 invSize;   //(1/width, 1/height)
      
  4. float4 uvRange;   //(left, top, right, bottom) of uv
      
  5. float4 ps_main( float2 tex : TEXCOORD0 ) : COLOR  
  6. {  
  7.    // convert original uv to atlas uv's unit
      
  8.    tex *= texRect.zw;  
  9.      
  10.    float2 uv = tex - texRect.xy;  
  11.    // bring coordinates into normalized local texture coord [0..1]
      
  12.    uv *= invSize;  
  13.    // if texture repeats then coords are > 1, use frc to bring 
      
  14.    // these coords back into [0, 1) interval.
      
  15.    uv = frac(uv);  
  16.    // transform coords back to texture atlas coords
      
  17.    uv = uv * texRect.zw + texRect.xy;  
  18.    // clamp to inside texture (to avoid bi-linear filter pulling in foreign texels)
      
  19.    uv = clamp(uv, uvRange.xy, uvRange.zw);  
  20.    // use the original coords for mip-map calculation
      
  21.    return tex2Dgrad(atlasTexture, uv, ddx(tex), ddy(tex));  
  22. }  

 

Reference:

ShaderX3 : Improved Batching via Texture Atlases

抱歉!评论已关闭.