0

I am trying to apply a random texture with a number on it based on the tile input. My efforts thus far cause patterns to emerge in the number selected.

My method for going about it has been multiplying each tile value by a random scalar and then either multiplying or adding them together. Call the operations I do to each tile "salt".

Example:

float seed = fract(fract((tile.x+23.42f)*189.28148f) + (tile.y*92001.302+1.235801f));

When I add the two "salted" tile values together a pattern emerges. The numbers are the same along the diagonal.

When I multipy the two "salted" tile values together the numbers are mirrored along the diagonal.

I want a little more randomness out of these tile values, can someone decent at math help?

Shader code segment:

vec2 tile = vec2(floor(texCoord_vs.x), floor(texCoord_vs.y));
float seed = fract(fract((tile.x+23.42f)*189.28148f) + (tile.y*92001.302+1.235801f));
vec2 offset = offset_vs;
offset.x += floor(seed*7.0f)*TYPE_UNIT_SIZE;
vec3 diffuseColor = materialDiffuseColor * texture(
    typesheet, vec2((texCoord_vs.x - tile.x)*TYPE_UNIT_SIZE, 
    (texCoord_vs.y - tile.y)*TYPE_UNIT_SIZE) + offset);

Picture: https://gyazo.com/aada74e81fdc3dbd0220263993ed7d01

Evan Baad
  • 91
  • 5

1 Answers1

0

You might be able to use the technique described in this question for hashing 2D vectors, which in turn was taken from Optimized Spatial Hashing for Collision Detection of Deformable Objects:

index = (x p1 xor y p2) mod n

Where p1, & p2, are large prime numbers (they used 73856093, & 83492791) and n is the hash table size or in this case the max random index you wish to generate.

Community
  • 1
  • 1
Pikalek
  • 926
  • 7
  • 21
  • 1
    This would work, accept there is symmetry because x and y are interchangeable if they are the same values. x*p1 ^ y*p2 ^ x*p3 mod n would be better. – Evan Baad May 16 '16 at 18:11