miepzerino
2025-04-03 d9d5b7b5c58a7b044b78ee7993f0a15e5b79f689
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
using System;
using System.Collections;
using System.Collections.Generic;
using Unity.Collections;
using Unity.Jobs;
using Unity.Mathematics;
using UnityEngine;
 
public struct TerrainGenerationJob : IJobParallelFor
{
    [ReadOnly] public int ChunkStartX;
    [ReadOnly] public int ChunkStartY;
    [ReadOnly] public int ChunkSize;
    [ReadOnly] public float Scale;
    [ReadOnly] public float OffsetX;
    [ReadOnly] public float OffsetY;
    [ReadOnly] public int MaxWidth;
    [ReadOnly] public int MaxDepth;
 
    [WriteOnly] public NativeArray<bool> TerrainMap;
    [WriteOnly] public NativeArray<float> NoiseMap;
 
    public void Execute(int index)
    {
        int x = ChunkStartX + (index % ChunkSize);
        int y = ChunkStartY - (index / ChunkSize);
 
        if (x < 1 || x >= MaxWidth || y < -MaxDepth || y >= 0)
        {
            TerrainMap[index] = false;
            NoiseMap[index] = 0f;
            return;
        }
 
        float xPerlin = ((float)x / MaxWidth) * Scale + OffsetX;
        float yPerlin = ((float)Mathf.Abs(y) / MaxDepth) * Scale + OffsetY;
        float perlinNoise = noise.snoise(new float2(xPerlin, yPerlin)); // Using Unity.Mathematics noise
 
        NoiseMap[index] = perlinNoise;
        TerrainMap[index] = perlinNoise <= 0.7f;
    }
}