create a unity script procedural earth generator using jobs.

PHOTO EMBED

Tue Dec 06 2022 18:16:19 GMT+0000 (Coordinated Universal Time)

Saved by @Ezio727 #c#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Unity.Jobs;
using Unity.Collections;

public class EarthGenerator : MonoBehaviour
{
    public int width;
    public int height;
    public float scale;

    [HideInInspector]
    public NativeArray<float> heightMapData;

    void Start()
    {
        // Create the job
        var job = new GenerateHeightMapJob()
        {
            width = width,
            height = height,
            scale = scale,
            heightMapData = new NativeArray<float>(width * height, Allocator.TempJob)
        };

        // Create the job handle
        var jobHandle = job.Schedule();

        // Wait for the job to complete
        jobHandle.Complete();

        // Get the results
        heightMapData = job.heightMapData;
    }

}

public struct GenerateHeightMapJob : IJob
{
    public int width;
    public int height;
    public float scale;
    public NativeArray<float> heightMapData;

    public void Execute()
    {
        for (int x = 0; x < width; x++)
        {
            for (int y = 0; y < height; y++)
            {
                float xCoord = (float)x / width * scale;
                float yCoord = (float)y / height * scale;

                float sample = Mathf.PerlinNoise(xCoord, yCoord);
                heightMapData[y * width + x] = sample;
            }
        }
    }
}
content_copyCOPY