-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNoiseTesting.cs
More file actions
99 lines (87 loc) · 2.66 KB
/
NoiseTesting.cs
File metadata and controls
99 lines (87 loc) · 2.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class NoiseTesting : MonoBehaviour
{
public int rayCastMask;
public int width = 256;
public int height = 256;
public float scale = 10f;
public float appearingThreshold = 0.4f;
public GameObject terrain;
Renderer r;
private float[,] grid;
public void Start()
{
r = terrain.GetComponent<Renderer>();
// grid = new float[width, height];
// Texture2D texture = new Texture2D(width, height);
// for (int x = 0; x < width; x++)
// {
// for (int y = 0; y < height; y++)
// {
// Color color;
// if (grid[x, y] < appearingThreshold)
// {
// color = new Color(0, 0, 0);
// texture.SetPixel(x, y, color);
// }
// else
// {
// color = new Color(1, 1, 1);
// texture.SetPixel(x, y, color);
// }
// }
// }
r.material.mainTexture = GenerateTexture();
}
void Update()
{
if(Input.GetKey(KeyCode.Keypad8)){
}
r.material.mainTexture = GenerateTexture();
}
Texture2D GenerateTexture()
{
Texture2D texture = new Texture2D(width, height);
for (int x = 0; x < width; x++)
{
for (int y = 0; y < height; y++)
{
Color color = calculateColor(x, y);
texture.SetPixel(x, y, color);
}
}
texture.Apply();
return texture;
}
public float lacunarity = 1;
public float persistance = 1;
public float scrollX = 0;
public float scrollY = 0;
public float octaves = 4;
// Scrolling + frequency
// add cleaning for straglers
// A nice config: scale 8 ,persistance 1.1 octaves 4 lacunarit 1, appearnce at 1
Color calculateColor(int x, int y)
{
float rgb = 0;
float amplitude = 1;
float frequency = 1;
for (int i = 0; i < octaves; i++)
{
float perlinPosX = (float)(x+scrollX) / width * scale * frequency;
float perlinPosY = (float)(y+scrollY) / height * scale * frequency;
float perlin = Mathf.PerlinNoise(perlinPosX, perlinPosY);
rgb += perlin * amplitude;
amplitude *= persistance;
frequency *= lacunarity;
}
// if (rgb < appearingThreshold)
// {
// return new Color(0, 0, 0);
// }
// return new Color(1, 1, 1);
return new Color(rgb, rgb, rgb);
}
}