Texture lod计算

          #define SUB_TEXTURE_SIZE 512.0
          #define SUB_TEXTURE_MIPCOUNT 10
          
          float MipLevel( float2 uv )
          {
            float2 dx = ddx( uv * SUB_TEXTURE_SIZE );
            float2 dy = ddy( uv * SUB_TEXTURE_SIZE );
            float d = max( dot( dx, dx ), dot( dy, dy ) );
          
            // Clamp the value to the max mip level counts
            const float rangeClamp = pow(2, (SUB_TEXTURE_MIPCOUNT - 1) * 2);
            d = clamp(d, 1.0, rangeClamp);
                
            float mipLevel = 0.5 * log2(d);
            mipLevel = floor(mipLevel);   
            
            return mipLevel;
          }         

 

(注:mipmap计算,其中ddx可能对应opengl的dFdx,ddy对应opengl的dFdy。)

#define SUB_TEXTURE_SIZE 512.0
          #define SUB_TEXTURE_MIPCOUNT 10
          
          float MipLevel( float2 uv, out float a_halfOffset )
          {
            float2 dx = ddx( uv * SUB_TEXTURE_SIZE );
            float2 dy = ddy( uv * SUB_TEXTURE_SIZE );
            float d = max( dot( dx, dx ), dot( dy, dy ) );
            
            // Clamp the value to the max mip level counts
            const float rangeClamp = pow(2, SUB_TEXTURE_MIPCOUNT - 1);
            d = clamp(sqrt(d), 1.0, rangeClamp);
                
            float mipLevel = log2(d);
            mipLevel = floor(mipLevel);   
            
            a_halfOffset = d * (1.0 / pow(2.0, SUB_TEXTURE_MIPCOUNT));
            
            return mipLevel;
          }
          

 

          (计算half texel offset for selected mip level)
          
posted @ 2012-09-09 17:57  ActionFG  阅读(1192)  评论(0)    收藏  举报