Deterministic randomness solution
Context:
We need a viable deterministic randomness solution to provide reproducible consistency in various randomly derived features, such as texture sprite assignment for building facades and the position of accessory deco objects created with SpawnInsideModifier
Problem:
We have no deterministic random solution; either prefabs will always be placed in the same location, or it'll always be random, but not predictably random.
Solution:
http://blog.runevision.com/2015/01/primer-on-repeatable-random-numbers.html
We have two main options for generating random numbers; random number generation (RNG) and random hash functions.
TL;DR... RNG's will not provide the predictable deterministic quality we need, due to the fact that they produce a sequence of random numbers, which must be queried/accessed in a specific order to produce deterministic results. The MapsSDK relies on coroutines and web API calls for both querying map data and building the map, so there is no way we can guarantee that RNG values will be accessed in the same order each time.
Random hash functions do not produce a sequence of numbers, and deterministic random values can be mapped to arbitrary inputs. This approach will probably work much better than RNG's, and values could be derived from a combined input of 2d coordinates, tileIds, zoom level, etc.
https://wiki.openstreetmap.org/wiki/Zoom_levels
Next steps:
-
[ ] Evaluate existing C# random hashing libraries, with the desired end result being the identification of a computationally fast hash solution with minimal hash collisions that would work well if we packaged it inside our SDK. xxHash (https://code.google.com/archive/p/xxhash/) seems like a promising solution that offers non-crypto hashes of high quality with fast performance.
-
[ ] Determine how to structure the random hash values, given the potential number of tiles and max tile resolution. At zoom 16, the entire world is comprised of 4,294,967 tiles (see above link), and our current max tile resolution is 4096. For each map feature at zoom 16 to have a unique deterministic random value, this would potentially add significant computational overhead to operations that rely on this form of randomness.
-
One idea to explore is having nested hash functions that provide a value which can serve as a lookup for more granular hash methods. For example, we could use tileId's as an initial hash, then use the result combined with a feature's tile space coordinates to derive a more granular random hash value, which could then be used to drive feature texture sprite selection/etc.
-
Not sure how we'd compensate for tileId's changing with zoom levels.
-
[ ] Determine how to map returned values of arbitrary size and value to seeds and/or values usable by the various methods that currently use Unity's Random.Range.
-
[ ] Determine how to package this new system (static class, per-map class instance, etc) and how to expose various parameters to users.
-
[ ] Determine how to construct unit tests to validate the quality and performance of the hash values.
-
[ ] Build the solution and test.
Update 1:
Tested deterministic randomness solution that uses texture sampling to derive random values, with promising results so far.
Details:
- VectorFeatureUnity uses a float RandomValue getter. Modifiers can query this getter.
- the getter uses VectorFeatureUnity._geom[0][0] (a 2d point in 4096x4096 tile space coords), takes the x and y value of that, and uses it to query the red channel value of a black and white 4096x4096 noise texture, and returns this as a 0.0f - 1.0f float value.
- this value is then used by modifiers as a lookup index for building facade and roof textures, as well as scriptable palette color lookup for layers using a simple style.
Pros:
- _geom values are in 4096 tile space, meaning the pixel value lookup on a 4096 texture is 1:1 accuracy.
- produces 100% consistent deterministic values on repeated play sessions @ same zoom level.
Cons:
-
_geom position values fluctuate on different zoom levels, resulting in different random values, thus resulting in different textures being applied to structures. This is not a problem if the zoom is changed during separate play sessions, but if the zoom level is changed at runtime, the random value per feature (along with visual elements that rely on said value, such as textures) are not guaranteed to be the same, which may result in visual pops.
-
the noise texture is sampled via the same orientation per tile. While not extremely noticeable during development, this could result in tiles repeating similar texture patterns, especially in settings with alot of urban density.
Next steps:
- Find a way to mitigate random value differences at different zoom levels.
- Explore ideas on how to differ the random values per tile, such as rotating the texture in 90 degree increments per tile, etc, so as to reduce the chances of perceived repeated randomness patterns in larger areas.
- Find a way to use texture-derived random vals for non-index-based features, such as SpawnInsideModifier, which uses a random range inside collider bounds to derive x and z position.