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.

Discussion Algorithm Implementations of Random and PerlinNoise

Discussion in 'Scripting' started by crazicrafter1, Oct 15, 2022.

  1. crazicrafter1

    crazicrafter1

    Joined:
    Apr 13, 2018
    Posts:
    2
    I have been working on a personal project on-off for a few months now. It is a dedicated server for Valheim called Valhalla, written in c++ (https://github.com/PeriodicSeizures/Valhalla). One of the primary mechanics requires the included Unity Random class and PerlinNoise function for consistency between the server and the client when provided a world generation seed (terrain generation, height map, etc...).

    I have scoured the internet high and low within the limits of my abilities, and have found frighteningly few sources on the exact algorithm implementation that the Random class and PerlinNoise functions use. Here are my findings: UnityCsReference, a forum post, and the C# documentation for Random and Perlin Noise. The Mathf.PerlinNoise function likely uses a variation of the algorithm by Ken Perlin. Unity Random uses the Xorshift 128 algorithm to generate random numbers, but again, the implementation varies. I thought I could just create a texture to hold precomputed Unity PerlinNoise generated values, but I was wrong because I assumed the input values had to be within 0 to 1 (they are not).

    I have no financial motive for requesting this information. This project is a totally personal hobby of mine, formed out of self-interest. I could always try spending countless hours reverse engineering the exact algorithm implementation with AI and pattern recognition— something that I have sparse knowledge of. Doing this on my own would be like trying to find a needle in a haystack. By native code, I am referring to the under-the-hood calls made by C# scripts to the internal UnityEngine. These functions are native :
    upload_2022-10-14_21-23-34.png

    Let me be clear in saying that I am not looking for the proprietary native code for these implementations (although that would be nice). I am simply looking for an algorithm that produces identical results to the Unity implementation; an algorithm consistent with that of the Random/Mathf.PerlinNoise algorithms. Native code or accurate pseudocode, I have no preference. If someone can point me toward a meaningful direction to how exactly Unity implements these, that would be grand.

    TLDR; I am looking for an algorithm for Random and PerlinNoise that spits out values equal to the Unity counterparts.

    Thank you, and I really appreciate your time in reading this.
     
  2. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    8,885
    The documentation forum isn't the place to discuss this. The documentation team won't be able to provide any insight into source access or algorithm implementation. I'll move your post to the scripting forum.

    for exact results you'd need the native code as obviously even the smallest change might give different results. Nevertheless, all I can really post are the references that the source implementation cites:

    // Xorshift 128 implementation
    // Xorshift paper: http://www.jstatsoft.org/v08/i14/paper
    // Wikipedia: http://en.wikipedia.org/wiki/Xorshift

    The wikipedia article looks pretty similar to some of the code although note that I have don't know anything about the details of the implementation itself.
     
  3. crazicrafter1

    crazicrafter1

    Joined:
    Apr 13, 2018
    Posts:
    2
    I managed to reverse engineer the algorithm for both Random and Mathf.PerlinNoise.

    PerlinNoise is basically a port of Ken Perlins Noise algorithm with some output differences https://cs.nyu.edu/~perlin/noise/. Here is my implementation https://github.com/PeriodicSeizures...9fc518f687274c946b2a9/src/VUtilsMath.cpp#L207.

    For Random, someone already appeared to have recreated the algorithm, but I didn't get to see it before I did this https://gist.github.com/macklinb/a00be6b616cbf20fa95e4227575fe50b. My implementation https://github.com/PeriodicSeizures/Valhalla/blob/steam-server/src/VUtilsRandom.cpp with several other functions. Comparative results:

    I have not extensively tested the above, but as you see, they are very similar.
    I hope this helps anyone who was in my prior position.