Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by Luke Phillips · Nov 09, 2010 at 10:16 PM · shaderoffsetcg

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.

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image VS48 · Nov 10, 2010 at 05:31 PM 0
Share

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.

avatar image Luke Phillips · Nov 10, 2010 at 10:58 PM 0
Share

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?

4 Replies

· Add your reply
  • Sort: 
avatar image
6

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"
     }
 
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
5

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);
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
1

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Peter G · Jan 12, 2012 at 09:56 PM 0
Share

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.

avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Shader CG not taking tiling into account 2 Answers

How to force the compilation of a shader in Unity? 5 Answers

Cg Language incompatibility Desktop VS GLSL Android 0 Answers

Can anyone help me with reflective shader with fall off property? 0 Answers

Sprite deformation in shader? 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges