Skip to content
Snippets Groups Projects
TerrainGenJob.cs 2.4 KiB
Newer Older
Chang, Ryan's avatar
Yee
Chang, Ryan committed
using System.Collections.Generic;
Chang, Ryan's avatar
Chang, Ryan committed
using Unity.Collections;
Chang, Ryan's avatar
Yee
Chang, Ryan committed
using Unity.Jobs;
using Unity.Mathematics;
using UnityEngine;

public struct TerrainGenJob : IJob
{
    public Vector2 InitialCrawlPosition { get; private set; }
    public Vector2Int GridPosition { get; private set; }
Chang, Ryan's avatar
Chang, Ryan committed

    [ReadOnly]
    private NativeArray<Octave> octaves;

Chang, Ryan's avatar
Yee
Chang, Ryan committed
    public int Samples { get; private set; }
Chang, Ryan's avatar
Chang, Ryan committed
    private NativeArray<NativeArray<float>> heights;
Chang, Ryan's avatar
Yee
Chang, Ryan committed
    private bool ready;

Chang, Ryan's avatar
Chang, Ryan committed
    public TerrainGenJob(Vector2 initialCrawlPos, Vector2Int gridPos, NativeArray<Octave> octaves, int samples)
Chang, Ryan's avatar
Yee
Chang, Ryan committed
    {
Chang, Ryan's avatar
Chang, Ryan committed
        heights = new();
Chang, Ryan's avatar
Yee
Chang, Ryan committed
        ready = false;

        InitialCrawlPosition = initialCrawlPos;
        GridPosition = gridPos;
        Samples = samples;
        this.octaves = octaves;
    }

    public float[,] GetHeights()
    {
        if (ready)
Chang, Ryan's avatar
Chang, Ryan committed
        {

            foreach (var height in heights)
            {

            }
            return ;
        }
Chang, Ryan's avatar
Yee
Chang, Ryan committed
        else
            throw new System.InvalidOperationException("Heights not built yet.");
    }

Chang, Ryan's avatar
Chang, Ryan committed
    public void Execute()
Chang, Ryan's avatar
Yee
Chang, Ryan committed
    {
        heights = new float[Samples, Samples];

        foreach (var octave in octaves)
        {
            for (int x = 0; x < Samples; x++)
            {
                for (int y = 0; y < Samples; y++)
                {
                    float2 crawlPos = new((x + InitialCrawlPosition.x * (Samples - 1)) * octave.frequency,
                        (y + InitialCrawlPosition.y * (Samples - 1)) * octave.frequency);
Chang, Ryan's avatar
Chang, Ryan committed

Chang, Ryan's avatar
Yee
Chang, Ryan committed
                    float addition;

                    switch (octave.type)
                    {
                        default:
                        case NoiseType.MathfPerlin:
                            addition = Mathf.PerlinNoise(crawlPos.x, crawlPos.y);
                            break;
                        case NoiseType.MathematicsPerlin:
                            addition = noise.cnoise(crawlPos) / 2.3f + 0.5f;
                            break;
                        case NoiseType.Simplex:
                            addition = noise.snoise(crawlPos) / 4.6f + 0.5f;
                            break;
                        case NoiseType.Celluar:
                            addition = noise.cellular(crawlPos).x / 2.3f + 0.5f;
                            break;
                    }
                    heights[y, x] += addition * octave.amplitude;
                }
            }
        }

        ready = true;
    }
}