Search Unity

Material.SetTextureOffset only working for _MainTex, Unity 5.6.0f3

Discussion in 'Scripting' started by Recluse, May 25, 2017.

  1. Recluse

    Recluse

    Joined:
    May 16, 2010
    Posts:
    485
    I want to scroll the Bump Map and Detail Normal Map in opposite directions on a standard Unity material.

    But only "_MainTex" responds to SetTextureOffset.

    The exact same code applied to "_BumpMap" or "_DetailNormalMap" does nothing.

    I can adjust the UV tiling of these independently in the editor, why not in code?


    Code (CSharp):
    1.  
    2.  
    3. public Vector2 MainTexOffset;
    4. public Renderer materialRenderer;
    5.  
    6. void Start ()
    7.     {
    8.         if (materialRenderer == null) materialRenderer = GetComponent<Renderer>();
    9.     }
    10.  
    11.     private void Update()
    12.     {
    13.         // works
    14.         materialRenderer.sharedMaterial.SetTextureOffset("_MainTex", MainTexOffset * Time.time);
    15.  
    16.         // doesn't
    17.  
    18.         // materialRenderer.sharedMaterial.SetTextureOffset("_BumpMap", MainTexOffset * Time.time);
    19.         // materialRenderer.sharedMaterial.SetTextureOffset("_DetailNormalMap", -MainTexOffset * Time.time);
    20.  
    21.     }
     
  2. LiterallyJeff

    LiterallyJeff

    Joined:
    Jan 21, 2015
    Posts:
    2,807
  3. Recluse

    Recluse

    Joined:
    May 16, 2010
    Posts:
    485
    Already seen that thread thanks, it doesn't work beyond Unity 5.5 (according to the final comment posted).

    If the only solution is to alter the standard shader, Unity need to rewrite their docs - which state that SetTextureOffset can be applied to named properties. This is the expected behaviour - normally, properties you can set in the inspector are accessible via script.

    https://docs.unity3d.com/ScriptReference/Material.SetTextureOffset.html

    There is already a separate mainTextureOffset method, which would seem to make SetTextureOffset pointless if it cannot be used with anything other than the main texture in standard shader.
     
  4. LiterallyJeff

    LiterallyJeff

    Joined:
    Jan 21, 2015
    Posts:
    2,807
    My bad for linking out of date information, I don't claim to be an expert on shaders and I'm not up to date on the state of this sort of thing. I agree, it's not intuitive at all, and should be supported. It definitely seems odd that you can individually alter offsets in the inspector but not in code.

    I would post this over in one of the graphics/shader specific subforums and see if you can get a more technical explanation or workaround.