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;
}
}
}
}
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter