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;
- using UnityEngine.Rendering;
- using System.Security.Cryptography;
- public class Noise
- {
- NativeArray<float> noiseFrequencies;
- NativeArray<float> noiseAmplitudes;
- int octaves;
- int3 chunkDimensions;
- NativeArray<int3> cornerOffsets;
- float warpingStrength;
- float warpingFrequency;
- float mountainHeightOffset;
- ComputeShader noiseShader;
- public Noise(float frequency, float persistance, float lacunarity, float warpingStrength, float warpingFrequency, float mountainHeightOffset, int octaves, ComputeShader noiseShader)
- {
- 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.warpingStrength = warpingStrength;
- this.warpingFrequency = warpingFrequency;
- this.mountainHeightOffset = mountainHeightOffset;
- this.noiseShader = noiseShader;
- 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(Vector2 offset, int lod)
- {
- 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,
- offset = new float2(offset.x, offset.y),
- warpingStrength = warpingStrength,
- warpingFrequency = warpingFrequency,
- mountainHeightOffset = mountainHeightOffset
- };
- job.Schedule(x + z + 1, 10).Complete();
- return noiseMap;
- }
- public NativeArray<float> Fractal3DNoiseMap(Vector2 offset, int lod)
- {
- float[] noiseGrid = new float[chunkDimensions.x / lod * chunkDimensions.y / lod * chunkDimensions.z / lod * 8];
- ComputeBuffer noiseGridBuffer = new ComputeBuffer(noiseGrid.Length, sizeof(float));
- noiseGridBuffer.SetData(noiseGrid);
- noiseShader.SetBuffer(0, "noiseMap", noiseGridBuffer);
- noiseShader.SetBuffer(1, "cornerOffsets", cornerOffsets);
- noiseShader.SetInt("chunkDimensionsX", chunkDimensions.x);
- noiseShader.SetInt("chunkDimensionsY", chunkDimensions.y);
- noiseShader.SetInt("lod", lod);
- Debug.Log(noiseGrid.Length / 20);
- noiseShader.Dispatch(0, noiseGrid.Length / 20, 1, 1);
- noiseGridBuffer.GetData(noiseGrid);
- noiseGridBuffer.Dispose();
- //NativeArray<float> noiseMap = new NativeArray<float>(chunkDimensions.x / lod * chunkDimensions.y / lod * chunkDimensions.z / lod * 8, Allocator.TempJob);
- //Noise3DJob job = new Noise3DJob
- //{
- // noiseMap = noiseMap,
- // chunkDimensions = chunkDimensions,
- // cornerOffsets = cornerOffsets,
- // octaves = octaves,
- // noiseFrequencies = noiseFrequencies,
- // noiseAmplitudes = noiseAmplitudes,
- // lod = lod,
- // offset = new float2(offset.x, offset.y),
- // warpingFrequency = warpingFrequency,
- // warpingStrength = warpingStrength
- //};
- //job.Schedule(chunkDimensions.x / lod * chunkDimensions.y / lod * chunkDimensions.z / lod, 10).Complete();
- NativeArray<float> noiseMap = new NativeArray<float>(chunkDimensions.x / lod * chunkDimensions.y / lod * chunkDimensions.z / lod * 8, Allocator.TempJob);
- return noiseMap;
- }
- [BurstCompile]
- struct Noise2DJob : IJobParallelFor
- {
- [NativeDisableParallelForRestriction]
- public NativeArray<float> noiseMap;
- [ReadOnly] public int3 chunkDimensions;
- [ReadOnly] public NativeArray<int3> cornerOffsets;
- [ReadOnly] public int octaves;
- [ReadOnly] public int lod;
- [ReadOnly] public float2 offset;
- [ReadOnly] public NativeArray<float> noiseFrequencies;
- [ReadOnly] public NativeArray<float> noiseAmplitudes;
- [ReadOnly] public float warpingStrength;
- [ReadOnly] public float warpingFrequency;
- [ReadOnly] public float mountainHeightOffset;
- public void Execute(int i)
- {
- int y = i / chunkDimensions.x * lod;
- int x = i % chunkDimensions.x * lod;
- for (int j = 0; j < 8; j++)
- {
- float xCoord = x + offset.x + cornerOffsets[j].x * lod;
- float yCoord = y + offset.y + cornerOffsets[j].z * lod;
- float xWarp = FractalNoise((xCoord + 1000) * warpingFrequency, (yCoord - 1000) * warpingFrequency) * warpingStrength;
- float yWarp = FractalNoise((xCoord + 10000) * warpingFrequency, (yCoord - 10000) * warpingFrequency) * warpingStrength;
- noiseMap[(i * 8) + j] = FractalNoise(xCoord + xWarp, yCoord + yWarp);
- }
- }
- float FractalNoise(float x, float 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 float2 offset;
- [ReadOnly] public NativeArray<float> noiseFrequencies;
- [ReadOnly] public NativeArray<float> noiseAmplitudes;
- [ReadOnly] public int lod;
- [ReadOnly] public float warpingStrength;
- [ReadOnly] public float warpingFrequency;
- public void Execute(int i)
- {
- int z = i / (chunkDimensions.x / lod * chunkDimensions.y / lod) * lod;
- int y = (i / (chunkDimensions.x / lod) * lod) - z / lod * chunkDimensions.y;
- int x = i % (chunkDimensions.x / lod) * lod;
- //Noise value at each corner of a voxel
- for (int j = 0; j < 8; j++)
- {
- float xCoord = x + offset.x + cornerOffsets[j].x * lod;
- float yCoord = y + cornerOffsets[j].y * lod;
- float zCoord = z + offset.y + cornerOffsets[j].z * lod;
- float xWarp = Fractal3DNoise((xCoord - 3000f) * warpingFrequency, (yCoord + 8000) * warpingFrequency, (zCoord - 10000) * warpingFrequency) * warpingStrength;
- float yWarp = Fractal3DNoise((xCoord + 1000) * warpingFrequency, (yCoord - 5000) * warpingFrequency, (zCoord + 30000) * warpingFrequency) * warpingStrength;
- float zWarp = Fractal3DNoise((xCoord - 11000) * warpingFrequency, (yCoord + 2000) * warpingFrequency, (zCoord + 9000) * warpingFrequency) * warpingStrength;
- noiseMap[(i * 8) + j] = Fractal3DNoise(xCoord + xWarp, yCoord + yWarp, zCoord + zWarp);
- }
- }
- public float Fractal3DNoise(float x, float y, float 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;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement