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;
- public Noise(float frequency, float persistance, float lacunarity, float warpingStrength, float warpingFrequency, float mountainHeightOffset, int octaves)
- {
- 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;
- 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)
- {
- 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();
- 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].y * lod;
- float xWarp = FractalNoise((xCoord + 1000) * warpingFrequency, (yCoord - 1000) * warpingFrequency) * warpingStrength;
- float yWarp = FractalNoise((yCoord + 10000) * warpingFrequency, (yCoord - 10000) * warpingFrequency) * warpingStrength;
- //float mountains = MountainFractalNoise(x + 6000 + cornerOffsets[j].x * lod, y - 7000 + cornerOffsets[j].z * lod);
- float regular = FractalNoise(x + xWarp + offset.x + cornerOffsets[j].x * lod, y + yWarp + offset.x + cornerOffsets[j].z * lod);
- //float height01 = Utils.Remap(y, 0, chunkDimensions.y - 1, 0, 1);
- noiseMap[(i * 8) + j] = regular; //BlendNoise(regular, mountains, height01);
- }
- }
- 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;
- }
- float MountainFractalNoise(float x, float y)
- {
- float noiseValue = 0;
- for (int i = 0; i < octaves; i++)
- {
- float frequency = noiseFrequencies[i] / 2f;
- float amplitude = noiseAmplitudes[i] * 2f;
- noiseValue += (1 - math.abs(noise.snoise(new float2((x + offset.x) * frequency, (y + offset.y) * frequency)))) * amplitude;
- }
- return noiseValue;
- }
- float BlendNoise(float noise1, float noise2, float height)
- {
- float hMin = .4f; // Start blending at this height
- float hMax = .7f; // End blending at this height
- if (height <= hMin)
- {
- return noise1;
- }
- else if (height >= hMax)
- {
- return noise2;
- }
- else
- {
- float t = (height - hMin) / (hMax - hMin);
- return (1 - t) * noise1 + t * noise2;
- }
- }
- }
- [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