Search Unity

Question How can I get the same results as Mathf.PerlinNoise from a C# Job?

Discussion in 'Entity Component System' started by Snubber, Sep 19, 2022.

  1. Snubber

    Snubber

    Joined:
    Jan 20, 2020
    Posts:
    65
    Hello, I am developing a game with procedural terrain generation. I'm working on transitioning all the mesh generation code from .NET threads to Unity C# Jobs. I was using Mathf.PerlinNoise in before but I can't use it in a Job. I know there are many other noise options that can be accessed from a Job but the problem is that I need it to have exactly the same output because players are currently playing the game and if I updated the game to use a noise function with a different output then all of the player's buildings will no longer be on the terrain.

    Does anyone have any insight into how the Mathf.PerlinNoise function works and how I could possibly replicate it with Unity.Mathematics.noise.cnoise?
     
  2. xVergilx

    xVergilx

    Joined:
    Dec 22, 2014
    Posts:
    3,296
    Hi, unfortunately PerlinNoise is implemented on the native side of the engine. And its highly unlikely that you'll get related code exposed, as UT member mentioned already in a different thread.

    But, you can use Mathf.PerlinNoise as is. And it will also [burst] compile just fine.
    It seems to be thread-safe, and Burst is able to call into extern functions.
    That may not be as fast as math implementation, but it should provide identical results.
     
    Last edited: Sep 19, 2022
    Snubber likes this.
  3. Snubber

    Snubber

    Joined:
    Jan 20, 2020
    Posts:
    65
    Thank you! I don't know why I thought Mathf.PerlinNoise wasn't accessible from c# jobs but you are correct, it is!
     
    samkirton likes this.
  4. runner78

    runner78

    Joined:
    Mar 14, 2015
    Posts:
    792
    The Unity.Mathematics package has multiple burstable noise functions (perlin, simplex, cellular)
     
    mmcveigh33, Yuchen_Chang and Krajca like this.