Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using Unity.Jobs;
- using UnityEngine;
- using Unity.Mathematics;
- using Unity.Collections;
- using Unity.Burst;
- public class Noise
- {
- NativeArray<float> noiseFrequencies;
- NativeArray<float> noiseAmplitudes;
- int octaves;
- int lod;
- int3 chunkDimensions;
- NativeArray<int3> cornerOffsets;
- public Noise(float frequency, float persistance, float lacunarity, int octaves, int lod)
- {
- noiseFrequencies = new NativeArray<float>(octaves, Allocator.Persistent);
- noiseAmplitudes = new NativeArray<float>(octaves, Allocator.Persistent);
- for (int i = 0; i < octaves; i++)
- {
- noiseFrequencies[i] = Mathf.Pow(lacunarity, i) * frequency;
- noiseAmplitudes[i] = Mathf.Pow(persistance, i);
- }
- this.octaves = octaves;
- this.lod = lod;
- chunkDimensions = new int3(MeshGenerator.chunkDimensions.x, MeshGenerator.chunkDimensions.y, MeshGenerator.chunkDimensions.z);
- cornerOffsets = new NativeArray<int3>(MeshGenerator.cornerOffsets.Length, Allocator.Persistent);
- for (int i = 0; i < cornerOffsets.Length; i++)
- {
- cornerOffsets[i] = new int3(MeshGenerator.cornerOffsets[i].x, MeshGenerator.cornerOffsets[i].y, MeshGenerator.cornerOffsets[i].z);
- }
- }
- ~Noise()
- {
- noiseFrequencies.Dispose();
- noiseAmplitudes.Dispose();
- cornerOffsets.Dispose();
- }
- public NativeArray<float> FractalNoiseMap()
- {
- int x = (chunkDimensions.x - lod) / lod;
- int z = ((chunkDimensions.z - lod) / lod) * chunkDimensions.x;
- NativeArray<float> noiseMap = new NativeArray<float>((x + z) * 8 + 8, Allocator.TempJob);
- Noise2DJob job = new Noise2DJob
- {
- noiseMap = noiseMap,
- chunkDimensions = chunkDimensions,
- cornerOffsets = cornerOffsets,
- octaves = octaves,
- lod = lod,
- noiseFrequencies = noiseFrequencies,
- noiseAmplitudes = noiseAmplitudes
- };
- job.Schedule(x + z + 1, 10).Complete();
- return noiseMap;
- }
- public NativeArray<float> Fractal3DNoiseMap()
- {
- NativeArray<float> noiseMap = new NativeArray<float>(chunkDimensions.x * chunkDimensions.y * chunkDimensions.z* 8, Allocator.TempJob);
- Noise3DJob job = new Noise3DJob
- {
- noiseMap = noiseMap,
- chunkDimensions = chunkDimensions,
- cornerOffsets = cornerOffsets,
- octaves = octaves,
- noiseFrequencies = noiseFrequencies,
- noiseAmplitudes = noiseAmplitudes,
- lod = lod
- };
- job.Schedule(chunkDimensions.x * chunkDimensions.y * chunkDimensions.z , 10).Complete();
- return noiseMap;
- }
- [BurstCompile]
- struct Noise2DJob : IJobParallelFor
- {
- [NativeDisableParallelForRestriction]
- [WriteOnly] public NativeArray<float> noiseMap;
- [ReadOnly] public int3 chunkDimensions;
- [ReadOnly] public NativeArray<int3> cornerOffsets;
- [ReadOnly] public int octaves;
- [ReadOnly] public int lod;
- [ReadOnly] public NativeArray<float> noiseFrequencies;
- [ReadOnly] public NativeArray<float> noiseAmplitudes;
- public void Execute(int i)
- {
- int y = i / chunkDimensions.x; //* lod;
- int x = i % chunkDimensions.x; //* lod;
- for (int j = 0; j < 8; j++)
- {
- //Noise value at each corner of a voxel
- noiseMap[(i * 8) + j] = FractalNoise(x + (cornerOffsets[j].x / lod), y + (cornerOffsets[j].z / lod));
- }
- }
- float FractalNoise(int x, int y)
- {
- float noiseValue = 0;
- for (int i = 0; i < octaves; i++)
- {
- noiseValue += (noise.snoise(new float2(x * noiseFrequencies[i], y * noiseFrequencies[i])) + 1) * .5f * noiseAmplitudes[i];
- }
- return noiseValue;
- }
- }
- [BurstCompile]
- struct Noise3DJob : IJobParallelFor
- {
- [NativeDisableParallelForRestriction]
- [WriteOnly] public NativeArray<float> noiseMap;
- [ReadOnly] public int3 chunkDimensions;
- [ReadOnly] public NativeArray<int3> cornerOffsets;
- [ReadOnly] public int octaves;
- [ReadOnly] public NativeArray<float> noiseFrequencies;
- [ReadOnly] public NativeArray<float> noiseAmplitudes;
- [ReadOnly] public int lod;
- public void Execute(int i)
- {
- int z = i / (chunkDimensions.x * chunkDimensions.y);
- int y = i % (chunkDimensions.x * chunkDimensions.y) / chunkDimensions.x;
- int x = i % chunkDimensions.x;
- //Noise value at each corner of a voxel
- for (int j = 0; j < 8; j++)
- {
- int xCoord = x + (cornerOffsets[j].x / lod);
- int yCoord = y + (cornerOffsets[j].y / lod);
- int zCoord = z + (cornerOffsets[j].z / lod);
- noiseMap[(i * 8) + j] = Fractal3DNoise(xCoord, yCoord, zCoord);
- }
- }
- public float Fractal3DNoise(int x, int y, int z)
- {
- float noiseValue = 0;
- for (int i = 0; i < octaves; i++)
- {
- noiseValue += (noise.snoise(new float3(x * noiseFrequencies[i], y * noiseFrequencies[i], z * noiseFrequencies[i]) + 1) * .5f * noiseAmplitudes[i]);
- }
- return noiseValue; //- density;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement