Search Unity

Changing MainTex Offset on material

Discussion in 'Shaders' started by Shadowing, Jul 20, 2017.

  1. Shadowing

    Shadowing

    Joined:
    Jan 29, 2015
    Posts:
    1,648
    I can't seem to change the offset on my material of the texture
    This is the two things I've tried.
    Also it doesn't work by changing it in the inspector either.

    Code (csharp):
    1.  
    2. material.mainTextureOffset = new Vector2(Random.Range(0,1f),0);
    3. material.SetTextureOffset("_MainTex", new Vector2(Random.Range(0,1f),0));
    4.  
    Using Unity 5.6.1 p4
     
  2. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,342
    Then it's likely not implemented in the shader you're using.
     
  3. npatch

    npatch

    Joined:
    Jun 26, 2015
    Posts:
    247
    I have problems even with shaders properly set up in many cases.
    Definitely have <tex_name>_ST declared and using TRANSFORM_TEX as well.
    Used to work in 5.4.3.
     
  4. Shadowing

    Shadowing

    Joined:
    Jan 29, 2015
    Posts:
    1,648
    Oh so this is something that can be added to the shade script?
     
    Last edited: Jul 20, 2017
  5. npatch

    npatch

    Joined:
    Jun 26, 2015
    Posts:
    247
  6. Shadowing

    Shadowing

    Joined:
    Jan 29, 2015
    Posts:
    1,648
    Thanks @npatch
    My large issue right now is I don't know C++ or what ever shaders use.
    Here is my shader script below
    I couldn't make those changes work.
    Gives me fatal error with using TRANSFORM_TEX return value on o.textcoord

    Code (csharp):
    1.  
    2. // Upgrade NOTE: replaced 'mul(UNITY_MATRIX_MVP,*)' with 'UnityObjectToClipPos(*)'
    3.  
    4. Shader "FORGE3D/Warp Jump" {
    5. Properties {
    6.     _TintColorA ("Tint Color A", Color) = (0.5,0.5,0.5,0.5)
    7.     _TintColorB ("Tint Color B", Color) = (0.5,0.5,0.5,0.5)
    8.     _Mult ("Color strength", float) = 1
    9.     _MainTex ("Particle Texture", 2D) = "white" {}
    10.     _InvFade ("Soft Particles Factor", Range(0.01,3.0)) = 1.0
    11. }
    12.  
    13. Category {
    14.     Tags { "Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Transparent" }
    15.     Blend One One
    16.     AlphaTest Greater .01
    17.     ColorMask RGB
    18.     Cull Off Lighting Off ZWrite Off Fog { Mode Off }
    19.  
    20.     SubShader {
    21.         Pass {
    22.      
    23.             CGPROGRAM
    24.             #pragma vertex vert
    25.             #pragma fragment frag
    26.             #pragma multi_compile_particles
    27.  
    28.             #include "UnityCG.cginc"
    29.  
    30.             sampler2D _MainTex;
    31.             float4 _TintColorA, _TintColorB;
    32.          
    33.             struct appdata_t {
    34.                 float4 vertex : POSITION;
    35.                 fixed4 color : COLOR;
    36.                 float2 texcoord : TEXCOORD0;
    37.              
    38.             };
    39.  
    40.             struct v2f {
    41.                 float4 vertex : SV_POSITION;
    42.                 fixed4 color : COLOR;
    43.                 float2 texcoord : TEXCOORD0;
    44.                 #ifdef SOFTPARTICLES_ON
    45.                 float4 projPos : TEXCOORD1;
    46.                 #endif
    47.  
    48.             };
    49.          
    50.             //float4 _MainTex_ST;
    51.             uniform float4 _MainTex_ST;
    52.  
    53.             v2f vert (appdata_t v)
    54.             {
    55.                 v2f o;
    56.                 o.vertex = UnityObjectToClipPos(v.vertex);
    57.                 #ifdef SOFTPARTICLES_ON
    58.                 o.projPos = ComputeScreenPos (o.vertex);
    59.                 COMPUTE_EYEDEPTH(o.projPos.z);
    60.                 #endif
    61.                 o.color = v.color;
    62.                 //o.texcoord = v.texcoord;
    63.                 o.textcoord = TRANSFORM_TEX(v.texcoord, _MainTex);
    64.                 return o;
    65.             }
    66.  
    67.             sampler2D_float _CameraDepthTexture;
    68.             float _InvFade, _Mult;
    69.          
    70.             float4 frag (v2f i) : SV_Target
    71.             {
    72.                 #ifdef SOFTPARTICLES_ON
    73.                 float sceneZ = LinearEyeDepth (SAMPLE_DEPTH_TEXTURE_PROJ(_CameraDepthTexture, UNITY_PROJ_COORD(i.projPos)));
    74.                 float partZ = i.projPos.z;
    75.                 float fade = saturate (_InvFade * (sceneZ-partZ));
    76.                 i.color.a *= fade;
    77.                 #endif
    78.              
    79.                 float4 final = tex2D(_MainTex, i.texcoord);
    80.                 final = lerp(_TintColorA * _TintColorA.a * _Mult, _TintColorB * _TintColorB.a * _Mult, final * _Mult) * final;
    81.  
    82.              
    83.  
    84.                 return float4(i.color.a * final.xyz * i.color.xyz, 1);
    85.             }
    86.             ENDCG
    87.         }
    88.     }  
    89. }
    90. }
    91.  
    92.  
     
  7. Shadowing

    Shadowing

    Joined:
    Jan 29, 2015
    Posts:
    1,648
    Ahh nevermind the guy who made the effect package I use told me what to edit to fix it.
    Its all good now.

    I had to change
    Code (csharp):
    1. float4 final = tex2D(_MainTex, i.texcoord);
    with this below

    Code (csharp):
    1. float4 final = tex2D(_MainTex, TRANSFORM_TEX(i.texcoord, _MainTex));
     
  8. npatch

    npatch

    Joined:
    Jun 26, 2015
    Posts:
    247
    Shaders are never written in C++. Here it's a variant of HLSL , also called Cg.

    Yeah, that's because you've made a typo. o.textcoord should be o.texcoord.

    PS: Your change works too but the previous version would have also worked if not for the typo.
     
  9. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,342
    Unity shaders are just HLSL now, not Cg. It was originally Cg, but if you try to use features that were unique to Cg it will fail. The code outside of the CGPROGRAM blocks (which are really HLSLPROGRAM blocks) is Unity's own ShaderLab language which is a very simple markup language used to tell Unity how it should use the shader code when rendering. Because it was originally written to wrap around Cg there are some vestiges of that, like the use of CGPROGRAM / ENDCG and "fragment" shader vs "pixel" shader.
     
  10. Shadowing

    Shadowing

    Joined:
    Jan 29, 2015
    Posts:
    1,648
    oh wow nice catch haha. I guess I rely on my editor to much. My editor doesn't support that language so it didn't catch that lol.
    It works now. I don't any difference though.
     
  11. npatch

    npatch

    Joined:
    Jun 26, 2015
    Posts:
    247
    @bgolus indeed.

    @Shadowing Do you mean no difference when you alter the Tiling/Offset variables in the Inspector?
     
  12. Shadowing

    Shadowing

    Joined:
    Jan 29, 2015
    Posts:
    1,648
    I mean i see no difference from doing the changes in the link you gave.
    Everything works now after the changes I made posted above.
     
  13. npatch

    npatch

    Joined:
    Jun 26, 2015
    Posts:
    247
    Yes because there was a typo. But you're doing the same thing so it doesn't make a difference.