Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Question Generate random vertical lines in 2D Shader

Discussion in 'Shader Graph' started by StoneWillFall, Jan 14, 2023.

  1. StoneWillFall

    StoneWillFall

    Joined:
    Nov 9, 2017
    Posts:
    4
    Hi,

    I've made some shaders for my 2D game already, but I'd say I'm still very new to this topic.
    Broader context:
    I want to add grass "pattern" on specific color of the Sprite. I know how to do some modifications on specific color like add noise or modify color etc.

    Exact problem:
    How to generate random vertex lines (preferably wavy, but that's the next step).

    I was thinking about using Noise but I have no idea how to "stretch" it vertically.
    Ilustration of what I want to achieve:
    upload_2023-1-14_19-41-26.png

    And maybe a different idea: connecting points vertically from Gradient:
    upload_2023-1-14_19-44-58.png

    But I have no idea how to achieve any of these ideas.
    Are there Shader Nodes which I can use to do that?
     
  2. Lo-renzo

    Lo-renzo

    Joined:
    Apr 8, 2018
    Posts:
    1,323
    To stretch vertically, create a UV node, plug that UV node into a Tiling and Offset node, and plug that Tiling and Offset node into the Noise's UV slot. Then on the Tiling and Offset, adjust the Tiling's y-value so it's (1x, 3+y) - stretched!
     
    StoneWillFall likes this.
  3. StoneWillFall

    StoneWillFall

    Joined:
    Nov 9, 2017
    Posts:
    4
    Thank you, it is quite what I wanted! Two things:

    1. I need to increase the X value to stretch vertically or maybe I'm doing something wrong.
    upload_2023-1-15_16-26-17.png

    2. When I use this UV, in game, when I move the camera left/right, the vertical lines change position (or disappear and reappear). Is this related to some aliasing issue or maybe the resolution of noise generation? The larger the value of X (the higher and thinner the lines), the greater the problem. I marked a few places to make it easier to see the differences. This is with Tiling X50 Y1 and Gradient Noise scale 300
    upload_2023-1-15_16-42-21.png

    Can i do something about it or the only solution is to use smaller Tiling?
     
  4. Lo-renzo

    Lo-renzo

    Joined:
    Apr 8, 2018
    Posts:
    1,323
    Yeah, that could be the source of the problem.

    I suggest switching from noise to sampling a texture that has the exact pattern you want with a width that appropriate and doesn't produce artifacts like that. There's no need to use Tiling and Offset to stretch in this case. This will also be more performant.
     
    StoneWillFall likes this.