Search Unity

Question "Random Tiles" shader using Object's position coordinates as the 'seed'

Discussion in 'Shader Graph' started by bunp, Jun 16, 2019.

  1. bunp

    bunp

    Joined:
    Feb 21, 2019
    Posts:
    70
    upload_2019-6-15_19-4-19.png

    I am making a 'random stone tiles' shader and hit a snag I need help resolving. I am trying to introduce a lot of randomness in the shader to conceal the repeating nature of the tile as much as possible.

    One thing I would like to do to break up the repeating is to take each tile and apply it randomly, at a random 90 degree rotation. The 'Lines' texture is 6x6.

    My issue is how do I take the position of the mesh, and somehow use that number to map a fixed random UV coordinate onto the mesh? Like, as I move the tile around through space, I want to see the black lines part suddenly change to random rotations and coordinates of the 6x6, constantly giving me a different looking tile.

    Currently my shader uses world space position, which doesnt at all do what I want, you can see in the top left when the tiles arent perfectly world aligned that the tiles go 'off' I was thinking of trying to use some kind of 'stepped' world position where it only changes every X distance, so that I could have tiles off of the world's 'grid' but it makes more sense to use the objects position and somehow use that to move the UV around

    upload_2019-6-15_19-17-11.png

    I have all of the words I think I need to achieve this, but I'm unsure how to attach them all together so that as the mesh translates through space, I constantly get a random tile, at a random 90 degree rotation, from my tile map of 6x6 tiles, and apply it to the mesh, using the mesh's position in world space as the 'seed'
    .
     
    Last edited: Jun 16, 2019
  2. bunp

    bunp

    Joined:
    Feb 21, 2019
    Posts:
    70
    upload_2019-6-15_19-31-45.png

    I thought this would work to start with but it doesnt.
    Im getting the world position X and Y, rounding it so its only a whole number like 1,2,3, etc, then using that as the offset for the texture coordinates, but all my tiles still look exactly the same.
     
  3. bunp

    bunp

    Joined:
    Feb 21, 2019
    Posts:
    70
    upload_2019-6-15_19-44-30.png
    I thought this would work, getting the position, outputting just the XY, rounding it, dividing that by 6, and using it as the offset, but the end result is... wacky. GIf too big to post.

    https://imgur.com/a/gyQlSHw
     
    morpheus101 likes this.
  4. bunp

    bunp

    Joined:
    Feb 21, 2019
    Posts:
    70
    upload_2019-6-15_20-13-44.png

    This works if I replace the 'random seed' with a slider between 1-6, but as soon as I replace that Vec 1 with world position, it stops working.

    The slider as I slide it offsets by exacty 1 tile each #, but world position I feel like it isnt being rounded because as soon as I use world position instead of a pre-set vector, it scrolls the texture across the face instead of offseting by exactly 1 tile.
     
  5. bunp

    bunp

    Joined:
    Feb 21, 2019
    Posts:
    70
    I haven't been able to debug why World Position doesnt work when a slider does. Can World Position not be rounded in shader graph?
     
  6. babels

    babels

    Joined:
    Feb 1, 2020
    Posts:
    2
    you are getting one to work or the other but not both if I understand to mix world and local coordinates. Basically you want to duplicate everything, have the first one run local, the last one run world then add or blend them together if you are doing normals. Shader amplifier does this well with instancing but it doesn't read your graphics config so good luck if you are doing anything other than basic