why we use _MainText_ST and what does the mean of TRANSFORM_TEX

this is the same question.
i thank the website:
https://answers.unity.com/questions/33404/accessing-uv-offset-in-cg-shader.html

just post the following code to your shader file:

Shader "Course/MainTextureST" 
{
    Properties 
    {
        _MainTex ("Texture", 2D) = "white" { }
    }

    SubShader
    {
        pass    
        {
            CGPROGRAM

            #pragma vertex vert
            #pragma fragment frag
            #include "UnityCG.cginc"

            sampler2D _MainTex;
            float4 _MainTex_ST;

            struct v2f 
            {
                float4  pos : SV_POSITION;
                float2  uv : TEXCOORD0;
            };

            v2f vert (appdata_base v)
            {
                v2f o;
                o.pos = UnityObjectToClipPos(v.vertex);
                o.uv = v.texcoord.xy;
                //o.uv = v.texcoord.xy * _MainTex_ST.xy + _MainTex_ST.zw;
                return o;
            }

            float4 frag (v2f i) : COLOR
            {
                float4 texCol = tex2D(_MainTex,i.uv);
                float4 outp = texCol;
                return outp;
            }
            ENDCG
        }
    }
}

and change the value of the tiling and offset of the shader.
这里写图片描述

you find that the tiling/offset value can not affect the rendering effect.

and then ,you just comment the o.uv = v.texcoord.xy; and uncomment the next line.

//o.uv = v.texcoord.xy;
o.uv = v.texcoord.xy * _MainTex_ST.xy + _MainTex_ST.zw;

you will find, the tiling and offset value is valid. they can affect the rendering effect.
这里写图片描述

so, now, you can clearly understand why we using _MainText_ST and how to translate vertex’s original uv value when tiling and offset value of texture change.

you can also use a macro to replace the line:
o.uv = v.texcoord.xy * _MainTex_ST.xy + _MainTex_ST.zw;
that is :

//o.uv = v.texcoord.xy;
//o.uv = v.texcoord.xy * _MainTex_ST.xy + _MainTex_ST.zw;
o.uv = TRANSFORM_TEX(v.texcoord, _MainTex);

you can find the macro defined in the UnityCG.cginc file.

// Transforms 2D UV by scale/bias property
#define TRANSFORM_TEX(tex,name) (tex.xy * name##_ST.xy + name##_ST.zw)

so so , the ending comes.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值