How does one access the "Tiling" and "Offset" values set on a Texture in a Cg shader? I assume it's set somewhere, but I can't find documentation about it.
Inside the UnityCG.cginc, you will find a macro:
// Transforms 2D UV by scale/bias property
#define TRANSFORM_TEX(tex,name) (tex.xy * name##_ST.xy + name##_ST.zw)
Where the first parameter is the texcoord and the second parameter is the texture:
Example:
TRANSFORM_TEX(v.texcoord, _MainTex);
I’ve made an example, there are two lines that are of interest, Ive marked them with “LOOK”
//Flat color texture, does not support alpha channels
Shader "Viking/FlatAdv"
{
Properties
{
_MainTex ( "Main Texture", 2D ) = "white" {}
}
SubShader
{
Tags { "Queue" = "Geometry" }
ZWrite On
Blend SrcAlpha OneMinusSrcAlpha
Pass
{
CGPROGRAM
#pragma exclude_renderers ps3 xbox360 flash
#pragma fragmentoption ARB_precision_hint_fastest
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
// uniforms
uniform sampler2D _MainTex;
//LOOK! The texture name + ST is needed to get the tiling/offset
uniform float4 _MainTex_ST;
struct vertexInput
{
float4 vertex : POSITION;
float4 texcoord : TEXCOORD0;
};
struct fragmentInput
{
float4 pos : SV_POSITION;
half2 uv : TEXCOORD0;
};
fragmentInput vert( vertexInput i )
{
fragmentInput o;
o.pos = mul( UNITY_MATRIX_MVP, i.vertex );
//This is a standard defined function in Unity,
//Does exactly the same as the next line of code
//o.uv = TRANSFORM_TEX( i.texcoord, _MainTex );
//LOOK! _MainTex_ST.xy is tiling and _MainTex_ST.zw is offset
o.uv = i.texcoord.xy * _MainTex_ST.xy + _MainTex_ST.zw;
return o;
}
half4 frag( fragmentInput i ) : COLOR
{
return half4( tex2D( _MainTex, i.uv ).rgb, 1.0);
}
ENDCG
} // end Pass
} // end SubShader
FallBack "Diffuse"
}
Hi, I think this will be to late for an answer but if you are interested you can try this:
// this is the main form to access the tile (scale) and offset in a texture property.
renderer.material.SetTextureOffset("_MainTex", Vector2(.25,.15));
renderer.material.SetTextureScale("_MainTex", Vector2(2,3));
This use the properties in the shader no matter it’s a CG or not, with you can manipulate it using a simple script, you can check this to see how it’s applied. ;-), the previous answer is to use it inside the shader.
This is a bit old, I guess, but if you’re still looking, there’s some information in:
http://unity3d.com/support/documentation/Manual/ShaderTut2.html
There’s a function in UnityCG.cginc that handles the offset and scaling, so going through that would be a good place to start.