Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Resolved How to get non-repeated UV in shader graph

Discussion in 'Shader Graph' started by atrivedi7, Jun 30, 2022.

  1. atrivedi7

    atrivedi7

    Joined:
    Jul 25, 2013
    Posts:
    92
    Hi all! I'm trying to make a soft-mask for my lit sprite in unity 2021. I have a shader graph setup like this

    upload_2022-6-29_19-27-54.png

    This works well when my SpriteRenderer's Draw Mode is set to Simple. But when I set it to "Tiled" I get this result

    upload_2022-6-29_19-29-2.png

    This is of course correct based on the fact I haven't sampled the mask differently. Each tiled image has a tiled version of the mask.

    My question is; how can I set up the UVs for the mask so that it always only goes from 0-1 for the full range of the rect instead of repeating like the base image

    Thanks in advance!
     
  2. Lo-renzo

    Lo-renzo

    Joined:
    Apr 8, 2018
    Posts:
    1,503
    Try this: change the SpriteRenderer back to simple. For the MainTex, set the Texture to Repeat Mode and for its UVs use a Tiling and Offset node with the Tiling adjusted to your liking. Optionally, use a Position node fed into the UVs of the Tiling and Offset node.
     
    atrivedi7 likes this.
  3. atrivedi7

    atrivedi7

    Joined:
    Jul 25, 2013
    Posts:
    92
    Your first solution worked great! Thank you so much :)

    For anyone else interested, I also made a script that can set the tiling and offset per instance.

    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class SoftMaskTilingAndOffset : MonoBehaviour
    4. {
    5.     [SerializeField] SpriteRenderer spriteRenderer;
    6.     [SerializeField] Vector2 tiling = new(1, 1);
    7.     [SerializeField] Vector2 offset;
    8.  
    9.     void Start()
    10.     {
    11.         if (spriteRenderer == null) return;
    12. #if UNITY_EDITOR
    13.         var mat = spriteRenderer.sharedMaterial;
    14. #else
    15.         var mat = spriteRenderer.material;
    16.  
    17. #endif
    18.         mat.SetVector("_Tiling", tiling);
    19.         mat.SetVector("_Offset", offset);
    20.     }
    21. }