-
Notifications
You must be signed in to change notification settings - Fork 0
/
SurfaceGenerator.cs
186 lines (131 loc) · 7.66 KB
/
SurfaceGenerator.cs
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
174
175
176
177
178
179
180
181
182
183
184
185
186
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SurfaceGenerator
{
public static GameObject generate(ComputeShader computeShader, Material material, int width, int height, Color[] colors){
return generate(computeShader, material, width, height, null, null, null, colors);
}
public static GameObject generate(ComputeShader computeShader, Material material, int width, int height, Texture2D tileTexture){
return generate(computeShader, material, width, height, null, null, tileTexture, null);
}
public static GameObject generate(ComputeShader computeShader, Material material, int width, int height, Texture2D tileTexture, Color[] colors){
return generate(computeShader, material, width, height, null, null, tileTexture, colors);
}
public static GameObject generate(ComputeShader computeShader, Material material, int width, int height, Vector2 scale){
return generate(computeShader, material, width, height, null, scale, null, null);
}
public static GameObject generate(ComputeShader computeShader, Material material, int width, int height, Vector2 scale, Color[] colors){
return generate(computeShader, material, width, height, null, scale, null, colors);
}
public static GameObject generate(ComputeShader computeShader, Material material, int width, int height, Vector2 scale, Texture2D tileTexture){
return generate(computeShader, material, width, height, null, scale, tileTexture, null);
}
public static GameObject generate(ComputeShader computeShader, Material material, int width, int height, Vector2 scale, Texture2D tileTexture, Color[] colors){
return generate(computeShader, material, width, height, null, scale, tileTexture, colors);
}
public static GameObject generate(ComputeShader computeShader, Material material, int width, int height, float[,] y, Color[] colors){
return generate(computeShader, material, width, height, y, null, null, colors);
}
public static GameObject generate(ComputeShader computeShader, Material material, int width, int height, float[,] y, Texture2D tileTexture){
return generate(computeShader, material, width, height, y, null, tileTexture, null);
}
public static GameObject generate(ComputeShader computeShader, Material material, int width, int height, float[,] y, Texture2D tileTexture, Color[] colors){
return generate(computeShader, material, width, height, y, null, tileTexture, colors);
}
public static GameObject generate(ComputeShader computeShader, Material material, int width, int height, float[,] y ,Vector2 scale, Color[] colors){
return generate(computeShader, material, width, height, y, scale, null, colors);
}
public static GameObject generate(ComputeShader computeShader, Material material, int width, int height, float[,] y = null, Vector2? scale = null, Texture2D tileTexture = null, Color[] colors = null){
if(!scale.HasValue) scale = Vector2.one;
int[] textureSize = {(int)(width * scale.Value.x), (int)(height * scale.Value.y)};
for(int i = 0; i < textureSize.Length; i++){
if(textureSize[i] < 1) textureSize[i] = 1;
}
GameObject surface = new GameObject();
// Components
MeshFilter meshFilter = surface.AddComponent<MeshFilter>();
MeshRenderer meshRenderer = surface.AddComponent<MeshRenderer>();
Mesh mesh = generateMesh(width, height, y, scale);
meshFilter.sharedMesh = mesh;
Material textureMaterial = new Material(material);
textureMaterial.mainTexture = generateTexture(computeShader, textureSize[0], textureSize[1], tileTexture, colors);
meshRenderer.sharedMaterial = textureMaterial;
return surface;
}
public static Mesh generateMesh(int width, int height, float[,] y = null, Vector2? scale = null){
Mesh generatedMesh = new Mesh();
if(!scale.HasValue) scale = Vector2.one;
// Setup Vertices And UV
Vector3[] vertices = new Vector3[(width + 1) * (height + 1)];
for(int z = 0; z < height + 1; z++){
for(int x = 0; x < width + 1; x++){
vertices[z * (width + 1) + x] = new Vector3((x - (width / 2f)) * scale.Value.x, (y != null) ? y[x, z]: 0, (z - (height / 2f)) * scale.Value.y);
}
}
generatedMesh.vertices = vertices;
// Setup Triangles
int[] triangles = new int[width * height * 6];
for(int z = 0, i = 0, t = 0; z < height; z++, i++){
for(int x = 0; x < width; x++, i++, t+=6){
triangles[t] = i;
triangles[t + 1] = triangles[t + 4] = width + i + 1;
triangles[t + 2] = triangles[t + 3] = i + 1;
triangles[t + 5] = width + i + 2;
}
}
generatedMesh.triangles = triangles;
generatedMesh.RecalculateNormals();
// Setup UV
Vector2[] uv = new Vector2[vertices.Length];
for(int i = 0; i < uv.Length; i++){
uv[i] = new Vector2((vertices[i].x / scale.Value.x + width / 2f) / width, (vertices[i].z / scale.Value.y + height / 2f) / height);
}
generatedMesh.uv = uv;
return generatedMesh;
}
public static RenderTexture generateTexture(ComputeShader computeShader, int width, int height, Texture2D tileTexture, Color[] colors){
int[] resolution = {1, 1};
if(tileTexture == null){
tileTexture = new Texture2D(1, 1);
}
resolution[0] = tileTexture.width;
resolution[1] = tileTexture.height;
if(colors == null){
colors = new Color[width * height];
for(int i = 0; i < colors.Length; i++)
colors[i] = Color.white;
}
RenderTexture renderTexture = new RenderTexture(width * resolution[0], height * resolution[1], 16);
renderTexture.enableRandomWrite = true;
renderTexture.filterMode = FilterMode.Point;
renderTexture.Create();
computeShader.SetTexture(0, "Result", renderTexture);
computeShader.SetTexture(0, "TileTexture", tileTexture);
computeShader.SetInt("TextureWidth", tileTexture.width);
computeShader.SetInt("TextureHeight", tileTexture.height);
ComputeBuffer computeBuffer = new ComputeBuffer(colors.Length, sizeof(float) * 4);
computeBuffer.SetData(colors);
computeShader.SetBuffer(0, "Colors", computeBuffer);
computeShader.SetInt("Width", width - 1);
int threadGroupX = (width * resolution[0]) / 8;
int threadGroupY = (height * resolution[1]) / 8;
threadGroupX += (threadGroupX == 0)? 1:0;
threadGroupY += (threadGroupY == 0)? 1:0;
computeShader.Dispatch(0, threadGroupX, threadGroupY, 1);
computeBuffer.Release();
return renderTexture;
}
public static int calculateColorsLength(int width, int height, Vector2? scale = null){
if(!scale.HasValue) scale = Vector2.one;
return (int)((width + 1) * (height + 1) * scale.Value.x * scale.Value.y);
}
public static Color[] createColorArray(int width, int height, Vector2? scale = null, Color? color = null){
Color[] colors = new Color[calculateColorsLength(width, height, scale)];
Color _color = (color.HasValue)? color.Value: Color.gray;
for(int i = 0; i < colors.Length; i++){
colors[i] = _color;
}
return colors;
}
}