KITASENJU DESIGN BLOG

memo, html, javascript, unity

how to access _MainTex_ST

How to get tile/offset information about texture in the surface shader? - Unity Forum

Shader "Custom/ST access"
{
    Properties
    {
        _MainTex ("Albedo (RGB)", 2D) = "white" {}
    }
    SubShader
    {
        Tags { "RenderType"="Opaque" }
 
        CGPROGRAM
        #pragma surface surf Standard vertex:vert
        #pragma target 3.0
 
        sampler2D _MainTex;
        float4 _MainTex_ST;
 
        struct Input
        {
            float2 custom_uv; // cannot start with "uv"
        };
 
        void vert (inout appdata_full v, out Input o)
        {
            // copy the unmodified texture coordinates (aka UVs)
            o.custom_uv = v.texcoord.xy;
        }
 
        void surf (Input IN, inout SurfaceOutputStandard o)
        {
            // apply the tiling and offset (_MainTex_ST) you would normally get from using uv_MainTex
            float2 uv = TRANSFORM_TEX(IN.custom_uv, _MainTex);
            fixed4 c = tex2D (_MainTex, uv);
 
            // access _MainTex_ST directly
            fixed4 c2 = tex2D (_MainTex, uv + _MainTex_ST.xy);
            o.Albedo = c.rgb * c2.rgb;
        }
        ENDCG
    }
    FallBack "Diffuse"
}
"FOOTER"