- Home /
Accessing uv offset in Cg shader
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.
Don't take my word for it, but I don't think there is a way. It's probably something that happens internally before (and possibly after) the vertex shader. I'd just pass those as separate parameters, that's how I do it in other shading languages. Or you could probably do multiple passes and record the Input.uv values in the 1st pass... if you really need those tiling/offset values for whatever reason.
Hrm. If they used the offset to modify the texture matrix, then my scrolling effect via the offset should be visible, but it isn't. I figured they were using the offset to modify the UVs in the vertex shader (which is why the scrolling doesn't work in my custom shader), which is why I posted this question about how to get the offset values. If the offset values were applied before or after the shaders, I would expect my texture scrolling to still work. Am I just way off base here?
Answer by vikingfabian-com · Dec 15, 2013 at 08:17 AM
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"
}
Answer by Peter G · Jan 10, 2012 at 08:58 PM
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);
Answer by 3D-Magic-VR · Jan 10, 2012 at 11:55 PM
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.
Actually, that doesn't work with old style CG shaders right off the back. Hence the macro that I mentioned in my answer. That accounts for the scaling.
Answer by Exsanguinatus · Jan 10, 2012 at 08:40 PM
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.
Your answer
![](https://koobas.hobune.stream/wayback/20220613082513im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How to force the compilation of a shader in Unity? 5 Answers
Shader CG not taking tiling into account 2 Answers
Modify vertex position using shaders on Windows Phone 8 0 Answers
Offset textures individually into specific world directions 2 Answers
Can I read `CGPROGRAM` `#define` constants in `.cs` at runtime? 0 Answers