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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
/**
* ALICE-Zip Unity Texture Generator
*
* Unity-specific helpers for generating textures from ALICE-Zip data.
*/
using UnityEngine;
namespace AliceZip.Unity
{
/// <summary>
/// Unity-specific texture generation utilities
/// </summary>
public static class AliceTextureGenerator
{
/// <summary>
/// Generate a Perlin noise Texture2D
/// </summary>
/// <param name="width">Texture width</param>
/// <param name="height">Texture height</param>
/// <param name="seed">Random seed for reproducibility</param>
/// <param name="scale">Noise scale (larger = more zoomed out)</param>
/// <param name="octaves">Number of octaves for fractal noise</param>
/// <param name="format">Texture format</param>
/// <returns>Generated Texture2D</returns>
public static Texture2D GeneratePerlinTexture(
int width, int height,
ulong seed = 42,
float scale = 10.0f,
uint octaves = 4,
TextureFormat format = TextureFormat.R8)
{
float[] noise = Alice.PerlinNoise2D(width, height, seed, scale, octaves);
return FloatArrayToTexture(noise, width, height, format);
}
/// <summary>
/// Generate an advanced Perlin noise Texture2D
/// </summary>
public static Texture2D GeneratePerlinTextureAdvanced(
int width, int height,
ulong seed = 42,
float scale = 10.0f,
uint octaves = 4,
float persistence = 0.5f,
float lacunarity = 2.0f,
TextureFormat format = TextureFormat.R8)
{
float[] noise = Alice.PerlinNoiseAdvanced(
width, height, seed, scale, octaves, persistence, lacunarity);
return FloatArrayToTexture(noise, width, height, format);
}
/// <summary>
/// Convert a float array to a grayscale Texture2D
/// </summary>
/// <param name="data">Float array (0.0 to 1.0 range)</param>
/// <param name="width">Texture width</param>
/// <param name="height">Texture height</param>
/// <param name="format">Texture format</param>
/// <returns>Generated Texture2D</returns>
public static Texture2D FloatArrayToTexture(
float[] data, int width, int height,
TextureFormat format = TextureFormat.R8)
{
Texture2D texture = new Texture2D(width, height, format, false);
if (format == TextureFormat.R8)
{
byte[] bytes = new byte[data.Length];
for (int i = 0; i < data.Length; i++)
{
bytes[i] = (byte)(Mathf.Clamp01(data[i]) * 255);
}
texture.LoadRawTextureData(bytes);
}
else if (format == TextureFormat.RFloat)
{
texture.SetPixelData(data, 0);
}
else
{
// Generic fallback using SetPixels
Color[] colors = new Color[data.Length];
for (int i = 0; i < data.Length; i++)
{
float v = Mathf.Clamp01(data[i]);
colors[i] = new Color(v, v, v, 1.0f);
}
texture.SetPixels(colors);
}
texture.Apply();
return texture;
}
/// <summary>
/// Convert a float array to a colored Texture2D using a gradient
/// </summary>
/// <param name="data">Float array (0.0 to 1.0 range)</param>
/// <param name="width">Texture width</param>
/// <param name="height">Texture height</param>
/// <param name="gradient">Color gradient to apply</param>
/// <returns>Generated Texture2D</returns>
public static Texture2D FloatArrayToTexture(
float[] data, int width, int height, Gradient gradient)
{
Texture2D texture = new Texture2D(width, height, TextureFormat.RGBA32, false);
Color[] colors = new Color[data.Length];
for (int i = 0; i < data.Length; i++)
{
colors[i] = gradient.Evaluate(Mathf.Clamp01(data[i]));
}
texture.SetPixels(colors);
texture.Apply();
return texture;
}
/// <summary>
/// Generate a normal map from a height map
/// </summary>
/// <param name="heightMap">Height map float array</param>
/// <param name="width">Texture width</param>
/// <param name="height">Texture height</param>
/// <param name="strength">Normal strength multiplier</param>
/// <returns>Normal map Texture2D</returns>
public static Texture2D HeightMapToNormalMap(
float[] heightMap, int width, int height, float strength = 1.0f)
{
Texture2D normalMap = new Texture2D(width, height, TextureFormat.RGBA32, false);
Color[] normals = new Color[width * height];
for (int y = 0; y < height; y++)
{
for (int x = 0; x < width; x++)
{
// Sample neighboring pixels
float left = GetHeight(heightMap, x - 1, y, width, height);
float right = GetHeight(heightMap, x + 1, y, width, height);
float up = GetHeight(heightMap, x, y - 1, width, height);
float down = GetHeight(heightMap, x, y + 1, width, height);
// Calculate normal
float dx = (right - left) * strength;
float dy = (down - up) * strength;
Vector3 normal = new Vector3(-dx, -dy, 1.0f).normalized;
// Convert to color (0-1 range)
normals[y * width + x] = new Color(
normal.x * 0.5f + 0.5f,
normal.y * 0.5f + 0.5f,
normal.z * 0.5f + 0.5f,
1.0f
);
}
}
normalMap.SetPixels(normals);
normalMap.Apply();
return normalMap;
}
private static float GetHeight(float[] heightMap, int x, int y, int width, int height)
{
x = Mathf.Clamp(x, 0, width - 1);
y = Mathf.Clamp(y, 0, height - 1);
return heightMap[y * width + x];
}
}
}