Hey there,
I am having some struggles making some cell noise, and i really have no clue what is going on, any improvements on my code are always welcome.
my problem is is that a certain part of the texture is being coppied in x en y direction:
Also any feedback on my thread skills are welcome(this is my second one)
using UnityEngine;
using System.Collections;
public static class Noise {
public static float[,] GenerateNoiseMap(int mapWidth, int mapHeight, int seed, float scale, int octaves, float persistance, float lacunarity, Vector2 offset) {
float[,] noiseMap = new float[mapWidth,mapHeight];
System.Random prng = new System.Random (seed);
Vector2[] octaveOffsets = new Vector2[octaves];
for (int i = 0; i < octaves; i++) {
float offsetX = prng.Next (-100000, 100000) + offset.x;
float offsetY = prng.Next (-100000, 100000) + offset.y;
octaveOffsets [i] = new Vector2 (offsetX, offsetY);
}
if (scale <= 0) {
scale = 0.0001f;
}
float maxNoiseHeight = float.MinValue;
float minNoiseHeight = float.MaxValue;
float halfWidth = mapWidth / 2f;
float halfHeight = mapHeight / 2f;
for (int y = 0; y < mapHeight; y++) {
for (int x = 0; x < mapWidth; x++) {
float amplitude = 1;
float frequency = 1;
float noiseHeight = 0;
for (int i = 0; i < octaves; i++) {
float sampleX = (x-halfWidth) / scale * frequency + octaveOffsets[i].x;
float sampleY = (y-halfHeight) / scale * frequency + octaveOffsets[i].y;
float perlinValue = Worley.worleyNoise (sampleX, sampleY, 2) * 2 - 1;
noiseHeight += perlinValue * amplitude;
amplitude *= persistance;
frequency *= lacunarity;
}
if (noiseHeight > maxNoiseHeight) {
maxNoiseHeight = noiseHeight;
} else if (noiseHeight < minNoiseHeight) {
minNoiseHeight = noiseHeight;
}
noiseMap [x, y] = noiseHeight;
}
}
for (int y = 0; y < mapHeight; y++) {
for (int x = 0; x < mapWidth; x++) {
noiseMap [x, y] = Mathf.InverseLerp (minNoiseHeight, maxNoiseHeight, noiseMap [x, y]);
}
}
return noiseMap;
}
}
using UnityEngine;
using System.Collections;
public class MapGenerator {
public int mapWidth;
public int mapHeight;
public float noiseScale;
[Range(0,8)]
public int octaves;
[Range(0,1)]
public float persistance;
public float lacunarity;
public int seed;
public Vector2 offset;
public float[,] GenerateMap() {
float[,] noiseMap = Noise.GenerateNoiseMap (mapWidth, mapHeight, seed, noiseScale, octaves, persistance, lacunarity, offset);
return noiseMap;
}
void OnValidate() {
if (mapWidth < 1) {
mapWidth = 1;
}
if (mapHeight < 1) {
mapHeight = 1;
}
if (lacunarity < 1) {
lacunarity = 1;
}
if (octaves < 1) {
octaves = 1;
}
}
}
using System;
using System.Collections.Generic;
using UnityEngine;
public static class Worley {
public static float worleyNoise(float x, float y, int seed)
{
//correct
int xBox = Mathf.FloorToInt(x);
int yBox = Mathf.FloorToInt(y);
Vector2[] points = new Vector2[9];
for (int xs = 0; xs < 3; xs++)
{
for (int ys = 0; ys < 3; ys++)
{
System.Random xr = new System.Random(unint(xBox + xs - 1));
float xPos = (xr.Next(0, 1000) / 1000) + xBox + xs - 1;
System.Random yr = new System.Random(unint(yBox + ys - 1));
float yPos = (yr.Next(0, 1000) / 1000) + yBox + ys - 1;
points[xs + ys] = new Vector2(xPos, yPos);
}
}
//klopt
float lowestDist = 10000000;
for (int i = 0; i < 9; i++)
{
float dist = EuclidianDistanceFunc(new Vector2(x,y) , points[i]);
if (dist < lowestDist)
{
lowestDist = dist;
}
}
return lowestDist;
}
public static float EuclidianDistanceFunc(Vector2 p1, Vector2 p2)
{
return (p1.x - p2.x) * (p1.x - p2.x) + (p1.y - p2.y) * (p1.y - p2.y);
}
public static float ManhattanDistanceFunc(Vector2 p1, Vector2 p2)
{
return Mathf.Abs(p1.x - p2.x) + Mathf.Abs(p1.y - p2.y);
}
public static float ChebyshevDistanceFunc(Vector2 p1, Vector2 p2)
{
Vector3 diff = p1 - p2;
return Mathf.Max(Mathf.Max(Mathf.Abs(diff.x), Mathf.Abs(diff.y)));
}
private static int unint(int lastValue)
{
System.Random r = new System.Random(lastValue);
for (int i = 0; i < r.Next(0,10); i++)
{
r.Next(0,10);
}
return r.Next(0,1000) / 1000;
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
[CustomEditor(typeof(Mapper))]
[CanEditMultipleObjects]
public class Mapper_Editor : Editor {
public override void OnInspectorGUI()
{
Mapper mapper = (Mapper) target;
base.OnInspectorGUI();
if (GUILayout.Button("Generate"))
{
mapper.generate();
}
}
}
using UnityEngine;
using System.Collections;
public class MapDisplay : MonoBehaviour {
public Renderer textureRender;
public void DrawNoiseMap(float[,] noiseMap, Gradient colors) {
int width = noiseMap.GetLength (0);
int height = noiseMap.GetLength (1);
Texture2D texture = new Texture2D (width, height);
Color[] colourMap = new Color[width * height];
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
colourMap[y * width + x] = colors.Evaluate(noiseMap[x,y]) ;
}
}
texture.SetPixels (colourMap);
texture.Apply ();
textureRender.sharedMaterial.mainTexture = texture;
textureRender.transform.localScale = new Vector3 (width, 1, height);
}
}
using UnityEngine;
using System.Collections.Generic;
[ExecuteInEditMode]
public class Mapper : MonoBehaviour {
public int mapWidth = 100;
public int mapHeight = 100;
public float scale = 1;
public int seed;
public Vector2 offset;
public Gradient gradient;
public Layer[] layers;
public void generate()
{
float[,] uitkomst = new float[mapWidth,mapHeight];
for (int i = 0; i < layers.Length; i++)
{
MapGenerator mg = new MapGenerator();
mg.mapWidth = mapWidth;
mg.mapHeight = mapHeight;
mg.noiseScale = scale;
mg.octaves = layers[i].octaves;
mg.persistance = layers[i].persistance;
mg.lacunarity = layers[i].lacunarity;
mg.seed = seed;
mg.offset = offset;
float[,] values = Noise.GenerateNoiseMap(mapWidth, mapHeight, seed, scale, layers[i].octaves, layers[i].persistance, layers[i].lacunarity, offset);
for (int x = 0; x < mapWidth; x++)
{
for (int y = 0; y < mapHeight; y++)
{
if (layers[i].filter == Layer.Filters.Abs)
{
values[x, y] = Mathf.Abs(values[x, y] / 2 - 1);
} else if (layers[i].filter == Layer.Filters.Round)
{
values[x, y] = Mathf.RoundToInt(values[x, y]);
}
if (layers[i].invert)
{
values[x, y] = -values[x, y];
}
if (layers[i].mixMode == Layer.MixMode.add)
{
uitkomst[x, y] += values[x, y];
}
else if(layers[i].mixMode == Layer.MixMode.average)
{
uitkomst[x, y] = (uitkomst[x, y] + values[x, y]) / 2;
}
else if (layers[i].mixMode == Layer.MixMode.devide)
{
uitkomst[x, y] /= values[x, y];
}
else if (layers[i].mixMode == Layer.MixMode.multiply)
{
uitkomst[x, y] *= values[x, y];
}
else if (layers[i].mixMode == Layer.MixMode.subtract)
{
uitkomst[x, y] -= values[x, y];
}
else
{
}
}
}
}
float maxVal = 0;
float minVal = 0;
for (int x = 0; x < mapWidth; x++)
{
for (int y = 0; y < mapHeight; y++)
{
if (uitkomst[x,y] < minVal)
{
minVal = uitkomst[x, y];
}
if (uitkomst[x,y] > maxVal)
{
maxVal = uitkomst[x, y];
}
}
}
for (int x = 0; x < mapWidth; x++)
{
for (int y = 0; y < mapHeight; y++)
{
uitkomst[x, y] = Mathf.InverseLerp(minVal, maxVal, uitkomst[x,y]);
}
}
MapDisplay display = GetComponent<MapDisplay>();
display.DrawNoiseMap(uitkomst, gradient);
}
}
[System.Serializable]
public class Layer
{
[Range(1, 8)]
public int octaves;
[Range(0, 1)]
public float persistance;
public float lacunarity;
public bool invert ;
public enum Filters { none, Abs, Round};
public Filters filter;
public enum MixMode { add, subtract, multiply, devide, average, none};
public MixMode mixMode;
}
I am using this tutorial for creating the noise AFTbit. I am not using the given code because i have not learnt all the fancy stuf with the lcg stuff and the uint’s etc
3278403–253798–worley.unitypackage (20.1 KB)