Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using Unity.Jobs;
- using UnityEngine;
- using Unity.Mathematics;
- using System;
- using System.Collections;
- using Unity.Collections;
- public class Noise
- {
- float[] noiseFrequencies;
- float[] noiseAmplitudes;
- float density;
- int octaves;
- Vector3Int chunkDimensions;
- public Noise(float frequency, float persistance, float lacunarity, float density, int octaves)
- {
- noiseFrequencies = new float[octaves];
- noiseAmplitudes = new float[octaves];
- for (int i = 0; i < octaves; i++)
- {
- noiseFrequencies[i] = Mathf.Pow(lacunarity, i) * frequency;
- noiseAmplitudes[i] = Mathf.Pow(persistance, i);
- }
- this.density = density;
- this.octaves = octaves;
- chunkDimensions = MeshGenerator.chunkDimensions;
- }
- public float[] FractalNoiseMap()
- {
- float[] noiseMap = new float[chunkDimensions.x * chunkDimensions.z * 8];
- Vector3Int[] cornerOffsets = MeshGenerator.cornerOffsets;
- int length = chunkDimensions.x * chunkDimensions.z;
- for (int i = 0; i < length; i++)
- {
- int y = i / chunkDimensions.x;
- int x = i % chunkDimensions.x;
- for (int j = 0; j < 8; j++)
- {
- //Noise value at each corner of a voxel
- noiseMap[(i * 8) + j] = FractalNoise(x + cornerOffsets[j].x, y + cornerOffsets[j].z);
- }
- }
- return noiseMap;
- }
- public float[] Fractal3DNoiseMap()
- {
- float[] noiseMap = new float[chunkDimensions.x * chunkDimensions.y * chunkDimensions.z * 8];
- Vector3Int[] cornerOffsets = MeshGenerator.cornerOffsets;
- int length = chunkDimensions.x * chunkDimensions.y * chunkDimensions.z;
- for (int i = 0, h = 0; i < length; i++)
- {
- int z = i / (chunkDimensions.x * chunkDimensions.y);
- int x = (i % (chunkDimensions.x * chunkDimensions.y)) / chunkDimensions.y;
- int y = i % chunkDimensions.y;
- //Noise value at each corner of a voxel
- for (int j = 0; j < 8; j++, h++)
- {
- int xCoord = x + cornerOffsets[j].x;
- int yCoord = y + cornerOffsets[j].y;
- int zCoord = z + cornerOffsets[j].z;
- noiseMap[h] = Fractal3DNoise(xCoord, yCoord, zCoord);
- }
- }
- return noiseMap;
- }
- float FractalNoise(int x, int y)
- {
- float noise = 0;
- for (int i = 0; i < octaves; i++)
- {
- noise += Mathf.PerlinNoise(x * noiseFrequencies[i], y * noiseFrequencies[i]) * noiseAmplitudes[i];
- }
- return noise;
- }
- public float Fractal3DNoise(int x, int y, int z)
- {
- float noise = 0;
- for (int i = 0; i < octaves; i++)
- {
- noise += Perlin.Noise(x * noiseFrequencies[i], y * noiseFrequencies[i], z * noiseFrequencies[i]) * noiseAmplitudes[i];
- }
- return noise; //- density;
- }
- struct Noise2DJob : IJobParallelFor
- {
- [WriteOnly] public float[] noiseMap;
- [ReadOnly] public int3 chunkDimensions;
- [ReadOnly] public int3[] cornerOffsets;
- [ReadOnly] public int octaves;
- [ReadOnly] public float[] noiseFrequencies;
- [ReadOnly] public float[] noiseAmplitudes;
- public void Execute(int i)
- {
- int y = i / chunkDimensions.x;
- int x = i % chunkDimensions.x;
- for (int j = 0; j < 8; j++)
- {
- //Noise value at each corner of a voxel
- noiseMap[(i * 8) + j] = FractalNoise(x + cornerOffsets[j].x, y + cornerOffsets[j].z);
- }
- }
- float FractalNoise(int x, int y)
- {
- float noise = 0;
- for (int i = 0; i < octaves; i++)
- {
- noise += Mathf.PerlinNoise(x * noiseFrequencies[i], y * noiseFrequencies[i]) * noiseAmplitudes[i];
- }
- return noise;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement