Search Unity

RuleTile random distribution not random enough

Discussion in '2D' started by johanolofsson, May 15, 2020.

  1. johanolofsson

    johanolofsson

    Joined:
    Nov 21, 2018
    Posts:
    94
    Hi, I created a RuleTile yesterday. It has a total of 4 inner/outer corners, 3 tiles per side (left/right/floor/roof = 12 pieces) and a middle tile. I set up rules so that each side has a random distribution of the 3 versions of tiles for that side. However, when drawing, all though it does randomize them, they occur much too often in clusters of the the same tile. I've tried changing the noise but can't find a number which seems to improve the distribution. I did however find numbers which made it even worse :) Any ideas on what I could do to improve this?
     
  2. Lo-renzo

    Lo-renzo

    Joined:
    Apr 8, 2018
    Posts:
    1,514
    The RuleTile class has some spot where it gets/applies the random. I can't remember what it uses, perlin noise or whatever. You could replace that by using something like Xorshift to increase the randomness. Shift the x, shift the y (and if applicable) shift the z. Some of xorshifts aren't well tuned for sequential inputs (0, 1, 2, 3) so you may need to increase the noise by adding/mult w/ a prime here or there.

    If you need to get an index out of an (x, y) position, you could use some algorithm for long int64 xorshift and combine the x, y together like so:
    Code (CSharp):
    1. long both = x;
    2. both = both << 32;  // shift the bits over
    3. both = both | (long)(uint)y;
    Then
    Code (CSharp):
    1. long xorshiftedLong = SomeXorshiftAlgoForLongs(both);
    2. int index = Mathf.Abs((int)(xorshiftedLong % lengthOfArray));
    3. Sprite mySprite = mySprites[index];
    Thus, you get a much-more-random sprite based on its (x,y) position.
     
    Tom-Atom likes this.