Search Unity

Question How do I access a material's mainTextureScale and mainTextureOffset in a LWRP shader graph?

Discussion in 'Shader Graph' started by MCrafterzz, Apr 17, 2019.

  1. MCrafterzz

    MCrafterzz

    Joined:
    Jun 3, 2017
    Posts:
    354
    Thanks.
     
  2. vakabaka

    vakabaka

    Joined:
    Jul 21, 2014
    Posts:
    1,153
    I have tried to do this yesterday :D

    this was my try:
    Code (CSharp):
    1.     public float speed;
    2.     MeshRenderer rdr;
    3.     float offset;
    4.  
    5.     void Start()
    6.     {
    7.         rdr = GetComponent<MeshRenderer>();
    8.     }
    9.  
    10.     void Update ()
    11.     {
    12.         offset = offset + speed * Time.deltaTime;
    13.         if (offset >= 1)
    14.             offset = 0;
    15.         rdr.material.SetVector("_Offset", new Vector2(offset, 0));
    16.     }
    ok, not sure if this will work good but it can be the direction for the solution :rolleyes:
    test.png
     
    zacharyaghaizu and Circool like this.
  3. MCrafterzz

    MCrafterzz

    Joined:
    Jun 3, 2017
    Posts:
    354
    Well yeah it works but it feels like a kinda hacky way as you don't have to do anything like that in a standard shader (non graph)
     
    Last edited: Apr 17, 2019
  4. andrzej_

    andrzej_

    Joined:
    Dec 2, 2016
    Posts:
    81
    As it's standard those parameters are exposed as standard. Shader graph by definition is a custom thing and you decide which parameters to access (and how to name them). I don't really see how this could have been done differently.
     
    MCrafterzz likes this.
  5. Arycama

    Arycama

    Joined:
    May 25, 2014
    Posts:
    184
    I was also suprised to not see this as a built-in feature, seeing as virtually every single builtin shader does this in the previous render pipelines.

    It's not too difficult to do this yourself, declare a Vector4 property called _MainTex_ST, or whatever your texture is called. Then Split it into two Vector2's, and plug it into a UV/Tiling and Offset node, then use that as the input to a texture sample node.

    You can convert this to a subgraph so you can re-use it in other shader graphs.

    One thing I don't like is not knowing/having control over whether the tiling/offset is done per-vertex or per-pixel. In previous shaders it would have been done per-vertex, as there's no reason to do it per pixel as the results would be the same.

    upload_2019-4-21_11-44-8.png
     
  6. Arycama

    Arycama

    Joined:
    May 25, 2014
    Posts:
    184
    I slightly misread the question, but if you take my above approach, you can then access/modify the main texture's scale/offset in your scripts just with material.mainTextureScale and material.mainTextureOffset and it will work like builtin shaders.

    Under the hood, material.mainTexture probably calls material.GetTexture("_MainTex"), and scale/offset would be material.GetVector("_MainTex_ST").
     
    Tristan-Moore and MCrafterzz like this.
  7. oyokuva

    oyokuva

    Joined:
    Jun 21, 2019
    Posts:
    2
    dude seems like you gave a killer answer, I wish I could get such an accurate answer to every question.