Search Unity

Help Wanted Automatic tiling when scaling object?

Discussion in 'Shader Graph' started by dgoyette, Apr 27, 2019.

  1. dgoyette

    dgoyette

    Joined:
    Jul 1, 2016
    Posts:
    2,300
    I'm trying to find a way to cause my object's texture to repeat, instead of stretching, when I scale the object.

    For example:

    upload_2019-4-27_18-41-7.png

    Here the texture is repeating a few times because I'm manually adjusted the Tiling. But this requires me to set the Tiling manually. Instead, I want to be able to stretch the object (long ways) arbitrarily, and have the tiling adjust automatically such that the texture is already a const size, and repeats as the object scales.

    The closest I've gotten to this is using a World UV approach, but that's not really what I want. With a world UV approach, I can't rotate the object, as the texture no longer lines up with the object.

    Is there a Shadergraph approach to repeating on an object's local axis?
     
    Mauri and NeatWolf like this.
  2. dgoyette

    dgoyette

    Joined:
    Jul 1, 2016
    Posts:
    2,300
    This ended up being pretty simple, as it turns out. It's based on getting the scale of the object via its transformation matrix, and using that to set the tiling factor. The matrix transformation logic is based on the ExtractScale function described here:

    https://forum.unity.com/threads/how-to-assign-matrix4x4-to-transform.121966/

    Plugging this into my shader, it looks like this:

    upload_2019-4-27_19-40-39.png

    In my case, I only want to tile on the object's local X axis, which is my I'm only getting the X scale. So this approach works well regardless of whether the object is placed within a scaled parent, or regardless of its world rotation:


    upload_2019-4-27_19-42-14.png
     
    Horus_Sungod42 and NeatWolf like this.
  3. dgoyette

    dgoyette

    Joined:
    Jul 1, 2016
    Posts:
    2,300
    Just to improve on the last post, now that I have a slightly better understanding of things, here's a better way to get the object's scale. (I didn't realize at first that it's simple to get the columns of a matrix instead of splitting and recombining the rows).

    upload_2019-4-28_16-59-54.png
     
    bgolus, ph_ and NeatWolf like this.
  4. MariusToma

    MariusToma

    Joined:
    Jul 13, 2018
    Posts:
    1
    Hey dgoyette,

    Would you be willing to show the rest of the graph? I assumed it will be connected to a tilling and offset note, which has an exposed texture input, but that doesn't work.
    I can't seem to find a similar post anywhere and I am a noob at shader graph :/
     
  5. dgoyette

    dgoyette

    Joined:
    Jul 1, 2016
    Posts:
    2,300
    Sure. There are really only two interesting parts to my shader. The first part plugs into the UV node of all the other Sample Texture nodes. It's responsible for scaling. Note that I've parameterized whether to perform the scaling along U and V, as I had some cases where I wanted one or the other, or both:

    upload_2020-2-5_11-19-22.png

    This other part you might not need. I plug this part into the Position output. I also needed this to get the scaling to look right on some objects:

    upload_2020-2-5_11-23-49.png

    The rest of the graph is just the normal texture samples for base color, normal, mask map, and emission, with the first two sections getting plugged in:

    upload_2020-2-5_11-24-48.png
     
unityunity